|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | +from fastapi import HTTPException |
| 5 | + |
| 6 | +from ocpa.auth import verify_bearer_token |
| 7 | + |
| 8 | + |
| 9 | +def test_verify_bearer_token_with_role(monkeypatch): |
| 10 | + monkeypatch.setenv("OCPA_JWT_SECRET", "secret") |
| 11 | + monkeypatch.setenv("OCPA_ADMIN_ROLE", "admin") |
| 12 | + import jwt |
| 13 | + |
| 14 | + token = jwt.encode({"roles": ["admin"]}, "secret", algorithm="HS256") |
| 15 | + auth_header = f"Bearer {token}" |
| 16 | + |
| 17 | + verify_bearer_token(auth_header, "admin") |
| 18 | + |
| 19 | + |
| 20 | +def test_verify_bearer_token_missing(monkeypatch): |
| 21 | + monkeypatch.setenv("OCPA_JWT_SECRET", "secret") |
| 22 | + with pytest.raises(HTTPException): |
| 23 | + verify_bearer_token(None, "admin") |
| 24 | + |
| 25 | + |
| 26 | +def test_verify_bearer_token_expired(monkeypatch): |
| 27 | + monkeypatch.setenv("OCPA_JWT_SECRET", "secret") |
| 28 | + import jwt, datetime |
| 29 | + |
| 30 | + token = jwt.encode( |
| 31 | + {"roles": ["admin"], "exp": datetime.datetime.utcnow() - datetime.timedelta(seconds=1)}, |
| 32 | + "secret", |
| 33 | + algorithm="HS256", |
| 34 | + ) |
| 35 | + auth_header = f"Bearer {token}" |
| 36 | + with pytest.raises(HTTPException): |
| 37 | + verify_bearer_token(auth_header, "admin") |
| 38 | + |
| 39 | + |
| 40 | +def test_verify_bearer_token_with_audience_and_issuer(monkeypatch): |
| 41 | + monkeypatch.setenv("OCPA_JWT_SECRET", "secret") |
| 42 | + monkeypatch.setenv("OCPA_JWT_AUDIENCE", "ocpa") |
| 43 | + monkeypatch.setenv("OCPA_JWT_ISSUER", "https://issuer.example.com") |
| 44 | + import jwt |
| 45 | + |
| 46 | + token = jwt.encode( |
| 47 | + {"roles": ["admin"], "aud": "ocpa", "iss": "https://issuer.example.com"}, |
| 48 | + "secret", |
| 49 | + algorithm="HS256", |
| 50 | + ) |
| 51 | + auth_header = f"Bearer {token}" |
| 52 | + verify_bearer_token(auth_header, "admin") |
| 53 | + |
| 54 | + |
| 55 | +def test_verify_bearer_token_missing_role(monkeypatch): |
| 56 | + monkeypatch.setenv("OCPA_JWT_SECRET", "secret") |
| 57 | + import jwt |
| 58 | + |
| 59 | + token = jwt.encode({"roles": ["user"]}, "secret", algorithm="HS256") |
| 60 | + auth_header = f"Bearer {token}" |
| 61 | + with pytest.raises(HTTPException): |
| 62 | + verify_bearer_token(auth_header, "admin") |
| 63 | + |
| 64 | + |
| 65 | +def test_verify_bearer_token_invalid_token(monkeypatch): |
| 66 | + monkeypatch.setenv("OCPA_JWT_SECRET", "secret") |
| 67 | + # malformed token |
| 68 | + with pytest.raises(HTTPException): |
| 69 | + verify_bearer_token("Bearer not-a-jwt", "admin") |
| 70 | + |
| 71 | + |
| 72 | +def test_verify_bearer_token_missing_secret(monkeypatch): |
| 73 | + monkeypatch.delenv("OCPA_JWT_SECRET", raising=False) |
| 74 | + with pytest.raises(HTTPException): |
| 75 | + verify_bearer_token("Bearer abc", "admin") |
| 76 | + |
| 77 | + |
| 78 | +def test_verify_bearer_token_role_as_string(monkeypatch): |
| 79 | + monkeypatch.setenv("OCPA_JWT_SECRET", "secret") |
| 80 | + import jwt |
| 81 | + |
| 82 | + token = jwt.encode({"roles": "admin"}, "secret", algorithm="HS256") |
| 83 | + auth_header = f"Bearer {token}" |
| 84 | + verify_bearer_token(auth_header, "admin") |
0 commit comments