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

import json

import pytest

from tests.generated_helpers import load_fixture
from workos import AsyncWorkOSClient, WorkOSClient
from workos._errors import (
    AuthenticationError,
    BadRequestError,
    NotFoundError,
    RateLimitExceededError,
    ServerError,
    UnprocessableEntityError,
)
from workos._pagination import AsyncPage, SyncPage
from workos.common.models import (
    PaginationOrder,
    User,
    UserSessionsListItem,
)
from workos.user_management._resource import PasswordPlaintext
from workos.user_management.models import (
    AuthenticateResponse,
    AuthorizedConnectApplicationListData,
    CORSOriginResponse,
    DeviceAuthorizationResponse,
    EmailChange,
    EmailChangeConfirmation,
    EmailVerification,
    Invitation,
    JwksResponse,
    JWTTemplateResponse,
    MagicAuth,
    MagicAuthSendMagicAuthCodeAndReturnResponse,
    PasswordReset,
    RadarChallenge,
    RedirectUri,
    ResetPasswordResponse,
    SendRadarSmsChallengeResponse,
    SendVerificationEmailResponse,
    UserApiKey,
    UserApiKeyWithValue,
    UserCreateResponse,
    UserIdentitiesGetItem,
    UserInvite,
    VerifyEmailResponse,
)


class TestUserManagement:
    def test_get_jwks(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("jwks_response.json"),
        )
        result = workos.user_management.get_jwks("test_clientId")
        assert isinstance(result, JwksResponse)
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/sso/jwks/test_clientId")

    def test_create_authenticate(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("authenticate_response.json"),
        )
        result = workos.user_management.create_authenticate(
            body=load_fixture("authorization_code_session_authenticate_request.json")
        )
        assert isinstance(result, AuthenticateResponse)
        assert result.organization_id == "org_01H945H0YD4F97JN9MATX7BYAG"
        assert result.authkit_authorization_code == "authkit_authz_code_abc123"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/authenticate")

    def test_get_authorization_url(self, workos):
        result = workos.user_management.get_authorization_url(
            redirect_uri="test_redirect_uri"
        )
        assert isinstance(result, str)
        assert result.startswith("http")

    def test_create_device(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("device_authorization_response.json"),
        )
        result = workos.user_management.create_device(client_id="test_client_id")
        assert isinstance(result, DeviceAuthorizationResponse)
        assert (
            result.device_code
            == "CVE2wOfIFK4vhmiDBntpX9s8KT2f0qngpWYL0LGy9HxYgBRXUKIUkZB9BgIFho5h"
        )
        assert result.user_code == "BCDF-GHJK"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/authorize/device")
        body = json.loads(request.content)
        assert body["client_id"] == "test_client_id"

    def test_create_radar_challenge(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("send_radar_sms_challenge_response.json"),
        )
        result = workos.user_management.create_radar_challenge(
            user_id="test_user_id",
            pending_authentication_token="test_pending_authentication_token",
            phone_number="test_phone_number",
        )
        assert isinstance(result, SendRadarSmsChallengeResponse)
        assert result.verification_id == "vrf_01HXYZ123456789ABCDEFGHIJ"
        assert result.phone_number == "+15555550123"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/radar_challenges")
        body = json.loads(request.content)
        assert body["user_id"] == "test_user_id"
        assert (
            body["pending_authentication_token"] == "test_pending_authentication_token"
        )
        assert body["phone_number"] == "test_phone_number"

    def test_get_radar_challenge(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("radar_challenge.json"),
        )
        result = workos.user_management.get_radar_challenge("test_id")
        assert isinstance(result, RadarChallenge)
        assert result.object == "radar_challenge"
        assert result.id == "radar_challenge_01HWZBQZY2M3AMQW166Q22K88F"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/radar_challenges/test_id")

    def test_get_logout_url(self, workos):
        result = workos.user_management.get_logout_url(session_id="test_session_id")
        assert isinstance(result, str)
        assert result.startswith("http")

    def test_revoke_session(self, workos, httpx_mock):
        httpx_mock.add_response(json={})
        workos.user_management.revoke_session(session_id="test_session_id")
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/sessions/revoke")

    def test_list_cors_origins(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_cors_origin_response.json"),
        )
        page = workos.user_management.list_cors_origins()
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], CORSOriginResponse)

    def test_list_cors_origins_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.user_management.list_cors_origins()
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_cors_origins_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.user_management.list_cors_origins(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        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"

    def test_create_cors_origin(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("cors_origin_response.json"),
        )
        result = workos.user_management.create_cors_origin(origin="test_origin")
        assert isinstance(result, CORSOriginResponse)
        assert result.object == "cors_origin"
        assert result.id == "cors_origin_01HXYZ123456789ABCDEFGHIJ"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/cors_origins")
        body = json.loads(request.content)
        assert body["origin"] == "test_origin"

    def test_get_email_verification(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("email_verification.json"),
        )
        result = workos.user_management.get_email_verification("test_id")
        assert isinstance(result, EmailVerification)
        assert result.object == "email_verification"
        assert result.id == "email_verification_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/email_verification/test_id")

    def test_reset_password(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("password_reset.json"),
        )
        result = workos.user_management.reset_password(email="test_email")
        assert isinstance(result, PasswordReset)
        assert result.object == "password_reset"
        assert result.id == "password_reset_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/password_reset")
        body = json.loads(request.content)
        assert body["email"] == "test_email"

    def test_confirm_password_reset(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("reset_password_response.json"),
        )
        result = workos.user_management.confirm_password_reset(
            token="test_token", new_password="test_new_password"
        )
        assert isinstance(result, ResetPasswordResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/password_reset/confirm")
        body = json.loads(request.content)
        assert body["token"] == "test_token"
        assert body["new_password"] == "test_new_password"

    def test_get_password_reset(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("password_reset.json"),
        )
        result = workos.user_management.get_password_reset("test_id")
        assert isinstance(result, PasswordReset)
        assert result.object == "password_reset"
        assert result.id == "password_reset_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/password_reset/test_id")

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

    def test_list_users_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.user_management.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.user_management.list_users(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            organization="value organization/test",
            organization_id="value organization_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["organization"] == "value organization/test"
        assert request.url.params["organization_id"] == "value organization_id/test"
        assert request.url.params["email"] == "value email/test"

    def test_create_user(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user_create_response.json"),
        )
        result = workos.user_management.create_user(
            email="test_email", password=PasswordPlaintext(password="test_value")
        )
        assert isinstance(result, UserCreateResponse)
        assert result.object == "user"
        assert result.id == "user_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/users")
        body = json.loads(request.content)
        assert body["email"] == "test_email"

    def test_get_user_by_external_id(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user.json"),
        )
        result = workos.user_management.get_user_by_external_id("test_external_id")
        assert isinstance(result, User)
        assert result.object == "user"
        assert result.id == "user_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith(
            "/user_management/users/external_id/test_external_id"
        )

    def test_get_user(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user.json"),
        )
        result = workos.user_management.get_user("test_id")
        assert isinstance(result, User)
        assert result.object == "user"
        assert result.id == "user_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/users/test_id")

    def test_update_user(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user.json"),
        )
        result = workos.user_management.update_user(
            "test_id", password=PasswordPlaintext(password="test_value")
        )
        assert isinstance(result, User)
        assert result.object == "user"
        assert result.id == "user_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "PUT"
        assert request.url.path.endswith("/user_management/users/test_id")

    def test_delete_user(self, workos, httpx_mock):
        httpx_mock.add_response(status_code=204)
        result = workos.user_management.delete_user("test_id")
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith("/user_management/users/test_id")

    def test_confirm_email_change(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("email_change_confirmation.json"),
        )
        result = workos.user_management.confirm_email_change(
            "test_id", code="test_code"
        )
        assert isinstance(result, EmailChangeConfirmation)
        assert result.object == "email_change_confirmation"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/user_management/users/test_id/email_change/confirm"
        )
        body = json.loads(request.content)
        assert body["code"] == "test_code"

    def test_send_email_change(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("email_change.json"),
        )
        result = workos.user_management.send_email_change(
            "test_id", new_email="test_new_email"
        )
        assert isinstance(result, EmailChange)
        assert result.object == "email_change"
        assert result.new_email == "new.email@example.com"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/user_management/users/test_id/email_change/send"
        )
        body = json.loads(request.content)
        assert body["new_email"] == "test_new_email"

    def test_verify_email(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("verify_email_response.json"),
        )
        result = workos.user_management.verify_email("test_id", code="test_code")
        assert isinstance(result, VerifyEmailResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/user_management/users/test_id/email_verification/confirm"
        )
        body = json.loads(request.content)
        assert body["code"] == "test_code"

    def test_send_verification_email(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("send_verification_email_response.json"),
        )
        result = workos.user_management.send_verification_email("test_id")
        assert isinstance(result, SendVerificationEmailResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/user_management/users/test_id/email_verification/send"
        )

    def test_get_user_identities(self, workos, httpx_mock):
        httpx_mock.add_response(json=[load_fixture("user_identities_get_item.json")])
        result = workos.user_management.get_user_identities("test_id")
        assert isinstance(result, list)
        assert len(result) == 1
        assert isinstance(result[0], UserIdentitiesGetItem)

    def test_list_sessions(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_user_sessions_list_item.json"),
        )
        page = workos.user_management.list_sessions("test_id")
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], UserSessionsListItem)

    def test_list_sessions_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.user_management.list_sessions("test_id")
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_sessions_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.user_management.list_sessions(
            "test_id",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        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"

    def test_list_invitations(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_user_invite.json"),
        )
        page = workos.user_management.list_invitations()
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], UserInvite)

    def test_list_invitations_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.user_management.list_invitations()
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_invitations_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.user_management.list_invitations(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            organization_id="value organization_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["organization_id"] == "value organization_id/test"
        assert request.url.params["email"] == "value email/test"

    def test_send_invitation(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user_invite.json"),
        )
        result = workos.user_management.send_invitation(email="test_email")
        assert isinstance(result, UserInvite)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/invitations")
        body = json.loads(request.content)
        assert body["email"] == "test_email"

    def test_find_invitation_by_token(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user_invite.json"),
        )
        result = workos.user_management.find_invitation_by_token("test_token")
        assert isinstance(result, UserInvite)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith(
            "/user_management/invitations/by_token/test_token"
        )

    def test_get_invitation(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user_invite.json"),
        )
        result = workos.user_management.get_invitation("test_id")
        assert isinstance(result, UserInvite)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/invitations/test_id")

    def test_accept_invitation(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("invitation.json"),
        )
        result = workos.user_management.accept_invitation("test_id")
        assert isinstance(result, Invitation)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/invitations/test_id/accept")

    def test_resend_invitation(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user_invite.json"),
        )
        result = workos.user_management.resend_invitation("test_id")
        assert isinstance(result, UserInvite)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/invitations/test_id/resend")

    def test_revoke_invitation(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("invitation.json"),
        )
        result = workos.user_management.revoke_invitation("test_id")
        assert isinstance(result, Invitation)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/invitations/test_id/revoke")

    def test_list_jwt_template(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("jwt_template_response.json"),
        )
        result = workos.user_management.list_jwt_template()
        assert isinstance(result, JWTTemplateResponse)
        assert result.object == "jwt_template"
        assert result.created_at == "2026-01-15T12:00:00.000Z"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/jwt_template")

    def test_update_jwt_template(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("jwt_template_response.json"),
        )
        result = workos.user_management.update_jwt_template(content="test_content")
        assert isinstance(result, JWTTemplateResponse)
        assert result.object == "jwt_template"
        assert result.created_at == "2026-01-15T12:00:00.000Z"
        request = httpx_mock.get_request()
        assert request.method == "PUT"
        assert request.url.path.endswith("/user_management/jwt_template")
        body = json.loads(request.content)
        assert body["content"] == "test_content"

    def test_create_magic_auth(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture(
                "magic_auth_send_magic_auth_code_and_return_response.json"
            ),
        )
        result = workos.user_management.create_magic_auth(email="test_email")
        assert isinstance(result, MagicAuthSendMagicAuthCodeAndReturnResponse)
        assert result.object == "magic_auth"
        assert result.id == "magic_auth_01HWZBQZY2M3AMQW166Q22K88F"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/magic_auth")
        body = json.loads(request.content)
        assert body["email"] == "test_email"

    def test_get_magic_auth(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("magic_auth.json"),
        )
        result = workos.user_management.get_magic_auth("test_id")
        assert isinstance(result, MagicAuth)
        assert result.object == "magic_auth"
        assert result.id == "magic_auth_01HWZBQZY2M3AMQW166Q22K88F"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/magic_auth/test_id")

    def test_list_redirect_uris(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_redirect_uri.json"),
        )
        page = workos.user_management.list_redirect_uris()
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], RedirectUri)

    def test_list_redirect_uris_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.user_management.list_redirect_uris()
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_redirect_uris_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.user_management.list_redirect_uris(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        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"

    def test_create_redirect_uri(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("redirect_uri.json"),
        )
        result = workos.user_management.create_redirect_uri(uri="test_uri")
        assert isinstance(result, RedirectUri)
        assert result.object == "redirect_uri"
        assert result.id == "redir_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/redirect_uris")
        body = json.loads(request.content)
        assert body["uri"] == "test_uri"

    def test_delete_redirect_uris(self, workos, httpx_mock):
        httpx_mock.add_response(status_code=204)
        result = workos.user_management.delete_redirect_uris("test_id")
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith("/user_management/redirect_uris/test_id")

    def test_list_user_authorized_applications(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_authorized_connect_application_list_data.json"),
        )
        page = workos.user_management.list_user_authorized_applications("test_user_id")
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], AuthorizedConnectApplicationListData)

    def test_list_user_authorized_applications_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.user_management.list_user_authorized_applications("test_user_id")
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_user_authorized_applications_encodes_query_params(
        self, workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.user_management.list_user_authorized_applications(
            "test_user_id",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        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"

    def test_delete_user_authorized_application(self, workos, httpx_mock):
        httpx_mock.add_response(status_code=204)
        result = workos.user_management.delete_user_authorized_application(
            "test_application_id", "test_user_id"
        )
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith(
            "/user_management/users/test_user_id/authorized_applications/test_application_id"
        )

    def test_list_user_api_keys(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_user_api_key.json"),
        )
        page = workos.user_management.list_user_api_keys("test_userId")
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], UserApiKey)

    def test_list_user_api_keys_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.user_management.list_user_api_keys("test_userId")
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_user_api_keys_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.user_management.list_user_api_keys(
            "test_userId",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            organization_id="value organization_id/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"

    def test_create_user_api_key(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("user_api_key_with_value.json"),
        )
        result = workos.user_management.create_user_api_key(
            "test_userId", name="test_name", organization_id="test_organization_id"
        )
        assert isinstance(result, UserApiKeyWithValue)
        assert result.object == "api_key"
        assert result.id == "api_key_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/users/test_userId/api_keys")
        body = json.loads(request.content)
        assert body["name"] == "test_name"
        assert body["organization_id"] == "test_organization_id"

    def test_authenticate_with_password(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_password(
            email="test_email", password="test_password"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "password"

    def test_authenticate_with_code(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_code(code="test_code")
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "authorization_code"

    def test_authenticate_with_refresh_token(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_refresh_token(
            refresh_token="test_refresh_token"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "refresh_token"

    def test_authenticate_with_magic_auth(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_magic_auth(
            code="test_code", email="test_email"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "urn:workos:oauth:grant-type:magic-auth:code"

    def test_authenticate_with_email_verification(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_email_verification(
            code="test_code",
            pending_authentication_token="test_pending_authentication_token",
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert (
            body["grant_type"] == "urn:workos:oauth:grant-type:email-verification:code"
        )

    def test_authenticate_with_totp(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_totp(
            code="test_value",
            pending_authentication_token="test_value",
            authentication_challenge_id="test_value",
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "urn:workos:oauth:grant-type:mfa-totp"

    def test_authenticate_with_organization_selection(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_organization_selection(
            pending_authentication_token="test_pending_authentication_token",
            organization_id="test_organization_id",
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert (
            body["grant_type"] == "urn:workos:oauth:grant-type:organization-selection"
        )

    def test_authenticate_with_device_code(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_device_code(
            device_code="test_device_code"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "urn:ietf:params:oauth:grant-type:device_code"

    def test_authenticate_with_radar_email_challenge(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_radar_email_challenge(
            code="test_code",
            radar_challenge_id="test_radar_challenge_id",
            pending_authentication_token="test_pending_authentication_token",
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert (
            body["grant_type"]
            == "urn:workos:oauth:grant-type:radar-email-challenge:code"
        )

    def test_authenticate_with_radar_sms_challenge(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = workos.user_management.authenticate_with_radar_sms_challenge(
            code="test_code",
            verification_id="test_verification_id",
            phone_number="test_phone_number",
            pending_authentication_token="test_pending_authentication_token",
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert (
            body["grant_type"] == "urn:workos:oauth:grant-type:radar-sms-challenge:code"
        )

    def test_get_jwks_with_request_options(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("jwks_response.json"))
        workos.user_management.get_jwks(
            "test_clientId", request_options={"extra_headers": {"X-Custom": "value"}}
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    def test_get_jwks_unauthorized(self, workos, httpx_mock):
        httpx_mock.add_response(
            status_code=401,
            json={"message": "Unauthorized"},
        )
        with pytest.raises(AuthenticationError):
            workos.user_management.get_jwks("test_clientId")

    def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            workos.close()

    def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            workos.close()

    def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            workos.close()

    def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            workos.close()

    def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            workos.close()


class TestAsyncUserManagement:
    @pytest.mark.asyncio
    async def test_get_jwks(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("jwks_response.json"))
        result = await async_workos.user_management.get_jwks("test_clientId")
        assert isinstance(result, JwksResponse)
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/sso/jwks/test_clientId")

    @pytest.mark.asyncio
    async def test_create_authenticate(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = await async_workos.user_management.create_authenticate(
            body=load_fixture("authorization_code_session_authenticate_request.json")
        )
        assert isinstance(result, AuthenticateResponse)
        assert result.organization_id == "org_01H945H0YD4F97JN9MATX7BYAG"
        assert result.authkit_authorization_code == "authkit_authz_code_abc123"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/authenticate")

    def test_get_authorization_url(self, async_workos):
        result = async_workos.user_management.get_authorization_url(
            redirect_uri="test_redirect_uri"
        )
        assert isinstance(result, str)
        assert result.startswith("http")

    @pytest.mark.asyncio
    async def test_create_device(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("device_authorization_response.json"))
        result = await async_workos.user_management.create_device(
            client_id="test_client_id"
        )
        assert isinstance(result, DeviceAuthorizationResponse)
        assert (
            result.device_code
            == "CVE2wOfIFK4vhmiDBntpX9s8KT2f0qngpWYL0LGy9HxYgBRXUKIUkZB9BgIFho5h"
        )
        assert result.user_code == "BCDF-GHJK"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/authorize/device")

    @pytest.mark.asyncio
    async def test_create_radar_challenge(self, async_workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("send_radar_sms_challenge_response.json")
        )
        result = await async_workos.user_management.create_radar_challenge(
            user_id="test_user_id",
            pending_authentication_token="test_pending_authentication_token",
            phone_number="test_phone_number",
        )
        assert isinstance(result, SendRadarSmsChallengeResponse)
        assert result.verification_id == "vrf_01HXYZ123456789ABCDEFGHIJ"
        assert result.phone_number == "+15555550123"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/radar_challenges")

    @pytest.mark.asyncio
    async def test_get_radar_challenge(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("radar_challenge.json"))
        result = await async_workos.user_management.get_radar_challenge("test_id")
        assert isinstance(result, RadarChallenge)
        assert result.object == "radar_challenge"
        assert result.id == "radar_challenge_01HWZBQZY2M3AMQW166Q22K88F"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/radar_challenges/test_id")

    def test_get_logout_url(self, async_workos):
        result = async_workos.user_management.get_logout_url(
            session_id="test_session_id"
        )
        assert isinstance(result, str)
        assert result.startswith("http")

    @pytest.mark.asyncio
    async def test_revoke_session(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={})
        await async_workos.user_management.revoke_session(session_id="test_session_id")
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/sessions/revoke")

    @pytest.mark.asyncio
    async def test_list_cors_origins(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_cors_origin_response.json"))
        page = await async_workos.user_management.list_cors_origins()
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], CORSOriginResponse)

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

    @pytest.mark.asyncio
    async def test_list_cors_origins_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.user_management.list_cors_origins(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        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"

    @pytest.mark.asyncio
    async def test_create_cors_origin(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("cors_origin_response.json"))
        result = await async_workos.user_management.create_cors_origin(
            origin="test_origin"
        )
        assert isinstance(result, CORSOriginResponse)
        assert result.object == "cors_origin"
        assert result.id == "cors_origin_01HXYZ123456789ABCDEFGHIJ"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/cors_origins")

    @pytest.mark.asyncio
    async def test_get_email_verification(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("email_verification.json"))
        result = await async_workos.user_management.get_email_verification("test_id")
        assert isinstance(result, EmailVerification)
        assert result.object == "email_verification"
        assert result.id == "email_verification_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/email_verification/test_id")

    @pytest.mark.asyncio
    async def test_reset_password(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("password_reset.json"))
        result = await async_workos.user_management.reset_password(email="test_email")
        assert isinstance(result, PasswordReset)
        assert result.object == "password_reset"
        assert result.id == "password_reset_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/password_reset")

    @pytest.mark.asyncio
    async def test_confirm_password_reset(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("reset_password_response.json"))
        result = await async_workos.user_management.confirm_password_reset(
            token="test_token", new_password="test_new_password"
        )
        assert isinstance(result, ResetPasswordResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/password_reset/confirm")

    @pytest.mark.asyncio
    async def test_get_password_reset(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("password_reset.json"))
        result = await async_workos.user_management.get_password_reset("test_id")
        assert isinstance(result, PasswordReset)
        assert result.object == "password_reset"
        assert result.id == "password_reset_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/password_reset/test_id")

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

    @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.user_management.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.user_management.list_users(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            organization="value organization/test",
            organization_id="value organization_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["organization"] == "value organization/test"
        assert request.url.params["organization_id"] == "value organization_id/test"
        assert request.url.params["email"] == "value email/test"

    @pytest.mark.asyncio
    async def test_create_user(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user_create_response.json"))
        result = await async_workos.user_management.create_user(
            email="test_email", password=PasswordPlaintext(password="test_value")
        )
        assert isinstance(result, UserCreateResponse)
        assert result.object == "user"
        assert result.id == "user_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/users")

    @pytest.mark.asyncio
    async def test_get_user_by_external_id(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user.json"))
        result = await async_workos.user_management.get_user_by_external_id(
            "test_external_id"
        )
        assert isinstance(result, User)
        assert result.object == "user"
        assert result.id == "user_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith(
            "/user_management/users/external_id/test_external_id"
        )

    @pytest.mark.asyncio
    async def test_get_user(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user.json"))
        result = await async_workos.user_management.get_user("test_id")
        assert isinstance(result, User)
        assert result.object == "user"
        assert result.id == "user_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/users/test_id")

    @pytest.mark.asyncio
    async def test_update_user(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user.json"))
        result = await async_workos.user_management.update_user(
            "test_id", password=PasswordPlaintext(password="test_value")
        )
        assert isinstance(result, User)
        assert result.object == "user"
        assert result.id == "user_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "PUT"
        assert request.url.path.endswith("/user_management/users/test_id")

    @pytest.mark.asyncio
    async def test_delete_user(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=204)
        result = await async_workos.user_management.delete_user("test_id")
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith("/user_management/users/test_id")

    @pytest.mark.asyncio
    async def test_confirm_email_change(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("email_change_confirmation.json"))
        result = await async_workos.user_management.confirm_email_change(
            "test_id", code="test_code"
        )
        assert isinstance(result, EmailChangeConfirmation)
        assert result.object == "email_change_confirmation"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/user_management/users/test_id/email_change/confirm"
        )

    @pytest.mark.asyncio
    async def test_send_email_change(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("email_change.json"))
        result = await async_workos.user_management.send_email_change(
            "test_id", new_email="test_new_email"
        )
        assert isinstance(result, EmailChange)
        assert result.object == "email_change"
        assert result.new_email == "new.email@example.com"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/user_management/users/test_id/email_change/send"
        )

    @pytest.mark.asyncio
    async def test_verify_email(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("verify_email_response.json"))
        result = await async_workos.user_management.verify_email(
            "test_id", code="test_code"
        )
        assert isinstance(result, VerifyEmailResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/user_management/users/test_id/email_verification/confirm"
        )

    @pytest.mark.asyncio
    async def test_send_verification_email(self, async_workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("send_verification_email_response.json")
        )
        result = await async_workos.user_management.send_verification_email("test_id")
        assert isinstance(result, SendVerificationEmailResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/user_management/users/test_id/email_verification/send"
        )

    @pytest.mark.asyncio
    async def test_get_user_identities(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=[load_fixture("user_identities_get_item.json")])
        result = await async_workos.user_management.get_user_identities("test_id")
        assert isinstance(result, list)
        assert len(result) == 1
        assert isinstance(result[0], UserIdentitiesGetItem)

    @pytest.mark.asyncio
    async def test_list_sessions(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_user_sessions_list_item.json"))
        page = await async_workos.user_management.list_sessions("test_id")
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], UserSessionsListItem)

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

    @pytest.mark.asyncio
    async def test_list_sessions_encodes_query_params(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.user_management.list_sessions(
            "test_id",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        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"

    @pytest.mark.asyncio
    async def test_list_invitations(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_user_invite.json"))
        page = await async_workos.user_management.list_invitations()
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], UserInvite)

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

    @pytest.mark.asyncio
    async def test_list_invitations_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.user_management.list_invitations(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            organization_id="value organization_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["organization_id"] == "value organization_id/test"
        assert request.url.params["email"] == "value email/test"

    @pytest.mark.asyncio
    async def test_send_invitation(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user_invite.json"))
        result = await async_workos.user_management.send_invitation(email="test_email")
        assert isinstance(result, UserInvite)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/invitations")

    @pytest.mark.asyncio
    async def test_find_invitation_by_token(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user_invite.json"))
        result = await async_workos.user_management.find_invitation_by_token(
            "test_token"
        )
        assert isinstance(result, UserInvite)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith(
            "/user_management/invitations/by_token/test_token"
        )

    @pytest.mark.asyncio
    async def test_get_invitation(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user_invite.json"))
        result = await async_workos.user_management.get_invitation("test_id")
        assert isinstance(result, UserInvite)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/invitations/test_id")

    @pytest.mark.asyncio
    async def test_accept_invitation(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("invitation.json"))
        result = await async_workos.user_management.accept_invitation("test_id")
        assert isinstance(result, Invitation)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/invitations/test_id/accept")

    @pytest.mark.asyncio
    async def test_resend_invitation(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user_invite.json"))
        result = await async_workos.user_management.resend_invitation("test_id")
        assert isinstance(result, UserInvite)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/invitations/test_id/resend")

    @pytest.mark.asyncio
    async def test_revoke_invitation(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("invitation.json"))
        result = await async_workos.user_management.revoke_invitation("test_id")
        assert isinstance(result, Invitation)
        assert result.object == "invitation"
        assert result.id == "invitation_01E4ZCR3C56J083X43JQXF3JK5"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/invitations/test_id/revoke")

    @pytest.mark.asyncio
    async def test_list_jwt_template(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("jwt_template_response.json"))
        result = await async_workos.user_management.list_jwt_template()
        assert isinstance(result, JWTTemplateResponse)
        assert result.object == "jwt_template"
        assert result.created_at == "2026-01-15T12:00:00.000Z"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/jwt_template")

    @pytest.mark.asyncio
    async def test_update_jwt_template(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("jwt_template_response.json"))
        result = await async_workos.user_management.update_jwt_template(
            content="test_content"
        )
        assert isinstance(result, JWTTemplateResponse)
        assert result.object == "jwt_template"
        assert result.created_at == "2026-01-15T12:00:00.000Z"
        request = httpx_mock.get_request()
        assert request.method == "PUT"
        assert request.url.path.endswith("/user_management/jwt_template")

    @pytest.mark.asyncio
    async def test_create_magic_auth(self, async_workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture(
                "magic_auth_send_magic_auth_code_and_return_response.json"
            )
        )
        result = await async_workos.user_management.create_magic_auth(
            email="test_email"
        )
        assert isinstance(result, MagicAuthSendMagicAuthCodeAndReturnResponse)
        assert result.object == "magic_auth"
        assert result.id == "magic_auth_01HWZBQZY2M3AMQW166Q22K88F"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/magic_auth")

    @pytest.mark.asyncio
    async def test_get_magic_auth(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("magic_auth.json"))
        result = await async_workos.user_management.get_magic_auth("test_id")
        assert isinstance(result, MagicAuth)
        assert result.object == "magic_auth"
        assert result.id == "magic_auth_01HWZBQZY2M3AMQW166Q22K88F"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/user_management/magic_auth/test_id")

    @pytest.mark.asyncio
    async def test_list_redirect_uris(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_redirect_uri.json"))
        page = await async_workos.user_management.list_redirect_uris()
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], RedirectUri)

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

    @pytest.mark.asyncio
    async def test_list_redirect_uris_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.user_management.list_redirect_uris(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        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"

    @pytest.mark.asyncio
    async def test_create_redirect_uri(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("redirect_uri.json"))
        result = await async_workos.user_management.create_redirect_uri(uri="test_uri")
        assert isinstance(result, RedirectUri)
        assert result.object == "redirect_uri"
        assert result.id == "redir_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/redirect_uris")

    @pytest.mark.asyncio
    async def test_delete_redirect_uris(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=204)
        result = await async_workos.user_management.delete_redirect_uris("test_id")
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith("/user_management/redirect_uris/test_id")

    @pytest.mark.asyncio
    async def test_list_user_authorized_applications(self, async_workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_authorized_connect_application_list_data.json")
        )
        page = await async_workos.user_management.list_user_authorized_applications(
            "test_user_id"
        )
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], AuthorizedConnectApplicationListData)

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

    @pytest.mark.asyncio
    async def test_list_user_authorized_applications_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.user_management.list_user_authorized_applications(
            "test_user_id",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        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"

    @pytest.mark.asyncio
    async def test_delete_user_authorized_application(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=204)
        result = await async_workos.user_management.delete_user_authorized_application(
            "test_application_id", "test_user_id"
        )
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith(
            "/user_management/users/test_user_id/authorized_applications/test_application_id"
        )

    @pytest.mark.asyncio
    async def test_list_user_api_keys(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_user_api_key.json"))
        page = await async_workos.user_management.list_user_api_keys("test_userId")
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], UserApiKey)

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

    @pytest.mark.asyncio
    async def test_list_user_api_keys_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.user_management.list_user_api_keys(
            "test_userId",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
            organization_id="value organization_id/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"

    @pytest.mark.asyncio
    async def test_create_user_api_key(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("user_api_key_with_value.json"))
        result = await async_workos.user_management.create_user_api_key(
            "test_userId", name="test_name", organization_id="test_organization_id"
        )
        assert isinstance(result, UserApiKeyWithValue)
        assert result.object == "api_key"
        assert result.id == "api_key_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/user_management/users/test_userId/api_keys")

    @pytest.mark.asyncio
    async def test_authenticate_with_password(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = await async_workos.user_management.authenticate_with_password(
            email="test_email", password="test_password"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "password"

    @pytest.mark.asyncio
    async def test_authenticate_with_code(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = await async_workos.user_management.authenticate_with_code(
            code="test_code"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "authorization_code"

    @pytest.mark.asyncio
    async def test_authenticate_with_refresh_token(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = await async_workos.user_management.authenticate_with_refresh_token(
            refresh_token="test_refresh_token"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "refresh_token"

    @pytest.mark.asyncio
    async def test_authenticate_with_magic_auth(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = await async_workos.user_management.authenticate_with_magic_auth(
            code="test_code", email="test_email"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "urn:workos:oauth:grant-type:magic-auth:code"

    @pytest.mark.asyncio
    async def test_authenticate_with_email_verification(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = (
            await async_workos.user_management.authenticate_with_email_verification(
                code="test_code",
                pending_authentication_token="test_pending_authentication_token",
            )
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert (
            body["grant_type"] == "urn:workos:oauth:grant-type:email-verification:code"
        )

    @pytest.mark.asyncio
    async def test_authenticate_with_totp(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = await async_workos.user_management.authenticate_with_totp(
            code="test_value",
            pending_authentication_token="test_value",
            authentication_challenge_id="test_value",
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "urn:workos:oauth:grant-type:mfa-totp"

    @pytest.mark.asyncio
    async def test_authenticate_with_organization_selection(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = (
            await async_workos.user_management.authenticate_with_organization_selection(
                pending_authentication_token="test_pending_authentication_token",
                organization_id="test_organization_id",
            )
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert (
            body["grant_type"] == "urn:workos:oauth:grant-type:organization-selection"
        )

    @pytest.mark.asyncio
    async def test_authenticate_with_device_code(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = await async_workos.user_management.authenticate_with_device_code(
            device_code="test_device_code"
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert body["grant_type"] == "urn:ietf:params:oauth:grant-type:device_code"

    @pytest.mark.asyncio
    async def test_authenticate_with_radar_email_challenge(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = (
            await async_workos.user_management.authenticate_with_radar_email_challenge(
                code="test_code",
                radar_challenge_id="test_radar_challenge_id",
                pending_authentication_token="test_pending_authentication_token",
            )
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert (
            body["grant_type"]
            == "urn:workos:oauth:grant-type:radar-email-challenge:code"
        )

    @pytest.mark.asyncio
    async def test_authenticate_with_radar_sms_challenge(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json=load_fixture("authenticate_response.json"))
        result = (
            await async_workos.user_management.authenticate_with_radar_sms_challenge(
                code="test_code",
                verification_id="test_verification_id",
                phone_number="test_phone_number",
                pending_authentication_token="test_pending_authentication_token",
            )
        )
        assert isinstance(result, AuthenticateResponse)
        request = httpx_mock.get_request()
        assert request.method == "POST"
        body = json.loads(request.content)
        assert (
            body["grant_type"] == "urn:workos:oauth:grant-type:radar-sms-challenge:code"
        )

    @pytest.mark.asyncio
    async def test_get_jwks_with_request_options(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("jwks_response.json"))
        await async_workos.user_management.get_jwks(
            "test_clientId", request_options={"extra_headers": {"X-Custom": "value"}}
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

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

    @pytest.mark.asyncio
    async def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_get_jwks_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.user_management.get_jwks("test_clientId")
        finally:
            await workos.close()
