# 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.admin_portal.models import PortalLinkResponse
from workos._errors import (
    AuthenticationError,
    BadRequestError,
    NotFoundError,
    RateLimitExceededError,
    ServerError,
    UnprocessableEntityError,
)


class TestAdminPortal:
    def test_generate_link(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("portal_link_response.json"),
        )
        result = workos.admin_portal.generate_link(organization="test_organization")
        assert isinstance(result, PortalLinkResponse)
        assert (
            result.link
            == "https://setup.workos.com?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
        )
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/portal/generate_link")
        body = json.loads(request.content)
        assert body["organization"] == "test_organization"

    def test_generate_link_with_request_options(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("portal_link_response.json"))
        workos.admin_portal.generate_link(
            organization="test_organization",
            request_options={"extra_headers": {"X-Custom": "value"}},
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    def test_generate_link_unauthorized(self, workos, httpx_mock):
        httpx_mock.add_response(
            status_code=401,
            json={"message": "Unauthorized"},
        )
        with pytest.raises(AuthenticationError):
            workos.admin_portal.generate_link(organization="test_organization")

    def test_generate_link_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.admin_portal.generate_link(organization="test_organization")
        finally:
            workos.close()

    def test_generate_link_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.admin_portal.generate_link(organization="test_organization")
        finally:
            workos.close()

    def test_generate_link_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.admin_portal.generate_link(organization="test_organization")
        finally:
            workos.close()

    def test_generate_link_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.admin_portal.generate_link(organization="test_organization")
        finally:
            workos.close()

    def test_generate_link_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.admin_portal.generate_link(organization="test_organization")
        finally:
            workos.close()


class TestAsyncAdminPortal:
    @pytest.mark.asyncio
    async def test_generate_link(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("portal_link_response.json"))
        result = await async_workos.admin_portal.generate_link(
            organization="test_organization"
        )
        assert isinstance(result, PortalLinkResponse)
        assert (
            result.link
            == "https://setup.workos.com?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
        )
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/portal/generate_link")

    @pytest.mark.asyncio
    async def test_generate_link_with_request_options(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("portal_link_response.json"))
        await async_workos.admin_portal.generate_link(
            organization="test_organization",
            request_options={"extra_headers": {"X-Custom": "value"}},
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    @pytest.mark.asyncio
    async def test_generate_link_unauthorized(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=401, json={"message": "Unauthorized"})
        with pytest.raises(AuthenticationError):
            await async_workos.admin_portal.generate_link(
                organization="test_organization"
            )

    @pytest.mark.asyncio
    async def test_generate_link_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.admin_portal.generate_link(
                    organization="test_organization"
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_generate_link_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.admin_portal.generate_link(
                    organization="test_organization"
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_generate_link_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.admin_portal.generate_link(
                    organization="test_organization"
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_generate_link_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.admin_portal.generate_link(
                    organization="test_organization"
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_generate_link_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.admin_portal.generate_link(
                    organization="test_organization"
                )
        finally:
            await workos.close()
