def email_change_confirm(request, token): user = request.user email_change = UserEmailChangeTokenGenerator() if email_change.is_valid(user, token): user.email = email_change.get_email() user.save() messages.info(request, _("Your email has been changed!")) return redirect(reverse('foro:profile-update'))
def monkey_sender(request, subject, template_name, context, email): self.assertEqual(request, req) self.assertEqual(email, [self.user.email, ]) change_token = UserEmailChangeTokenGenerator() token = change_token.generate(self.user, new_email) self.assertDictEqual(context, {'token': token, }) self.assertEqual(subject, _("Email change")) self.assertEqual(template_name, 'foro/user/email_change_email.html') self._monkey_sender_called = True
def monkey_sender(request, subject, template_name, context, email): self.assertEqual(request, req) self.assertEqual(email, [ self.user.email, ]) change_token = UserEmailChangeTokenGenerator() token = change_token.generate(self.user, new_email) self.assertDictEqual(context, { 'token': token, }) self.assertEqual(subject, _("Email change")) self.assertEqual(template_name, 'foro/user/email_change_email.html') self._monkey_sender_called = True
def test_email_change_confirm(self): """ email change confirmation """ utils.login(self) new_email = "*****@*****.**" token = UserEmailChangeTokenGenerator().generate(self.user, new_email) response = self.client.get( reverse('foro:email-change-confirm', kwargs={'token': token})) expected_url = reverse("foro:profile-update") self.assertRedirects(response, expected_url, status_code=302) self.assertEqual(User.objects.get(pk=self.user.pk).email, new_email)
def test_email_change_confirm_invalid(self): """ The token should expire after email change """ utils.login(self) old_email = "*****@*****.**" token = UserEmailChangeTokenGenerator().generate(self.user, old_email) new_email = "*****@*****.**" User.objects.filter(pk=self.user.pk).update(email=new_email) response = self.client.get( reverse('foro:email-change-confirm', kwargs={'token': token})) expected_url = reverse("foro:profile-update") self.assertRedirects(response, expected_url, status_code=302) self.assertEqual(User.objects.get(pk=self.user.pk).email, new_email)
def test_user_email_change_token_generator(self): """ Email change """ new_email = "*****@*****.**" email_change_token = UserEmailChangeTokenGenerator() token = email_change_token.generate(self.user, new_email) self.assertTrue(email_change_token.is_valid(self.user, token)) self.assertFalse(email_change_token.is_valid(self.user, "bad token")) # get new email self.assertTrue(email_change_token.is_valid(self.user, token)) self.assertEqual(email_change_token.get_email(), new_email) # Invalid for different user user2 = test_utils.create_user() self.assertFalse(email_change_token.is_valid(user2, token)) # Invalid after email change self.user.email = "*****@*****.**" self.assertFalse(email_change_token.is_valid(self.user, token))