comparison accounts/tests/view_tests.py @ 659:8e6b8ffe5f34

For issue #31, implement a forgot username feature.
author Brian Neal <bgneal@gmail.com>
date Sat, 11 May 2013 23:39:46 -0500
parents ee87ea74d46b
children 988782c6ce6c
comparison
equal deleted inserted replaced
658:2adf01661ac5 659:8e6b8ffe5f34
4 """ 4 """
5 import datetime 5 import datetime
6 6
7 from django.test import TestCase 7 from django.test import TestCase
8 from django.core.urlresolvers import reverse 8 from django.core.urlresolvers import reverse
9 from django.core import mail
9 from django.contrib.auth.models import User 10 from django.contrib.auth.models import User
10 from django.contrib.auth.hashers import check_password 11 from django.contrib.auth.hashers import check_password
11 12
12 from antispam.rate_limit import unblock_ip 13 from antispam.rate_limit import unblock_ip
13 from accounts.models import PendingUser 14 from accounts.models import PendingUser
250 251
251 self.assertEqual(pending.email, 'test@example.com') 252 self.assertEqual(pending.email, 'test@example.com')
252 self.assertTrue(datetime.datetime.now() - pending.date_joined < 253 self.assertTrue(datetime.datetime.now() - pending.date_joined <
253 datetime.timedelta(minutes=1)) 254 datetime.timedelta(minutes=1))
254 self.assertTrue(check_password('my_password', pending.password)) 255 self.assertTrue(check_password('my_password', pending.password))
256
257
258 class ForgotUsernameTest(TestCase):
259
260 def setUp(self):
261 u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw')
262 u.save()
263
264 def test_get_query_view(self):
265 """Test a simple get of the username query view"""
266 response = self.client.get(reverse('accounts-username_query'))
267 self.assertEqual(response.status_code, 200)
268
269 def test_get_username_sent_view(self):
270 """Test a simple get of the username sent view"""
271 response = self.client.get(reverse('accounts-username_sent'))
272 self.assertEqual(response.status_code, 200)
273
274 def test_invalid_email(self):
275 """Test form submittal of unknown email address."""
276 response = self.client.post(reverse('accounts-username_query'), {
277 'email': 'bad_address@example.com',
278 },
279 follow=True)
280
281 self.assertRedirects(response, reverse('accounts-username_sent'))
282
283 self.assertEqual(len(mail.outbox), 0)
284
285 def test_valid_email(self):
286 """Test form submittal of valid email address."""
287 response = self.client.post(reverse('accounts-username_query'), {
288 'email': 'existing_user@example.com',
289 },
290 follow=True)
291
292 self.assertRedirects(response, reverse('accounts-username_sent'))
293
294 self.assertEqual(len(mail.outbox), 1)
295 if len(mail.outbox):
296 self.assertTrue(mail.outbox[0].subject.startswith('Forgotten username'))