-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /maintenance and /maintenance/scheduled GET, POST endpoints
- Loading branch information
1 parent
30ecd20
commit 8a5999e
Showing
12 changed files
with
355 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"show": false, | ||
"message": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"show": false, | ||
"message": "", | ||
"severity": "info" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from functools import lru_cache | ||
import json | ||
import logging | ||
|
||
from fastapi import APIRouter, Depends | ||
from typing_extensions import Annotated | ||
|
||
from operationsgateway_api.src.auth.authorisation import authorise_route | ||
from operationsgateway_api.src.config import Config | ||
from operationsgateway_api.src.models import MaintenanceModel, ScheduledMaintenanceModel | ||
|
||
|
||
log = logging.getLogger() | ||
router = APIRouter() | ||
AuthoriseRoute = Annotated[str, Depends(authorise_route)] | ||
|
||
|
||
@router.get( | ||
"/maintenance", | ||
summary="Get the current maintenance message and whether to show it", | ||
response_description="The current maintenance message and whether to show it", | ||
tags=["Maintenance"], | ||
) | ||
def get_maintenance(access_token: AuthoriseRoute) -> MaintenanceModel: | ||
return _get_maintenance() | ||
|
||
|
||
@router.post( | ||
"/maintenance", | ||
summary="Set the current maintenance message and whether to show it", | ||
tags=["Maintenance"], | ||
) | ||
def set_maintenance( | ||
access_token: AuthoriseRoute, | ||
maintenance_body: MaintenanceModel, | ||
) -> None: | ||
with open(Config.config.app.maintenance_file, "w") as f: | ||
f.write(maintenance_body.model_dump_json()) | ||
_get_maintenance.cache_clear() | ||
|
||
|
||
@router.get( | ||
"/maintenance/scheduled", | ||
summary="Get the current scheduled maintenance message and whether to show it", | ||
response_description=( | ||
"The current scheduled maintenance message and whether to show it" | ||
), | ||
tags=["Maintenance"], | ||
) | ||
def get_scheduled_maintenance( | ||
access_token: AuthoriseRoute, | ||
) -> ScheduledMaintenanceModel: | ||
return _get_scheduled_maintenance() | ||
|
||
|
||
@router.post( | ||
"/maintenance/scheduled", | ||
summary="Set the current scheduled maintenance message and whether to show it", | ||
tags=["Maintenance"], | ||
) | ||
def set_scheduled_maintenance( | ||
access_token: AuthoriseRoute, | ||
maintenance_body: ScheduledMaintenanceModel, | ||
) -> None: | ||
with open(Config.config.app.scheduled_maintenance_file, "w") as f: | ||
f.write(maintenance_body.model_dump_json()) | ||
_get_scheduled_maintenance.cache_clear() | ||
|
||
|
||
@lru_cache | ||
def _get_maintenance() -> dict: | ||
with open(Config.config.app.maintenance_file, "rb") as f: | ||
return json.load(f) | ||
|
||
|
||
@lru_cache | ||
def _get_scheduled_maintenance() -> dict: | ||
with open(Config.config.app.scheduled_maintenance_file, "rb") as f: | ||
return json.load(f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import json | ||
import tempfile | ||
from unittest.mock import patch | ||
|
||
from fastapi.testclient import TestClient | ||
|
||
|
||
class TestMaintenance: | ||
""" | ||
Both test combine the GET and POST requests in order to test the cache is cleared | ||
after POSTing a new message. | ||
""" | ||
|
||
def test_maintenance(self, test_app: TestClient, login_and_get_token: str): | ||
initial_content = {"show": False, "message": ""} | ||
updated_content = {"show": True, "message": "message"} | ||
target = "operationsgateway_api.src.config.Config.config.app.maintenance_file" | ||
tmp_file = tempfile.NamedTemporaryFile() | ||
with patch(target, tmp_file.name): | ||
# POST the initial contents (we are using a tmpfile, which will start empty) | ||
response = test_app.post( | ||
url="/maintenance", | ||
content=json.dumps(initial_content), | ||
headers={"Authorization": f"Bearer {login_and_get_token}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content) is None | ||
assert json.load(tmp_file) == initial_content | ||
tmp_file.seek(0) # Reset so we can read again later | ||
|
||
# Calling GET will save the return value to cache | ||
response = test_app.get( | ||
url="/maintenance", | ||
headers={"Authorization": f"Bearer {login_and_get_token}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content) == initial_content | ||
|
||
# Calling POST will clear the cache after writing to file | ||
response = test_app.post( | ||
url="/maintenance", | ||
content=json.dumps(updated_content), | ||
headers={"Authorization": f"Bearer {login_and_get_token}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content) is None | ||
assert json.load(tmp_file) == updated_content | ||
|
||
# Cache has been cleared so should get the new message back | ||
response = test_app.get( | ||
url="/maintenance", | ||
headers={"Authorization": f"Bearer {login_and_get_token}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content) == updated_content | ||
|
||
def test_scheduled_maintenance( | ||
self, | ||
test_app: TestClient, | ||
login_and_get_token: str, | ||
): | ||
initial_content = {"show": False, "message": "", "severity": "info"} | ||
updated_content = {"show": True, "message": "message", "severity": "warning"} | ||
target = ( | ||
"operationsgateway_api.src.config.Config.config.app." | ||
"scheduled_maintenance_file" | ||
) | ||
tmp_file = tempfile.NamedTemporaryFile() | ||
with patch(target, tmp_file.name): | ||
# POST the initial contents (we are using a tmpfile, which will start empty) | ||
response = test_app.post( | ||
url="/maintenance/scheduled", | ||
content=json.dumps(initial_content), | ||
headers={"Authorization": f"Bearer {login_and_get_token}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content) is None | ||
assert json.load(tmp_file) == initial_content | ||
tmp_file.seek(0) # Reset so we can read again later | ||
|
||
# Calling GET will save the return value to cache | ||
response = test_app.get( | ||
url="/maintenance/scheduled", | ||
headers={"Authorization": f"Bearer {login_and_get_token}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content) == initial_content | ||
|
||
# Calling POST will clear the cache after writing to file | ||
response = test_app.post( | ||
url="/maintenance/scheduled", | ||
content=json.dumps(updated_content), | ||
headers={"Authorization": f"Bearer {login_and_get_token}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content) is None | ||
assert json.load(tmp_file) == updated_content | ||
|
||
# Cache has been cleared so should get the new message back | ||
response = test_app.get( | ||
url="/maintenance/scheduled", | ||
headers={"Authorization": f"Bearer {login_and_get_token}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content) == updated_content |
Oops, something went wrong.