This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
/
test_rest_api.py
112 lines (89 loc) · 3.66 KB
/
test_rest_api.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
"""Basic REST API tests."""
import json
def api_route_for(route):
"""Construct an URL to the endpoint for given route."""
return '/api/v1/' + route
def get_json_from_response(response):
"""Decode JSON from response."""
return json.loads(response.data.decode('utf8'))
def test_readiness_endpoint(client):
"""Test the /api/v1/readiness endpoint."""
response = client.get(api_route_for("readiness"))
assert response.status_code == 200
json_data = get_json_from_response(response)
assert json_data == {}, "Empty JSON response expected"
def test_readiness_endpoint_wrong_http_method(client):
"""Test the /api/v1/readiness endpoint by calling it with wrong HTTP method."""
url = api_route_for("readiness")
response = client.post(url)
assert response.status_code == 405
response = client.put(url)
assert response.status_code == 405
response = client.patch(url)
assert response.status_code == 405
response = client.delete(url)
assert response.status_code == 405
def test_liveness_endpoint(client):
"""Test the /api/v1/liveness endpoint."""
response = client.get(api_route_for("liveness"))
assert response.status_code == 200
json_data = get_json_from_response(response)
assert json_data == {}, "Empty JSON response expected"
def test_liveness_endpoint_wrong_http_method(client):
"""Test the /api/v1/liveness endpoint by calling it with wrong HTTP method."""
url = api_route_for("liveness")
response = client.post(url)
assert response.status_code == 405
response = client.put(url)
assert response.status_code == 405
response = client.patch(url)
assert response.status_code == 405
response = client.delete(url)
assert response.status_code == 405
def test_ingest_endpoint_wrong_http_method(client):
"""Test the /api/v1/ingest endpoint with wrong method."""
response = client.get(api_route_for("ingest"))
assert response.status_code == 405
def test_ingest_endpoint_wrong_content_type(client):
"""Test the /api/v1/ingest endpoint with wrong content-type."""
headers = {
'Content-Type': 'image/gif'
}
response = client.post(api_route_for("ingest"), headers=headers)
assert response.status_code == 400
json_data = get_json_from_response(response)
assert "success" in json_data
assert "summary" in json_data
assert not json_data["success"]
assert json_data["summary"] == "Set content type to application/json"
def test_ingest_endpoint_no_payload(client):
"""Test the /api/v1/ingest endpoint with no payload."""
headers = {
'Content-Type': 'application/json'
}
response = client.post(api_route_for("ingest"), headers=headers)
assert response.status_code == 400
json_data = get_json_from_response(response)
assert json_data is not None
def test_ingest_endpoint_empty_payload(client):
"""Test the /api/v1/ingest endpoint with empty payload."""
headers = {
'Content-Type': 'application/json'
}
payload = {}
response = client.post(api_route_for("ingest"), headers=headers, json=payload)
assert response.status_code == 404
json_data = get_json_from_response(response)
assert "success" in json_data
assert "summary" in json_data
assert not json_data["success"]
assert json_data["summary"] == "Invalid: input is empty"
if __name__ == '__main__':
test_readiness_endpoint()
test_readiness_endpoint_wrong_http_method()
test_liveness_endpoint()
test_liveness_endpoint_wrong_http_method()
test_ingest_endpoint_wrong_http_method()
test_ingest_endpoint_wrong_content_type()
test_ingest_endpoint_no_payload()
test_ingest_endpoint_empty_payload()