def test_old_signup_key(self): profile = SignUpProfile(email='*****@*****.**', expiry_date=datetime(1970, 1, 1)) profile.save() count = SignUpProfile.objects.count() response = self.client.get(reverse("signup_activate", kwargs=dict(signup_key=profile.signup_key))) self.assertRedirects(response, reverse("signup_invalid_key")) self.assertEqual(SignUpProfile.objects.count(), count - 1)
def test_send_activation_email(self): from signup.views import _send_activation_email from django.core import mail profile = SignUpProfile(email='*****@*****.**') profile.save() _send_activation_email(profile) self.assertEqual(len(mail.outbox), 1) msg = mail.outbox[0] self.assertTrue(profile.signup_key in msg.body) self.assertEqual(msg.to, [profile.email])
def test_has_key_expired(self): profile = SignUpProfile(email='*****@*****.**', expiry_date=datetime(1970, 1, 1)) profile.save() self.assertTrue(profile.has_key_expired) profile = SignUpProfile(email='*****@*****.**', expiry_date=datetime(2038, 1, 1)) profile.save() self.assertFalse(profile.has_key_expired)
def setUp(self): self.profile = SignUpProfile(email='*****@*****.**') self.profile.save()
class TestActivate(TestCase): def setUp(self): self.profile = SignUpProfile(email='*****@*****.**') self.profile.save() def test_activate_invalid_key(self): response = self.client.get(reverse("signup_activate", kwargs=dict(signup_key='zzz'))) self.assertRedirects(response, reverse("signup_invalid_key")) def test_has_key_expired(self): profile = SignUpProfile(email='*****@*****.**', expiry_date=datetime(1970, 1, 1)) profile.save() self.assertTrue(profile.has_key_expired) profile = SignUpProfile(email='*****@*****.**', expiry_date=datetime(2038, 1, 1)) profile.save() self.assertFalse(profile.has_key_expired) def test_old_signup_key(self): profile = SignUpProfile(email='*****@*****.**', expiry_date=datetime(1970, 1, 1)) profile.save() count = SignUpProfile.objects.count() response = self.client.get(reverse("signup_activate", kwargs=dict(signup_key=profile.signup_key))) self.assertRedirects(response, reverse("signup_invalid_key")) self.assertEqual(SignUpProfile.objects.count(), count - 1) def test_activate_user(self): """ Provide a valid signup key and create the user. """ url = reverse("signup_activate", kwargs=dict(signup_key=self.profile.signup_key)) response = self.client.post(url, dict(username='******', password='******')) self.assertRedirects(response, reverse("signup_success")) self.assertEqual(User.objects.count(), 1) user = User.objects.get(email='*****@*****.**') self.assertEqual(user.username, 'foo') self.assertEqual(SignUpProfile.objects.count(), 0) def test_user_clicks_activate_link(self): url = reverse("signup_activate", kwargs=dict(signup_key=self.profile.signup_key)) response = self.client.get(url) self.assertTemplateUsed(response, 'signup/activate_form.html') def test_activate_user_invalid_form(self): url = reverse("signup_activate", kwargs=dict(signup_key=self.profile.signup_key)) response = self.client.post(url) self.assertFormError(response, "form", "username", "This field is required.") self.assertFormError(response, "form", "password", "This field is required.") def test_activate_with_existing_user(self): """ Somebody has created a given username in the time between getting the signup_key and now activating it. """ User.objects.create_user('foo2', '*****@*****.**', 'password') url = reverse("signup_activate", kwargs=dict(signup_key=self.profile.signup_key)) response = self.client.post(url, dict(username='******', password='******')) self.assertFormError(response, "form", "username", "Username already in use.")