# HG changeset patch # User Brian Neal # Date 1388349926 21600 # Node ID 8789299c75b142c1a588aca30afaba5d29837cbb # Parent 66d46d31d543d75aa8bacfca574b2df3451db361 Django 1.6: test discovery as per unittest. diff -r 66d46d31d543 -r 8789299c75b1 accounts/tests/__init__.py --- a/accounts/tests/__init__.py Sun Dec 29 13:45:49 2013 -0600 +++ b/accounts/tests/__init__.py Sun Dec 29 14:45:26 2013 -0600 @@ -1,1 +0,0 @@ -from view_tests import * diff -r 66d46d31d543 -r 8789299c75b1 accounts/tests/test_views.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/accounts/tests/test_views.py Sun Dec 29 14:45:26 2013 -0600 @@ -0,0 +1,292 @@ +""" +View tests for the accounts application. + +""" +import datetime + +from django.test import TestCase +from django.core.urlresolvers import reverse +from django.core import mail +from django.contrib.auth.models import User +from django.contrib.auth.hashers import check_password + +from accounts.models import PendingUser +from accounts.models import IllegalUsername +from accounts.models import IllegalEmail + + +class RegistrationTest(TestCase): + + def setUp(self): + u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw') + u.save() + + # a 2nd user has the same email as another + u = User.objects.create_user('existing_user2', 'existing_user@example.com', 'pw') + u.save() + + PendingUser.objects.create(username='pending_user', + email='pending_user@example.com', + password='pw', + date_joined=datetime.datetime.now(), + key='key') + + IllegalUsername.objects.create(username='illegalusername') + IllegalEmail.objects.create(email='illegal@example.com') + + def test_get_view(self): + """ + Test a simple get of the registration view + + """ + response = self.client.get(reverse('accounts-register')) + self.assertEqual(response.status_code, 200) + + def test_existing_user(self): + """ + Ensure we can't register with an existing username. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'existing_user', + 'email': 'test@example.com', + 'password1': 'my_password', + 'password2': 'my_password', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': '', + }) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'A user with that username already exists') + + def test_pending_user(self): + """ + Ensure we can't register with a pending username. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'pending_user', + 'email': 'test@example.com', + 'password1': 'my_password', + 'password2': 'my_password', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': '', + }) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'A pending user with that username already exists') + + def test_illegal_username(self): + """ + Ensure we can't register with a banned username. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'illegalusername', + 'email': 'test@example.com', + 'password1': 'my_password', + 'password2': 'my_password', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': '', + }) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'That username is not allowed') + + def test_duplicate_existing_email(self): + """ + Ensure we can't register with a duplicate email address. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'a_new_user', + 'email': 'existing_user@example.com', + 'password1': 'my_password', + 'password2': 'my_password', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': '', + }) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'A user with that email address already exists') + + def test_duplicate_pending_email(self): + """ + Ensure we can't register with a duplicate email address. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'a_new_user', + 'email': 'pending_user@example.com', + 'password1': 'my_password', + 'password2': 'my_password', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': '', + }) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'A pending user with that email address already exists') + + def test_illegal_email(self): + """ + Ensure we can't register with a banned email address. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'a_new_user', + 'email': 'illegal@example.com', + 'password1': 'my_password', + 'password2': 'my_password', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': '', + }) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'That email address is not allowed') + + def test_password_match(self): + """ + Ensure the passwords match. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'a_new_user', + 'email': 'test@example.com', + 'password1': 'my_password', + 'password2': 'my_password_doesnt match', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': '', + }) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, "The two password fields didn't match") + + def test_question1(self): + """ + Ensure our anti-spam question is answered. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'a_new_user', + 'email': 'test@example.com', + 'password1': 'my_password', + 'password2': 'my_password_doesnt match', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': 'huh', + 'question2': '', + }) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, "Incorrect answer to our anti-spam question") + + def test_question2(self): + """ + Ensure our honeypot question check works. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'a_new_user', + 'email': 'test@example.com', + 'password1': 'my_password', + 'password2': 'my_password_doesnt match', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': 'non blank', + }) + + self.assertEqual(response.status_code, 200) + + def test_success(self): + """ + Ensure we can successfully register. + + """ + response = self.client.post(reverse('accounts-register'), { + 'username': 'a_new_user', + 'email': 'test@example.com', + 'password1': 'my_password', + 'password2': 'my_password', + 'agree_age': 'on', + 'agree_tos': 'on', + 'agree_privacy': 'on', + 'question1': '101', + 'question2': '', + }) + + self.assertEqual(response.status_code, 302) + + try: + pending = PendingUser.objects.get(username='a_new_user') + except PendingUser.DoesNotExist: + self.fail("PendingUser was not created") + + self.assertEqual(pending.email, 'test@example.com') + self.assertTrue(datetime.datetime.now() - pending.date_joined < + datetime.timedelta(minutes=1)) + self.assertTrue(check_password('my_password', pending.password)) + + +class ForgotUsernameTest(TestCase): + + def setUp(self): + u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw') + u.save() + + def test_get_query_view(self): + """Test a simple get of the username query view""" + response = self.client.get(reverse('accounts-username_query')) + self.assertEqual(response.status_code, 200) + + def test_get_username_sent_view(self): + """Test a simple get of the username sent view""" + response = self.client.get(reverse('accounts-username_sent')) + self.assertEqual(response.status_code, 200) + + def test_invalid_email(self): + """Test form submittal of unknown email address.""" + response = self.client.post(reverse('accounts-username_query'), { + 'email': 'bad_address@example.com', + }, + follow=True) + + self.assertRedirects(response, reverse('accounts-username_sent')) + + self.assertEqual(len(mail.outbox), 0) + + def test_valid_email(self): + """Test form submittal of valid email address.""" + response = self.client.post(reverse('accounts-username_query'), { + 'email': 'existing_user@example.com', + }, + follow=True) + + self.assertRedirects(response, reverse('accounts-username_sent')) + + self.assertEqual(len(mail.outbox), 1) + if len(mail.outbox): + self.assertTrue(mail.outbox[0].subject.startswith('Forgotten username')) diff -r 66d46d31d543 -r 8789299c75b1 accounts/tests/view_tests.py --- a/accounts/tests/view_tests.py Sun Dec 29 13:45:49 2013 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,292 +0,0 @@ -""" -View tests for the accounts application. - -""" -import datetime - -from django.test import TestCase -from django.core.urlresolvers import reverse -from django.core import mail -from django.contrib.auth.models import User -from django.contrib.auth.hashers import check_password - -from accounts.models import PendingUser -from accounts.models import IllegalUsername -from accounts.models import IllegalEmail - - -class RegistrationTest(TestCase): - - def setUp(self): - u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw') - u.save() - - # a 2nd user has the same email as another - u = User.objects.create_user('existing_user2', 'existing_user@example.com', 'pw') - u.save() - - PendingUser.objects.create(username='pending_user', - email='pending_user@example.com', - password='pw', - date_joined=datetime.datetime.now(), - key='key') - - IllegalUsername.objects.create(username='illegalusername') - IllegalEmail.objects.create(email='illegal@example.com') - - def test_get_view(self): - """ - Test a simple get of the registration view - - """ - response = self.client.get(reverse('accounts-register')) - self.assertEqual(response.status_code, 200) - - def test_existing_user(self): - """ - Ensure we can't register with an existing username. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'existing_user', - 'email': 'test@example.com', - 'password1': 'my_password', - 'password2': 'my_password', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': '', - }) - - self.assertEqual(response.status_code, 200) - self.assertContains(response, 'A user with that username already exists') - - def test_pending_user(self): - """ - Ensure we can't register with a pending username. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'pending_user', - 'email': 'test@example.com', - 'password1': 'my_password', - 'password2': 'my_password', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': '', - }) - - self.assertEqual(response.status_code, 200) - self.assertContains(response, 'A pending user with that username already exists') - - def test_illegal_username(self): - """ - Ensure we can't register with a banned username. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'illegalusername', - 'email': 'test@example.com', - 'password1': 'my_password', - 'password2': 'my_password', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': '', - }) - - self.assertEqual(response.status_code, 200) - self.assertContains(response, 'That username is not allowed') - - def test_duplicate_existing_email(self): - """ - Ensure we can't register with a duplicate email address. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'a_new_user', - 'email': 'existing_user@example.com', - 'password1': 'my_password', - 'password2': 'my_password', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': '', - }) - - self.assertEqual(response.status_code, 200) - self.assertContains(response, 'A user with that email address already exists') - - def test_duplicate_pending_email(self): - """ - Ensure we can't register with a duplicate email address. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'a_new_user', - 'email': 'pending_user@example.com', - 'password1': 'my_password', - 'password2': 'my_password', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': '', - }) - - self.assertEqual(response.status_code, 200) - self.assertContains(response, 'A pending user with that email address already exists') - - def test_illegal_email(self): - """ - Ensure we can't register with a banned email address. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'a_new_user', - 'email': 'illegal@example.com', - 'password1': 'my_password', - 'password2': 'my_password', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': '', - }) - - self.assertEqual(response.status_code, 200) - self.assertContains(response, 'That email address is not allowed') - - def test_password_match(self): - """ - Ensure the passwords match. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'a_new_user', - 'email': 'test@example.com', - 'password1': 'my_password', - 'password2': 'my_password_doesnt match', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': '', - }) - - self.assertEqual(response.status_code, 200) - self.assertContains(response, "The two password fields didn't match") - - def test_question1(self): - """ - Ensure our anti-spam question is answered. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'a_new_user', - 'email': 'test@example.com', - 'password1': 'my_password', - 'password2': 'my_password_doesnt match', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': 'huh', - 'question2': '', - }) - - self.assertEqual(response.status_code, 200) - self.assertContains(response, "Incorrect answer to our anti-spam question") - - def test_question2(self): - """ - Ensure our honeypot question check works. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'a_new_user', - 'email': 'test@example.com', - 'password1': 'my_password', - 'password2': 'my_password_doesnt match', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': 'non blank', - }) - - self.assertEqual(response.status_code, 200) - - def test_success(self): - """ - Ensure we can successfully register. - - """ - response = self.client.post(reverse('accounts-register'), { - 'username': 'a_new_user', - 'email': 'test@example.com', - 'password1': 'my_password', - 'password2': 'my_password', - 'agree_age': 'on', - 'agree_tos': 'on', - 'agree_privacy': 'on', - 'question1': '101', - 'question2': '', - }) - - self.assertEqual(response.status_code, 302) - - try: - pending = PendingUser.objects.get(username='a_new_user') - except PendingUser.DoesNotExist: - self.fail("PendingUser was not created") - - self.assertEqual(pending.email, 'test@example.com') - self.assertTrue(datetime.datetime.now() - pending.date_joined < - datetime.timedelta(minutes=1)) - self.assertTrue(check_password('my_password', pending.password)) - - -class ForgotUsernameTest(TestCase): - - def setUp(self): - u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw') - u.save() - - def test_get_query_view(self): - """Test a simple get of the username query view""" - response = self.client.get(reverse('accounts-username_query')) - self.assertEqual(response.status_code, 200) - - def test_get_username_sent_view(self): - """Test a simple get of the username sent view""" - response = self.client.get(reverse('accounts-username_sent')) - self.assertEqual(response.status_code, 200) - - def test_invalid_email(self): - """Test form submittal of unknown email address.""" - response = self.client.post(reverse('accounts-username_query'), { - 'email': 'bad_address@example.com', - }, - follow=True) - - self.assertRedirects(response, reverse('accounts-username_sent')) - - self.assertEqual(len(mail.outbox), 0) - - def test_valid_email(self): - """Test form submittal of valid email address.""" - response = self.client.post(reverse('accounts-username_query'), { - 'email': 'existing_user@example.com', - }, - follow=True) - - self.assertRedirects(response, reverse('accounts-username_sent')) - - self.assertEqual(len(mail.outbox), 1) - if len(mail.outbox): - self.assertTrue(mail.outbox[0].subject.startswith('Forgotten username')) diff -r 66d46d31d543 -r 8789299c75b1 antispam/tests/__init__.py --- a/antispam/tests/__init__.py Sun Dec 29 13:45:49 2013 -0600 +++ b/antispam/tests/__init__.py Sun Dec 29 14:45:26 2013 -0600 @@ -1,1 +0,0 @@ -from utils_tests import * diff -r 66d46d31d543 -r 8789299c75b1 antispam/tests/test_utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/antispam/tests/test_utils.py Sun Dec 29 14:45:26 2013 -0600 @@ -0,0 +1,115 @@ +""" +Tests for the antispam application. + +""" +import datetime + +from django.test import TestCase +from django.contrib.auth.models import User +from django.contrib.contenttypes.models import ContentType +from django.core.cache import cache + +from antispam import SPAM_PHRASE_KEY +from antispam.models import SpamPhrase +from antispam.utils import contains_spam, deactivate_spammer + +from comments.models import Comment +from polls.models import Poll +from elsewhere.models import WebsiteProfile +from shoutbox.models import Shout +from bio.models import STA_SPAMMER + + +class AntispamCase(TestCase): + + def test_no_phrases(self): + """ + Tests that an empty spam phrase table works. + + """ + cache.delete(SPAM_PHRASE_KEY) + self.assertFalse(contains_spam("Here is some random text.")) + + def test_phrases(self): + """ + Simple test of some phrases. + + """ + SpamPhrase.objects.create(phrase="grytner") + SpamPhrase.objects.create(phrase="allday.ru") + SpamPhrase.objects.create(phrase="stefa.pl") + + self.assert_(contains_spam("grytner")) + self.assert_(contains_spam("11grytner")) + self.assert_(contains_spam("11grytner>")) + self.assert_(contains_spam("1djkl jsd stefa.pl")) + self.assert_(contains_spam("1djkl jsd ")) - self.assert_(contains_spam("1djkl jsd stefa.pl")) - self.assert_(contains_spam("1djkl jsd