Skip to content
Snippets Groups Projects
Commit 445ea2a9 authored by Niels Alexander Buegel's avatar Niels Alexander Buegel
Browse files

Formatting fixes

parent bb5ae342
No related branches found
No related tags found
No related merge requests found
......@@ -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
```
......@@ -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()
......
......@@ -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}")
......
......@@ -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")
......
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:
......
......@@ -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}"})
......
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)
......
import pytest
def test_status_can_be_done_without_authentication(client_with_auth):
response = client_with_auth.get("/status")
assert response.status_code == 200
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment