Skip to content
This repository has been archived by the owner on Dec 23, 2018. It is now read-only.

Commit

Permalink
Merge branch 'tests' into cbv
Browse files Browse the repository at this point in the history
  • Loading branch information
degenhard committed Mar 11, 2013
2 parents abb3c09 + bb29753 commit 1508a76
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -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*
2 changes: 2 additions & 0 deletions test_project/settings.py
Expand Up @@ -16,6 +16,7 @@
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'userprofiles',
'userprofiles.contrib.accountverification',
'userprofiles.contrib.emailverification',
Expand All @@ -30,3 +31,4 @@
ROOT_URLCONF = 'test_project.urls'

SECRET_KEY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
SITE_ID = 1
Empty file.
58 changes: 58 additions & 0 deletions userprofiles/contrib/accountverification/tests/test_models.py
@@ -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)
38 changes: 38 additions & 0 deletions userprofiles/contrib/accountverification/tests/test_views.py
@@ -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)
1 change: 0 additions & 1 deletion userprofiles/contrib/accountverification/views.py
Expand Up @@ -17,4 +17,3 @@ def get_context_data(self, **kwargs):
}

registration_activate = RegistrationActivateView.as_view()

Empty file.
56 changes: 56 additions & 0 deletions userprofiles/contrib/emailverification/tests/test_views.py
@@ -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)
3 changes: 2 additions & 1 deletion userprofiles/contrib/emailverification/views.py
Expand Up @@ -37,10 +37,11 @@ def get_context_data(self, **kwargs):

class EmailChangeApproveView(LoginRequiredMixin, RedirectView):
permanent = False

def get_redirect_url(self, token, code):
try:
verification = EmailVerification.objects.get(token=token, code=code,
user=self.request.user,is_expired=False, is_approved=False)
user=self.request.user, is_expired=False, is_approved=False)
verification.is_approved = True
verification.save()
messages.success(self.request, _(u'E-mail address changed to %(email)s' % {
Expand Down
Empty file.
55 changes: 55 additions & 0 deletions userprofiles/contrib/profiles/tests/test_views.py
@@ -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)
2 changes: 1 addition & 1 deletion userprofiles/contrib/profiles/views.py
Expand Up @@ -4,7 +4,7 @@
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView, FormView

from userprofiles import settings as up_settings
from userprofiles.settings import up_settings
from userprofiles.mixins import LoginRequiredMixin
from userprofiles.utils import get_form_class

Expand Down

0 comments on commit 1508a76

Please sign in to comment.