bgneal@565: """ bgneal@565: View tests for the accounts application. bgneal@565: bgneal@565: """ bgneal@565: import datetime bgneal@565: bgneal@565: from django.test import TestCase bgneal@565: from django.core.urlresolvers import reverse bgneal@576: from django.contrib.auth.models import User bgneal@576: from django.contrib.auth.hashers import check_password bgneal@565: bgneal@565: from antispam.rate_limit import unblock_ip bgneal@565: from accounts.models import PendingUser bgneal@565: from accounts.models import IllegalUsername bgneal@565: from accounts.models import IllegalEmail bgneal@565: bgneal@565: bgneal@565: class RegistrationTest(TestCase): bgneal@565: bgneal@565: def setUp(self): bgneal@565: u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw') bgneal@565: u.save() bgneal@565: bgneal@565: # a 2nd user has the same email as another bgneal@565: u = User.objects.create_user('existing_user2', 'existing_user@example.com', 'pw') bgneal@565: u.save() bgneal@565: bgneal@565: PendingUser.objects.create(username='pending_user', bgneal@565: email='pending_user@example.com', bgneal@565: password='pw', bgneal@565: date_joined=datetime.datetime.now(), bgneal@565: key='key') bgneal@565: bgneal@565: IllegalUsername.objects.create(username='illegalusername') bgneal@565: IllegalEmail.objects.create(email='illegal@example.com') bgneal@565: bgneal@565: def tearDown(self): bgneal@565: unblock_ip('127.0.0.1') bgneal@565: bgneal@565: def test_get_view(self): bgneal@565: """ bgneal@565: Test a simple get of the registration view bgneal@565: bgneal@565: """ bgneal@565: response = self.client.get(reverse('accounts-register')) bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: bgneal@565: def test_existing_user(self): bgneal@565: """ bgneal@565: Ensure we can't register with an existing username. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'existing_user', bgneal@565: 'email': 'test@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: self.assertContains(response, 'A user with that username already exists') bgneal@565: bgneal@565: def test_pending_user(self): bgneal@565: """ bgneal@565: Ensure we can't register with a pending username. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'pending_user', bgneal@565: 'email': 'test@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: self.assertContains(response, 'A pending user with that username already exists') bgneal@565: bgneal@565: def test_illegal_username(self): bgneal@565: """ bgneal@565: Ensure we can't register with a banned username. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'illegalusername', bgneal@565: 'email': 'test@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: self.assertContains(response, 'That username is not allowed') bgneal@565: bgneal@565: def test_duplicate_existing_email(self): bgneal@565: """ bgneal@565: Ensure we can't register with a duplicate email address. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'a_new_user', bgneal@565: 'email': 'existing_user@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: self.assertContains(response, 'A user with that email address already exists') bgneal@565: bgneal@565: def test_duplicate_pending_email(self): bgneal@565: """ bgneal@565: Ensure we can't register with a duplicate email address. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'a_new_user', bgneal@565: 'email': 'pending_user@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: self.assertContains(response, 'A pending user with that email address already exists') bgneal@565: bgneal@565: def test_illegal_email(self): bgneal@565: """ bgneal@565: Ensure we can't register with a banned email address. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'a_new_user', bgneal@565: 'email': 'illegal@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: self.assertContains(response, 'That email address is not allowed') bgneal@565: bgneal@565: def test_password_match(self): bgneal@565: """ bgneal@565: Ensure the passwords match. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'a_new_user', bgneal@565: 'email': 'test@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password_doesnt match', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: self.assertContains(response, "The two password fields didn't match") bgneal@565: bgneal@565: def test_question1(self): bgneal@565: """ bgneal@565: Ensure our anti-spam question is answered. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'a_new_user', bgneal@565: 'email': 'test@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password_doesnt match', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': 'huh', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 200) bgneal@565: self.assertContains(response, "Incorrect answer to our anti-spam question") bgneal@565: bgneal@565: def test_question2(self): bgneal@565: """ bgneal@565: Ensure our honeypot question check works. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'a_new_user', bgneal@565: 'email': 'test@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password_doesnt match', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': 'non blank', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 403) bgneal@565: bgneal@565: def test_success(self): bgneal@565: """ bgneal@565: Ensure we can successfully register. bgneal@565: bgneal@565: """ bgneal@565: response = self.client.post(reverse('accounts-register'), { bgneal@565: 'username': 'a_new_user', bgneal@565: 'email': 'test@example.com', bgneal@565: 'password1': 'my_password', bgneal@565: 'password2': 'my_password', bgneal@565: 'agree_age': 'on', bgneal@565: 'agree_tos': 'on', bgneal@565: 'agree_privacy': 'on', bgneal@565: 'question1': '101', bgneal@565: 'question2': '', bgneal@565: }) bgneal@565: bgneal@565: self.assertEqual(response.status_code, 302) bgneal@565: bgneal@565: try: bgneal@565: pending = PendingUser.objects.get(username='a_new_user') bgneal@565: except PendingUser.DoesNotExist: bgneal@565: self.fail("PendingUser was not created") bgneal@565: bgneal@565: self.assertEqual(pending.email, 'test@example.com') bgneal@565: self.assertTrue(datetime.datetime.now() - pending.date_joined < bgneal@565: datetime.timedelta(minutes=1)) bgneal@565: self.assertTrue(check_password('my_password', pending.password))