/
test_rest_api.py
72 lines (56 loc) · 2.16 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
"""REST API tests."""
import pytest
import json
import os
os.environ.setdefault("USE_AWS", "False")
import insights_engine.rest_api as rest_api
@pytest.fixture(scope='module')
def client():
"""Create a test client for flask app."""
with rest_api.app.app.test_client() as c:
yield c
def get_json_from_response(response):
"""Decode JSON from response."""
return json.loads(response.data.decode('utf8'))
def test_readiness_endpoint(client):
"""Test the liveness probe."""
response = client.get('/api/v1/readiness')
assert response.status_code == 200
def test_readiness_endpoint_wrong_http_method(client):
"""Test the /api/v1/readiness endpoint by calling it with wrong HTTP method."""
response = client.post('/api/v1/readiness')
assert response.status_code == 405
response = client.put('/api/v1/readiness')
assert response.status_code == 405
response = client.patch('/api/v1/readiness')
assert response.status_code == 405
response = client.delete('/api/v1/readiness')
assert response.status_code == 405
def test_liveness_endpoint(client):
"""Test the liveness probe."""
response = client.get('/api/v1/liveness')
assert response.status_code == 200
def test_liveness_endpoint_wrong_http_method(client):
"""Test the /api/v1/liveness endpoint by calling it with wrong HTTP method."""
response = client.post('/api/v1/liveness')
assert response.status_code == 405
response = client.put('/api/v1/liveness')
assert response.status_code == 405
response = client.patch('/api/v1/liveness')
assert response.status_code == 405
response = client.delete('/api/v1/liveness')
assert response.status_code == 405
def test_companion(client):
"""Test the companion recommendation endpoint."""
data = [
{
"package_list": [
"github.com/gogo/protobuf/proto"
],
"comp_package_count_threshold": 1
}
]
response = client.post('/api/v1/companion_recommendation',
data=json.dumps(data),
headers={'content-type': 'application/json'})
assert response.status_code == 200