def authorized_client(client: AsyncClient, token: str, authorization_prefix: str) -> AsyncClient: client.headers = { "Authorization": f"{authorization_prefix} {token}", **client.headers, } # type: ignore return client
def authorized_client(client: AsyncClient, test_user: UserInDB) -> AsyncClient: access_token = auth_service.create_access_token_for_user( user=test_user, secret_key=str(SECRET_KEY)) client.headers = { **client.headers, "Authorization": f"{JWT_TOKEN_PREFIX} {access_token}" } return client
async def logged_session(client: AsyncClient, user_id: int = None) -> None: cookies = {} headers = {} if user_id: session_id = await create_session(dict(id=user_id)) csrf_token = create_csrf(session_id) cookies = {'session_id': session_id} headers = {'x-csrf-token': csrf_token} client.cookies = cookies client.headers = headers return
def authorized_client(client: AsyncClient, test_user: UserInDB) -> AsyncClient: """ Creates an authorized test_user to test authorized requests instead of the generic ``client`` fixture. """ access_token = auth_service.create_access_token_for_user( user=test_user, secret_key=str(SECRET_KEY)) client.headers = { **client.headers, "Authorization": f"{JWT_TOKEN_PREFIX} {access_token}", } return client
def authorized_client(client: AsyncClient, token: str, authorization_prefix: str, monkeypatch) -> AsyncClient: async def return_test_user_sub(authorization: str) -> str: return authorization import app.api.dependencies.auth as auth monkeypatch.setattr(auth, "get_sub", return_test_user_sub) client.headers = { "Authorization": f"{test_user_sub}", **client.headers, } return client
def test_client_headers(): client = AsyncClient() client.headers = {"a": "b"} assert isinstance(client.headers, Headers) assert client.headers["A"] == "b"