-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVTN_event_tester.py
196 lines (163 loc) · 8.26 KB
/
VTN_event_tester.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import requests
import json
import os
class VTN_event_tester():
"""VTN integration tests"""
event_id = 0
ven_token = 'ven_token'
bl_token = 'bl_token'
def __init__(self, base_url):
self.events_baseUrl = base_url+"/events"
pass
def search_all_events(self, token):
headers = {'Authorization': 'Bearer ' + token}
try:
# search_all_events
response = requests.get(self.events_baseUrl, headers=headers)
# print(f"search_all_events: response.json={response.json()}")
except requests.exceptions.RequestException as e:
print("Exception when calling search_all_events(): %s\n" % e)
return response
def test_search_all_events(self, token):
# TBD: add test for query params
print("\n################################################################")
print(f"test_search_all_events(): token={token}")
response = self.search_all_events(token)
if response.status_code != 200:
print(f"test_search_all_events(): FAILED response.status_code != 200 response.status_code={response.status_code}")
return False
events = response.json()
if events is not None:
print("test_search_all_events(): PASSED")
return True
else:
print("test_search_all_events(): FAILED. events is None")
return False
def search_event_by_id(self, event_id, token):
url = self.events_baseUrl + '/' + str(event_id)
headers = {'Authorization': 'Bearer ' + token}
try:
# search_all_events
response = requests.get(url, headers = headers)
# print(f"search_event: response.json={response.json()}")
except requests.exceptions.RequestException as e:
print("Exception when calling search_event(): %s\n" % e)
return response
def test_search_event_by_id(self, token):
# TBD: add test for query params
print("\n################################################################")
print (f"test_search_event_by_id(): token={token}")
response = self.search_event_by_id(self.event_id, token)
if response.status_code != 200:
print(f"test_search_event_by_id(): FAILED response.status_code != 200 response.status_code={response.status_code}")
return False
event = response.json()
if event["ID"] == self.event_id:
print("test_search_event_by_id(): PASSED")
return True
else:
eventID = event["ID"]
print(f"test_search_event_by_id(): FAILED. event ID {eventID} does not match searched event ID {self.event_id}")
return False
def create_event(self, token):
event = '{ "name": "myEvent", "programID": 0, "intervalPeriod": {"start": "0"}, "intervals": [{"ID": 0, "payloads": [{"payloadType": "PRICE", "values": [0.17]}]}] }'
headers = {'Content-type': 'application/json', 'Accept': 'text/plain', 'Authorization': 'Bearer ' + token}
# print(f"create_event: event={event}")
try:
# create_event
response = requests.post(self.events_baseUrl, data=event, headers=headers)
# print(f"create_event: response={response.json()}")
except requests.exceptions.RequestException as e:
print("Exception when calling create_event(): %s\n" % e)
return response
def test_create_event(self, token):
print("\n################################################################")
print (f"test_create_event(): token={token}")
response = self.create_event(token)
# print (f"test_create_event(): status_code= {response.status_code}")
event = response.json()
# print (f"test_create_event(): event= {event}")
if token == self.bl_token and response.status_code != 200:
print(f"test_create_event(): FAILED response.status_code != 200 response.status_code={response.status_code}")
return False
elif token == self.ven_token and (response.status_code == 403 or response.status_code == 400):
print("test_create_event(): PASSED")
return True
if event['name'] == "myEvent":
print("test_create_event(): PASSED")
self.event_id = event['ID']
return True
else:
print("test_create_event(): FAILED. event name does not match created event name")
return False
def update_event(self, event_id, token):
event = '{ "name": "myNewEvent", "programID": 0, "intervalPeriod": {"start": "0"}, "intervals": [{"ID": 0, "payloads": [{"payloadType": "PRICE", "values": [0.17]}]}] }'
url = self.events_baseUrl + '/' + str(event_id)
headers = {'Content-type': 'application/json', 'Accept': 'text/plain', 'Authorization': 'Bearer ' + token}
try:
# update
response = requests.put(url, data=event, headers=headers)
# print(f"update_event: response={response.json()}")
except requests.exceptions.RequestException as e:
print("Exception when calling update_event(): %s\n" % e)
return response
def test_update_event(self, token):
print("\n################################################################")
print (f"test_update_event(): token={token}")
response = self.update_event(self.event_id, token)
if token == self.bl_token and response.status_code != 200:
print(f"test_update_event(): FAILED response.status_code != 200 response.status_code={response.status_code}")
return False
elif token == self.ven_token and response.status_code == 403:
print("test_update_event(): PASSED")
return True
event = response.json()
if event['name'] == "myNewEvent":
print("test_update_event(): PASSED")
return True
else:
print("test_update_event(): FAILED. event name does not match updated event name")
return False
def delete_event(self, event_id, token):
url = self.events_baseUrl + '/' + str(event_id)
headers = {'Content-type': 'application/json', 'Accept': 'text/plain', 'Authorization': 'Bearer ' + token}
try:
# delete event
response = requests.delete(url, headers=headers)
# print(f"delete_event: response={response.json()}")
except requests.exceptions.RequestException as e:
print("Exception when calling delete_event(): %s\n" % e)
return response
def test_delete_event(self, token):
print("\n################################################################")
print (f"test_delete_event(): token={token}")
response = self.delete_event(self.event_id, token)
if token == self.bl_token and response.status_code != 200:
print(f"test_delete_event(): FAILED response.status_code != 200 response.status_code={response.status_code}")
return False
elif token == self.ven_token and response.status_code == 403:
print("test_delete_event(): PASSED")
return True
event = response.json()
if event['ID'] == self.event_id:
print("test_delete_event(): PASSED")
return True
else:
print("test_delete_event(): FAILED. event ID does not match deleted event ID")
return False
def run_tests(self):
# Verify that events can be read
assert (self.test_search_all_events(self.ven_token))
assert (self.test_search_all_events(self.bl_token))
# Verify that a event resource may be created
assert(self.test_create_event(self.ven_token))
assert(self.test_create_event(self.bl_token))
# # Verify that the event resource created above is available
assert(self.test_search_event_by_id(self.ven_token))
assert(self.test_search_event_by_id(self.bl_token))
# Verify that the event resource created above cen be updated
assert(self.test_update_event(self.ven_token))
assert(self.test_update_event(self.bl_token))
# Verify that the event resource created above cen be deleted
assert(self.test_delete_event(self.ven_token))
assert(self.test_delete_event(self.bl_token))