def registration_activation(request, pk, token): user = get_object_or_404(User, pk=pk) activation = UserActivationTokenGenerator() if activation.is_valid(user, token): user.is_active = True user.save() messages.info(request, _("Your account has been activated!")) return redirect(reverse('spirit:user-login'))
def monkey_sender(request, subject, template_name, context, email): self.assertEqual(request, req) self.assertEqual(email, [self.user.email, ]) activation_token = UserActivationTokenGenerator() token = activation_token.generate(self.user) self.assertDictEqual(context, {'token': token, 'user_id': self.user.pk}) self.assertEqual(subject, _("User activation")) self.assertEqual(template_name, 'spirit/user/activation_email.html') self._monkey_sender_called = True
def test_registration_activation(self): """ registration activation """ self.user.is_active = False self.user.save() token = UserActivationTokenGenerator().generate(self.user) response = self.client.get( reverse('spirit:registration-activation', kwargs={ 'pk': self.user.pk, 'token': token })) expected_url = reverse("spirit:user-login") self.assertRedirects(response, expected_url, status_code=302) self.assertTrue(User.objects.get(pk=self.user.pk).is_active)
def test_registration_activation_invalid(self): """ Activation token should expire after first login ActiveUserMiddleware required """ token = UserActivationTokenGenerator().generate(self.user) utils.login(self) User.objects.filter(pk=self.user.pk).update(is_active=False) response = self.client.get( reverse('spirit:registration-activation', kwargs={ 'pk': self.user.pk, 'token': token })) expected_url = reverse("spirit:user-login") self.assertRedirects(response, expected_url, status_code=302) self.assertFalse(User.objects.get(pk=self.user.pk).is_active)
def test_user_activation_token_generator(self): """ Validate if user can be activated """ activation_token = UserActivationTokenGenerator() token = activation_token.generate(self.user) self.assertTrue(activation_token.is_valid(self.user, token)) self.assertFalse(activation_token.is_valid(self.user, "bad token")) # Invalid for different user user2 = test_utils.create_user() self.assertFalse(activation_token.is_valid(user2, token)) # Invalid after login test_utils.login(self) user = test_utils.User.objects.get(pk=self.user.pk) self.assertFalse(activation_token.is_valid(user, token))
def test_user_activation_token_generator(self): """ Validate if user can be activated """ self.user.is_verified = False activation_token = UserActivationTokenGenerator() token = activation_token.generate(self.user) self.assertTrue(activation_token.is_valid(self.user, token)) self.assertFalse(activation_token.is_valid(self.user, "bad token")) # Invalid after verification self.user.is_verified = True self.assertFalse(activation_token.is_valid(self.user, token)) # Invalid for different user user2 = test_utils.create_user() self.assertFalse(activation_token.is_valid(user2, token))