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.

2.7 KB
Raw Blame
"""Integration coverage for the remaining WebAppAuthService responsibilities."""

import uuid

import pytest
from faker import Faker
from sqlalchemy.orm import Session
from werkzeug.exceptions import NotFound

from models import Account, AccountStatus, Tenant, TenantAccountJoin, TenantAccountRole, TenantStatus
from models.enums import AppStatus, CustomizeTokenStrategy
from models.model import App, Site
from services.webapp_auth_service import WebAppAuthService


def _create_account_and_tenant(session: Session) -> tuple[Account, Tenant]:
    fake = Faker()
    account = Account(
        email=f"test_{uuid.uuid4().hex[:8]}@example.com",
        name=fake.name(),
        interface_language="en-US",
        status=AccountStatus.ACTIVE,
    )
    tenant = Tenant(name=fake.company(), status=TenantStatus.NORMAL)
    session.add_all([account, tenant])
    session.flush()
    session.add(
        TenantAccountJoin(
            tenant_id=tenant.id,
            account_id=account.id,
            role=TenantAccountRole.OWNER,
            current=True,
        )
    )
    session.commit()
    return account, tenant


def _create_app_and_site(session: Session, tenant: Tenant) -> tuple[App, Site]:
    fake = Faker()
    app = App(
        tenant_id=tenant.id,
        name=fake.company(),
        description=fake.text(max_nb_chars=100),
        mode="chat",
        icon_type="emoji",
        icon="🤖",
        icon_background="#FF6B6B",
        api_rph=100,
        api_rpm=10,
        enable_site=True,
        enable_api=True,
    )
    session.add(app)
    session.flush()
    site = Site(
        app_id=app.id,
        title=fake.company(),
        code=fake.unique.lexify(text="??????"),
        description=fake.text(max_nb_chars=100),
        default_language="en-US",
        status=AppStatus.NORMAL,
        customize_token_strategy=CustomizeTokenStrategy.NOT_ALLOW,
    )
    session.add(site)
    session.commit()
    return app, site


def test_create_end_user(db_session_with_containers: Session) -> None:
    _, tenant = _create_account_and_tenant(db_session_with_containers)
    app, site = _create_app_and_site(db_session_with_containers, tenant)

    result = WebAppAuthService.create_end_user(site.code, "[email protected]", db_session_with_containers)

    assert result.tenant_id == app.tenant_id
    assert result.app_id == app.id
    assert result.type == "browser"
    assert result.is_anonymous is False
    assert result.session_id == "[email protected]"


def test_create_end_user_rejects_unknown_site(db_session_with_containers: Session) -> None:
    with pytest.raises(NotFound, match="Site not found"):
        WebAppAuthService.create_end_user("missing", "[email protected]", db_session_with_containers)