|
3 | 3 | import jwt |
4 | 4 | import pytest |
5 | 5 | from fastapi.testclient import TestClient |
| 6 | +from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer |
| 7 | +from fastapi_azure_auth.auth import AzureAuthorizationCodeBearerBase |
| 8 | +from pytest import MonkeyPatch |
| 9 | + |
| 10 | +from isar.apis.security.authentication import build_azure_scheme |
| 11 | +from isar.config.settings import settings |
6 | 12 |
|
7 | 13 |
|
8 | 14 | def stub_access_token() -> str: |
@@ -30,3 +36,55 @@ def test_authentication( |
30 | 36 | ) |
31 | 37 |
|
32 | 38 | assert response.status_code == expected_status_code |
| 39 | + |
| 40 | + |
| 41 | +class TestBuildAzureScheme: |
| 42 | + def test_defaults_to_single_tenant_azure_scheme( |
| 43 | + self, monkeypatch: MonkeyPatch |
| 44 | + ) -> None: |
| 45 | + monkeypatch.setattr(settings, "OPENID_CONFIG_URL", None) |
| 46 | + |
| 47 | + scheme = build_azure_scheme() |
| 48 | + |
| 49 | + assert isinstance(scheme, SingleTenantAzureAuthorizationCodeBearer) |
| 50 | + # No override, so the discovery document URL is derived from the tenant ID |
| 51 | + # and points at Azure Entra ID. |
| 52 | + assert scheme.openid_config.config_url is None |
| 53 | + assert scheme.openid_config.tenant_id == settings.AZURE_TENANT_ID |
| 54 | + assert scheme.app_client_id == settings.AZURE_CLIENT_ID |
| 55 | + |
| 56 | + def test_openid_config_url_is_honoured(self, monkeypatch: MonkeyPatch) -> None: |
| 57 | + config_url = "http://oauth-mock:8080/.well-known/openid-configuration" |
| 58 | + authorization_url = "http://oauth-mock:8080/authorize" |
| 59 | + token_url = "http://oauth-mock:8080/token" |
| 60 | + |
| 61 | + monkeypatch.setattr(settings, "OPENID_CONFIG_URL", config_url) |
| 62 | + monkeypatch.setattr(settings, "OPENAPI_AUTHORIZATION_URL", authorization_url) |
| 63 | + monkeypatch.setattr(settings, "OPENAPI_TOKEN_URL", token_url) |
| 64 | + |
| 65 | + scheme = build_azure_scheme() |
| 66 | + |
| 67 | + assert not isinstance(scheme, SingleTenantAzureAuthorizationCodeBearer) |
| 68 | + assert isinstance(scheme, AzureAuthorizationCodeBearerBase) |
| 69 | + assert scheme.openid_config.config_url == config_url |
| 70 | + assert scheme.authorization_url == authorization_url |
| 71 | + assert scheme.token_url == token_url |
| 72 | + # The audience is still ISAR's own client ID, and issuer validation stays on. |
| 73 | + assert scheme.app_client_id == settings.AZURE_CLIENT_ID |
| 74 | + assert scheme.validate_iss is True |
| 75 | + |
| 76 | + def test_openapi_urls_fall_back_to_azure_when_unset( |
| 77 | + self, monkeypatch: MonkeyPatch |
| 78 | + ) -> None: |
| 79 | + monkeypatch.setattr( |
| 80 | + settings, |
| 81 | + "OPENID_CONFIG_URL", |
| 82 | + "http://oauth-mock:8080/.well-known/openid-configuration", |
| 83 | + ) |
| 84 | + monkeypatch.setattr(settings, "OPENAPI_AUTHORIZATION_URL", None) |
| 85 | + monkeypatch.setattr(settings, "OPENAPI_TOKEN_URL", None) |
| 86 | + |
| 87 | + scheme = build_azure_scheme() |
| 88 | + |
| 89 | + assert scheme.authorization_url is not None |
| 90 | + assert settings.AZURE_TENANT_ID in scheme.authorization_url |
0 commit comments