From f11094c67ae5dd8c75c15623e72207a7a9543d8d Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 9 Aug 2018 17:20:37 +0200 Subject: [PATCH] allow custom authentication method in registration view (fixes #25623) --- src/authentic2/registration_backend/views.py | 11 ++++-- tests/test_registration.py | 35 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/authentic2/registration_backend/views.py b/src/authentic2/registration_backend/views.py index 98fb0bfd..3338dbc2 100644 --- a/src/authentic2/registration_backend/views.py +++ b/src/authentic2/registration_backend/views.py @@ -135,6 +135,7 @@ class RegistrationCompletionView(CreateView): def dispatch(self, request, *args, **kwargs): self.token = request.token + self.authentication_method = self.token.get('authentication_method', 'email') self.email = request.token['email'] if 'ou' in self.token: self.ou = OrganizationalUnit.objects.get(pk=self.token['ou']) @@ -265,7 +266,9 @@ class RegistrationCompletionView(CreateView): def get(self, request, *args, **kwargs): if len(self.users) == 1 and self.email_is_unique: # Found one user, EMAIL is unique, log her in - simulate_authentication(request, self.users[0], method='email', service_slug=self.service) + simulate_authentication(request, self.users[0], + method=self.authentication_method, + service_slug=self.service) return redirect(request, self.get_success_url()) confirm_data = self.token.get('confirm_data', False) @@ -300,7 +303,8 @@ class RegistrationCompletionView(CreateView): uid = request.POST['uid'] for user in self.users: if str(user.id) == uid: - simulate_authentication(request, user, method='email', + simulate_authentication(request, user, + method=self.authentication_method, service_slug=self.service) return redirect(request, self.get_success_url()) return super(RegistrationCompletionView, self).post(request, *args, **kwargs) @@ -332,8 +336,9 @@ class RegistrationCompletionView(CreateView): def registration_success(self, request, user, form): hooks.call_hooks('event', name='registration', user=user, form=form, view=self, + authentication_method=self.authentication_method, token=request.token, service=self.service) - simulate_authentication(request, user, method='email', + simulate_authentication(request, user, method=self.authentication_method, service_slug=self.service) messages.info(self.request, _('You have just created an account.')) self.send_registration_success_email(user) diff --git a/tests/test_registration.py b/tests/test_registration.py index bbd040e7..672a965c 100644 --- a/tests/test_registration.py +++ b/tests/test_registration.py @@ -602,3 +602,38 @@ def test_registration_activate_passwords_not_equal(app, db, settings, mailoutbox response.form.set('password2', 'AAAazerty12AZ') response = response.form.submit() assert "The two password fields didn't match." in response.content + + +def test_authentication_method(app, db, rf, hooks): + activation_url = utils.build_activation_url( + rf.post('/accounts/register/'), + email='john.doe@example.com', + next_url='/', + first_name='John', + last_name='Doe', + no_password=True, + confirm_data=False) + app.get(activation_url) + + assert len(hooks.calls['event']) == 2 + assert hooks.calls['event'][-2]['kwargs']['name'] == 'registration' + assert hooks.calls['event'][-2]['kwargs']['authentication_method'] == 'email' + assert hooks.calls['event'][-1]['kwargs']['name'] == 'login' + assert hooks.calls['event'][-1]['kwargs']['how'] == 'email' + + activation_url = utils.build_activation_url( + rf.post('/accounts/register/'), + email='jane.doe@example.com', + next_url='/', + first_name='Jane', + last_name='Doe', + no_password=True, + authentication_method='another', + confirm_data=False) + app.get(activation_url) + + assert len(hooks.calls['event']) == 4 + assert hooks.calls['event'][-2]['kwargs']['name'] == 'registration' + assert hooks.calls['event'][-2]['kwargs']['authentication_method'] == 'another' + assert hooks.calls['event'][-1]['kwargs']['name'] == 'login' + assert hooks.calls['event'][-1]['kwargs']['how'] == 'another' -- 2.18.0