Commits

Mikhail Korobov committed cd4acba

assertRedirects is fixed for authenticated requests. Tests are updated for django post-1.4. Bump version.

  • Participants
  • Parent commits 3e262d0
  • Tags 1.5.3

Comments (0)

Files changed (6)

 CHANGES
 =======
 
+1.5.3 (2012-04-25)
+------------------
+
+- self.assertRedirects is fixed for authenticated requests.
+
 1.5.2 (2012-04-01)
 ------------------
 

File django_webtest/response.py

 # -*- coding: utf-8 -*-
+
 try:
     import urlparse
 except ImportError: #python 3
 
 from webtest import TestResponse
 from django.test import Client
+from django.http import SimpleCookie
 
 class DjangoWebtestResponse(TestResponse):
     """
 
     @property
     def client(self):
-        return Client()
+        client = Client()
+        client.cookies = SimpleCookie(self.test_app.cookies)
+        return client
 
     def __getitem__(self, item):
         if item != 'Location':

File django_webtest_tests/testapp_tests/tests.py

 
 
 
-class DjangoAssertsTest(WebTest):
+class DjangoAssertsTest(BaseAuthTest):
 
     def test_assert_template_used(self):
         response = self.app.get('/template/index.html')
         resp = page.form.submit()
         self.assertRedirects(resp, '/')
 
+    def test_redirects_noauth(self):
+        self.app.get(reverse('redirect-to-protected')).follow(status=302)
+
+    def test_redirects(self):
+        self.app.get(reverse('redirect-to-protected'), user=self.user).follow()
+
+    def test_assert_redirects_auth(self):
+        page = self.app.get(reverse('redirect-to-protected'), user=self.user)
+        self.assertRedirects(page, reverse('protected'))
+
+
 
 class DisableAuthSetupTest(WebTest):
     setup_auth = False

File django_webtest_tests/testapp_tests/views.py

 from django import forms
-from django.http import HttpResponseRedirect
-from django.views.generic.simple import direct_to_template
+from django.contrib.auth.decorators import login_required
+from django.http import HttpResponseRedirect, HttpResponse
+from django.shortcuts import render_to_response, redirect
+from django.template.context import RequestContext
 
 class PasswordForm(forms.Form):
     password = forms.CharField(widget=forms.PasswordInput())
     form = PasswordForm(request.POST or None)
     if form.is_valid():
         return HttpResponseRedirect('/')
-    return direct_to_template(request, 'form.html', {'form': form})
+    ctx = RequestContext(request, {'form': form})
+    return render_to_response('form.html', ctx)
 
 
 class SearchForm(forms.Form):
     q = forms.CharField(required=False)
-    
+
 def search(request):
     form = SearchForm(request.GET)
     q = None
     if form.is_valid():
         q = form.cleaned_data['q']
-    return direct_to_template(request, 'get_form.html', 
-                              {'form': form, 'q': q})
+    ctx = RequestContext(request, {'form': form, 'q': q})
+    return render_to_response('get_form.html', ctx)
 
 def set_session(request):
     request.session['test'] = 'foo'
     return HttpResponseRedirect('/')
+
+def redirect_to_protected(request):
+    return redirect('protected')
+
+@login_required
+def protected(request):
+    return HttpResponse('ok')

File django_webtest_tests/urls.py

     url(r'^check-password/$', 'testapp_tests.views.check_password', name='check_password'),
     url(r'^search/$', 'testapp_tests.views.search', name='search'),
     url(r'^login/$', 'django.contrib.auth.views.login', name='auth_login'),
-    url(r'^set-session/$', 'testapp_tests.views.set_session', name='set_session')
+    url(r'^set-session/$', 'testapp_tests.views.set_session', name='set_session'),
+    url(r'^protected/$', 'testapp_tests.views.protected', name='protected'),
+    url(r'^redirect-to-protected/$', 'testapp_tests.views.redirect_to_protected', name='redirect-to-protected'),
 )
     if cmd in sys.argv:
         from setuptools import setup
 
-version='1.5.2'
+version='1.5.3'
 
 setup(
     name='django-webtest',