mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 23:20:56 +01:00
63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
import pytest
|
|
from fastapi import status
|
|
from app.services import user_service
|
|
|
|
|
|
def test_get_oauth_provider_known_unknown():
|
|
# Known providers should return a provider instance
|
|
bankid = user_service.get_oauth_provider("BankID")
|
|
mojeid = user_service.get_oauth_provider("MojeID")
|
|
assert bankid is not None
|
|
assert mojeid is not None
|
|
|
|
# Unknown should return None
|
|
assert user_service.get_oauth_provider("DoesNotExist") is None
|
|
|
|
|
|
def test_get_jwt_strategy_lifetime():
|
|
strategy = user_service.get_jwt_strategy()
|
|
assert strategy is not None
|
|
# Basic smoke check: strategy has a lifetime set to 604800
|
|
assert getattr(strategy, "lifetime_seconds", None) in (604800,)
|
|
|
|
def test_root_ok(client):
|
|
resp = client.get("/")
|
|
assert resp.status_code == status.HTTP_200_OK
|
|
assert resp.json() == {"status": "ok"}
|
|
|
|
|
|
def test_authenticated_route_requires_auth(client):
|
|
resp = client.get("/authenticated-route")
|
|
assert resp.status_code in (status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_on_after_request_verify_enqueues_email(monkeypatch):
|
|
calls = {}
|
|
|
|
def fake_enqueue_email(to: str, subject: str, body: str):
|
|
calls.setdefault("emails", []).append({
|
|
"to": to,
|
|
"subject": subject,
|
|
"body": body,
|
|
})
|
|
|
|
# Patch the enqueue_email used inside user_service
|
|
monkeypatch.setattr(user_service, "enqueue_email", fake_enqueue_email)
|
|
|
|
class DummyUser:
|
|
def __init__(self, email):
|
|
self.email = email
|
|
|
|
mgr = user_service.UserManager(user_db=None) # user_db not needed for this method
|
|
user = DummyUser("test@example.com")
|
|
|
|
# Call the hook
|
|
await mgr.on_after_request_verify(user, token="abc123", request=None)
|
|
|
|
# Verify one email has been enqueued with expected content
|
|
assert len(calls.get("emails", [])) == 1
|
|
email = calls["emails"][0]
|
|
assert email["to"] == "test@example.com"
|
|
assert "ověření účtu" in email["subject"].lower()
|
|
assert "abc123" in email["body"]
|