Skip to content

Commit 6912fe7

Browse files
author
Bryan Sieber
committed
Adding api tests
1 parent 76edde4 commit 6912fe7

File tree

6 files changed

+68
-3
lines changed

6 files changed

+68
-3
lines changed

src/app/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_version():
2121
Return contents of version.json.
2222
This has generic data in repo, but gets the build details in CI.
2323
"""
24-
_root = os.path.dirname(os.path.dirname(__file__))
24+
_root = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
2525
version_path = os.path.join(_root, "version.json")
2626
info = {}
2727
if os.path.exists(version_path):

src/app/monitor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ def heartbeat(request: Request, settings: environment.Settings):
1313
"""Return status of backing services, as required by Dockerflow."""
1414
data: Dict = {}
1515
data.update(jbi_service_health_map(settings))
16-
1716
status_code = 200
1817
for _, health in data.items():
19-
if not health["up"]:
18+
if not health.get("up"):
2019
status_code = 503
2120

2221
return JSONResponse(content=data, status_code=status_code)

tests/unit/app/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
from fastapi.testclient import TestClient
3+
4+
from src.app.api import app
5+
from src.app.environment import Settings
6+
7+
8+
@pytest.fixture
9+
def anon_client():
10+
"""A test client with no authorization."""
11+
return TestClient(app)
12+
13+
14+
@pytest.fixture
15+
def settings():
16+
return Settings()

tests/unit/app/test_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def test_read_root(anon_client):
2+
"""The site root redirects to the Swagger docs"""
3+
resp = anon_client.get("/")
4+
assert resp.status_code == 200
5+
assert len(resp.history) == 1
6+
prev_resp = resp.history[0]
7+
assert prev_resp.status_code == 307 # Temporary Redirect
8+
assert prev_resp.headers["location"] == "./docs"

tests/unit/app/test_monitor.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import json
2+
import os.path
3+
from unittest.mock import patch
4+
5+
6+
def test_read_version(anon_client):
7+
"""__version__ returns the contents of version.json."""
8+
here = os.path.dirname(__file__)
9+
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(here)))
10+
version_path = os.path.join(root_dir, "version.json")
11+
with open(version_path, "r", encoding="utf8") as vp_file:
12+
version_contents = vp_file.read()
13+
expected = json.loads(version_contents)
14+
resp = anon_client.get("/__version__")
15+
assert resp.status_code == 200
16+
assert resp.json() == expected
17+
18+
19+
def test_read_heartbeat_no_services_fails(anon_client):
20+
"""/__heartbeat__ returns 503 when the services are unavailable."""
21+
resp = anon_client.get("/__heartbeat__")
22+
assert resp.status_code == 503
23+
data = resp.json()
24+
expected = {"bugzilla": {"up": False}, "jira": {"up": False}}
25+
assert data == expected
26+
27+
28+
def test_read_heartbeat_success(anon_client):
29+
"""/__heartbeat__ returns 200 when measuring the acoustic backlog fails."""
30+
expected = {
31+
"jira": {
32+
"up": True,
33+
},
34+
"bugzilla": {
35+
"up": True,
36+
},
37+
}
38+
with patch("src.app.monitor.jbi_service_health_map", return_value=expected):
39+
resp = anon_client.get("/__heartbeat__")
40+
assert resp.status_code == 200
41+
data = resp.json()
42+
assert data == expected
File renamed without changes.

0 commit comments

Comments
 (0)