# This file is auto-generated by oagen. Do not edit.

import json

import pytest
from workos import WorkOSClient, AsyncWorkOSClient
from tests.generated_helpers import load_fixture

from workos.client_api.models import ClientApiTokenResponse
from workos._errors import (
    AuthenticationError,
    BadRequestError,
    NotFoundError,
    RateLimitExceededError,
    ServerError,
    UnprocessableEntityError,
)


class TestClientApi:
    def test_create_token(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("client_api_token_response.json"),
        )
        result = workos.client_api.create_token(
            organization_id="test_organization_id", user_id="test_user_id"
        )
        assert isinstance(result, ClientApiTokenResponse)
        assert result.token == "eyJhbGciOiJSUzI1NiIsImtpZCI6InNlc3Npb24..."
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/client/token")
        body = json.loads(request.content)
        assert body["organization_id"] == "test_organization_id"
        assert body["user_id"] == "test_user_id"

    def test_create_token_with_request_options(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("client_api_token_response.json"))
        workos.client_api.create_token(
            organization_id="test_organization_id",
            user_id="test_user_id",
            request_options={"extra_headers": {"X-Custom": "value"}},
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    def test_create_token_unauthorized(self, workos, httpx_mock):
        httpx_mock.add_response(
            status_code=401,
            json={"message": "Unauthorized"},
        )
        with pytest.raises(AuthenticationError):
            workos.client_api.create_token(
                organization_id="test_organization_id", user_id="test_user_id"
            )

    def test_create_token_not_found(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=404, json={"message": "Not found"})
            with pytest.raises(NotFoundError):
                workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            workos.close()

    def test_create_token_rate_limited(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(
                status_code=429,
                headers={"Retry-After": "0"},
                json={"message": "Slow down"},
            )
            with pytest.raises(RateLimitExceededError):
                workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            workos.close()

    def test_create_token_server_error(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=500, json={"message": "Server error"})
            with pytest.raises(ServerError):
                workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            workos.close()

    def test_create_token_bad_request(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=400, json={"message": "Bad request"})
            with pytest.raises(BadRequestError):
                workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            workos.close()

    def test_create_token_unprocessable(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=422, json={"message": "Unprocessable"})
            with pytest.raises(UnprocessableEntityError):
                workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            workos.close()


class TestAsyncClientApi:
    @pytest.mark.asyncio
    async def test_create_token(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("client_api_token_response.json"))
        result = await async_workos.client_api.create_token(
            organization_id="test_organization_id", user_id="test_user_id"
        )
        assert isinstance(result, ClientApiTokenResponse)
        assert result.token == "eyJhbGciOiJSUzI1NiIsImtpZCI6InNlc3Npb24..."
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/client/token")

    @pytest.mark.asyncio
    async def test_create_token_with_request_options(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("client_api_token_response.json"))
        await async_workos.client_api.create_token(
            organization_id="test_organization_id",
            user_id="test_user_id",
            request_options={"extra_headers": {"X-Custom": "value"}},
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    @pytest.mark.asyncio
    async def test_create_token_unauthorized(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=401, json={"message": "Unauthorized"})
        with pytest.raises(AuthenticationError):
            await async_workos.client_api.create_token(
                organization_id="test_organization_id", user_id="test_user_id"
            )

    @pytest.mark.asyncio
    async def test_create_token_not_found(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=404, json={"message": "Not found"})
            with pytest.raises(NotFoundError):
                await workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_create_token_rate_limited(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(
                status_code=429,
                headers={"Retry-After": "0"},
                json={"message": "Slow down"},
            )
            with pytest.raises(RateLimitExceededError):
                await workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_create_token_server_error(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=500, json={"message": "Server error"})
            with pytest.raises(ServerError):
                await workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_create_token_bad_request(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=400, json={"message": "Bad request"})
            with pytest.raises(BadRequestError):
                await workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_create_token_unprocessable(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=422, json={"message": "Unprocessable"})
            with pytest.raises(UnprocessableEntityError):
                await workos.client_api.create_token(
                    organization_id="test_organization_id", user_id="test_user_id"
                )
        finally:
            await workos.close()
