from fastapi import status
from fastapi.testclient import TestClient


class TestTenantAuthInfoGet:
    PATH = "/v1/tenants"

    def test_returns_tenant_auth_info(
        self,
        test_client: TestClient,
        registered_identifiers: dict[str, str],
    ) -> None:
        """TenantNameからユーザー認証情報を取得できることを確認する。"""
        tenant_name = registered_identifiers["tenant_alpha_name"]

        response = test_client.get(url=f"{self.PATH}/{tenant_name}/auth-info")

        assert response.status_code == status.HTTP_200_OK
        assert response.json() == {
            "tenant_id": registered_identifiers["tenant_alpha_id"],
            "application_client_id": registered_identifiers["tenant_alpha_user_application_client_id"],
        }

    def test_tenant_not_found(
        self,
        test_client: TestClient,
    ) -> None:
        """存在しないTenantNameの取得リクエストを拒否できることを確認する。"""
        response = test_client.get(url=f"{self.PATH}/not_found/auth-info")

        assert response.status_code == status.HTTP_404_NOT_FOUND
        assert response.json() == {
            "detail": "Requested resource was not found.",
            "invalid_parameters": None,
        }

    def test_application_client_id_missing(
        self,
        test_client: TestClient,
        registered_identifiers: dict[str, str],
    ) -> None:
        """ユーザー認証用のクライアントIDが存在しない場合、サーバーエラーになることを確認する。"""
        tenant_name = registered_identifiers["tenant_beta_name"]

        response = test_client.get(url=f"{self.PATH}/{tenant_name}/auth-info")

        assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
        assert response.json() == {
            "detail": "An unexpected error occurred. Please try again later.",
            "invalid_parameters": None,
        }
