Permalink
Browse files

New tests for user activation.

  • Loading branch information...
1 parent 3eddaef commit ce1430cdbc8428eb438cd46ad2774f27a665a1d6 @onyxfish onyxfish committed May 9, 2012
Showing with 55 additions and 7 deletions.
  1. +1 −1 panda/tests/__init__.py
  2. +2 −0 panda/tests/test_user.py
  3. +52 −6 panda/tests/test_views.py
@@ -21,5 +21,5 @@
from panda.tests.test_related_upload import TestRelatedUpload
from panda.tests.test_user import TestUser
from panda.tests.test_utils import TestCSV, TestXLS, TestXLSX, TestTypeCoercion
-from panda.tests.test_views import TestLogin, TestActivate
+from panda.tests.test_views import TestLogin, TestActivate, TestForgotPassword
@@ -2,6 +2,7 @@
from django.conf import settings
from django.test import TransactionTestCase
+from django.utils.timezone import now
from django.contrib.auth.models import User
from panda.tests import utils
@@ -27,4 +28,5 @@ def test_create_user(self):
self.assertNotEqual(user_profile, None)
self.assertNotEqual(user_profile.activation_key, None)
+ self.assertGreater(user_profile.activation_key_expiration, now())
@@ -4,8 +4,9 @@
from django.test import TransactionTestCase
from django.test.client import Client
from django.utils import simplejson as json
+from django.utils.timezone import now
-from panda.models import User
+from panda.models import User, UserProfile
from panda.tests import utils
class TestLogin(TransactionTestCase):
@@ -69,7 +70,7 @@ def test_no_get(self):
self.assertEqual(body, None)
-class TestActivate(TransactionTestCase):
+class TestActivate(TransactionTestCase):
fixtures = ['init_panda.json']
def setUp(self):
@@ -84,15 +85,15 @@ def test_check_activation_key_valid(self):
is_active=False
)
- key = new_user.get_profile().activation_key
+ user_profile = new_user.get_profile()
- response = self.client.get('/check_activation_key/%s/' % key)
+ response = self.client.get('/check_activation_key/%s/' % user_profile.activation_key)
self.assertEqual(response.status_code, 200)
body = json.loads(response.content)
- self.assertEqual(body['activation_key'], key)
+ self.assertEqual(body['activation_key'], user_profile.activation_key)
self.assertEqual(body['email'], new_user.email)
self.assertEqual(body['first_name'], '')
self.assertEqual(body['last_name'], '')
@@ -109,8 +110,12 @@ def test_activate(self):
is_active=False
)
+ user_profile = new_user.get_profile()
+ self.assertNotEqual(user_profile.activation_key, None)
+ self.assertGreater(user_profile.activation_key_expiration, now())
+
activation_data = {
- 'activation_key': new_user.get_profile().activation_key,
+ 'activation_key': user_profile.activation_key,
'email': 'foo@bar.com',
'password': 'foobarbaz',
'reenter_password': 'foobarbaz',
@@ -124,3 +129,44 @@ def test_activate(self):
self.assertEqual(authenticate(username='foo@bar.com', password='foobarbaz'), new_user)
+ # Refresh
+ user_profile = UserProfile.objects.get(id=user_profile.id)
+
+ self.assertNotEqual(user_profile.activation_key, None)
+ self.assertLess(user_profile.activation_key_expiration, now())
+
+class TestForgotPassword(TransactionTestCase):
+ fixtures = ['init_panda.json']
+
+ def setUp(self):
+ self.user = utils.get_panda_user()
+
+ self.client = Client()
+
+ def test_forgot_password(self):
+ new_user = User.objects.create(
+ email="foo@bar.com",
+ username="foo@bar.com",
+ is_active=True
+ )
+
+ new_user.set_password('foobarbaz')
+ new_user.save()
+ self.assertEqual(authenticate(username='foo@bar.com', password='foobarbaz'), new_user)
+
+ # Force expiration date into the past
+ user_profile = new_user.get_profile()
+ user_profile.activation_key_expiration = now()
+ user_profile.save()
+
+ response = self.client.post('/forgot_password/', { 'email': 'foo@bar.com' })
+
+ self.assertEqual(response.status_code, 200)
+
+ # Refresh
+ user_profile = UserProfile.objects.get(id=user_profile.id)
+
+ # Expiration date should be pushed back into the future
+ self.assertNotEqual(user_profile.activation_key, None)
+ self.assertGreater(user_profile.activation_key_expiration, now())
+

0 comments on commit ce1430c

Please sign in to comment.