Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Signin view should have argument for inactive url redirect #539

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ usedevelop = True
deps =
django{15,16}: south
django{15,16}: django-guardian<1.4.0
django{15,16}: unittest2
django15: django==1.5.12
django16: django==1.6.11
django17: django==1.7.11
Expand Down
37 changes: 37 additions & 0 deletions userena/tests/tests_views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
import re
import sys
if sys.version_info < (2, 7):
# unittest2 for backport compatibility
from unittest2 import skipIf
else:
from unittest import skipIf

from datetime import datetime, timedelta
import django
from django.contrib.auth import get_user_model
from django.conf.urls import url
from django.core.urlresolvers import reverse
from django.core import mail
from django.contrib.auth.forms import PasswordChangeForm
from django.views.generic import TemplateView
from django.test import TestCase
from django.test.utils import override_settings

from userena import forms
from userena import views as userena_views
from userena import settings as userena_settings
from userena.utils import get_user_profile

User = get_user_model()


class SigninInactiveURLs(object):
""" Override default signin urls to test inactive redirect """
urlpatterns = [
url(r'^signup/$',userena_views.signin,
{'inactive_url': '/disabled/'},
name='custom_signin'),
url(r'^disabled/$',
TemplateView.as_view(template_name='userena/disabled.html'),
name='custom_disabled')
]


class UserenaViewsTests(TestCase):
""" Test the account views """
fixtures = ['users', 'profiles']
Expand Down Expand Up @@ -293,6 +316,20 @@ def test_signin_view_inactive(self):
reverse('userena_disabled',
kwargs={'username': user.username}))

@skipIf(django.VERSION < (1, 7, 0), "override `ROOT_URLCONF` not supported")
@override_settings(ROOT_URLCONF=SigninInactiveURLs)
def test_signin_view_inactive_redirect_specified(self):
""" A ``POST`` from a inactive user with inactive url specified """
user = User.objects.get(email='[email protected]')
user.is_active = False
user.save()

response = self.client.post(reverse('custom_signin'),
data={'identification': '[email protected]',
'password': 'blowfish'})

self.assertRedirects(response, reverse('custom_disabled'))

def test_signin_view_success(self):
"""
A valid ``POST`` to the signin view should redirect the user to it's
Expand Down
15 changes: 11 additions & 4 deletions userena/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def disabled_account(request, username, template_name, extra_context=None):

:param template_name:
String defining the name of the template to use. Defaults to
``userena/signup_complete.html``.
``userena/disabled.html``.

**Keyword arguments**

Expand Down Expand Up @@ -390,7 +390,8 @@ def disabled_account(request, username, template_name, extra_context=None):
def signin(request, auth_form=AuthenticationForm,
template_name='userena/signin_form.html',
redirect_field_name=REDIRECT_FIELD_NAME,
redirect_signin_function=signin_redirect, extra_context=None):
redirect_signin_function=signin_redirect,
inactive_url=None, extra_context=None):
"""
Signin using email or username with password.

Expand Down Expand Up @@ -421,6 +422,10 @@ def signin(request, auth_form=AuthenticationForm,
``REDIRECT_FIELD_NAME`` and the :class:`User` who has logged in. It
must return a string which specifies the URI to redirect to.

:param inactive_url:
Named URL which will be passed on to a django ``reverse`` function if
account is disabled. Defaults to the ``userena_disabled`` url.

:param extra_context:
A dictionary containing extra variables that should be passed to the
rendered template. The ``form`` key is always the ``auth_form``.
Expand Down Expand Up @@ -459,8 +464,10 @@ def signin(request, auth_form=AuthenticationForm,
request.POST.get(redirect_field_name)), user)
return HttpResponseRedirect(redirect_to)
else:
return redirect(reverse('userena_disabled',
kwargs={'username': user.username}))
if inactive_url: redirect_to = inactive_url
else: redirect_to = reverse('userena_disabled',
kwargs={'username': user.username})
return redirect(redirect_to)

if not extra_context: extra_context = dict()
extra_context.update({
Expand Down