-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
executable file
·167 lines (136 loc) · 5.53 KB
/
tests.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest
from ldapom import LDAPConnection
from ldapom_model import LDAPModel, LDAPAttr
from flask.ext.servicelayer import LDAPOMService
import test_server
import werkzeug
class LDAPServerMixin(object):
"""Mixin to set up an LDAPConnection connected to a testing LDAP server."""
def setUp(self):
self.ldap_server = test_server.LDAPServer()
self.ldap_server.start()
self.ldap = LDAPConnection(
uri=self.ldap_server.ldapi_url(),
base='dc=example,dc=com',
bind_dn='cn=admin,dc=example,dc=com',
bind_password='admin')
def tearDown(self):
self.ldap_server.stop()
class Person(LDAPModel):
_class = 'person'
_class_attrs = {'cn': LDAPAttr('cn'),
'lastname': LDAPAttr('sn', server_default="Default"),
'invalidAttribute': LDAPAttr('invalid'),
'shell': LDAPAttr('loginShell'),
'phone': LDAPAttr('telephoneNumber', multiple=True),
'home': LDAPAttr('homeDirectory', nullable=False),
'description': LDAPAttr('description', default="Default"),
'photo': LDAPAttr('jpegPhoto')}
_rdn = 'cn'
def __str__(self):
return self.name
@property
def name(self):
return ' '.join([self.givenName, self.sn]) if self.givenName else self.sn
class PersonService(LDAPOMService):
__model__ = Person
def __init__(self, ldap):
super().__init__()
self.__ldap__ = ldap
class TestLDAPModel(LDAPServerMixin, unittest.TestCase):
def setUp(self):
super().setUp()
self.service = PersonService(self.ldap)
def test_ok(self):
self.assertTrue(True)
def test_all(self):
people = self.service.all()
self.assertEqual(len(people), 4)
def test_get(self):
self.service.get("jack")
with self.assertRaises(Exception):
self.service.get("nobody")
with self.assertRaises(Exception):
self.service.get("*a*")
def test_get_or_404(self):
self.service.get_or_404("jack")
with self.assertRaises(werkzeug.exceptions.NotFound):
self.service.get_or_404("nobody")
with self.assertRaises(Exception):
self.service.get_or_404("*a*")
def test_get_all(self):
people = self.service.get_all("sam")
self.assertEqual(len(people), 1)
people = self.service.get_all("sam", "jack")
self.assertEqual(len(people), 2)
with self.assertRaises(Exception):
people = self.service.get_all("nobody")
def test_find(self):
people = self.service.find()
self.assertEqual(len(people), 4)
people = self.service.find(shell="/bin/bash")
self.assertEqual(len(people), 4)
people = self.service.find(lastname="Carter")
self.assertEqual(len(people), 1)
self.assertEqual(people[0].cn, "sam")
people = self.service.find(lastname="Carter", shell="/bin/bash")
self.assertEqual(len(people), 1)
people = self.service.find(lastname="nobody")
self.assertEqual(len(people), 0)
# :TODO:maethor:140604: search with multiple values attribute ?
def test_first(self):
self.service.first()
self.service.first(shell="/bin/bash")
self.service.first(lastname="Carter")
with self.assertRaises(Exception):
self.service.first(lastname="nobody")
def test_one(self):
self.service.one(lastname="Carter")
with self.assertRaises(Exception):
self.service.one(shell="/bin/bash")
with self.assertRaises(Exception):
self.service.first(lastname="nobody")
def test_new(self):
george = self.service.new(cn="george", lastname="Hammond")
self.assertEqual(george.cn, "george")
self.assertEqual(george.lastname, "Hammond")
self.assertEqual(george.dn, "cn=george,dc=example,dc=com")
with self.assertRaises(Exception):
self.service.get("george")
george = self.service.save(george)
self.assertEqual(george.cn, "george")
george = self.service.get("george")
self.assertEqual(george.dn, "cn=george,dc=example,dc=com")
def test_create(self):
george = self.service.create(cn="george", lastname="Hammond")
self.assertEqual(george.cn, "george")
self.service.get("george")
with self.assertRaises(Exception):
george = self.service.create(cn="sam", lastname="Hammond")
def test_update(self):
jack = self.service.get("jack")
self.assertEqual(jack.shell, "/bin/bash")
self.service.update(jack, shell="/bin/zsh")
self.assertEqual(jack.shell, "/bin/zsh")
jack = self.service.get("jack")
self.assertEqual(jack.shell, "/bin/zsh")
self.service.update(jack, shell="/bin/bash", phone="4242424242")
jack = self.service.get("jack")
self.assertEqual(jack.shell, "/bin/bash")
self.assertEqual(jack.phone, {"4242424242"})
jack = self.service.get("jack")
self.service.update(jack, phone="")
jack = self.service.get("jack")
self.assertEqual(jack.phone, set())
def test_delete(self):
jack = self.service.get("jack")
self.service.delete(jack)
with self.assertRaises(Exception):
self.service.get("jack")
def test_paginate(self):
# :TODO:maethor:140604:
pass
if __name__ == '__main__':
unittest.main()