Commit 291f11c2 authored by Sergio Gimenez's avatar Sergio Gimenez
Browse files

partner authentication use casee

parent 99c471b9
Loading
Loading
Loading
Loading
+0 −0

Empty file added.

+16 −0
Original line number Diff line number Diff line
from federation_manager.domain.errors import PartnerNotActive, PartnerUnknown
from federation_manager.domain.models import PartnerOP
from federation_manager.domain.ports import PartnerRepositoryPort


class PartnerAuthenticator:
    def __init__(self, partner_repo: PartnerRepositoryPort) -> None:
        self._partner_repo = partner_repo

    async def authenticate(self, cert_thumbprint: str) -> PartnerOP:
        partner = await self._partner_repo.find_by_cert_thumbprint(cert_thumbprint)
        if partner is None:
            raise PartnerUnknown(cert_thumbprint)
        if not partner.is_active():
            raise PartnerNotActive(partner.status)
        return partner
+14 −0
Original line number Diff line number Diff line
class FederationError(Exception):
    pass


class PartnerUnknown(FederationError):
    def __init__(self, cert_thumbprint: str) -> None:
        super().__init__(f"no partner registered for thumbprint {cert_thumbprint}")
        self.cert_thumbprint = cert_thumbprint


class PartnerNotActive(FederationError):
    def __init__(self, status: str) -> None:
        super().__init__(f"partner status is {status}")
        self.status = status
+33 −0
Original line number Diff line number Diff line
from uuid import uuid4

import pytest

from federation_manager.application.authentication import PartnerAuthenticator
from federation_manager.domain.errors import PartnerNotActive, PartnerUnknown
from federation_manager.domain.models import PartnerOP
from tests.fakes import InMemoryPartnerRepo


def _partner(status: str = "active") -> PartnerOP:
    return PartnerOP(id=uuid4(), mcc_mnc="214-07", cert_thumbprint="AA:BB", status=status)


async def test_authenticates_known_active_partner() -> None:
    partner = _partner()
    auth = PartnerAuthenticator(InMemoryPartnerRepo([partner]))

    assert await auth.authenticate("AA:BB") is partner


async def test_unknown_thumbprint_is_rejected() -> None:
    auth = PartnerAuthenticator(InMemoryPartnerRepo([_partner()]))

    with pytest.raises(PartnerUnknown):
        await auth.authenticate("CC:DD")


async def test_suspended_partner_is_rejected() -> None:
    auth = PartnerAuthenticator(InMemoryPartnerRepo([_partner(status="suspended")]))

    with pytest.raises(PartnerNotActive):
        await auth.authenticate("AA:BB")