forked from privacyidea/privacyidea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
124 lines (102 loc) · 4.49 KB
/
base.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
import unittest
import json
from privacyidea.app import create_app
from privacyidea.models import db
from privacyidea.lib.resolver import (save_resolver)
from privacyidea.lib.realm import (set_realm)
from privacyidea.lib.user import User
from privacyidea.lib.auth import create_db_admin
PWFILE = "tests/testdata/passwords"
class FakeFlaskG():
policy_object = None
class MyTestCase(unittest.TestCase):
resolvername1 = "resolver1"
resolvername2 = "Resolver2"
resolvername3 = "reso3"
realm1 = "realm1"
realm2 = "realm2"
serials = ["SE1", "SE2", "SE3"]
otpkey = "3132333435363738393031323334353637383930"
@classmethod
def setUpClass(cls):
cls.app = create_app('testing', "")
cls.app_context = cls.app.app_context()
cls.app_context.push()
db.create_all()
# Create an admin for tests.
create_db_admin(cls.app, "testadmin", "[email protected]", "testpw")
def setUp_user_realms(self):
# create user realm
rid = save_resolver({"resolver": self.resolvername1,
"type": "passwdresolver",
"fileName": PWFILE})
self.assertTrue(rid > 0, rid)
(added, failed) = set_realm(self.realm1,
[self.resolvername1])
self.assertTrue(len(failed) == 0)
self.assertTrue(len(added) == 1)
user = User(login="root",
realm=self.realm1,
resolver=self.resolvername1)
user_str = "%s" % user
self.assertTrue(user_str == "<root.resolver1@realm1>", user_str)
self.assertFalse(user.is_empty())
self.assertTrue(User().is_empty())
user_repr = "%r" % user
expected = "User(login='root', realm='realm1', resolver='resolver1')"
self.assertTrue(user_repr == expected, user_repr)
def setUp_user_realm2(self):
# create user realm
rid = save_resolver({"resolver": self.resolvername1,
"type": "passwdresolver",
"fileName": PWFILE})
self.assertTrue(rid > 0, rid)
(added, failed) = set_realm(self.realm2,
[self.resolvername1])
self.assertTrue(len(failed) == 0)
self.assertTrue(len(added) == 1)
user = User(login="root",
realm=self.realm2,
resolver=self.resolvername1)
user_str = "%s" % user
self.assertTrue(user_str == "<root.resolver1@realm2>", user_str)
self.assertFalse(user.is_empty())
self.assertTrue(User().is_empty())
user_repr = "%r" % user
expected = "User(login='root', realm='realm2', resolver='resolver1')"
self.assertTrue(user_repr == expected, user_repr)
@classmethod
def tearDownClass(cls):
db.session.remove()
db.drop_all()
cls.app_context.pop()
def setUp(self):
self.authenticate()
def authenticate(self):
with self.app.test_request_context('/auth',
data={"username": "testadmin",
"password": "testpw"},
method='POST'):
res = self.app.full_dispatch_request()
self.assertTrue(res.status_code == 200, res)
result = json.loads(res.data).get("result")
self.assertTrue(result.get("status"), res.data)
self.at = result.get("value").get("token")
def authenticate_selfserive_user(self):
with self.app.test_request_context('/auth',
method='POST',
data={"username":
"selfservice@realm1",
"password": "test"}):
res = self.app.full_dispatch_request()
self.assertTrue(res.status_code == 200, res)
result = json.loads(res.data).get("result")
self.assertTrue(result.get("status"), res.data)
# In self.at_user we store the user token
self.at_user = result.get("value").get("token")
# check that this is a user
role = result.get("value").get("role")
self.assertTrue(role == "user", result)
self.assertEqual(result.get("value").get("realm"), "realm1")
# Test logout time
self.assertEqual(result.get("value").get("logout_time"), 30)