diff options
author | Theo Chatzimichos <tampakrap@gentoo.org> | 2013-08-19 13:38:26 +0200 |
---|---|---|
committer | Theo Chatzimichos <tampakrap@gentoo.org> | 2013-08-19 23:22:10 +0200 |
commit | 09133e9727e284f3f7d82231abb8b5f9f3efe706 (patch) | |
tree | 48fe50c93c2cd9ec48c00625fd74d351a30ce772 | |
parent | Remove the fixture (diff) | |
download | identity.gentoo.org-09133e9727e284f3f7d82231abb8b5f9f3efe706.tar.gz identity.gentoo.org-09133e9727e284f3f7d82231abb8b5f9f3efe706.tar.bz2 identity.gentoo.org-09133e9727e284f3f7d82231abb8b5f9f3efe706.zip |
Add tests/vars.py that stores variables used by the tests
Since we tend to re-use the same vars between test files, keeping them in a
central place seems reasonable
-rw-r--r-- | okupy/common/test_helpers.py | 4 | ||||
-rw-r--r-- | okupy/tests/integration/test_index.py | 5 | ||||
-rw-r--r-- | okupy/tests/integration/test_login.py | 16 | ||||
-rw-r--r-- | okupy/tests/integration/test_signup.py | 42 | ||||
-rw-r--r-- | okupy/tests/settings.py | 31 | ||||
-rw-r--r-- | okupy/tests/unit/test_index.py | 13 | ||||
-rw-r--r-- | okupy/tests/unit/test_login.py | 34 | ||||
-rw-r--r-- | okupy/tests/unit/test_openid.py | 5 | ||||
-rw-r--r-- | okupy/tests/unit/test_secondary_password.py | 35 | ||||
-rw-r--r-- | okupy/tests/unit/test_signup.py | 141 | ||||
-rw-r--r-- | okupy/tests/vars.py | 69 |
11 files changed, 187 insertions, 208 deletions
diff --git a/okupy/common/test_helpers.py b/okupy/common/test_helpers.py index 35aa235..81581c2 100644 --- a/okupy/common/test_helpers.py +++ b/okupy/common/test_helpers.py @@ -9,6 +9,8 @@ from django.db import DatabaseError from django.test import TestCase, RequestFactory from django.utils.functional import curry +from okupy.tests import vars + import mock @@ -23,7 +25,7 @@ no_database = curry( def ldap_users(username=None, all=False, clean=False, - directory=settings.DIRECTORY): + directory=vars.DIRECTORY): """ Retrieve either a single LDAP user from the fake LDAP directory, or all the users, or clean up the users from the directory diff --git a/okupy/tests/integration/test_index.py b/okupy/tests/integration/test_index.py index e155c1d..af6cb3a 100644 --- a/okupy/tests/integration/test_index.py +++ b/okupy/tests/integration/test_index.py @@ -4,13 +4,14 @@ from django.conf import settings from django.test.client import Client from mockldap import MockLdap +from .. import vars from ...common.test_helpers import OkupyTestCase, ldap_users, set_search_seed -class IndexTests(OkupyTestCase): +class IndexIntegrationTests(OkupyTestCase): @classmethod def setUpClass(cls): - cls.mockldap = MockLdap(settings.DIRECTORY) + cls.mockldap = MockLdap(vars.DIRECTORY) def setUp(self): self.client = Client() diff --git a/okupy/tests/integration/test_login.py b/okupy/tests/integration/test_login.py index bf51551..ab3d515 100644 --- a/okupy/tests/integration/test_login.py +++ b/okupy/tests/integration/test_login.py @@ -6,18 +6,14 @@ from django.test.client import Client from mockldap import MockLdap +from .. import vars from ...common.test_helpers import ldap_users, set_search_seed -account1 = {'username': 'alice', 'password': 'ldaptest'} -account2 = {'username': 'bob', 'password': 'ldapmoretest'} -wrong_account = {'username': 'wrong', 'password': 'wrong'} - - class LoginIntegrationTests(TestCase): @classmethod def setUpClass(cls): - cls.mockldap = MockLdap(settings.DIRECTORY) + cls.mockldap = MockLdap(vars.DIRECTORY) def setUp(self): self.client = Client() @@ -34,20 +30,20 @@ class LoginIntegrationTests(TestCase): def test_correct_user_post_login_redirect(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - account = account1.copy() + account = vars.LOGIN_ALICE.copy() account['next'] = '' response = self.client.post('/login/', account) self.assertRedirects(response, '/', 302, 200) def test_already_authenticated_user_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/', account1) + self.client.post('/login/', vars.LOGIN_ALICE) response = self.client.get('/login/') self.assertRedirects(response, '/') def test_logout_for_logged_in_user_redirects_to_login(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - self.client.post('/login/', account1) + self.client.post('/login/', vars.LOGIN_ALICE) response = self.client.get('/logout/') self.assertRedirects(response, '/login/') @@ -57,7 +53,7 @@ class LoginIntegrationTests(TestCase): def test_logout_no_ldap_doesnt_raise_exception(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - self.client.post('/login/', account1) + self.client.post('/login/', vars.LOGIN_ALICE) self.mockldap.stop() response = self.client.get('/logout/') self.assertRedirects(response, '/login/', 302, 200) diff --git a/okupy/tests/integration/test_signup.py b/okupy/tests/integration/test_signup.py index 0dc9984..acd894e 100644 --- a/okupy/tests/integration/test_signup.py +++ b/okupy/tests/integration/test_signup.py @@ -1,41 +1,19 @@ # vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python 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, LDAPUser +from .. import vars +from ...accounts.models import LDAPUser from ...common.test_helpers import set_search_seed, ldap_users -import mock - - -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 SignupIntegrationTests(TestCase): @classmethod def setUpClass(cls): - cls.mockldap = MockLdap(settings.DIRECTORY) + cls.mockldap = MockLdap(vars.DIRECTORY) def setUp(self): self.client = Client() @@ -50,14 +28,14 @@ class SignupIntegrationTests(TestCase): self.assertTemplateUsed(response, 'signup.html') def test_wrong_activation_link_redirects_to_login(self): - queued_account.save() + vars.QUEUEDUSER.save() response = self.client.get('/activate/invalidurl/') self.assertRedirects(response, '/login/', 302, 200) 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('testuser'))(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) + response = self.client.post('/signup/', vars.SIGNUP_TESTUSER) self.assertRedirects(response, '/login/', 302, 200) def test_logged_in_user_signup_url_redirects_to_index(self): @@ -69,8 +47,8 @@ class SignupIntegrationTests(TestCase): 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 + vars.QUEUEDUSER.save() + activate_url = '/activate/%s/' % vars.QUEUEDUSER.encrypted_id self.client.post('/login/', {'username': 'alice', 'password': 'ldaptest'}) response = self.client.get(activate_url) self.assertRedirects(response, '/', 302, 200) @@ -81,8 +59,8 @@ class SignupIntegrationTestsNoLDAP(TestCase): self.client = Client() def test_activate_no_ldap_connection_redirects_to_login(self): - queued_account.save() - activate_url = '/activate/%s/' % queued_account.encrypted_id + vars.QUEUEDUSER.save() + activate_url = '/activate/%s/' % vars.QUEUEDUSER.encrypted_id response = self.client.get(activate_url) self.assertRedirects(response, '/login/', 302, 200) diff --git a/okupy/tests/settings.py b/okupy/tests/settings.py index b713032..f4d0c07 100644 --- a/okupy/tests/settings.py +++ b/okupy/tests/settings.py @@ -299,34 +299,3 @@ DATABASES['ldap'] = { DATABASE_ROUTERS = ['ldapdb.router.Router'] TEST_RUNNER = 'discover_runner.DiscoverRunner' - -DIRECTORY = { - "o=test": {}, - "cn=anon,o=test": { - "userPassword": ["{CRYPT}$1$n4jlXi20$.5a8UTvwIqVfVAMlXJ1EZ0"], - }, - "cn=Manager,o=test": { - "userPassword": ["{CRYPT}$1$sY4mlRve$0eg5TLYMyZfBCIUgU/RPf0"], - }, - "ou=people,o=test": {}, - "uid=alice,ou=people,o=test": { - "uid": ["alice"], - "userPassword": ['{CRYPT}$1$lO/RU6zz$2fJCOwurxBtCqdImkoLQo1'], - "objectClass": AUTH_LDAP_USER_OBJECTCLASS + AUTH_LDAP_DEV_OBJECTCLASS, - "uidNumber": ["1000"], - "gidNumber": ["1000"], - "givenName": ["Alice"], - "sn": ["Adams"], - "mail": ["alice@test.com"], - }, - "uid=bob,ou=people,o=test": { - "uid": ["bob"], - "userPassword": ['{CRYPT}$1$eFSQMJY6$8y.WUL/ONeEarVXqeCIbH.'], - "objectClass": AUTH_LDAP_USER_OBJECTCLASS, - "uidNumber": ["1001"], - "gidNumber": ["50"], - "givenName": ["Robert"], - "sn": ["Barker"], - "mail": ["bob@test.com"], - } -} diff --git a/okupy/tests/unit/test_index.py b/okupy/tests/unit/test_index.py index 5b6c30e..7e4a8eb 100644 --- a/okupy/tests/unit/test_index.py +++ b/okupy/tests/unit/test_index.py @@ -1,25 +1,20 @@ # vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python from django.conf import settings -from django.contrib.auth.models import User from django.core.urlresolvers import resolve from django.test import TestCase from mockldap import MockLdap +from .. import vars from ...accounts.views import index from ...common.test_helpers import set_request, ldap_users, set_search_seed -alice = User(username='alice', password='ldaptest') -account2 = {'username': 'bob', 'password': 'ldapmoretest'} -wrong_account = {'username': 'wrong', 'password': 'wrong'} - - class IndexUnitTests(TestCase): @classmethod def setUpClass(cls): - cls.mockldap = MockLdap(settings.DIRECTORY) + cls.mockldap = MockLdap(vars.DIRECTORY) def setUp(self): self.mockldap.start() @@ -34,13 +29,13 @@ class IndexUnitTests(TestCase): def test_index_page_returns_200_for_logged_in(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) response = index(request) self.assertEqual(response.status_code, 200) def test_rendered_index_page(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) response = index(request) nickname_html = '<tr class="even"><th>Nickname</th><td>alice</td></tr>' self.assertIn(nickname_html, response.content) diff --git a/okupy/tests/unit/test_login.py b/okupy/tests/unit/test_login.py index e146d06..58c01ea 100644 --- a/okupy/tests/unit/test_login.py +++ b/okupy/tests/unit/test_login.py @@ -12,21 +12,17 @@ from Crypto import Random from passlib.hash import ldap_md5_crypt from mockldap import MockLdap +from .. import vars from ...accounts.views import login, logout from ...accounts.forms import LoginForm from ...common.crypto import cipher from ...common.test_helpers import OkupyTestCase, set_request, no_database, ldap_users, set_search_seed -account1 = {'username': 'alice', 'password': 'ldaptest'} -account2 = {'username': 'bob', 'password': 'ldapmoretest'} -wrong_account = {'username': 'wrong', 'password': 'wrong'} - - class LoginUnitTests(OkupyTestCase): @classmethod def setUpClass(cls): - cls.mockldap = MockLdap(settings.DIRECTORY) + cls.mockldap = MockLdap(vars.DIRECTORY) def setUp(self): self.mockldap.start() @@ -36,13 +32,13 @@ class LoginUnitTests(OkupyTestCase): self.mockldap.stop() def test_incorrect_user_raises_login_failed(self): - request = set_request(uri='/login', post=wrong_account, messages=True) + request = set_request(uri='/login', post=vars.LOGIN_WRONG, messages=True) response = login(request) response.context = RequestContext(request) self.assertMessage(response, 'Login failed', 40) def test_incorrect_user_does_not_get_transferred_in_db(self): - request = set_request(uri='/login', post=wrong_account, messages=True) + request = set_request(uri='/login', post=vars.LOGIN_WRONG, messages=True) login(request) self.assertEqual(User.objects.count(), 0) @@ -51,7 +47,7 @@ class LoginUnitTests(OkupyTestCase): 'django_auth_ldap.backend.LDAPBackend', 'django.contrib.auth.backends.ModelBackend')) def test_no_database_raises_critical(self): - request = set_request(uri='/login', post=account1, messages=True) + request = set_request(uri='/login', post=vars.LOGIN_ALICE, messages=True) response = login(request) response.context = RequestContext(request) self.assertMessage(response, "Can't contact the LDAP server or the database", 40) @@ -61,7 +57,7 @@ class LoginUnitTests(OkupyTestCase): 'django_auth_ldap.backend.LDAPBackend', 'django.contrib.auth.backends.ModelBackend')) def test_no_database_sends_notification_mail(self): - request = set_request(uri='/login', post=account1, messages=True) + request = set_request(uri='/login', post=vars.LOGIN_ALICE, messages=True) response = login(request) response.context = RequestContext(request) self.assertEqual(len(mail.outbox), 1) @@ -69,20 +65,20 @@ class LoginUnitTests(OkupyTestCase): def test_correct_user_gets_transferred_in_db(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - request = set_request(uri='/login', post=account1) + request = set_request(uri='/login', post=vars.LOGIN_ALICE) login(request) self.assertEqual(User.objects.count(), 1) def test_authenticate_account_that_is_already_in_db(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - User.objects.create_user(username='alice') - request = set_request(uri='/login', post=account1) + vars.USER_ALICE.save() + request = set_request(uri='/login', post=vars.LOGIN_ALICE) login(request) self.assertEqual(User.objects.count(), 1) def test_secondary_password_is_added_in_login(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - request = set_request(uri='/login', post=account1) + request = set_request(uri='/login', post=vars.LOGIN_ALICE) login(request) self.assertEqual(len(ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']), 2) self.assertEqual(len(request.session['secondary_password']), 48) @@ -92,8 +88,7 @@ class LoginUnitTests(OkupyTestCase): secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(secondary_password)) self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append(secondary_password_crypt) self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice', directory=self.ldapobject.directory)]) - alice = User(username='alice') - request = set_request(uri='/login', post=account1, user=alice) + request = set_request(uri='/login', post=vars.LOGIN_ALICE, user=vars.USER_ALICE) request.session['secondary_password'] = cipher.encrypt(secondary_password) logout(request) self.assertEqual(len(ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']), 1) @@ -129,20 +124,19 @@ class LoginUnitTestsNoLDAP(OkupyTestCase): self.assertMessage(response, 'Login failed', 40) def test_dont_authenticate_from_db_when_ldap_is_down(self): - request = set_request(uri='/login', post=account2, messages=True) + request = set_request(uri='/login', post=vars.LOGIN_BOB, messages=True) response = login(request) response.context = RequestContext(request) self.assertMessage(response, 'Login failed', 40) def test_no_ldap_connection_raises_login_failed_in_login(self): - request = set_request(uri='/login', post=wrong_account, messages=True) + request = set_request(uri='/login', post=vars.LOGIN_WRONG, messages=True) response = login(request) response.context = RequestContext(request) self.assertMessage(response, 'Login failed', 40) def test_no_ldap_connection_in_logout_sends_notification_mail(self): - alice = User(username='alice') - request = set_request(uri='/login', post=account1, user=alice) + request = set_request(uri='/login', post=vars.LOGIN_ALICE, user=vars.USER_ALICE) request.session['secondary_password'] = 'test' logout(request) self.assertEqual(len(mail.outbox), 1) diff --git a/okupy/tests/unit/test_openid.py b/okupy/tests/unit/test_openid.py index b50928f..4d5a75a 100644 --- a/okupy/tests/unit/test_openid.py +++ b/okupy/tests/unit/test_openid.py @@ -1,12 +1,13 @@ # vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python +from django.test import TestCase + from ...accounts.openid_store import DjangoDBOpenIDStore -from ...common.test_helpers import OkupyTestCase import time -class OpenIDStoreTests(OkupyTestCase): +class OpenIDStoreTests(TestCase): def setUp(self): self.store = DjangoDBOpenIDStore() diff --git a/okupy/tests/unit/test_secondary_password.py b/okupy/tests/unit/test_secondary_password.py index 3811c5e..8338827 100644 --- a/okupy/tests/unit/test_secondary_password.py +++ b/okupy/tests/unit/test_secondary_password.py @@ -1,7 +1,6 @@ # vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python from django.conf import settings -from django.contrib.auth.models import User from django.test import TestCase from base64 import b64encode @@ -9,6 +8,7 @@ from Crypto import Random from mockldap import MockLdap from passlib.hash import ldap_md5_crypt +from .. import vars from ...common.crypto import cipher from ...common.ldap_helpers import set_secondary_password, remove_secondary_password from ...common.test_helpers import set_request, set_search_seed, ldap_users @@ -17,7 +17,7 @@ from ...common.test_helpers import set_request, set_search_seed, ldap_users class SecondaryPassword(TestCase): @classmethod def setUpClass(cls): - cls.mockldap = MockLdap(settings.DIRECTORY) + cls.mockldap = MockLdap(vars.DIRECTORY) def setUp(self): self.mockldap.start() @@ -28,15 +28,13 @@ class SecondaryPassword(TestCase): def test_secondary_password_gets_added_in_session(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) set_secondary_password(request, 'ldaptest') self.assertEqual(len(request.session['secondary_password']), 48) def test_secondary_password_gets_added_in_ldap(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) self.assertEqual(len(ldap_users('alice')[1]['userPassword']), 1) set_secondary_password(request, 'ldaptest') self.assertEqual(len(ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']), 2) @@ -45,8 +43,7 @@ class SecondaryPassword(TestCase): leftover = ldap_md5_crypt.encrypt('leftover_password') self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append(leftover) self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice', directory=self.ldapobject.directory)]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) set_secondary_password(request, 'ldaptest') self.assertNotIn(leftover, ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']) @@ -54,10 +51,9 @@ class SecondaryPassword(TestCase): leftover = ldap_md5_crypt.encrypt('leftover_password') self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append(leftover) self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice', directory=self.ldapobject.directory)]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) set_secondary_password(request, 'ldaptest') - self.assertTrue(ldap_md5_crypt.verify('ldaptest',ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword'][0])) + self.assertTrue(ldap_md5_crypt.verify('ldaptest', ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword'][0])) def test_dont_remove_unknown_hashes_while_cleaning_leftovers(self): leftover = ldap_md5_crypt.encrypt('leftover_password') @@ -65,15 +61,13 @@ class SecondaryPassword(TestCase): leftover2 = 'plain_leftover2' self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append(leftover2) self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice', directory=self.ldapobject.directory)]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) set_secondary_password(request, 'ldaptest') self.assertIn(leftover2, ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']) def test_session_and_ldap_secondary_passwords_match(self): self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice')]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) set_secondary_password(request, 'ldaptest') self.assertTrue(ldap_md5_crypt.verify(b64encode(cipher.decrypt(request.session['secondary_password'], 48)), ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword'][1])) @@ -82,8 +76,7 @@ class SecondaryPassword(TestCase): secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(secondary_password)) self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append(secondary_password_crypt) self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice', directory=self.ldapobject.directory)]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) request.session['secondary_password'] = cipher.encrypt(secondary_password) remove_secondary_password(request) self.assertNotIn(secondary_password_crypt, ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']) @@ -93,11 +86,10 @@ class SecondaryPassword(TestCase): secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(secondary_password)) self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append(secondary_password_crypt) self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice', directory=self.ldapobject.directory)]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) request.session['secondary_password'] = cipher.encrypt(secondary_password) remove_secondary_password(request) - self.assertTrue(ldap_md5_crypt.verify('ldaptest',ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword'][0])) + self.assertTrue(ldap_md5_crypt.verify('ldaptest', ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword'][0])) def test_dont_remove_unknown_hashes_while_removing_secondary_password(self): secondary_password = Random.get_random_bytes(48) @@ -105,8 +97,7 @@ class SecondaryPassword(TestCase): self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append('unknown_hash') self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append(secondary_password_crypt) self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([ldap_users('alice', directory=self.ldapobject.directory)]) - alice = User.objects.create(username='alice', password='ldaptest') - request = set_request(uri='/', user=alice) + request = set_request(uri='/', user=vars.USER_ALICE) request.session['secondary_password'] = cipher.encrypt(secondary_password) remove_secondary_password(request) self.assertIn('unknown_hash', ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']) diff --git a/okupy/tests/unit/test_signup.py b/okupy/tests/unit/test_signup.py index 9c4314e..c32869a 100644 --- a/okupy/tests/unit/test_signup.py +++ b/okupy/tests/unit/test_signup.py @@ -8,34 +8,17 @@ from django.template import RequestContext from mockldap import MockLdap from passlib.hash import ldap_md5_crypt +from .. import vars 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 -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) + cls.mockldap = MockLdap(vars.DIRECTORY) def setUp(self): self.mockldap.start() @@ -46,7 +29,7 @@ class SignupUnitTests(OkupyTestCase): 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 = vars.SIGNUP_TESTUSER.copy() _form['username'] = 'alice' request = set_request(uri='/signup', post=_form, messages=True) response = signup(request) @@ -54,9 +37,9 @@ class SignupUnitTests(OkupyTestCase): 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('testuser'))(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 = vars.SIGNUP_TESTUSER.copy() _form['email'] = 'alice@test.com' request = set_request(uri='/signup', post=_form, messages=True) response = signup(request) @@ -64,22 +47,22 @@ class SignupUnitTests(OkupyTestCase): self.assertMessage(response, 'Email already exists', 40) def test_username_already_pending_activation(self): - _form = form_data.copy() + _form = vars.SIGNUP_TESTUSER.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() + vars.QUEUEDUSER.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 = vars.SIGNUP_TESTUSER.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('testuser'))(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() + vars.QUEUEDUSER.save() request = set_request(uri='/signup', post=_form, messages=True) response = signup(request) response.context = RequestContext(request) @@ -87,89 +70,89 @@ class SignupUnitTests(OkupyTestCase): 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 + vars.QUEUEDUSER.save() + activate_url = '/activate/%s/' % vars.QUEUEDUSER.encrypted_id request = set_request(uri=activate_url, messages=True) - response = activate(request, queued_account.encrypted_id) + response = activate(request, vars.QUEUEDUSER.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 + vars.QUEUEDUSER.save() + activate_url = '/activate/%s/' % vars.QUEUEDUSER.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] + activate(request, vars.QUEUEDUSER.encrypted_id) + self.assertTrue(ldap_users(vars.QUEUEDUSER.username, directory=self.ldapobject.directory)) + ldap_account = ldap_users(vars.QUEUEDUSER.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['sn'][0], vars.QUEUEDUSER.last_name) + self.assertEqual(ldap_account['cn'][0], '%s %s' % (vars.QUEUEDUSER.first_name, vars.QUEUEDUSER.last_name)) + self.assertTrue(ldap_md5_crypt.verify(vars.QUEUEDUSER.password, ldap_account['userPassword'][0])) + self.assertEqual(ldap_account['givenName'][0], vars.QUEUEDUSER.first_name) + self.assertEqual(ldap_account['mail'][0], vars.QUEUEDUSER.email) + self.assertEqual(ldap_account['uid'][0], vars.QUEUEDUSER.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['gecos'][0], '%s %s' % (vars.QUEUEDUSER.first_name, vars.QUEUEDUSER.last_name)) + self.assertEqual(ldap_account['homeDirectory'][0], '/home/%s' % vars.QUEUEDUSER.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 + vars.QUEUEDUSER.save() + activate_url = '/activate/%s/' % vars.QUEUEDUSER.encrypted_id request = set_request(activate_url, messages=True) - activate(request, queued_account.encrypted_id) + activate(request, vars.QUEUEDUSER.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('testuser'))(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) + request = set_request(uri='/signup', post=vars.SIGNUP_TESTUSER, 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('testuser'))(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) + request = set_request(uri='/signup', post=vars.SIGNUP_TESTUSER, 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('testuser'))(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) + request = set_request(uri='/signup', post=vars.SIGNUP_TESTUSER, 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']) + vars.QUEUEDUSER = Queue.objects.get(pk=1) + self.assertEqual(vars.QUEUEDUSER.username, vars.SIGNUP_TESTUSER['username']) + self.assertEqual(vars.QUEUEDUSER.first_name, vars.SIGNUP_TESTUSER['first_name']) + self.assertEqual(vars.QUEUEDUSER.last_name, vars.SIGNUP_TESTUSER['last_name']) + self.assertEqual(vars.QUEUEDUSER.email, vars.SIGNUP_TESTUSER['email']) + self.assertEqual(vars.QUEUEDUSER.password, vars.SIGNUP_TESTUSER['password_origin']) # note: this needs to be kept in line with used cipher - self.assertRegexpMatches(queued_account.encrypted_id, '^[a-f0-9]{16}$') + self.assertRegexpMatches(vars.QUEUEDUSER.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('testuser'))(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) + request = set_request(uri='/signup', post=vars.SIGNUP_TESTUSER, 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('testuser'))(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) + request = set_request(uri='/signup', post=vars.SIGNUP_TESTUSER, messages=True) response = signup(request) response.context = RequestContext(request) self.assertEqual(len(mail.outbox), 1) @@ -179,7 +162,7 @@ class SignupUnitTests(OkupyTestCase): 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 = activate(request, vars.QUEUEDUSER.encrypted_id) response.context = RequestContext(request) self.assertMessage(response, "Can't contact the database", 40) @@ -187,19 +170,19 @@ class SignupUnitTests(OkupyTestCase): 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) + activate(request, vars.QUEUEDUSER.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 + vars.QUEUEDUSER.save() + activate_url = '/activate/%s/' % vars.QUEUEDUSER.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') + activate(request, vars.QUEUEDUSER.encrypted_id) + self.assertTrue(ldap_users(vars.QUEUEDUSER.username, directory=self.ldapobject.directory)) + self.assertEqual(ldap_users(vars.QUEUEDUSER.username, directory=self.ldapobject.directory)[1]['uidNumber'][0], '1') class SignupunitTestsNoLDAP(OkupyTestCase): @@ -230,7 +213,7 @@ class SignupunitTestsNoLDAP(OkupyTestCase): self.assertFormError(response, 'signup_form', 'password_verify', 'This field is required.') def test_passwords_dont_match(self): - _form = form_data.copy() + _form = vars.SIGNUP_TESTUSER.copy() _form['password_verify'] = 'wrong' request = set_request(uri='/signup', post=_form) response = signup(request) @@ -244,31 +227,31 @@ class SignupunitTestsNoLDAP(OkupyTestCase): 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) + request = set_request(uri='/signup', post=vars.SIGNUP_TESTUSER, 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) + request = set_request(uri='/signup', post=vars.SIGNUP_TESTUSER, 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 + vars.QUEUEDUSER.save() + activate_url = '/activate/%s/' % vars.QUEUEDUSER.encrypted_id request = set_request(activate_url, messages=True) - response = activate(request, queued_account.encrypted_id) + response = activate(request, vars.QUEUEDUSER.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 + vars.QUEUEDUSER.save() + activate_url = '/activate/%s/' % vars.QUEUEDUSER.encrypted_id request = set_request(activate_url, messages=True) - response = activate(request, queued_account.encrypted_id) + response = activate(request, vars.QUEUEDUSER.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) diff --git a/okupy/tests/vars.py b/okupy/tests/vars.py new file mode 100644 index 0000000..f4edbc1 --- /dev/null +++ b/okupy/tests/vars.py @@ -0,0 +1,69 @@ +# vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python + +""" Various variables used by the tests """ + +from django.conf import settings +from django.contrib.auth.models import User + +from ..accounts.models import Queue + + +# LDAP directory +DIRECTORY = { + "o=test": {}, + "cn=anon,o=test": { + "userPassword": ["{CRYPT}$1$n4jlXi20$.5a8UTvwIqVfVAMlXJ1EZ0"], + }, + "cn=Manager,o=test": { + "userPassword": ["{CRYPT}$1$sY4mlRve$0eg5TLYMyZfBCIUgU/RPf0"], + }, + "ou=people,o=test": {}, + "uid=alice,ou=people,o=test": { + "uid": ["alice"], + "userPassword": ['{CRYPT}$1$lO/RU6zz$2fJCOwurxBtCqdImkoLQo1'], + "objectClass": settings.AUTH_LDAP_USER_OBJECTCLASS + + settings.AUTH_LDAP_DEV_OBJECTCLASS, + "uidNumber": ["1000"], + "gidNumber": ["1000"], + "givenName": ["Alice"], + "sn": ["Adams"], + "mail": ["alice@test.com"], + }, + "uid=bob,ou=people,o=test": { + "uid": ["bob"], + "userPassword": ['{CRYPT}$1$eFSQMJY6$8y.WUL/ONeEarVXqeCIbH.'], + "objectClass": settings.AUTH_LDAP_USER_OBJECTCLASS, + "uidNumber": ["1001"], + "gidNumber": ["50"], + "givenName": ["Robert"], + "sn": ["Barker"], + "mail": ["bob@test.com"], + } +} + +# User objects +USER_ALICE = User(username='alice', password='ldaptest') + +# Queue objects +QUEUEDUSER = Queue( + username='queueduser', + password='queuedpass', + email='queued_user@test.com', + first_name='queued_first_name', + last_name='queued_last_name', +) + +# login form data +LOGIN_ALICE = {'username': 'alice', 'password': 'ldaptest'} +LOGIN_BOB = {'username': 'bob', 'password': 'ldapmoretest'} +LOGIN_WRONG = {'username': 'wrong', 'password': 'wrong'} + +# signup form data +SIGNUP_TESTUSER = { + 'username': 'testuser', + 'first_name': 'testfirstname', + 'last_name': 'testlastname', + 'email': 'test@test.com', + 'password_origin': 'testpassword', + 'password_verify': 'testpassword', +} |