From 55d6aebfecb527d64f50e608bb51cb5ea81f4c62 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 14 Sep 2007 19:25:37 +0000 Subject: Fixed #5394 -- REDIRECT_FIELD_NAME is now configurable. Thanks, Petr Marhoun, DavidReynolds and effbot git-svn-id: http://code.djangoproject.com/svn/django/trunk@6206 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/test_client/models.py | 16 ++++++++++++++++ tests/modeltests/test_client/urls.py | 1 + tests/modeltests/test_client/views.py | 8 ++++++++ 3 files changed, 25 insertions(+) (limited to 'tests') diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py index 32647552eb..2df5d3cf77 100644 --- a/tests/modeltests/test_client/models.py +++ b/tests/modeltests/test_client/models.py @@ -250,6 +250,22 @@ class ClientTest(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(response.context['user'].username, 'testclient') + def test_view_with_login_and_custom_redirect(self): + "Request a page that is protected with @login_required(redirect_field_name='redirect_to')" + + # Get the page without logging in. Should result in 302. + response = self.client.get('/test_client/login_protected_view_custom_redirect/') + self.assertRedirects(response, 'http://testserver/accounts/login/?redirect_to=/test_client/login_protected_view_custom_redirect/') + + # Log in + login = self.client.login(username='testclient', password='password') + self.failUnless(login, 'Could not log in') + + # Request a page that requires a login + response = self.client.get('/test_client/login_protected_view_custom_redirect/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['user'].username, 'testclient') + def test_view_with_bad_login(self): "Request a page that is protected with @login, but use bad credentials" diff --git a/tests/modeltests/test_client/urls.py b/tests/modeltests/test_client/urls.py index 538c0e4b43..3779a0ecd1 100644 --- a/tests/modeltests/test_client/urls.py +++ b/tests/modeltests/test_client/urls.py @@ -13,6 +13,7 @@ urlpatterns = patterns('', (r'^form_view/$', views.form_view), (r'^form_view_with_template/$', views.form_view_with_template), (r'^login_protected_view/$', views.login_protected_view), + (r'^login_protected_view_custom_redirect/$', views.login_protected_view_changed_redirect), (r'^session_view/$', views.session_view), (r'^broken_view/$', views.broken_view), (r'^mail_sending_view/$', views.mail_sending_view), diff --git a/tests/modeltests/test_client/views.py b/tests/modeltests/test_client/views.py index e2a9081fb2..c406e17d30 100644 --- a/tests/modeltests/test_client/views.py +++ b/tests/modeltests/test_client/views.py @@ -122,6 +122,14 @@ def login_protected_view(request): return HttpResponse(t.render(c)) login_protected_view = login_required(login_protected_view) +def login_protected_view_changed_redirect(request): + "A simple view that is login protected with a custom redirect field set" + t = Template('This is a login protected test. Username is {{ user.username }}.', name='Login Template') + c = Context({'user': request.user}) + + return HttpResponse(t.render(c)) +login_protected_view_changed_redirect = login_required(redirect_field_name="redirect_to")(login_protected_view_changed_redirect) + def session_view(request): "A view that modifies the session" request.session['tobacconist'] = 'hovercraft' -- cgit v1.3