Permalink
Browse files

added some tests.

  • Loading branch information...
1 parent fd069c5 commit bb29753d6d3677ed0dd1f852f16de62d391d0102 @degenhard degenhard committed Mar 11, 2013
View
@@ -1,4 +1,4 @@
test:
flake8 userprofiles --ignore=E501,E128
coverage run --branch --source=userprofiles `which django-admin.py` test --settings=test_project.settings userprofiles
- coverage report --show-missing --omit=userprofiles/test*
+ coverage report --show-missing --omit=*test*
View
@@ -16,6 +16,7 @@
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
+ 'django.contrib.sites',
'userprofiles',
'userprofiles.contrib.accountverification',
'userprofiles.contrib.emailverification',
@@ -30,3 +31,4 @@
ROOT_URLCONF = 'test_project.urls'
SECRET_KEY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
+SITE_ID = 1
@@ -0,0 +1,58 @@
+from datetime import timedelta
+from django.test import TestCase
+from userprofiles.contrib.accountverification.models import AccountVerification
+from userprofiles.settings import up_settings
+
+
+class ViewTests(TestCase):
+ def setUp(self):
+ self.data = {
+ 'username': 'newuser',
+ 'email': 'newuser@example.com',
+ 'email_repeat': 'newuser@example.com',
+ 'password': 'newuserpass',
+ 'password_repeat': 'newuserpass',
+ 'first_name': 'New',
+ 'last_name': 'User',
+ }
+
+ def test_activation(self):
+ user = AccountVerification.objects.create_inactive_user(
+ self.data['username'], self.data['password'], self.data['email'])
+ user.date_joined = user.date_joined - timedelta(days=up_settings.ACCOUNT_VERIFICATION_DAYS + 1)
+ user.save()
+ verification = AccountVerification.objects.get(user=user)
+
+ self.assertFalse(
+ AccountVerification.objects.activate_user('wrong-pattern-format'))
+
+ self.assertFalse(
+ AccountVerification.objects.activate_user('f4a80274f851cb41ef9c20d00426d72fc4874471'))
+
+ self.assertFalse(
+ AccountVerification.objects.activate_user(verification.activation_key))
+
+ def test_delete_expired_users(self):
+ user = AccountVerification.objects.create_inactive_user(
+ self.data['username'], self.data['password'], self.data['email'])
+
+ user.is_active = True
+ user.save()
+ AccountVerification.objects.delete_expired_users()
+
+ user.date_joined = user.date_joined - timedelta(days=up_settings.ACCOUNT_VERIFICATION_DAYS + 1)
+ user.save()
+ AccountVerification.objects.delete_expired_users()
+ self.assertTrue(AccountVerification.objects.all().exists())
+
+ user.is_active = False
+ user.save()
+ AccountVerification.objects.delete_expired_users()
+ self.assertFalse(AccountVerification.objects.all().exists())
+
+ def test_unicode(self):
+ user = AccountVerification.objects.create_inactive_user(
+ self.data['username'], self.data['password'], self.data['email'])
+ self.assertEqual(
+ AccountVerification.objects.get(user=user).__unicode__(),
+ 'Account verification: %s' % user.username)
@@ -0,0 +1,38 @@
+from django.core.urlresolvers import reverse
+from django.test import TestCase
+from django.test.utils import override_settings
+from userprofiles.contrib.accountverification.models import AccountVerification
+from django.core import mail
+
+import re
+
+
+class ViewTests(TestCase):
+ def setUp(self):
+ self.data = {
+ 'username': 'newuser',
+ 'email': 'newuser@example.com',
+ 'email_repeat': 'newuser@example.com',
+ 'password': 'newuserpass',
+ 'password_repeat': 'newuserpass',
+ 'first_name': 'New',
+ 'last_name': 'User',
+ }
+
+ @override_settings(USE_ACCOUNT_VERIFICATION=True)
+ def test_registration_activate(self):
+ AccountVerification.objects.create_inactive_user(
+ username=self.data['username'],
+ password=self.data['password'],
+ email=self.data['email'])
+ mailbody = mail.outbox[0].body
+
+ activation_key = re.findall(
+ r'http://example.com/userprofiles/activate/(\w+)', mailbody, re.MULTILINE)[0]
+
+ url = reverse('userprofiles_registration_activate',
+ kwargs={'activation_key': activation_key})
+ response = self.client.get(url)
+ self.assertTrue(
+ 'We activated your account. You are now able to log in. Have fun!' in
+ response.content)
@@ -0,0 +1,56 @@
+from django.core.urlresolvers import reverse
+from django.test import TestCase
+from django.contrib.auth.models import User
+from django.test.utils import override_settings
+from userprofiles.contrib.emailverification.models import EmailVerification
+from userprofiles.settings import up_settings
+
+
+class ViewTests(TestCase):
+
+ def setUp(self):
+ self.data = {
+ 'username': 'newuser',
+ 'email': 'newuser@example.com',
+ 'email_repeat': 'newuser@example.com',
+ 'password': 'newuserpass',
+ 'password_repeat': 'newuserpass',
+ 'first_name': 'New',
+ 'last_name': 'User',
+ }
+
+ self.user = User.objects.create_user(self.data['username'], self.data['email'],
+ self.data['password'])
+
+ def tearDown(self):
+ self.user.delete()
+
+ @override_settings(USERPROFILES_USE_PROFILE=False, USERPROFILES_USE_ACCOUNT_VERIFICATION=True)
+ def test_email_change(self):
+ self.client.login(username=self.data['username'], password=self.data['password'])
+ response = self.client.post(reverse('userprofiles_email_change'), {'new_email': 'test@example.com'}, follow=True)
+ self.assertEqual(response.context['expiration_days'], up_settings.EMAIL_VERIFICATION_DAYS)
+
+ @override_settings(USERPROFILES_USE_PROFILE=False, USERPROFILES_USE_ACCOUNT_VERIFICATION=True)
+ def test_email_change_approve(self):
+ verification = EmailVerification.objects.create(
+ user=self.user, old_email=self.user.email, new_email='test@example.com')
+
+ self.client.login(username=self.data['username'], password=self.data['password'])
+ url = reverse('userprofiles_email_change_approve',
+ kwargs={'token': verification.token, 'code': verification.code})
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual(User.objects.get(pk=self.user.pk).email, 'test@example.com')
+
+ @override_settings(USERPROFILES_USE_PROFILE=False, USERPROFILES_USE_ACCOUNT_VERIFICATION=True)
+ def test_email_change_approve_not_exists(self):
+ verification = EmailVerification.objects.create(
+ user=self.user, old_email=self.user.email, new_email='test@example.com')
+
+ self.client.login(username=self.data['username'], password=self.data['password'])
+ url = reverse('userprofiles_email_change_approve',
+ kwargs={'token': verification.token, 'code': 'wrong-code'})
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual(User.objects.get(pk=self.user.pk).email, self.user.email)
@@ -0,0 +1,55 @@
+from django.test import TestCase
+from django.core.urlresolvers import reverse
+from django.contrib.auth.models import User
+from django.test.utils import override_settings
+from django.conf import settings
+from test_project.test_accounts.models import Profile
+
+
+@override_settings(INSTALLED_APPS=settings.INSTALLED_APPS + ['userprofiles.contrib.profiles'])
+class ViewTests(TestCase):
+ def setUp(self):
+ self.data = {
+ 'username': 'newuser',
+ 'email': 'newuser@example.com',
+ 'email_repeat': 'newuser@example.com',
+ 'password': 'newuserpass',
+ 'password_repeat': 'newuserpass',
+ 'first_name': 'New',
+ 'last_name': 'User',
+ }
+ user = User.objects.create_user(self.data['username'], self.data['email'],
+ self.data['password'])
+ Profile(user=user).save()
+
+ def tearDown(self):
+ User.objects.get(username=self.data['username']).delete()
+
+ @override_settings(USERPROFILES_USE_PROFILE=True)
+ def test_profile_view(self):
+ self.client.login(username=self.data['username'], password=self.data['password'])
+ url = reverse('userprofiles_profile')
+ response = self.client.get(url)
+ self.assertTrue('newuser' in response.content)
+
+ @override_settings(USERPROFILES_USE_PROFILE=True, USERPROFILES_REGISTRATION_FULLNAME=True)
+ def test_profile_change_fullname_enabled(self):
+ settings.AUTH_PROFILE_MODULE = 'test_accounts.Profile'
+ self.client.login(username=self.data['username'], password=self.data['password'])
+ url = reverse('userprofiles_profile_change')
+ response = self.client.get(url)
+ self.assertTrue('first_name' in response.content and
+ 'last_name' in response.content)
+ self.client.post(url, {'first_name': 'john', 'last_name': 'doe'})
+ response = self.client.get(url)
+ self.assertTrue('john' in response.content and
+ 'doe' in response.content)
+
+ @override_settings(USERPROFILES_USE_PROFILE=True, USERPROFILES_REGISTRATION_FULLNAME=False)
+ def test_profile_change_fullname_disabled(self):
+ settings.AUTH_PROFILE_MODULE = 'test_accounts.Profile'
+ self.client.login(username=self.data['username'], password=self.data['password'])
+ url = reverse('userprofiles_profile_change')
+ response = self.client.get(url)
+ self.assertFalse('first_name' in response.content and
+ 'last_name' in response.content)

0 comments on commit bb29753

Please sign in to comment.