Skip to content

Commit d7abfd7

Browse files
Jaap Roesclaudep
authored andcommitted
Use configured success_url
Restore the ability to override the success_url in urls.py
1 parent 864d696 commit d7abfd7

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

tests/test_views_setup.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ def test_setup_generator_with_multi_method(self):
105105
self.assertRedirects(response, reverse('two_factor:setup_complete'))
106106
self.assertEqual(1, self.user.totpdevice_set.count())
107107

108+
@method_registry(['generator'])
109+
def test_setup_custom_success_url(self):
110+
custom_setup = reverse('setup-backup_tokens-redirect')
111+
custom_redirect = reverse('two_factor:backup_tokens')
112+
response = self.client.post(
113+
custom_setup,
114+
data={
115+
'setup_view-current_step': 'method',
116+
'method-method': 'generator'
117+
}
118+
)
119+
key = response.context_data['keys'].get('generator')
120+
data = {
121+
'setup_view-current_step': 'generator',
122+
'generator-token': totp(unhexlify(key.encode()))
123+
}
124+
response = self.client.post(custom_setup, data=data)
125+
self.assertRedirects(response, custom_redirect)
126+
108127
def _post(self, data):
109128
return self.client.post(reverse('two_factor:setup'), data=data)
110129

tests/urls.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from two_factor.gateways.twilio.urls import urlpatterns as tf_twilio_urls
55
from two_factor.urls import urlpatterns as tf_urls
6-
from two_factor.views import LoginView
6+
from two_factor.views import LoginView, SetupView
77

88
from .views import SecureView, plain_view
99

@@ -37,7 +37,11 @@
3737
),
3838
name='custom-redirect-authenticated-user-login',
3939
),
40-
40+
path(
41+
'account/setup-backup-tokens-redirect/',
42+
SetupView.as_view(success_url='two_factor:backup_tokens'),
43+
name='setup-backup_tokens-redirect'
44+
),
4145
path(
4246
'plain/',
4347
plain_view,

two_factor/views/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ class SetupView(RedirectURLMixin, IdempotentSessionWizardView):
446446
# https://github.com/django/django/blob/58df8aa40fe88f753ba79e091a52f236246260b3/django/contrib/auth/views.py#L63
447447
def get_success_url(self):
448448
url = self.get_redirect_url()
449-
return url or reverse('two_factor:setup_complete')
449+
return url or reverse(self.success_url)
450450

451451
# Copied from django.contrib.auth.views.LoginView (Branch: stable/1.11.x)
452452
# https://github.com/django/django/blob/58df8aa40fe88f753ba79e091a52f236246260b3/django/contrib/auth/views.py#L67

0 commit comments

Comments
 (0)