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


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

from workos.common.models import DirectoryGroup, PaginationOrder
from workos.directory_sync.models import Directory, DirectoryUserWithGroups
from workos._pagination import AsyncPage, SyncPage
from workos._errors import (
    AuthenticationError,
    BadRequestError,
    NotFoundError,
    RateLimitExceededError,
    ServerError,
    UnprocessableEntityError,
)


class TestDirectorySync:
    def test_list_directories(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_directory.json"),
        )
        page = workos.directory_sync.list_directories()
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], Directory)

    def test_list_directories_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.directory_sync.list_directories()
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_directories_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.directory_sync.list_directories(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            organization_id="value organization_id/test",
            search="value search/test",
            domain="value domain/test",
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"
        assert request.url.params["organization_id"] == "value organization_id/test"
        assert request.url.params["search"] == "value search/test"
        assert request.url.params["domain"] == "value domain/test"

    def test_get_directory(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("directory.json"),
        )
        result = workos.directory_sync.get_directory("test_id")
        assert isinstance(result, Directory)
        assert result.object == "directory"
        assert result.id == "directory_01ECAZ4NV9QMV47GW873HDCX74"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/directories/test_id")

    def test_delete_directory(self, workos, httpx_mock):
        httpx_mock.add_response(status_code=200, content=b"\n")
        result = workos.directory_sync.delete_directory("test_id")
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith("/directories/test_id")

    def test_list_groups(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_directory_group.json"),
        )
        page = workos.directory_sync.list_groups()
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], DirectoryGroup)

    def test_list_groups_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.directory_sync.list_groups()
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_groups_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.directory_sync.list_groups(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            directory="value directory/test",
            user="value user/test",
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"
        assert request.url.params["directory"] == "value directory/test"
        assert request.url.params["user"] == "value user/test"

    def test_get_group(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("directory_group.json"),
        )
        result = workos.directory_sync.get_group("test_id")
        assert isinstance(result, DirectoryGroup)
        assert result.object == "directory_group"
        assert result.id == "directory_group_01E1JJS84MFPPQ3G655FHTKX6Z"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/directory_groups/test_id")

    def test_list_users(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_directory_user_with_groups.json"),
        )
        page = workos.directory_sync.list_users()
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], DirectoryUserWithGroups)

    def test_list_users_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.directory_sync.list_users()
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_users_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.directory_sync.list_users(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            directory="value directory/test",
            group="value group/test",
            idp_id="value idp_id/test",
            email="value email/test",
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"
        assert request.url.params["directory"] == "value directory/test"
        assert request.url.params["group"] == "value group/test"
        assert request.url.params["idp_id"] == "value idp_id/test"
        assert request.url.params["email"] == "value email/test"

    def test_get_user(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("directory_user_with_groups.json"),
        )
        result = workos.directory_sync.get_user("test_id")
        assert isinstance(result, DirectoryUserWithGroups)
        assert result.object == "directory_user"
        assert result.id == "directory_user_01E1JG7J09H96KYP8HM9B0G5SJ"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/directory_users/test_id")

    def test_list_directories_with_request_options(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.directory_sync.list_directories(
            request_options={"extra_headers": {"X-Custom": "value"}}
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    def test_list_directories_unauthorized(self, workos, httpx_mock):
        httpx_mock.add_response(
            status_code=401,
            json={"message": "Unauthorized"},
        )
        with pytest.raises(AuthenticationError):
            workos.directory_sync.list_directories()

    def test_list_directories_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.directory_sync.list_directories()
        finally:
            workos.close()

    def test_list_directories_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.directory_sync.list_directories()
        finally:
            workos.close()

    def test_list_directories_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.directory_sync.list_directories()
        finally:
            workos.close()

    def test_list_directories_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.directory_sync.list_directories()
        finally:
            workos.close()

    def test_list_directories_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.directory_sync.list_directories()
        finally:
            workos.close()


class TestAsyncDirectorySync:
    @pytest.mark.asyncio
    async def test_list_directories(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_directory.json"))
        page = await async_workos.directory_sync.list_directories()
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], Directory)

    @pytest.mark.asyncio
    async def test_list_directories_empty_page(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = await async_workos.directory_sync.list_directories()
        assert isinstance(page, AsyncPage)
        assert page.data == []

    @pytest.mark.asyncio
    async def test_list_directories_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.directory_sync.list_directories(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            organization_id="value organization_id/test",
            search="value search/test",
            domain="value domain/test",
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"
        assert request.url.params["organization_id"] == "value organization_id/test"
        assert request.url.params["search"] == "value search/test"
        assert request.url.params["domain"] == "value domain/test"

    @pytest.mark.asyncio
    async def test_get_directory(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("directory.json"))
        result = await async_workos.directory_sync.get_directory("test_id")
        assert isinstance(result, Directory)
        assert result.object == "directory"
        assert result.id == "directory_01ECAZ4NV9QMV47GW873HDCX74"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/directories/test_id")

    @pytest.mark.asyncio
    async def test_delete_directory(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=200, content=b"\n")
        result = await async_workos.directory_sync.delete_directory("test_id")
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith("/directories/test_id")

    @pytest.mark.asyncio
    async def test_list_groups(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_directory_group.json"))
        page = await async_workos.directory_sync.list_groups()
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], DirectoryGroup)

    @pytest.mark.asyncio
    async def test_list_groups_empty_page(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = await async_workos.directory_sync.list_groups()
        assert isinstance(page, AsyncPage)
        assert page.data == []

    @pytest.mark.asyncio
    async def test_list_groups_encodes_query_params(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.directory_sync.list_groups(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            directory="value directory/test",
            user="value user/test",
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"
        assert request.url.params["directory"] == "value directory/test"
        assert request.url.params["user"] == "value user/test"

    @pytest.mark.asyncio
    async def test_get_group(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("directory_group.json"))
        result = await async_workos.directory_sync.get_group("test_id")
        assert isinstance(result, DirectoryGroup)
        assert result.object == "directory_group"
        assert result.id == "directory_group_01E1JJS84MFPPQ3G655FHTKX6Z"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/directory_groups/test_id")

    @pytest.mark.asyncio
    async def test_list_users(self, async_workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_directory_user_with_groups.json")
        )
        page = await async_workos.directory_sync.list_users()
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], DirectoryUserWithGroups)

    @pytest.mark.asyncio
    async def test_list_users_empty_page(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = await async_workos.directory_sync.list_users()
        assert isinstance(page, AsyncPage)
        assert page.data == []

    @pytest.mark.asyncio
    async def test_list_users_encodes_query_params(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.directory_sync.list_users(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            directory="value directory/test",
            group="value group/test",
            idp_id="value idp_id/test",
            email="value email/test",
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"
        assert request.url.params["directory"] == "value directory/test"
        assert request.url.params["group"] == "value group/test"
        assert request.url.params["idp_id"] == "value idp_id/test"
        assert request.url.params["email"] == "value email/test"

    @pytest.mark.asyncio
    async def test_get_user(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("directory_user_with_groups.json"))
        result = await async_workos.directory_sync.get_user("test_id")
        assert isinstance(result, DirectoryUserWithGroups)
        assert result.object == "directory_user"
        assert result.id == "directory_user_01E1JG7J09H96KYP8HM9B0G5SJ"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/directory_users/test_id")

    @pytest.mark.asyncio
    async def test_list_directories_with_request_options(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.directory_sync.list_directories(
            request_options={"extra_headers": {"X-Custom": "value"}}
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    @pytest.mark.asyncio
    async def test_list_directories_unauthorized(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=401, json={"message": "Unauthorized"})
        with pytest.raises(AuthenticationError):
            await async_workos.directory_sync.list_directories()

    @pytest.mark.asyncio
    async def test_list_directories_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.directory_sync.list_directories()
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_list_directories_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.directory_sync.list_directories()
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_list_directories_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.directory_sync.list_directories()
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_list_directories_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.directory_sync.list_directories()
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_list_directories_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.directory_sync.list_directories()
        finally:
            await workos.close()
