aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'okupy/tests')
-rw-r--r--okupy/tests/integration/test_signup.py227
-rw-r--r--okupy/tests/unit/test_signup.py266
2 files changed, 317 insertions, 176 deletions
diff --git a/okupy/tests/integration/test_signup.py b/okupy/tests/integration/test_signup.py
index 3daabf2..0dc9984 100644
--- a/okupy/tests/integration/test_signup.py
+++ b/okupy/tests/integration/test_signup.py
@@ -4,203 +4,88 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.db import DatabaseError
+from django.test import TestCase
from django.test.client import Client
from mockldap import MockLdap
-from ...accounts.models import Queue
-from ...common.test_helpers import OkupyTestCase
+from ...accounts.models import Queue, LDAPUser
+from ...common.test_helpers import set_search_seed, ldap_users
import mock
-class SignupTestsEmptyDB(OkupyTestCase):
- form_data = {
- 'username': 'testusername',
- 'first_name': 'testfirstname',
- 'last_name': 'testlastname',
- 'email': 'test@test.com',
- 'password_origin': 'testpassword',
- 'password_verify': 'testpassword',
- }
- def setUp(self):
- self.client = Client()
-
- def test_template(self):
- response = self.client.get('/signup/')
- self.assertIn('signup_form', response.context)
- self.assertIn('messages', response.context)
-
- def test_empty_form(self):
- response = self.client.post('/signup/')
- self.assertFormError(response, 'signup_form', 'username', [u'This field is required.'])
- self.assertFormError(response, 'signup_form', 'first_name', [u'This field is required.'])
- self.assertFormError(response, 'signup_form', 'last_name', [u'This field is required.'])
- self.assertFormError(response, 'signup_form', 'email', [u'This field is required.'])
- self.assertFormError(response, 'signup_form', 'password_origin', [u'This field is required.'])
- self.assertFormError(response, 'signup_form', 'password_verify', [u'This field is required.'])
- self.assertEqual(Queue.objects.count(), 0)
-
- def test_passwords_dont_match(self):
- form_data = self.form_data.copy()
- form_data['password_verify'] = 'testpassword2'
- response = self.client.post('/signup/', form_data)
- self.assertMessage(response, "Passwords don't match", 40)
- self.assertEqual(Queue.objects.count(), 0)
-
- def test_invalid_email(self):
- form_data = self.form_data.copy()
- form_data['email'] = 'test'
- response = self.client.post('/signup/', form_data)
- self.assertFormError(response, 'signup_form', 'email', [u'Enter a valid email address.'])
- self.assertEqual(Queue.objects.count(), 0)
-
-class SignupTestsOneAccountInQueue(OkupyTestCase):
- fixtures = ['queued_account.json']
+form_data = {
+ 'username': 'testusername',
+ 'first_name': 'testfirstname',
+ 'last_name': 'testlastname',
+ 'email': 'test@test.com',
+ 'password_origin': 'testpassword',
+ 'password_verify': 'testpassword',
+}
- cursor_wrapper = mock.Mock()
- cursor_wrapper.side_effect = DatabaseError
+queued_account = Queue(
+ username='queueduser',
+ password='queuedpass',
+ email='queued_user@test.com',
+ first_name='queued_first_name',
+ last_name='queued_last_name',
+)
- form_data = {
- 'username': 'testusername',
- 'first_name': 'testfirstname',
- 'last_name': 'testlastname',
- 'email': 'test@test.com',
- 'password_origin': 'testpassword',
- 'password_verify': 'testpassword',
- }
+class SignupIntegrationTests(TestCase):
@classmethod
def setUpClass(cls):
cls.mockldap = MockLdap(settings.DIRECTORY)
def setUp(self):
self.client = Client()
- self.queued_account = Queue.objects.get(pk=1)
- self.activate_url = '/activate/%s/' % self.queued_account.encrypted_id
self.mockldap.start()
self.ldapobject = self.mockldap[settings.AUTH_LDAP_SERVER_URI]
def tearDown(self):
self.mockldap.stop()
- def test_add_queued_account_to_ldap(self):
- response = self.client.get(self.activate_url)
- self.assertRedirects(response, '/login/')
- self.assertMessage(response, 'Your account has been activated successfully', 25)
- self.assertEqual(Queue.objects.count(), 0)
- ldap_account = self.ldapobject.directory['uid=%s,ou=people,o=test' % self.queued_account.username]
- self.assertEqual(ldap_account['uid'][0], self.queued_account.username)
- self.assertEqual(ldap_account['givenName'][0], self.queued_account.first_name)
- self.assertEqual(ldap_account['sn'][0], self.queued_account.last_name)
- self.assertEqual(ldap_account['objectClass'], settings.AUTH_LDAP_USER_OBJECTCLASS)
- self.assertEqual(ldap_account['uidNumber'][0], '1002')
- self.assertEqual(ldap_account['mail'][0], self.queued_account.email)
- data = {'username': self.queued_account.username, 'password': 'queuedpass'}
- response = self.client.post('/login/', data)
- self.assertRedirects(response, '/')
- self.assertEqual(User.objects.count(), 1)
- response = self.client.get(self.activate_url)
- self.assertMessage(response, 'Invalid URL', 40)
-
- def test_signup_no_ldap(self):
- self.mockldap.stop()
- response = self.client.post('/signup/', self.form_data)
- self.assertMessage(response, "Can't contact LDAP server", 40)
- self.assertEqual(Queue.objects.count(), 1)
- self.assertEqual(len(mail.outbox), 1)
- self.assertEqual(mail.outbox[0].subject, '%sERROR: {\'desc\': "Can\'t contact LDAP server"}' % settings.EMAIL_SUBJECT_PREFIX)
- self.mockldap.start()
-
- def test_activate_no_ldap(self):
- self.mockldap.stop()
- response = self.client.get(self.activate_url)
- self.assertRedirects(response, '/login/')
- self.assertMessage(response, "Can't contact LDAP server", 40)
- self.assertEqual(Queue.objects.count(), 1)
- self.assertEqual(len(mail.outbox), 1)
- self.assertEqual(mail.outbox[0].subject, '%sERROR: {\'desc\': "Can\'t contact LDAP server"}' % settings.EMAIL_SUBJECT_PREFIX)
- self.mockldap.start()
+ def test_signup_page_uses_correct_template(self):
+ response = self.client.get('/signup/')
+ self.assertTemplateUsed(response, 'signup.html')
- def test_wrong_activation_link(self):
+ def test_wrong_activation_link_redirects_to_login(self):
+ queued_account.save()
response = self.client.get('/activate/invalidurl/')
- self.assertRedirects(response, '/login/')
- self.assertMessage(response, 'Invalid URL', 40)
- self.assertEqual(Queue.objects.count(), 1)
+ self.assertRedirects(response, '/login/', 302, 200)
- def test_username_already_exists_in_ldap(self):
- form_data = self.form_data.copy()
- form_data['username'] = 'alice'
+ def test_valid_data_to_signup_redirects_to_login(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('testusername'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('test@test.com', attr='mail'))(LDAPUser.DoesNotExist)
response = self.client.post('/signup/', form_data)
- self.assertMessage(response, 'Username already exists', 40)
+ self.assertRedirects(response, '/login/', 302, 200)
- def test_email_already_exists_in_ldap(self):
- form_data = self.form_data.copy()
- form_data['email'] = 'alice@test.com'
- response = self.client.post('/signup/', form_data)
- self.assertMessage(response, 'Email already exists', 40)
+ def test_logged_in_user_signup_url_redirects_to_index(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')])
+ self.client.post('/login/', {'username': 'alice', 'password': 'ldaptest'})
+ response = self.client.get('/signup/')
+ self.assertRedirects(response, '/', 302, 200)
- def test_username_already_pending_activation(self):
- form_data = self.form_data.copy()
- form_data['username'] = 'queueduser'
- response = self.client.post('/signup/', form_data)
- self.assertMessage(response, 'Account is already pending activation', 40)
- self.assertEqual(Queue.objects.count(), 1)
+ def test_logged_in_user_activate_url_redirects_to_index(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')])
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed())(ldap_users(all=True))
+ queued_account.save()
+ activate_url = '/activate/%s/' % queued_account.encrypted_id
+ self.client.post('/login/', {'username': 'alice', 'password': 'ldaptest'})
+ response = self.client.get(activate_url)
+ self.assertRedirects(response, '/', 302, 200)
- def test_email_already_pending_activation(self):
- form_data = self.form_data.copy()
- form_data['email'] = 'queueduser@test.com'
- response = self.client.post('/signup/', form_data)
- self.assertMessage(response, 'Account is already pending activation', 40)
- self.assertEqual(Queue.objects.count(), 1)
-
- def test_valid_data_to_form(self):
- response = self.client.post('/signup/', self.form_data)
- self.assertRedirects(response, '/login/')
- self.assertMessage(response, 'You will shortly receive an activation mail', 20)
- self.assertEqual(len(mail.outbox), 1)
- self.assertEqual(mail.outbox[0].subject, '%sAccount Activation' % settings.EMAIL_SUBJECT_PREFIX)
- self.assertEqual(Queue.objects.count(), 2)
- queued_account = Queue.objects.get(pk=2)
- self.assertEqual(queued_account.username, self.form_data['username'])
- self.assertEqual(queued_account.first_name, self.form_data['first_name'])
- self.assertEqual(queued_account.last_name, self.form_data['last_name'])
- self.assertEqual(queued_account.email, self.form_data['email'])
- self.assertEqual(queued_account.password, self.form_data['password_origin'])
- # note: this needs to be kept in line with used cipher
- self.assertRegexpMatches(queued_account.encrypted_id, '^[a-f0-9]{16}$')
-
- @mock.patch("django.db.backends.util.CursorWrapper", cursor_wrapper)
- def test_signup_no_database(self):
- response = self.client.post('/signup/', self.form_data)
- self.assertMessage(response, "Can't contact the database", 40)
- self.assertEqual(len(mail.outbox), 1)
- self.assertTrue(mail.outbox[0].subject.startswith('%sERROR:' % settings.EMAIL_SUBJECT_PREFIX))
-
- @mock.patch("django.db.backends.util.CursorWrapper", cursor_wrapper)
- def test_activate_no_database(self):
- response = self.client.post(self.activate_url)
- self.assertMessage(response, "Can't contact the database", 40)
- self.assertEqual(len(mail.outbox), 1)
- self.assertTrue(mail.outbox[0].subject.startswith('%sERROR:' % settings.EMAIL_SUBJECT_PREFIX))
-
- def test_add_first_user_empty_ldap_directory(self):
- for key in self.ldapobject.directory.keys():
- if key.startswith(settings.AUTH_LDAP_USER_ATTR) and key.endswith(settings.AUTH_LDAP_USER_BASE_DN):
- del self.ldapobject.directory[key]
- response = self.client.post(self.activate_url)
- self.assertRedirects(response, '/login/')
- self.assertMessage(response, 'Your account has been activated successfully', 25)
- self.assertEqual(Queue.objects.count(), 0)
- ldap_account = self.ldapobject.directory['uid=%s,ou=people,o=test' % self.queued_account.username]
- self.assertEqual(ldap_account['uid'][0], self.queued_account.username)
- self.assertEqual(ldap_account['givenName'][0], self.queued_account.first_name)
- self.assertEqual(ldap_account['sn'][0], self.queued_account.last_name)
- self.assertEqual(ldap_account['objectClass'], settings.AUTH_LDAP_USER_OBJECTCLASS)
- self.assertEqual(ldap_account['uidNumber'][0], '1')
- self.assertEqual(ldap_account['mail'][0], self.queued_account.email)
- data={'username': self.queued_account.username, 'password': 'queuedpass'}
- response = self.client.post('/login/', data)
- self.assertRedirects(response, '/')
- self.assertEqual(User.objects.count(), 1)
- response = self.client.get(self.activate_url)
- self.assertMessage(response, 'Invalid URL', 40)
+
+class SignupIntegrationTestsNoLDAP(TestCase):
+ def setUp(self):
+ self.client = Client()
+
+ def test_activate_no_ldap_connection_redirects_to_login(self):
+ queued_account.save()
+ activate_url = '/activate/%s/' % queued_account.encrypted_id
+ response = self.client.get(activate_url)
+ self.assertRedirects(response, '/login/', 302, 200)
+
+ def test_activate_page_without_token_returns_404(self):
+ response = self.client.get('/activate/')
+ self.assertTrue(response.status_code, 404)
diff --git a/okupy/tests/unit/test_signup.py b/okupy/tests/unit/test_signup.py
index 071c68e..9c4314e 100644
--- a/okupy/tests/unit/test_signup.py
+++ b/okupy/tests/unit/test_signup.py
@@ -1,18 +1,274 @@
# vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python
+from django.conf import settings
+from django.core import mail
from django.core.urlresolvers import resolve
-from django.test import TestCase
+from django.template import RequestContext
-from ...accounts.views import signup
-from ...common.test_helpers import set_request
+from mockldap import MockLdap
+from passlib.hash import ldap_md5_crypt
+from ...accounts.forms import SignupForm
+from ...accounts.models import LDAPUser, Queue
+from ...accounts.views import signup, activate
+from ...common.test_helpers import OkupyTestCase, set_request, set_search_seed, ldap_users, no_database
-class SignupUnitTests(TestCase):
+
+form_data = {
+ 'username': 'testusername',
+ 'first_name': 'testfirstname',
+ 'last_name': 'testlastname',
+ 'email': 'test@test.com',
+ 'password_origin': 'testpassword',
+ 'password_verify': 'testpassword',
+}
+
+queued_account = Queue(
+ username='queueduser',
+ password='queuedpass',
+ email='queued_user@test.com',
+ first_name='queued_first_name',
+ last_name='queued_last_name',
+)
+
+
+class SignupUnitTests(OkupyTestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.mockldap = MockLdap(settings.DIRECTORY)
+
+ def setUp(self):
+ self.mockldap.start()
+ self.ldapobject = self.mockldap[settings.AUTH_LDAP_SERVER_URI]
+
+ def tearDown(self):
+ self.mockldap.stop()
+
+ def test_username_already_exists_in_ldap(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')])
+ _form = form_data.copy()
+ _form['username'] = 'alice'
+ request = set_request(uri='/signup', post=_form, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertMessage(response, 'Username already exists', 40)
+
+ def test_email_already_exists_in_ldap(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('testusername'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice@test.com', attr='mail'))([ldap_users('alice')])
+ _form = form_data.copy()
+ _form['email'] = 'alice@test.com'
+ request = set_request(uri='/signup', post=_form, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertMessage(response, 'Email already exists', 40)
+
+ def test_username_already_pending_activation(self):
+ _form = form_data.copy()
+ _form['username'] = 'queueduser'
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('queueduser'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('test@test.com', attr='mail'))(LDAPUser.DoesNotExist)
+ queued_account.save()
+ request = set_request(uri='/signup', post=_form, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertMessage(response, 'Account is already pending activation', 40)
+
+ def test_email_already_pending_activation(self):
+ _form = form_data.copy()
+ _form['email'] = 'queued_user@test.com'
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('testusername'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('queued_user@test.com', attr='mail'))(LDAPUser.DoesNotExist)
+ queued_account.save()
+ request = set_request(uri='/signup', post=_form, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertMessage(response, 'Account is already pending activation', 40)
+
+ def test_add_queued_account_to_ldap_prints_success_message(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed())(ldap_users(all=True))
+ queued_account.save()
+ activate_url = '/activate/%s/' % queued_account.encrypted_id
+ request = set_request(uri=activate_url, messages=True)
+ response = activate(request, queued_account.encrypted_id)
+ response.context = RequestContext(request)
+ self.assertMessage(response, 'Your account has been activated successfully', 25)
+
+ def test_queued_account_gets_added_to_ldap(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed())(ldap_users(all=True))
+ queued_account.save()
+ activate_url = '/activate/%s/' % queued_account.encrypted_id
+ request = set_request(activate_url, messages=True)
+ activate(request, queued_account.encrypted_id)
+ self.assertTrue(ldap_users(queued_account.username, directory=self.ldapobject.directory))
+ ldap_account = ldap_users(queued_account.username, directory=self.ldapobject.directory)[1]
+ self.assertEqual(ldap_account['objectClass'], settings.AUTH_LDAP_USER_OBJECTCLASS)
+ self.assertEqual(ldap_account['sn'][0], queued_account.last_name)
+ self.assertEqual(ldap_account['cn'][0], '%s %s' % (queued_account.first_name, queued_account.last_name))
+ self.assertTrue(ldap_md5_crypt.verify(queued_account.password, ldap_account['userPassword'][0]))
+ self.assertEqual(ldap_account['givenName'][0], queued_account.first_name)
+ self.assertEqual(ldap_account['mail'][0], queued_account.email)
+ self.assertEqual(ldap_account['uid'][0], queued_account.username)
+ self.assertEqual(ldap_account['uidNumber'][0], '1002')
+ self.assertEqual(ldap_account['gidNumber'][0], '100')
+ self.assertEqual(ldap_account['gecos'][0], '%s %s' % (queued_account.first_name, queued_account.last_name))
+ self.assertEqual(ldap_account['homeDirectory'][0], '/home/%s' % queued_account.username)
+ self.assertEqual(ldap_account['gentooACL'][0], 'user.group')
+
+ def test_add_queued_account_remove_from_queue(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed())(ldap_users(all=True))
+ queued_account.save()
+ activate_url = '/activate/%s/' % queued_account.encrypted_id
+ request = set_request(activate_url, messages=True)
+ activate(request, queued_account.encrypted_id)
+ self.assertEqual(Queue.objects.count(), 0)
+
+ def test_valid_data_to_signup_form_prints_info_message(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('testusername'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('test@test.com', attr='mail'))(LDAPUser.DoesNotExist)
+ request = set_request(uri='/signup', post=form_data, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertMessage(response, 'You will shortly receive an activation mail', 20)
+
+ def test_valid_data_to_signup_form_sends_activation_mail(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('testusername'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('test@test.com', attr='mail'))(LDAPUser.DoesNotExist)
+ request = set_request(uri='/signup', post=form_data, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertEqual(mail.outbox[0].subject, '%sAccount Activation' % settings.EMAIL_SUBJECT_PREFIX)
+
+ def test_valid_data_to_signup_form_adds_user_to_queue(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('testusername'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('test@test.com', attr='mail'))(LDAPUser.DoesNotExist)
+ request = set_request(uri='/signup', post=form_data, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertEqual(Queue.objects.count(), 1)
+ queued_account = Queue.objects.get(pk=1)
+ self.assertEqual(queued_account.username, form_data['username'])
+ self.assertEqual(queued_account.first_name, form_data['first_name'])
+ self.assertEqual(queued_account.last_name, form_data['last_name'])
+ self.assertEqual(queued_account.email, form_data['email'])
+ self.assertEqual(queued_account.password, form_data['password_origin'])
+ # note: this needs to be kept in line with used cipher
+ self.assertRegexpMatches(queued_account.encrypted_id, '^[a-f0-9]{16}$')
+
+ @no_database()
+ def test_no_database_connection_raises_error_in_signup(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('testusername'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('test@test.com', attr='mail'))(LDAPUser.DoesNotExist)
+ request = set_request(uri='/signup', post=form_data, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertMessage(response, "Can't contact the database", 40)
+
+ @no_database()
+ def test_no_database_connection_sends_notification_mail_in_signup(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('testusername'))(LDAPUser.DoesNotExist)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('test@test.com', attr='mail'))(LDAPUser.DoesNotExist)
+ request = set_request(uri='/signup', post=form_data, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertTrue(mail.outbox[0].subject.startswith('%sERROR:' % settings.EMAIL_SUBJECT_PREFIX))
+
+ @no_database()
+ def test_no_database_connection_raises_error_in_activation(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed())(ldap_users(all=True))
+ request = set_request('/activate/test', messages=True)
+ response = activate(request, queued_account.encrypted_id)
+ response.context = RequestContext(request)
+ self.assertMessage(response, "Can't contact the database", 40)
+
+ @no_database()
+ def test_no_database_connection_sends_notification_mail_in_activation(self):
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed())(ldap_users(all=True))
+ request = set_request('/activate/test', messages=True)
+ activate(request, queued_account.encrypted_id)
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertTrue(mail.outbox[0].subject.startswith('%sERROR:' % settings.EMAIL_SUBJECT_PREFIX))
+
+ def test_add_first_user_in_empty_ldap_directory(self):
+ queued_account.save()
+ activate_url = '/activate/%s/' % queued_account.encrypted_id
+ self.ldapobject.directory = ldap_users(clean=True)
+ self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed())(ldap_users(all=True, directory=self.ldapobject.directory))
+ request = set_request(activate_url, messages=True)
+ activate(request, queued_account.encrypted_id)
+ self.assertTrue(ldap_users(queued_account.username, directory=self.ldapobject.directory))
+ self.assertEqual(ldap_users(queued_account.username, directory=self.ldapobject.directory)[1]['uidNumber'][0], '1')
+
+
+class SignupunitTestsNoLDAP(OkupyTestCase):
def test_signup_url_resolves_to_signup_view(self):
- found = resolve('/signup')
+ found = resolve('/signup/')
self.assertEqual(found.func, signup)
def test_signup_page_returns_200_for_anonymous(self):
request = set_request(uri='/signup')
response = signup(request)
self.assertEqual(response.status_code, 200)
+
+ def test_rendered_signup_form(self):
+ request = set_request(uri='/signup')
+ response = signup(request)
+ signup_form_part = '<label for="id_first_name">First Name:</label><input id="id_first_name" maxlength="100" name="first_name" type="text" />'
+ self.assertIn(signup_form_part, response.content)
+
+ def test_empty_signup_form_raises_form_error_messages(self):
+ request = set_request(uri='/signup')
+ response = signup(request)
+ response.context = RequestContext(request, {'signup_form': SignupForm(request.POST)})
+ self.assertFormError(response, 'signup_form', 'username', 'This field is required.')
+ self.assertFormError(response, 'signup_form', 'first_name', 'This field is required.')
+ self.assertFormError(response, 'signup_form', 'last_name', 'This field is required.')
+ self.assertFormError(response, 'signup_form', 'email', 'This field is required.')
+ self.assertFormError(response, 'signup_form', 'password_origin', 'This field is required.')
+ self.assertFormError(response, 'signup_form', 'password_verify', 'This field is required.')
+
+ def test_passwords_dont_match(self):
+ _form = form_data.copy()
+ _form['password_verify'] = 'wrong'
+ request = set_request(uri='/signup', post=_form)
+ response = signup(request)
+ response.context = RequestContext(request, {'signup_form': SignupForm(request.POST)})
+ self.assertFormError(response, 'signup_form', 'password_verify', "Passwords don't match")
+
+ def test_wrong_activaltion_link_raises_invalid_url(self):
+ request = set_request(uri='/activate/invalidurl', messages=True)
+ response = activate(request, 'invalidurl')
+ response.context = RequestContext(request)
+ self.assertMessage(response, 'Invalid URL', 40)
+
+ def test_no_ldap_connection_raises_error_in_signup(self):
+ request = set_request(uri='/signup', post=form_data, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertMessage(response, "Can't contact LDAP server", 40)
+
+ def test_no_ldap_connection_sends_notification_mail_in_signup(self):
+ request = set_request(uri='/signup', post=form_data, messages=True)
+ response = signup(request)
+ response.context = RequestContext(request)
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertEqual(mail.outbox[0].subject, '%sERROR: {\'desc\': "Can\'t contact LDAP server"}' % settings.EMAIL_SUBJECT_PREFIX)
+
+ def test_no_ldap_connection_raises_error_in_activation(self):
+ queued_account.save()
+ activate_url = '/activate/%s/' % queued_account.encrypted_id
+ request = set_request(activate_url, messages=True)
+ response = activate(request, queued_account.encrypted_id)
+ response.context = RequestContext(request)
+ self.assertMessage(response, "Can't contact LDAP server", 40)
+
+ def test_no_ldap_connection_sends_notification_mail_in_activation(self):
+ queued_account.save()
+ activate_url = '/activate/%s/' % queued_account.encrypted_id
+ request = set_request(activate_url, messages=True)
+ response = activate(request, queued_account.encrypted_id)
+ response.context = RequestContext(request)
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertEqual(mail.outbox[0].subject, '%sERROR: {\'desc\': "Can\'t contact LDAP server"}' % settings.EMAIL_SUBJECT_PREFIX)