diff --git a/api/README.md b/api/README.md index 1312ca1b32cc009f7695c0533e7867856a49104f..b97a8df4ffe2e058b6e9a934a047d3ccb1b0887c 100644 --- a/api/README.md +++ b/api/README.md @@ -63,3 +63,11 @@ Once this is done, simply execute the following. This commands adds the `-v` fla ```sh pytest tests/ -v ``` + +## Formatting + +Stop discussing and worrying about formatting. Just run the following before every commit and be done with it. Don't add exceptions to this. + +```sh +black . --line-length 120 +``` diff --git a/api/ctarestapi/dependencies.py b/api/ctarestapi/dependencies.py index 643a94142cca54853fc852057c2139f15f59aab2..723d6249c4f2046dee34985cf47df621dba20774 100644 --- a/api/ctarestapi/dependencies.py +++ b/api/ctarestapi/dependencies.py @@ -6,9 +6,7 @@ import os def get_connection_string() -> str: conn_str = os.environ.get("CTA_CATALOGUE_CONF") if not conn_str: - print( - "Environment variable CTA_CATALOGUE_CONF missing. Looking for file alternative..." - ) + print("Environment variable CTA_CATALOGUE_CONF missing. Looking for file alternative...") with open("/etc/cta/cta-catalogue.conf") as f: conn_str = f.read().strip() diff --git a/api/ctarestapi/middleware/jwt_middleware.py b/api/ctarestapi/middleware/jwt_middleware.py index b3f083e8bc356386f6f696222c6f414098c65646..874177148c9022a1c25fa02e8e0cb803700ade10 100644 --- a/api/ctarestapi/middleware/jwt_middleware.py +++ b/api/ctarestapi/middleware/jwt_middleware.py @@ -71,9 +71,7 @@ class JWTMiddleware: if alg not in self._allowed_algorithms: raise InvalidTokenError(f"Unsupported algorithm: {alg}") - jwt.decode( - token, signing_jwk.key, algorithms=[alg], options={"require": ["exp"]} - ) + jwt.decode(token, signing_jwk.key, algorithms=[alg], options={"require": ["exp"]}) except PyJWKClientConnectionError as error: logging.error(f"JWKS endpoint unavailable: {error}") diff --git a/api/ctarestapi/routers/drives.py b/api/ctarestapi/routers/drives.py index 67a9faa5569ebc059db132e3f6fab6e508b76b3a..cb28650dfa850a84825041e1da5b9aa279ee70e7 100644 --- a/api/ctarestapi/routers/drives.py +++ b/api/ctarestapi/routers/drives.py @@ -51,9 +51,7 @@ async def update_drive_state( if state_update.desired_state == DesiredDriveState.up: success = catalogue.drives.set_drive_up(drive_name, state_update.reason) else: - success = catalogue.drives.set_drive_down( - drive_name, state_update.reason, force - ) + success = catalogue.drives.set_drive_down(drive_name, state_update.reason, force) if not success: raise HTTPException(status_code=404, detail="Drive not found") diff --git a/api/test/conftest.py b/api/test/conftest.py index 155914009bd75df34040a0f5786b207ec3715626..765afce96198415afef9d84c9065938e4149de0b 100644 --- a/api/test/conftest.py +++ b/api/test/conftest.py @@ -1,20 +1,15 @@ +import jwt +import json import pytest from fastapi import FastAPI from fastapi.testclient import TestClient -from unittest.mock import patch, Mock +from unittest.mock import patch, MagicMock, Mock from ctarestapi.server import create_app from ctarestapi.dependencies import get_catalogue -import jwt -import json -import pytest -import datetime from fastapi.testclient import TestClient from jwt.utils import base64url_encode from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa -from unittest.mock import patch, MagicMock, Mock -from ctarestapi.server import create_app -from ctarestapi.dependencies import get_catalogue @pytest.fixture @@ -34,6 +29,7 @@ def client(monkeypatch): app.dependency_overrides.clear() + # For testing purposes class KeyPair: diff --git a/api/test/middleware/test_jwt_middleware.py b/api/test/middleware/test_jwt_middleware.py index ec41584383f48d84fd9eefaa48205b34e8db035c..1360026dffa6589b3595362d276533bbc9fe7b1b 100644 --- a/api/test/middleware/test_jwt_middleware.py +++ b/api/test/middleware/test_jwt_middleware.py @@ -23,7 +23,10 @@ def test_expired_token_returns_401(client_with_auth): def test_no_expiration_present_returns_401(client_with_auth): kp = client_with_auth.keyPair invalid_token = kp.generate_jwt( - headers={"kid": kp.get_kid()}, body={}, pem=kp.get_pem(), algorithm=kp.get_algorithm() + headers={"kid": kp.get_kid()}, + body={}, + pem=kp.get_pem(), + algorithm=kp.get_algorithm(), ) response = client_with_auth.get("/protected", headers={"Authorization": f"Bearer {invalid_token}"}) @@ -45,7 +48,10 @@ def test_incorrect_signature_returns_401(client_with_auth): exp = datetime.datetime.now() + datetime.timedelta(minutes=5) kp = client_with_auth.keyPair token = kp.generate_jwt( - headers={"kid": kp.get_kid()}, body={"exp": exp}, pem=kp.get_pem(), algorithm=kp.get_algorithm() + headers={"kid": kp.get_kid()}, + body={"exp": exp}, + pem=kp.get_pem(), + algorithm=kp.get_algorithm(), ) parts = token.split(".") assert len(parts) == 3 @@ -66,7 +72,10 @@ def test_nonexisting_kid_returns_401(client_with_auth): exp = datetime.datetime.now() + datetime.timedelta(minutes=5) kp = client_with_auth.keyPair invalid_token = kp.generate_jwt( - headers={"kid": "idontexist"}, body={"exp": exp}, pem=kp.get_pem(), algorithm=kp.get_algorithm() + headers={"kid": "idontexist"}, + body={"exp": exp}, + pem=kp.get_pem(), + algorithm=kp.get_algorithm(), ) response = client_with_auth.get("/protected", headers={"Authorization": f"Bearer {invalid_token}"}) diff --git a/api/test/routers/test_drives.py b/api/test/routers/test_drives.py index 6288032729d613b5d032fdc55ed117c90e903d2d..193db8a7351482ff7dde3c5c0b1b484c4a46aee9 100644 --- a/api/test/routers/test_drives.py +++ b/api/test/routers/test_drives.py @@ -1,5 +1,6 @@ import pytest + def test_get_drives(client): client.mock_catalogue.drives.get_all_drives.return_value = [] @@ -35,7 +36,10 @@ def test_update_drive_state_up(client): def test_update_drive_state_down_with_reason(client): client.mock_catalogue.drives.set_drive_down.return_value = True - response = client.put("/drives/test/state?force=true", json={"desired_state": "down", "reason": "maintenance"}) + response = client.put( + "/drives/test/state?force=true", + json={"desired_state": "down", "reason": "maintenance"}, + ) assert response.status_code == 200 client.mock_catalogue.drives.set_drive_down.assert_called_once_with("test", "maintenance", True) diff --git a/api/test/routers/test_home.py b/api/test/routers/test_home.py index 9ee05443cf5711660c2e0a448882f2f6510cbfca..e065c7f9e40b9a5d4332c5bb1192a82ed762d1b4 100644 --- a/api/test/routers/test_home.py +++ b/api/test/routers/test_home.py @@ -1,5 +1,6 @@ import pytest + def test_status_can_be_done_without_authentication(client_with_auth): response = client_with_auth.get("/status") assert response.status_code == 200