-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_planet.py
292 lines (226 loc) · 11 KB
/
test_planet.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
__author__ = "Manjunath Babu"
__version__ = "1.0"
__email__ = "mababu@syr.edu"
__doc__ = """ test_planet.py: All tests included using pytest package """
import planet, random, requests, pytest, sys, json
planet.assetStore = []
API_URL = "http://127.0.0.1:5000"
def server_up(f, *args, **kwargs):
def wraperFunc():
try:
requests.get(API_URL)
return f(*args, **kwargs)
except requests.ConnectionError:
assert True, f"{API_URL} is down! Check the server."
return wraperFunc
@server_up
def test_PlanetApplication():
response = requests.get(f'{API_URL}/',headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetAssetInvalidURL():
response = requests.get(f'{API_URL}/plan',headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostAssetInvalidURL():
response = requests.post(f'{API_URL}/plan',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"Star100","asset type":"satellite","asset class":"dove"})
assert response.ok, f"Error! {response.json()}"
return response
sampleAssets = [{"asset name": "MarsLander", "asset type": "satellite", "asset class": "dove"},
{"asset name": "Earth2-0", "asset type": "satellite", "asset class": "rapideye"},
{"asset name": "PoleStarViewer", "asset type": "antenna", "asset class": "dish", "asset details": {"diameter":"34.21", "radome":"False"}},
{"asset name": "SkyScanner99", "asset type": "antenna", "asset class": "yagi", "asset details": {"gain":"1.21"}}]
@server_up
def test_post4SampleAssets():
for someAsset in sampleAssets:
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json=someAsset)
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostAssetWrongFormat():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":123,"asset type":"antenna","asset class":"yagi","asset details":{"gain":100.12}})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetAllAssets():
response = requests.get(f'{API_URL}/planet',headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetOneAvailableAsset():
response = requests.get(f'{API_URL}/planet/MarsLander',headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetOneUnavailableAsset():
response = requests.get(f'{API_URL}/planet/SPY101',headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostInvalidUser():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'WhoIsIt'},
json={"asset name":"SF-SAT","asset type":"satellite","asset class":"dove"})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneValidAsset():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"SF-SAT","asset type":"satellite","asset class":"dove"})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneDuplicateAsset():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"SF-SAT","asset type":"satellite","asset class":"dove"})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneInvalidAssetName():
response = requests.post(f'{API_URL}/planet',headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"123","asset type":"satellite","asset class":"dove"})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneInvalidAssetType():
response = requests.post(f'{API_URL}/planet',headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"SF-SD","asset type":"rocket","asset class":"dove"})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneInvalidAssetClass():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"car101","asset type":"satellite","asset class":"car"})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneInvalidAssetDetailsDiameter():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"EclipseChaser","asset type":"antenna","asset class":"dish","asset details":{"diameter":"a32.11a","radome":"true"}})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneInvalidAssetDetailsRadome():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"Moon101","asset type":"antenna","asset class":"dish","asset details":{"diameter":"100.78","radome":"001trueFalsezz9"}})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneInvalidAssetDetailsGain():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"NeptuneLander","asset type":"antenna","asset class":"yagi","asset details":{"gain":"zz300.2zz"}})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_PostOneInvalidAssetDetailsSatellite():
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json={"asset name":"MarsTester","asset type":"satellite","asset class":"dove","asset details":{"diameter":"100.78","radome":"true"}})
assert response.ok, f"Error! {response.json()}"
return response
# ASSET_TYPE_CLASS = {"satellite":["dove", "rapideye"],
# "antenna" :["dish", "yagi"]}
@server_up
def test_post20RandomValidAssets():
for i in range(1,21):
assetType = random.choice(list(planet.ASSET_TYPE_CLASS.keys()))
assetClass = random.choice(planet.ASSET_TYPE_CLASS[assetType])
jsonVar = {"asset name":f'{assetType}_{i}',"asset type":assetType,"asset class":assetClass}
if(assetType == "antenna"):
if(assetClass == "dish"):
jsonVar['asset details'] = {"diameter":random.random()*100.0 , "radome":random.choice(["True","False"])}
if(assetClass == "yagi"):
jsonVar['asset details'] = {"gain":random.random()*10.0}
response = requests.post(f'{API_URL}/planet',
headers={'Content-Type':'application/json','X-User':'admin'},
json=jsonVar)
jsonVar = []
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetFilteredDoves():
response = requests.get(f'{API_URL}/planet/filter/dove',
headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetFilteredRapideyes():
response = requests.get(f'{API_URL}/planet/filter/rapideye',
headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetInvalidFilter():
response = requests.get(f'{API_URL}/planet/filter/nothing',
headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetFilteredSatellites():
response = requests.get(f'{API_URL}/planet/filter/satellite',
headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
@server_up
def test_GetFilteredAntennas():
response = requests.get(f'{API_URL}/planet/filter/antenna',
headers={'Content-Type':'application/json'})
assert response.ok, f"Error! {response.json()}"
return response
#Execute specific test cases that's passed from the main function
def call_Test_Case(f,title):
if not hasattr(call_Test_Case, "counter"):
call_Test_Case.counter = 1
print("\n"*5)
print("*"*len(title))
print(f"\tTest Case Number: {call_Test_Case.counter}")
print(title)
print("*"*len(title))
response = f
print(f"Testing_Response: {json.dumps(response.json(),indent=5)}")
call_Test_Case.counter += 1
def main():
print("Welcome to Planet Labs Asset Store Testing")
call_Test_Case(test_PlanetApplication(), "GET Request:\n Check server if its up and running")
call_Test_Case(test_GetAssetInvalidURL(), "GET Request:\n Get request from a malformed URL")
call_Test_Case(test_PostAssetInvalidURL(), "POST Request:\n Post request to a malformed URL")
call_Test_Case(test_post4SampleAssets(), "POST Request:\n Post 4 sample assets of all valid combinations of types and classes")
call_Test_Case(test_PostAssetWrongFormat(), "POST Request:\n Post an asset with wrong json format")
call_Test_Case(test_GetAllAssets(), "GET Request:\n Get all available assets")
call_Test_Case(test_GetOneAvailableAsset(), "GET Request:\n Get one available asset")
call_Test_Case(test_GetOneUnavailableAsset(), "GET Request:\n Get one unavailable asset")
call_Test_Case(test_PostInvalidUser(), "POST Request:\n Post an asset with invalid X-User")
call_Test_Case(test_PostOneValidAsset(),"POST Request:\n Post one valid asset")
call_Test_Case(test_PostOneDuplicateAsset(),"POST Request:\n Post one duplicate asset")
call_Test_Case(test_PostOneInvalidAssetName(), "POST Request:\n Post one asset with invalid asset name")
call_Test_Case(test_PostOneInvalidAssetType(), "POST Request:\n Post one asset with invalid asset type")
call_Test_Case(test_PostOneInvalidAssetClass(), "POST Request:\n Post one asset with invalid asset class")
call_Test_Case(test_PostOneInvalidAssetDetailsDiameter(), "POST Request:\n Post one antenna asset with invalid diameter float value")
call_Test_Case(test_PostOneInvalidAssetDetailsRadome(), "POST Request:\n Post one antenna asset with invalid radome bool value")
call_Test_Case(test_PostOneInvalidAssetDetailsGain(), "POST Request:\n Post one antenna asset with invalid gain float value")
call_Test_Case(test_PostOneInvalidAssetDetailsSatellite(), "POST Request:\n Post one satellite asset with invalid details that belongs to antenna type")
call_Test_Case(test_post20RandomValidAssets(), "POST Request:\n Post 20 Randomly generated valid assets")
call_Test_Case(test_GetFilteredDoves(),"GET Request:\n Filter all assets with only dove class")
call_Test_Case(test_GetFilteredRapideyes() ,"GET Request:\n Filter all assets with only rapideye class")
call_Test_Case(test_GetInvalidFilter() ,"GET Request:\n Try to filter with wrong class or type")
call_Test_Case(test_GetFilteredSatellites(),"GET Request:\n Filter all assets with only type satellite")
call_Test_Case(test_GetFilteredAntennas(),"GET Request:\n Filter all assets with only type antenna")
print("\nSUCCESSFULLY COMPLETED ALL TESTS. \nTHANK YOU")
if(__name__ == '__main__'):
print("Running all test cases")
mainRet = main()
sys.exit(mainRet)