-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
160 lines (150 loc) · 6.1 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
import base64
import unittest
from app import app, ISSUER_ID, ISSUER_SECRET, License, db
creds = base64.b64encode(f"{ISSUER_ID}:{ISSUER_SECRET}".encode("utf-8")).decode("utf-8")
class JsonTests(unittest.TestCase):
def test_get_license(self):
with app.test_client() as c:
rv = c.get(
"/get_license?aud=3e200daa-6bf8-470b-bd6a-4f55996052c3&key=LICENSE_KEY_3",
headers={"Authorization": f"Basic {creds}"},
)
json_data = rv.get_json()
self.assertEqual(json_data.get('id'), 'SERIAL_NO_3')
self.assertIn('key', json_data)
self.assertEqual(
json_data.get("aud"), "3e200daa-6bf8-470b-bd6a-4f55996052c3"
)
self.assertIsNotNone(json_data.get('iss'))
self.assertEqual(json_data.get("exp"), 1546128000)
self.assertIsNotNone(json_data.get("editions"))
self.assertEqual(json_data["editions"].get("en"), "Full Edition")
self.assertGreater(json_data.get('numberOfSeats'), 0)
def test_get_license_no_auth(self):
with app.test_client() as c:
rv = c.get(
"/get_license?aud=3e200daa-6bf8-470b-bd6a-4f55996052c3&key=LICENSE_KEY_3"
)
json_data = rv.get_json()
self.assertEqual(rv.status_code, 401)
self.assertEqual(json_data.get("description"), "Incorrect Credentials")
self.assertEqual(
rv.headers.get("WWW-Authenticate"), 'Basic realm="Credentials Required"'
)
def test_get_license_not_valid(self):
with app.test_client() as c:
rv = c.get(
"/get_license?aud=3e200daa-6bf8-470b-bd6a-4f55996052c3&key=LICENSE_KEY_0",
headers={"Authorization": f"Basic {creds}"},
)
json_data = rv.get_json()
# self.assertEqual(
# json_data.get("aud"), "3e200daa-6bf8-470b-bd6a-4f55996052c3"
# )
# self.assertIsNotNone(json_data.get("editions"))
# self.assertEqual(json_data["editions"].get("en"), "Full Edition")
# self.assertEqual(json_data.get("exp"), 1546128000)
def test_add_license(self):
with app.app_context():
lic = License.query.filter_by(serial='SERIAL_NO_5').first()
if lic:
db.session.delete(lic)
lic = License(
key="RH50-ABCD-EFGZ-HIJK-LMNO",
serial="SERIAL_NO_5",
product_id="PRODUCT-ID-HERE",
enabled=True,
entity_id=None,
date=None,
expiration_date=None,
)
db.session.add(lic)
db.session.commit()
payload = {
"entityId": "9304194021213-|-Group",
"entityType": "Group",
"license": {"key": "RH50-ABCD-EFGZ-HIJK-LMNO", "aud": "PRODUCT-ID-HERE"},
"userInfo": {
"sub": "43190412048124",
"email": "[email protected]",
"com.rhino3d.accounts.emails": [
],
"com.rhino3d.accounts.member_groups": [
{"id": "9304194021213", "name": "Marley’s Friends LLC"}
],
"com.rhino3d.accounts.admin_groups": [],
"com.rhino3d.accounts.owner_groups": [],
"name": "Marley",
"locale": "en-gb",
"picture": "http://marley.the.dog.com/images/coolpic.png",
},
"precondition": "RH40-ABCD-EFGZ-HIJK-LMNO",
}
with app.test_client() as c:
rv = c.post(
"/add_license",
json=payload,
headers={"Authorization": f"Basic {creds}"},
)
self.assertEqual(rv.status_code, 200)
json_data = rv.get_json()
self.assertEqual(json_data['licenses'][0]['id'], 'SERIAL_NO_5')
with app.app_context():
lic = License.query.filter_by(serial='SERIAL_NO_5').first()
self.assertEqual(lic.entity_id, '9304194021213-|-Group')
def test_remove_license(self):
with app.app_context():
lic = License.query.filter_by(serial='SERIAL_NO_6').first()
if lic:
db.session.delete(lic)
lic = License(
key='LICENSE_KEY_6',
serial='SERIAL_NO_6',
product_id='PRODUCT_ID_HERE',
enabled=True,
entity_id='9304194021213-|-Group',
date=None,
expiration_date=None
)
db.session.add(lic)
db.session.commit()
payload = {
"entityId": "9304194021213-|-Group",
"entityType": "Group",
"userInfo": {
"sub": "43190412048124",
"email": "[email protected]",
"name": "Marley",
"locale": "en-gb",
"picture":"http://marley.the.dog.com/images/coolpic.png"
},
"licenseCluster": {
"licenses": [
{
"id": "SERIAL_NO_6",
"key": "LICENSE_KEY_6",
"aud": "PRODUCT_ID_HERE",
"iss": ISSUER_ID,
"exp": None,
"numberOfSeats": 1,
"editions": {
"en": "Full Edition",
}
}
]
}
}
with app.test_client() as c:
rv = c.post(
"/remove_license",
json=payload,
headers={"Authorization": f"Basic {creds}"},
)
self.assertEqual(rv.status_code, 200)
with app.app_context():
lic = License.query.filter_by(serial='SERIAL_NO_6').first()
self.assertIsNone(lic.entity_id)
if __name__ == "__main__":
unittest.main()