aboutsummaryrefslogtreecommitdiff
blob: f793009ce3704d170e6f3ffaeecc12ffb03a3a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python

from django.conf import settings
from django.test import TestCase

from base64 import b64encode
from Crypto import Random
from mockldap import MockLdap
from passlib.hash import ldap_md5_crypt

from okupy import OkupyError
from okupy.accounts.models import LDAPUser
from okupy.common.ldap_helpers import get_bound_ldapuser
from okupy.common.test_helpers import ldap_users, set_request
from okupy.crypto.ciphers import cipher
from okupy.tests import vars

import ldap


class LDAPUserUnitTests(TestCase):
    @classmethod
    def setUpClass(cls):
        cls.mockldap = MockLdap(vars.DIRECTORY)

    def setUp(self):
        self.mockldap.start()
        self.ldapobject = self.mockldap[settings.AUTH_LDAP_SERVER_URI]

    def tearDown(self):
        self.mockldap.stop()

    def test_return_unicode_username(self):
        alice = LDAPUser.objects.get(username='alice')
        self.assertEqual(alice.__unicode__(), u'alice')
        self.assertTrue(isinstance(alice.__unicode__(), unicode))

    def test_get_bound_ldapuser_from_request(self):
        secondary_password = Random.get_random_bytes(48)
        secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(
            secondary_password))
        self.ldapobject.directory[ldap_users('alice')[0]][
            'userPassword'].append(secondary_password_crypt)
        request = set_request('/', user=vars.USER_ALICE)
        request.session['secondary_password'] = cipher.encrypt(
            secondary_password)
        user = get_bound_ldapuser(request)
        self.assertEqual(user.username, vars.USER_ALICE.username)

    def test_get_bound_ldapuser_bind_as_is_properly_set_from_request(self):
        secondary_password = Random.get_random_bytes(48)
        secondary_password_crypt = ldap_md5_crypt.encrypt(b64encode(
            secondary_password))
        self.ldapobject.directory[ldap_users('alice')[0]][
            'userPassword'].append(secondary_password_crypt)
        request = set_request('/', user=vars.USER_ALICE)
        request.session['secondary_password'] = cipher.encrypt(
            secondary_password)
        get_bound_ldapuser(request)
        self.assertEqual(settings.DATABASES['ldap_alice']['PASSWORD'],
                         b64encode(secondary_password))

    def test_get_bound_ldapuser_bind_as_is_properly_set_from_password(self):
        request = set_request('/', user=vars.USER_ALICE)
        get_bound_ldapuser(request, password='ldaptest')
        self.assertTrue(ldap_md5_crypt.verify(settings.DATABASES['ldap_alice'][
            'PASSWORD'], ldap_users('alice')[1]['userPassword'][0]))

    def test_get_bound_ldapuser_password_set(self):
        request = set_request('/', user=vars.USER_ALICE)
        user = get_bound_ldapuser(request, password='ldaptest')
        self.assertEqual(user.username, vars.USER_ALICE.username)

    def test_get_bound_ldapuser_no_password_available(self):
        request = set_request('/', user=vars.USER_ALICE)
        self.assertRaises(OkupyError, get_bound_ldapuser, request)

    def test_get_bound_ldapuser_invalid_secondary_password(self):
        secondary_password = Random.get_random_bytes(48)
        request = set_request('/', user=vars.USER_ALICE)
        request.session['secondary_password'] = cipher.encrypt(
            secondary_password)
        self.assertRaises(ldap.INVALID_CREDENTIALS, get_bound_ldapuser,
                          request)

    def test_get_bound_ldapuser_invalid_given_password(self):
        request = set_request('/', user=vars.USER_ALICE)
        self.assertRaises(ldap.INVALID_CREDENTIALS, get_bound_ldapuser,
                          request, 'test')