-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.py
116 lines (94 loc) · 3.25 KB
/
test.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
import requests
import sys
import urllib3
class AuthTest:
service_url = ""
auth = ""
def init(self, service_url="http://localhost:18000", auth=""):
self.service_url = service_url
self.auth = auth
def create(self):
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + self.auth
}
response = requests.post(self.service_url + "/", headers=headers, verify=False)
return response
def update(self, contact_id):
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + self.auth
}
response = requests.put(self.service_url + "/" + contact_id, headers=headers, verify=False)
return response
def get(self, contact_id):
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + self.auth
}
response = requests.get(self.service_url + "/" + contact_id, headers=headers, verify=False)
return response
def delete(self, contact_id):
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + self.auth
}
response = requests.delete(self.service_url + "/" + contact_id, headers=headers, verify=False)
return response
def main(args):
urllib3.disable_warnings()
api1 = AuthTest()
api1.init(auth="user1", service_url="http://localhost:18000/contact")
print("user1 creating new contact")
create = api1.create()
if create.status_code == 200:
contact_id = create.text
print("[OK] ContactId: " + contact_id)
else:
print("FAILED")
sys.exit(1)
print("user1 getting the contact")
get_resp1 = api1.get(contact_id=contact_id)
if get_resp1.status_code == 200:
print("[OK] Data: " + get_resp1.text)
else:
print("FAILED")
sys.exit(1)
print("user1 updating the contact")
upd_resp1 = api1.update(contact_id=contact_id)
if upd_resp1.status_code == 200:
print("[OK] Updated")
else:
print("FAILED")
sys.exit(1)
api2 = AuthTest()
api2.init(auth="user2", service_url="http://localhost:18000/contact")
print("user2 reading the contact")
get_resp2 = api2.get(contact_id=contact_id)
if get_resp2.status_code == 200:
print("[OK] Data: " + get_resp2.text)
else:
print("FAILED")
sys.exit(1)
print("user2 updating the contact")
upd_resp2 = api2.update(contact_id=contact_id)
if upd_resp2.status_code == 200:
print("[OK] Updated")
else:
print("FAILED")
sys.exit(1)
api3 = AuthTest()
api3.init(auth="user3", service_url="http://localhost:18000/contact")
print("user3 deleting the contact")
del_resp2 = api3.delete(contact_id=contact_id)
if del_resp2.status_code == 403:
print("[OK] Unable to delete. User has only update permission. Owner permission is required to delete item.")
else:
print("FAILED")
sys.exit(1)
if __name__ == "__main__":
main(sys.argv)