app.bettersight.io/backend/tests/services/test_auth_service.py

57 lines
2.1 KiB
Python

from services import auth_service
from repositories.tenant_repository import tenant_repository
from repositories.seat_repository import seat_repository
class _FakeRequest:
def __init__(self, auth_header=None):
self.headers = {'Authorization': auth_header} if auth_header else {}
def test_returns_none_without_bearer_prefix():
assert auth_service.get_tenant_id_from_request(_FakeRequest('not-bearer-token')) is None
def test_returns_none_without_auth_header():
assert auth_service.get_tenant_id_from_request(_FakeRequest()) is None
def test_returns_none_when_pocketbase_rejects_token(monkeypatch):
monkeypatch.setattr('services.auth_service.pb_client.auth_refresh', lambda token: None)
assert auth_service.get_tenant_id_from_request(_FakeRequest('Bearer badtoken')) is None
def test_returns_none_when_tenant_not_found(monkeypatch):
monkeypatch.setattr(
'services.auth_service.pb_client.auth_refresh',
lambda token: {'email': 'pm@unknown.com'}
)
assert auth_service.get_tenant_id_from_request(_FakeRequest('Bearer sometoken')) is None
def test_returns_none_when_email_has_no_active_seat(monkeypatch):
tenant = tenant_repository.create({
'name': 'G Adventures', 'domain': 'gadventures.com', 'tier': 'analyse',
'status': 'active', 'max_seats': 5,
})
monkeypatch.setattr(
'services.auth_service.pb_client.auth_refresh',
lambda token: {'email': 'pm@gadventures.com'}
)
assert auth_service.get_tenant_id_from_request(_FakeRequest('Bearer sometoken')) is None
def test_returns_tenant_id_for_valid_active_seat(monkeypatch):
tenant = tenant_repository.create({
'name': 'G Adventures', 'domain': 'gadventures.com', 'tier': 'analyse',
'status': 'active', 'max_seats': 5,
})
seat_repository.create({'tenant_id': tenant['id'], 'email': 'pm@gadventures.com', 'active': True})
monkeypatch.setattr(
'services.auth_service.pb_client.auth_refresh',
lambda token: {'email': 'pm@gadventures.com'}
)
result = auth_service.get_tenant_id_from_request(_FakeRequest('Bearer validtoken'))
assert result == tenant['id']