Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.1 KB
Raw Blame
from unittest.mock import MagicMock

import pytest
from pytest_mock import MockerFixture
from sqlalchemy.orm import Session, sessionmaker

from machinery.context import RequestContext
from models.account import Account
from repositories.account.repository import SQLAlchemyAccountRepository
from services.account_errors import AccountNotFoundError
from services.billing_client import billing
from services.billing_portal_service import BillingPortalService
from tests.helpers.unit.model_factories import make_account


def _context() -> RequestContext:
    return RequestContext(
        request_id="request-1",
        trace_id="trace-1",
        account_id="account-1",
        active_workspace_id="workspace-1",
    )


@pytest.fixture
def get_subscription() -> MagicMock:
    return MagicMock()


@pytest.fixture
def get_invoices() -> MagicMock:
    return MagicMock()


@pytest.fixture
def accounts(sqlite_session_factory: sessionmaker[Session]) -> SQLAlchemyAccountRepository:
    with sqlite_session_factory.begin() as session:
        session.add(make_account(email="[email protected]"))
    return SQLAlchemyAccountRepository(sqlite_session_factory)


@pytest.fixture
def service(
    accounts: SQLAlchemyAccountRepository, get_subscription: MagicMock, get_invoices: MagicMock
) -> BillingPortalService:
    return BillingPortalService(accounts=accounts, get_subscription=get_subscription, get_invoices=get_invoices)


def test_get_subscription_loads_email_and_delegates(
    service: BillingPortalService,
    accounts: SQLAlchemyAccountRepository,
    get_subscription: MagicMock,
    mocker: MockerFixture,
) -> None:
    lookup = mocker.spy(accounts, "get")
    link = billing.GetSubscriptionPaymentLinkReply(url="https://billing.example.com/checkout")
    get_subscription.return_value = link

    result = service.get_subscription(
        _context(),
        plan="professional",
        interval="month",
    )

    assert result is link
    lookup.assert_called_once_with("account-1")
    get_subscription.assert_called_once_with("professional", "month", "[email protected]", "workspace-1")


def test_get_invoices_loads_email_and_delegates(
    service: BillingPortalService,
    accounts: SQLAlchemyAccountRepository,
    get_invoices: MagicMock,
    mocker: MockerFixture,
) -> None:
    lookup = mocker.spy(accounts, "get")
    link = billing.GetInvoicesReply(url="https://billing.example.com/portal")
    get_invoices.return_value = link

    result = service.get_invoices(_context())

    assert result is link
    lookup.assert_called_once_with("account-1")
    get_invoices.assert_called_once_with("[email protected]", "workspace-1")


def test_missing_account_does_not_call_billing(
    service: BillingPortalService,
    sqlite_session_factory: sessionmaker[Session],
    get_invoices: MagicMock,
) -> None:
    with sqlite_session_factory.begin() as session:
        account = session.get(Account, "account-1")
        assert account is not None
        session.delete(account)

    with pytest.raises(AccountNotFoundError):
        service.get_invoices(_context())

    get_invoices.assert_not_called()