Skip to content

Commit 0522dfe

Browse files
committed
Added Try/Except Block for Shelterluv Requests
Also moved the tests to the bottom of the file.
1 parent 422dba8 commit 0522dfe

File tree

1 file changed

+102
-91
lines changed

1 file changed

+102
-91
lines changed

src/server/test_api.py

Lines changed: 102 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@
3131
SERVER_URL = "http://server:5000"
3232
IS_LOCAL = False
3333

34+
3435
try:
3536
from secrets_dict import SHELTERLUV_SECRET_TOKEN
3637
except ImportError:
3738
SHELTERLUV_SECRET_TOKEN = os.getenv("SHELTERLUV_SECRET_TOKEN")
38-
39-
SL_Token = True if SHELTERLUV_SECRET_TOKEN else False
40-
print(os.getenv("SHELTERLUV_SECRET_TOKEN"))
41-
39+
finally:
40+
SL_Token = True if SHELTERLUV_SECRET_TOKEN else False
4241

4342

4443
### DNS lookup tests ##############################
@@ -148,93 +147,6 @@ def test_inact_userblocked(state: State):
148147
response = requests.post(SERVER_URL + "/api/user/login", json=data)
149148
assert response.status_code == 401
150149

151-
152-
### Shelterluv API tests ######################################
153-
154-
@pytest.mark.skipif(SL_Token, reason="Not run when SL_Token Present")
155-
def test_user_get_person_animal_events(state: State):
156-
""" Test that the api returns mock data if the Shelterluv Token
157-
is missing from secrets
158-
"""
159-
160-
# Build auth string value including token from state
161-
b_string = 'Bearer ' + state.state['base_user']
162-
163-
assert len(b_string) > 24
164-
165-
auth_hdr = {'Authorization' : b_string}
166-
url = SERVER_URL + "/api/person/12345/animal/12345/events"
167-
response = requests.get(url, headers = auth_hdr)
168-
169-
assert response.status_code == 200
170-
171-
from api.fake_data import sl_mock_data
172-
assert response.json() == sl_mock_data("events")
173-
174-
175-
@pytest.mark.skipif(SL_Token, reason="Not run when SL_Token Present")
176-
def test_user_get_animals(state: State):
177-
""" Test that the api returns mock data if the Shelterluv Token
178-
is missing from secrets
179-
"""
180-
181-
# Build auth string value including token from state
182-
b_string = 'Bearer ' + state.state['base_user']
183-
184-
assert len(b_string) > 24
185-
186-
auth_hdr = {'Authorization' : b_string}
187-
url = SERVER_URL + "/api/person/12345/animals"
188-
response = requests.get(url, headers = auth_hdr)
189-
190-
assert response.status_code == 200
191-
192-
from api.fake_data import sl_mock_data
193-
assert response.json() == sl_mock_data("animals")
194-
195-
196-
@pytest.mark.skipif(not SL_Token, reason="Run when SL_Token Present")
197-
def test_user_get_animals_sl_token(state: State):
198-
""" Test to confirm api does not return mock values if the Shelterluv Token
199-
is present in the secrets_dict file.
200-
Note this works on the assumption the SL token is not valid, and returns
201-
a default empty value
202-
"""
203-
204-
# Build auth string value including token from state
205-
b_string = 'Bearer ' + state.state['base_user']
206-
207-
assert len(b_string) > 24
208-
209-
auth_hdr = {'Authorization' : b_string}
210-
url = SERVER_URL + "/api/person/12345/animals"
211-
response = requests.get(url, headers = auth_hdr)
212-
213-
assert response.status_code == 200
214-
assert response.json() == {'person_details': {}, 'animal_details': {}}
215-
216-
217-
@pytest.mark.skipif(not SL_Token, reason="Run when SL_Token Present")
218-
def test_user_get_person_animal_events_sl_token(state: State):
219-
""" Test to confirm api does not return mock values if the Shelterluv Token
220-
is present in the secrets_dict file.
221-
Note this works on the assumption the SL token is not valid, and returns
222-
a default empty value
223-
"""
224-
225-
# Build auth string value including token from state
226-
b_string = 'Bearer ' + state.state['base_user']
227-
228-
assert len(b_string) > 24
229-
230-
auth_hdr = {'Authorization' : b_string}
231-
url = SERVER_URL + "/api/person/12345/animal/12345/events"
232-
response = requests.get(url, headers = auth_hdr)
233-
234-
assert response.status_code == 200
235-
assert response.json() == {}
236-
237-
238150
### Admin-level tests ######################################
239151

240152
def test_adminlogin(state: State):
@@ -355,3 +267,102 @@ def test_statistics(state: State):
355267
response = requests.get(SERVER_URL + "/api/statistics", headers=auth_hdr)
356268
assert response.status_code == 200
357269

270+
271+
### Shelterluv API tests ######################################
272+
273+
@pytest.mark.skipif(SL_Token, reason="Not run when SL_Token Present")
274+
def test_user_get_person_animal_events(state: State):
275+
""" Test that the api returns mock data if the Shelterluv Token
276+
is missing from secrets
277+
"""
278+
279+
# Build auth string value including token from state
280+
b_string = 'Bearer ' + state.state['base_user']
281+
282+
assert len(b_string) > 24
283+
284+
auth_hdr = {'Authorization' : b_string}
285+
url = SERVER_URL + "/api/person/12345/animal/12345/events"
286+
287+
try:
288+
response = requests.get(url, headers = auth_hdr)
289+
except RuntimeError as err:
290+
print(err)
291+
else:
292+
assert response.status_code == 200
293+
from api.fake_data import sl_mock_data
294+
assert response.json() == sl_mock_data("events")
295+
296+
297+
@pytest.mark.skipif(SL_Token, reason="Not run when SL_Token Present")
298+
def test_user_get_animals(state: State):
299+
""" Test that the api returns mock data if the Shelterluv Token
300+
is missing from secrets
301+
"""
302+
303+
# Build auth string value including token from state
304+
b_string = 'Bearer ' + state.state['base_user']
305+
306+
assert len(b_string) > 24
307+
308+
auth_hdr = {'Authorization' : b_string}
309+
url = SERVER_URL + "/api/person/12345/animals"
310+
311+
try:
312+
response = requests.get(url, headers = auth_hdr)
313+
except RuntimeError as err:
314+
print(err)
315+
else:
316+
assert response.status_code == 200
317+
from api.fake_data import sl_mock_data
318+
assert response.json() == sl_mock_data("animals")
319+
320+
321+
@pytest.mark.skipif(not SL_Token, reason="Run when SL_Token Present")
322+
def test_user_get_animals_sl_token(state: State):
323+
""" Test to confirm api does not return mock values if the Shelterluv Token
324+
is present in the secrets_dict file.
325+
Note this works on the assumption the SL token is not valid, and returns
326+
a default empty value
327+
"""
328+
329+
# Build auth string value including token from state
330+
b_string = 'Bearer ' + state.state['base_user']
331+
332+
assert len(b_string) > 24
333+
334+
auth_hdr = {'Authorization' : b_string}
335+
url = SERVER_URL + "/api/person/12345/animals"
336+
337+
try:
338+
response = requests.get(url, headers = auth_hdr)
339+
except RuntimeError as err:
340+
print(err)
341+
else:
342+
assert response.status_code == 200
343+
assert response.json() == {'person_details': {}, 'animal_details': {}}
344+
345+
346+
@pytest.mark.skipif(not SL_Token, reason="Run when SL_Token Present")
347+
def test_user_get_person_animal_events_sl_token(state: State):
348+
""" Test to confirm api does not return mock values if the Shelterluv Token
349+
is present in the secrets_dict file.
350+
Note this works on the assumption the SL token is not valid, and returns
351+
a default empty value
352+
"""
353+
354+
# Build auth string value including token from state
355+
b_string = 'Bearer ' + state.state['base_user']
356+
357+
assert len(b_string) > 24
358+
359+
auth_hdr = {'Authorization' : b_string}
360+
url = SERVER_URL + "/api/person/12345/animal/12345/events"
361+
362+
try:
363+
response = requests.get(url, headers = auth_hdr)
364+
except RuntimeError as err:
365+
print(err)
366+
else:
367+
assert response.status_code == 200
368+
assert response.json() == {}

0 commit comments

Comments
 (0)