-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api.py
83 lines (66 loc) · 2.53 KB
/
test_api.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
import time
import unittest
import user_auth as ua
from custom_exception import User_exception, Token_exception, Role_exception
class Test_user_auth(unittest.TestCase):
def test_add_remove_user(self):
ur = ua.User_roles()
ur.create_user('tony','12345')
ur.delete_user('tony')
with self.assertRaises(User_exception):
ur.delete_user('tony')
def test_add_remove_role(self):
ur = ua.User_roles()
ur.create_role('job1')
ur.delete_role('job1')
with self.assertRaises(Role_exception):
ur.delete_role('job1')
def test_add_role_user(self):
ur = ua.User_roles()
with self.assertRaises(User_exception):
ur.add_role_user('tony', 'job1')
ur.create_user('tony', '12345')
with self.assertRaises(Role_exception):
ur.add_role_user('tony', 'job1')
ur.create_role('job1')
ur.add_role_user('tony', 'job1')
with self.assertRaises(KeyError):
ur.authenticate('tony','4321')
token = ur.authenticate('tony','12345')
ur.create_role('job2')
ur.add_role_user('tony', 'job2')
self.assertEqual(ur.all_roles(token), ['job1','job2'])
ur.delete_role('job1')
self.assertFalse(ur.check_role(token, 'job1'))
self.assertTrue(ur.check_role(token, 'job2'))
ur.delete_user('tony')
def test_authenticate(self):
ur = ua.User_roles()
with self.assertRaises(User_exception):
ur.authenticate('liangli','212131')
ur.create_user('tony', '12345')
token = ur.authenticate('tony', '12345')
self.assertEqual(ur.all_roles(token), [])
ur.create_role('job1')
ur.add_role_user('tony', 'job1')
def test_invalidate(self):
ur = ua.User_roles()
ur.create_user('tony', '12345')
token = ur.authenticate('tony', '12345')
ur.invalidate('some random token')
ur.invalidate(token)
with self.assertRaises(Token_exception):
ur.check_role(token, 'job1')
def test_expire_token(self):
ur = ua.User_roles(ttl=5)
ur.create_user('tony', '12345')
ur.create_role('job1')
ur.add_role_user('tony','job1')
token = ur.authenticate('tony', '12345')
self.assertTrue(ur.check_role(token, 'job1'))
self.assertFalse(ur.check_role(token, 'job2'))
time.sleep(10)
with self.assertRaises(Token_exception):
ur.check_role(token, 'job1')
if __name__ == '__main__':
unittest.main()