-
Notifications
You must be signed in to change notification settings - Fork 19
/
test_helpers.py
65 lines (58 loc) · 2.07 KB
/
test_helpers.py
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
import os
import unittest
from unittest.mock import patch
import ldap3
from django.conf import settings
from django.test import TestCase
from .utils import NEWUSER_DN
TEST_NEWUSER_PW = "ilovepnunez"
mock_ldap_server = ldap3.Server.from_definition(
"",
os.path.join(settings.BASE_DIR, "fixtures", "csua_ldap_info.json"),
os.path.join(settings.BASE_DIR, "fixtures", "csua_ldap_schema.json"),
)
class LDAPTestCase(TestCase):
def setUp(self):
self.patchers = [
patch("apps.ldap.utils.LDAP_SERVER", mock_ldap_server),
patch("apps.ldap.utils.LDAP_CLIENT_STRATEGY", ldap3.MOCK_SYNC),
patch("apps.ldap.utils.NEWUSER_PW", TEST_NEWUSER_PW),
]
for p in self.patchers:
p.start()
with ldap3.Connection(mock_ldap_server, client_strategy=ldap3.MOCK_SYNC) as c:
c.strategy.entries_from_json(
os.path.join(settings.BASE_DIR, "fixtures", "csua_ldap_entries.json")
)
c.strategy.add_entry(
"uid=cnunez,ou=People,dc=csua,dc=berkeley,dc=edu",
{
"uid": "cnunez",
"cn": "cnunez",
"gecos": "C Nunez,[email protected]",
"uidNumber": 420,
"userPassword": "pp",
"objectClass": ["posixAccount"],
},
)
c.strategy.add_entry(
"uid=test_user,ou=People,dc=csua,dc=berkeley,dc=edu",
{
"uid": "test_user",
"cn": "test_user",
"uidNumber": 31337,
"userPassword": "test_password",
"objectClass": ["posixAccount"],
},
)
c.strategy.add_entry(
NEWUSER_DN,
{
"uid": "newuser",
"userPassword": TEST_NEWUSER_PW,
"objectClass": ["posixAccount"],
},
)
def tearDown(self):
for p in self.patchers:
p.stop()