40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
import httpx
|
|
import pytest
|
|
|
|
from app.mywhoosh.client import MyWhooshClient, MyWhooshAuthError
|
|
from app.mywhoosh.models import MyWhooshToken
|
|
from app.mywhoosh.tokenstore import MyWhooshTokenStore
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_saves_access_refresh_and_whoosh_id(tmp_path) -> None:
|
|
async def handler(request: httpx.Request) -> httpx.Response:
|
|
assert request.url.path == "/http-service/api/login"
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"Success": True,
|
|
"AccessToken": "new-access",
|
|
"RefreshToken": "new-refresh",
|
|
"WhooshId": "w-1",
|
|
},
|
|
)
|
|
|
|
store = MyWhooshTokenStore(tmp_path / "mywhoosh.json")
|
|
client = MyWhooshClient(store, http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)))
|
|
await client.login("rider@example.com", "secret")
|
|
assert store.load() == MyWhooshToken("new-access", "new-refresh", "w-1")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_invalid_credentials_raise_auth_error(tmp_path) -> None:
|
|
async def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(200, json={"Success": False, "Message": "Invalid credentials"})
|
|
|
|
client = MyWhooshClient(
|
|
MyWhooshTokenStore(tmp_path / "mywhoosh.json"),
|
|
http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
|
|
)
|
|
with pytest.raises(MyWhooshAuthError):
|
|
await client.login("rider@example.com", "bad")
|