diff options
author | Theo Chatzimichos <tampakrap@gentoo.org> | 2013-08-19 11:58:15 +0200 |
---|---|---|
committer | Theo Chatzimichos <tampakrap@gentoo.org> | 2013-08-19 23:16:58 +0200 |
commit | 34b33bee91cdedbf8028c0597e8e222c586d9b4f (patch) | |
tree | 95c3e13225ce5f587162cf7b3059cf3c9d7a8016 /okupy/tests/unit | |
parent | Combine get_ldap_user and get_all_ldap_users to ldap_users (diff) | |
download | identity.gentoo.org-34b33bee91cdedbf8028c0597e8e222c586d9b4f.tar.gz identity.gentoo.org-34b33bee91cdedbf8028c0597e8e222c586d9b4f.tar.bz2 identity.gentoo.org-34b33bee91cdedbf8028c0597e8e222c586d9b4f.zip |
Apply the previous helper changes to the test suite
Move some login tests to the non-ldap class
Diffstat (limited to 'okupy/tests/unit')
-rw-r--r-- | okupy/tests/unit/test_index.py | 6 | ||||
-rw-r--r-- | okupy/tests/unit/test_login.py | 81 | ||||
-rw-r--r-- | okupy/tests/unit/test_secondary_password.py | 55 |
3 files changed, 75 insertions, 67 deletions
diff --git a/okupy/tests/unit/test_index.py b/okupy/tests/unit/test_index.py index bae044e..5b6c30e 100644 --- a/okupy/tests/unit/test_index.py +++ b/okupy/tests/unit/test_index.py @@ -8,7 +8,7 @@ from django.test import TestCase from mockldap import MockLdap from ...accounts.views import index -from ...common.test_helpers import set_request, get_ldap_user, set_search_seed +from ...common.test_helpers import set_request, ldap_users, set_search_seed alice = User(username='alice', password='ldaptest') @@ -33,13 +33,13 @@ class IndexUnitTests(TestCase): self.assertEqual(found.func, index) 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'))([get_ldap_user('alice')]) + 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) 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'))([get_ldap_user('alice')]) + 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) response = index(request) nickname_html = '<tr class="even"><th>Nickname</th><td>alice</td></tr>' diff --git a/okupy/tests/unit/test_login.py b/okupy/tests/unit/test_login.py index 6d5e94d..e146d06 100644 --- a/okupy/tests/unit/test_login.py +++ b/okupy/tests/unit/test_login.py @@ -15,7 +15,7 @@ from mockldap import MockLdap 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, get_ldap_user, set_search_seed +from ...common.test_helpers import OkupyTestCase, set_request, no_database, ldap_users, set_search_seed account1 = {'username': 'alice', 'password': 'ldaptest'} @@ -35,34 +35,6 @@ class LoginUnitTests(OkupyTestCase): def tearDown(self): self.mockldap.stop() - def test_login_url_resolves_to_login_view(self): - found = resolve('/login/') - self.assertEqual(found.func, login) - - def test_login_page_returns_200(self): - request = set_request(uri='/login') - response = login(request) - self.assertEqual(response.status_code, 200) - - def test_rendered_login_form(self): - request = set_request(uri='/login') - response = login(request) - login_form_part = '<input id="id_username" maxlength="100" name="username" type="text" />' - self.assertIn(login_form_part, response.content) - - def test_empty_user_raises_form_error_messages(self): - request = set_request(uri='/login') - response = login(request) - response.context = RequestContext(request, {'login_form': LoginForm(request.POST)}) - self.assertFormError(response, 'login_form', 'username', 'This field is required.') - self.assertFormError(response, 'login_form', 'password', 'This field is required.') - - def test_empty_user_raises_login_failed(self): - request = set_request(uri='/login', post=True, messages=True) - response = login(request) - response.context = RequestContext(request) - self.assertMessage(response, 'Login failed', 40) - def test_incorrect_user_raises_login_failed(self): request = set_request(uri='/login', post=wrong_account, messages=True) response = login(request) @@ -83,42 +55,79 @@ class LoginUnitTests(OkupyTestCase): response = login(request) response.context = RequestContext(request) self.assertMessage(response, "Can't contact the LDAP server or the database", 40) + + @no_database() + @override_settings(AUTHENTICATION_BACKENDS=( + '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) + response = login(request) + response.context = RequestContext(request) self.assertEqual(len(mail.outbox), 1) self.assertTrue(mail.outbox[0].subject.startswith('%sERROR:' % settings.EMAIL_SUBJECT_PREFIX)) 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'))([get_ldap_user('alice')]) + 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) 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'))([get_ldap_user('alice')]) + 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) 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'))([get_ldap_user('alice')]) + 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) login(request) - self.assertEqual(len(get_ldap_user('alice', directory=self.ldapobject.directory)[1]['userPassword']), 2) + self.assertEqual(len(ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']), 2) self.assertEqual(len(request.session['secondary_password']), 48) def test_secondary_password_is_removed_in_logout(self): secondary_password = Random.get_random_bytes(48) secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(secondary_password)) - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(secondary_password_crypt) - self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([get_ldap_user('alice', directory=self.ldapobject.directory)]) + 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.session['secondary_password'] = cipher.encrypt(secondary_password) logout(request) - self.assertEqual(len(get_ldap_user('alice', directory=self.ldapobject.directory)[1]['userPassword']), 1) + self.assertEqual(len(ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']), 1) class LoginUnitTestsNoLDAP(OkupyTestCase): + def test_login_url_resolves_to_login_view(self): + found = resolve('/login/') + self.assertEqual(found.func, login) + + def test_login_page_returns_200(self): + request = set_request(uri='/login') + response = login(request) + self.assertEqual(response.status_code, 200) + + def test_rendered_login_form(self): + request = set_request(uri='/login') + response = login(request) + login_form_part = '<input id="id_username" maxlength="100" name="username" type="text" />' + self.assertIn(login_form_part, response.content) + + def test_empty_user_raises_form_error_messages(self): + request = set_request(uri='/login') + response = login(request) + response.context = RequestContext(request, {'login_form': LoginForm(request.POST)}) + self.assertFormError(response, 'login_form', 'username', 'This field is required.') + self.assertFormError(response, 'login_form', 'password', 'This field is required.') + + def test_empty_user_raises_login_failed(self): + request = set_request(uri='/login', post=True, messages=True) + response = login(request) + response.context = RequestContext(request) + 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) response = login(request) diff --git a/okupy/tests/unit/test_secondary_password.py b/okupy/tests/unit/test_secondary_password.py index 4f9e4c6..3811c5e 100644 --- a/okupy/tests/unit/test_secondary_password.py +++ b/okupy/tests/unit/test_secondary_password.py @@ -11,7 +11,7 @@ from passlib.hash import ldap_md5_crypt 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, get_ldap_user +from ...common.test_helpers import set_request, set_search_seed, ldap_users class SecondaryPassword(TestCase): @@ -27,87 +27,86 @@ class SecondaryPassword(TestCase): self.mockldap.stop() 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'))([get_ldap_user('alice')]) + 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) 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'))([get_ldap_user('alice')]) + 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) - self.assertEqual(len(get_ldap_user('alice')[1]['userPassword']), 1) + self.assertEqual(len(ldap_users('alice')[1]['userPassword']), 1) set_secondary_password(request, 'ldaptest') - self.assertEqual(len(get_ldap_user('alice', directory=self.ldapobject.directory)[1]['userPassword']), 2) + self.assertEqual(len(ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']), 2) def test_remove_leftovers_before_adding_secondary_password(self): leftover = ldap_md5_crypt.encrypt('leftover_password') - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(leftover) - self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([get_ldap_user('alice', directory=self.ldapobject.directory)]) + 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) set_secondary_password(request, 'ldaptest') - self.assertNotIn(leftover, get_ldap_user('alice', directory=self.ldapobject.directory)[1]['userPassword']) + self.assertNotIn(leftover, ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']) def test_dont_remove_primary_password_while_cleaning_leftovers(self): leftover = ldap_md5_crypt.encrypt('leftover_password') - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(leftover) - self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([get_ldap_user('alice', directory=self.ldapobject.directory)]) + 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) set_secondary_password(request, 'ldaptest') - self.assertTrue(ldap_md5_crypt.verify('ldaptest',get_ldap_user('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') - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(leftover) + self.ldapobject.directory[ldap_users('alice')[0]]['userPassword'].append(leftover) leftover2 = 'plain_leftover2' - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(leftover2) - self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([get_ldap_user('alice', directory=self.ldapobject.directory)]) + 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) set_secondary_password(request, 'ldaptest') - self.assertIn(leftover2, get_ldap_user('alice', directory=self.ldapobject.directory)[1]['userPassword']) + 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'))([get_ldap_user('alice')]) + 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) set_secondary_password(request, 'ldaptest') - self.assertTrue(ldap_md5_crypt.verify(b64encode(cipher.decrypt(request.session['secondary_password'], 48)), get_ldap_user('alice', directory=self.ldapobject.directory)[1]['userPassword'][1])) + self.assertTrue(ldap_md5_crypt.verify(b64encode(cipher.decrypt(request.session['secondary_password'], 48)), ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword'][1])) def test_remove_secondary_password_from_ldap(self): secondary_password = Random.get_random_bytes(48) secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(secondary_password)) - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(secondary_password_crypt) - self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([get_ldap_user('alice', directory=self.ldapobject.directory)]) + 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.session['secondary_password'] = cipher.encrypt(secondary_password) remove_secondary_password(request) - self.assertNotIn(secondary_password_crypt, get_ldap_user('alice', directory=self.ldapobject.directory)[1]['userPassword']) + self.assertNotIn(secondary_password_crypt, ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']) def test_dont_remove_primary_password_while_removing_secondary_password(self): secondary_password = Random.get_random_bytes(48) secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(secondary_password)) - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(secondary_password_crypt) - self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([get_ldap_user('alice', directory=self.ldapobject.directory)]) + 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.session['secondary_password'] = cipher.encrypt(secondary_password) remove_secondary_password(request) - self.assertTrue(ldap_md5_crypt.verify('ldaptest',get_ldap_user('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) secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(secondary_password)) - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(secondary_password_crypt) - unknown_hash = 'unknown_hash' - self.ldapobject.directory[get_ldap_user('alice')[0]]['userPassword'].append(unknown_hash) - self.ldapobject.search_s.seed(settings.AUTH_LDAP_USER_BASE_DN, 2, set_search_seed('alice'))([get_ldap_user('alice', directory=self.ldapobject.directory)]) + 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.session['secondary_password'] = cipher.encrypt(secondary_password) remove_secondary_password(request) - self.assertIn(unknown_hash, get_ldap_user('alice', directory=self.ldapobject.directory)[1]['userPassword']) + self.assertIn('unknown_hash', ldap_users('alice', directory=self.ldapobject.directory)[1]['userPassword']) |