async def sign_in(client: AsyncClient): response = await client.get("/users/sign_in") page = LoginPage(await response.aread()) response = await client.post( f"/users/sign_in", data={ "utf-8": "", "authenticity_token": page.auth_token, "user[email]": CODEWARS_EMAIL.get(), "user[password]": CODEWARS_PASSWORD.get(), "user[remember_me]": "true", }, ) if b"<title>Home | Codewars</title>" not in await response.aread(): raise RuntimeError("Can't sign up") home_page = HomePage(response.content) *_, username = home_page.username_link.split("/") CODEWARS_USERNAME.set(username) def _auth(request: Request) -> Request: request.headers.update( {"Authorization": page.auth_token, "X-Requested-With": "XMLHttpRequest"} ) return request client.auth = _auth
async def test_auth_property() -> None: client = AsyncClient(transport=AsyncMockTransport()) assert client.auth is None client.auth = ("tomchristie", "password123") # type: ignore assert isinstance(client.auth, BasicAuth) url = "https://example.org/" response = await client.get(url) assert response.status_code == 200 assert response.json() == { "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM=" }
async def test_auth_invalid_type() -> None: with pytest.raises(TypeError): client = AsyncClient( transport=AsyncMockTransport(), auth="not a tuple, not a callable", # type: ignore ) client = AsyncClient(transport=AsyncMockTransport()) with pytest.raises(TypeError): await client.get(auth="not a tuple, not a callable") # type: ignore with pytest.raises(TypeError): client.auth = "not a tuple, not a callable" # type: ignore