|
@@ -0,0 +1,155 @@
|
|
1
|
+#-*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+import unittest
|
|
4
|
+import unittest.mock
|
|
5
|
+from unittest.mock import Mock
|
|
6
|
+
|
|
7
|
+from Lodel.user import authentication_method, identification_method, UserIdentity, UserContext
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+class AuthIdMethodTestCase(unittest.TestCase):
|
|
11
|
+
|
|
12
|
+ def test_authentication_method_registration(self):
|
|
13
|
+ before = set(authentication_method.list_methods())
|
|
14
|
+
|
|
15
|
+ def test_method(self):
|
|
16
|
+ pass
|
|
17
|
+ authentication_method(test_method) # faking test_method decoration
|
|
18
|
+
|
|
19
|
+ after = set(authentication_method.list_methods())
|
|
20
|
+ self.assertEqual(after - before, set([test_method]))
|
|
21
|
+
|
|
22
|
+ def test_identification_method_registration(self):
|
|
23
|
+ before = set(identification_method.list_methods())
|
|
24
|
+
|
|
25
|
+ def test_method(self):
|
|
26
|
+ pass
|
|
27
|
+ identification_method(test_method) # faking test_method decoration
|
|
28
|
+
|
|
29
|
+ after = set(identification_method.list_methods())
|
|
30
|
+ self.assertEqual(after - before, set([test_method]))
|
|
31
|
+
|
|
32
|
+ def test_authentication_method_calls(self):
|
|
33
|
+ mock_list = list()
|
|
34
|
+ for i in range(5):
|
|
35
|
+ auth_mock = Mock(return_value = False)
|
|
36
|
+ mock_list.append(auth_mock)
|
|
37
|
+ authentication_method(auth_mock)
|
|
38
|
+ ret = authentication_method.authenticate('login', 'password')
|
|
39
|
+ self.assertFalse(ret)
|
|
40
|
+ for mock in mock_list:
|
|
41
|
+ mock.assert_called_once_with('login', 'password')
|
|
42
|
+ # Adding a mock that will fake a successfull auth
|
|
43
|
+ user_id = UserIdentity(42, 'superlogin', authenticated = True)
|
|
44
|
+ authentication_method( Mock(return_value = user_id) )
|
|
45
|
+ ret = authentication_method.authenticate('login', 'password')
|
|
46
|
+ self.assertEqual(ret, user_id)
|
|
47
|
+
|
|
48
|
+ def test_identificatio_method_calls(self):
|
|
49
|
+ mock_list = list()
|
|
50
|
+ for i in range(5):
|
|
51
|
+ id_mock = Mock(return_value = False)
|
|
52
|
+ mock_list.append(id_mock)
|
|
53
|
+ identification_method(id_mock)
|
|
54
|
+ client_infos = {'ip':'127.0.0.1', 'user-agent': 'bla bla'}
|
|
55
|
+ ret = identification_method.identify(client_infos)
|
|
56
|
+ self.assertFalse(ret)
|
|
57
|
+ for mock in mock_list:
|
|
58
|
+ mock.assert_called_once_with(client_infos)
|
|
59
|
+ # Adding a mock that will fake a successfull identification
|
|
60
|
+ user_id = UserIdentity(42, 'login', identified = True)
|
|
61
|
+ identification_method( Mock(return_value = user_id) )
|
|
62
|
+ ret = identification_method.identify(client_infos)
|
|
63
|
+ self.assertEqual(ret, user_id)
|
|
64
|
+
|
|
65
|
+class UserIdentityTestCase(unittest.TestCase):
|
|
66
|
+
|
|
67
|
+ def test_init(self):
|
|
68
|
+ """ Testing UserIdentity constructor """
|
|
69
|
+ uid = UserIdentity(42, 'login', 'anonymous login')
|
|
70
|
+ self.assertEqual(uid.user_id, 42)
|
|
71
|
+ self.assertEqual(uid.username, 'login')
|
|
72
|
+ self.assertEqual(uid.fullname, 'anonymous login')
|
|
73
|
+ self.assertFalse(uid.is_authenticated)
|
|
74
|
+ self.assertFalse(uid.is_identified)
|
|
75
|
+ self.assertEqual(str(uid), uid.fullname)
|
|
76
|
+
|
|
77
|
+ def test_identified(self):
|
|
78
|
+ """ Testing identified flag relation with authenticated flag """
|
|
79
|
+ uid = UserIdentity(42, 'login', identified = True)
|
|
80
|
+ self.assertTrue(uid.is_identified)
|
|
81
|
+ self.assertFalse(uid.is_authenticated)
|
|
82
|
+
|
|
83
|
+ def test_authentified(self):
|
|
84
|
+ """ Testing identified flag relation with authenticated flag """
|
|
85
|
+ uid = UserIdentity(42, 'login', authenticated = True)
|
|
86
|
+ self.assertTrue(uid.is_identified)
|
|
87
|
+ self.assertTrue(uid.is_authenticated)
|
|
88
|
+
|
|
89
|
+ def test_anonymous(self):
|
|
90
|
+ """ Testing the anonymous UserIdentity """
|
|
91
|
+ anon = UserIdentity.anonymous()
|
|
92
|
+ self.assertEqual(anon, UserIdentity.anonymous())
|
|
93
|
+ self.assertFalse(anon.is_authenticated)
|
|
94
|
+ self.assertFalse(anon.is_identified)
|
|
95
|
+
|
|
96
|
+class UserContextTestCase(unittest.TestCase):
|
|
97
|
+
|
|
98
|
+ def test_static(self):
|
|
99
|
+ """ Testing that the class is static """
|
|
100
|
+ with self.assertRaises(NotImplementedError):
|
|
101
|
+ UserContext()
|
|
102
|
+
|
|
103
|
+ def test_not_init(self):
|
|
104
|
+ """ Testing method call with a non initialised class """
|
|
105
|
+ UserContext.__reset__()
|
|
106
|
+ with self.assertRaises(AssertionError):
|
|
107
|
+ UserContext.identity()
|
|
108
|
+ with self.assertRaises(AssertionError):
|
|
109
|
+ UserContext.authenticate('login', 'foobar')
|
|
110
|
+
|
|
111
|
+ def test_anon_init(self):
|
|
112
|
+ """ Testing class initialisation """
|
|
113
|
+ identification_method.__reset__()
|
|
114
|
+ authentication_method.__reset__()
|
|
115
|
+ UserContext.__reset__()
|
|
116
|
+ auth_id = UserIdentity(43, 'loggedlogin', authenticated = True)
|
|
117
|
+ # Add a fake authentication method
|
|
118
|
+ auth_mock = Mock(return_value = auth_id)
|
|
119
|
+ authentication_method(auth_mock)
|
|
120
|
+ # Are we anonymous ?
|
|
121
|
+ UserContext.init('localhost')
|
|
122
|
+ self.assertEqual(UserContext.identity(), UserIdentity.anonymous())
|
|
123
|
+ # Can we authenticate ourself ?
|
|
124
|
+ UserContext.authenticate('login', 'pass')
|
|
125
|
+ auth_mock.assert_called_once_with('login', 'pass')
|
|
126
|
+ self.assertEqual(auth_id, UserContext.identity())
|
|
127
|
+
|
|
128
|
+ def test_init(self):
|
|
129
|
+ """ Testing class initialisation being identified by client_infos"""
|
|
130
|
+ identification_method.__reset__()
|
|
131
|
+ authentication_method.__reset__()
|
|
132
|
+ UserContext.__reset__()
|
|
133
|
+ user_id = UserIdentity(42, 'login', identified = True)
|
|
134
|
+ auth_id = UserIdentity(43, 'loggedlogin', authenticated = True)
|
|
135
|
+ # Add a fake id method
|
|
136
|
+ id_mock = Mock(return_value = user_id)
|
|
137
|
+ identification_method(id_mock)
|
|
138
|
+ # Add a fake authentication method
|
|
139
|
+ auth_mock = Mock(return_value = auth_id)
|
|
140
|
+ authentication_method(auth_mock)
|
|
141
|
+ # testing lazy identification
|
|
142
|
+ UserContext.init('localhost')
|
|
143
|
+ id_mock.assert_not_called()
|
|
144
|
+ auth_mock.assert_not_called() # should really not be called yet
|
|
145
|
+ # triggering identification
|
|
146
|
+ ret_id = UserContext.identity()
|
|
147
|
+ id_mock.assert_called_once_with('localhost')
|
|
148
|
+ self.assertEqual(ret_id, user_id)
|
|
149
|
+ auth_mock.assert_not_called() # should really not be called yet
|
|
150
|
+ id_mock.reset_mock()
|
|
151
|
+ # Trying to auth
|
|
152
|
+ UserContext.authenticate('identifier', 'superproof')
|
|
153
|
+ id_mock.assert_not_called()
|
|
154
|
+ auth_mock.assert_called_once_with('identifier', 'superproof')
|
|
155
|
+ self.assertEqual(UserContext.identity(), auth_id)
|