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.

25.0 KB
Raw Blame
"""
Comprehensive unit tests for Account model.

This test suite covers:
- Account model validation
- Password hashing/verification
- Account status transitions
- Tenant relationship integrity
- Email uniqueness constraints
"""

import base64
import secrets
from collections.abc import Callable
from datetime import UTC, datetime
from uuid import uuid4

import pytest
from sqlalchemy.orm import Session

from libs.password import compare_password, hash_password, valid_password
from models.account import Account, AccountStatus, Tenant, TenantAccountJoin, TenantAccountRole, TenantCustomConfigDict


class TestAccountModelValidation:
    """Test suite for Account model validation and basic operations."""

    def test_account_creation_with_required_fields(self) -> None:
        """Test creating an account with all required fields."""
        # Arrange & Act
        account = Account(
            name="Test User",
            email="[email protected]",
            password="hashed_password",
            password_salt="salt_value",
        )

        # Assert
        assert account.name == "Test User"
        assert account.email == "[email protected]"
        assert account.password == "hashed_password"
        assert account.password_salt == "salt_value"
        assert account.status == "active"  # Default value

    def test_account_creation_with_optional_fields(self) -> None:
        """Test creating an account with optional fields."""
        # Arrange & Act
        account = Account(
            name="Test User",
            email="[email protected]",
            avatar="https://example.com/avatar.png",
            interface_language="en-US",
            interface_theme="dark",
            timezone="America/New_York",
        )

        # Assert
        assert account.avatar == "https://example.com/avatar.png"
        assert account.interface_language == "en-US"
        assert account.interface_theme == "dark"
        assert account.timezone == "America/New_York"

    def test_account_creation_without_password(self) -> None:
        """Test creating an account without password (for invite-based registration)."""
        # Arrange & Act
        account = Account(
            name="Invited User",
            email="[email protected]",
        )

        # Assert
        assert account.password is None
        assert account.password_salt is None
        assert not account.is_password_set

    def test_account_is_password_set_property(self) -> None:
        """Test the is_password_set property."""
        # Arrange
        account_with_password = Account(
            name="User With Password",
            email="[email protected]",
            password="hashed_password",
        )
        account_without_password = Account(
            name="User Without Password",
            email="[email protected]",
        )

        # Assert
        assert account_with_password.is_password_set
        assert not account_without_password.is_password_set

    def test_account_default_status(self) -> None:
        """Test that account has default status of 'active'."""
        # Arrange & Act
        account = Account(
            name="Test User",
            email="[email protected]",
        )

        # Assert
        assert account.status == AccountStatus.ACTIVE

    def test_account_get_status_method(self) -> None:
        """Test the get_status method returns AccountStatus enum."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
            status=AccountStatus.PENDING,
        )

        # Act
        status = account.get_status()

        # Assert
        assert status == AccountStatus.PENDING
        assert isinstance(status, AccountStatus)


class TestPasswordHashingAndVerification:
    """Test suite for password hashing and verification functionality."""

    def test_password_hashing_produces_consistent_result(self) -> None:
        """Test that hashing the same password with the same salt produces the same result."""
        # Arrange
        password = "TestPassword123"
        salt = secrets.token_bytes(16)

        # Act
        hash1 = hash_password(password, salt)
        hash2 = hash_password(password, salt)

        # Assert
        assert hash1 == hash2

    def test_password_hashing_different_salts_produce_different_hashes(self) -> None:
        """Test that different salts produce different hashes for the same password."""
        # Arrange
        password = "TestPassword123"
        salt1 = secrets.token_bytes(16)
        salt2 = secrets.token_bytes(16)

        # Act
        hash1 = hash_password(password, salt1)
        hash2 = hash_password(password, salt2)

        # Assert
        assert hash1 != hash2

    def test_password_comparison_success(self) -> None:
        """Test successful password comparison."""
        # Arrange
        password = "TestPassword123"
        salt = secrets.token_bytes(16)
        password_hashed = hash_password(password, salt)

        # Encode to base64 as done in the application
        base64_salt = base64.b64encode(salt).decode()
        base64_password_hashed = base64.b64encode(password_hashed).decode()

        # Act
        result = compare_password(password, base64_password_hashed, base64_salt)

        # Assert
        assert result is True

    def test_password_comparison_failure(self) -> None:
        """Test password comparison with wrong password."""
        # Arrange
        correct_password = "TestPassword123"
        wrong_password = "WrongPassword456"
        salt = secrets.token_bytes(16)
        password_hashed = hash_password(correct_password, salt)

        # Encode to base64
        base64_salt = base64.b64encode(salt).decode()
        base64_password_hashed = base64.b64encode(password_hashed).decode()

        # Act
        result = compare_password(wrong_password, base64_password_hashed, base64_salt)

        # Assert
        assert result is False

    def test_valid_password_with_correct_format(self) -> None:
        """Test password validation with correct format."""
        # Arrange
        valid_passwords = [
            "Password123",
            "Test1234",
            "MySecure1Pass",
            "abcdefgh1",
        ]

        # Act & Assert
        for password in valid_passwords:
            result = valid_password(password)
            assert result == password

    def test_valid_password_with_incorrect_format(self) -> None:
        """Test password validation with incorrect format."""
        # Arrange
        invalid_passwords = [
            "short1",  # Too short
            "NoNumbers",  # No numbers
            "12345678",  # No letters
            "Pass1",  # Too short
        ]

        # Act & Assert
        for password in invalid_passwords:
            with pytest.raises(ValueError, match="Password must contain letters and numbers"):
                valid_password(password)

    def test_password_hashing_integration_with_account(self) -> None:
        """Test password hashing integration with Account model."""
        # Arrange
        password = "SecurePass123"
        salt = secrets.token_bytes(16)
        base64_salt = base64.b64encode(salt).decode()
        password_hashed = hash_password(password, salt)
        base64_password_hashed = base64.b64encode(password_hashed).decode()

        # Act
        account = Account(
            name="Test User",
            email="[email protected]",
            password=base64_password_hashed,
            password_salt=base64_salt,
        )

        # Assert
        assert account.is_password_set
        assert compare_password(password, account.password, account.password_salt)


class TestAccountStatusTransitions:
    """Test suite for account status transitions."""

    def test_account_status_enum_values(self) -> None:
        """Test that AccountStatus enum has all expected values."""
        # Assert
        assert AccountStatus.PENDING == "pending"
        assert AccountStatus.UNINITIALIZED == "uninitialized"
        assert AccountStatus.ACTIVE == "active"
        assert AccountStatus.BANNED == "banned"
        assert AccountStatus.CLOSED == "closed"

    def test_account_status_transition_pending_to_active(self) -> None:
        """Test transitioning account status from pending to active."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
            status=AccountStatus.PENDING,
        )

        # Act
        account.status = AccountStatus.ACTIVE
        account.initialized_at = datetime.now(UTC)

        # Assert
        assert account.get_status() == AccountStatus.ACTIVE
        assert account.initialized_at is not None

    def test_account_status_transition_active_to_banned(self) -> None:
        """Test transitioning account status from active to banned."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
            status=AccountStatus.ACTIVE,
        )

        # Act
        account.status = AccountStatus.BANNED

        # Assert
        assert account.get_status() == AccountStatus.BANNED

    def test_account_status_transition_active_to_closed(self) -> None:
        """Test transitioning account status from active to closed."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
            status=AccountStatus.ACTIVE,
        )

        # Act
        account.status = AccountStatus.CLOSED

        # Assert
        assert account.get_status() == AccountStatus.CLOSED

    def test_account_status_uninitialized(self) -> None:
        """Test account with uninitialized status."""
        # Arrange & Act
        account = Account(
            name="Test User",
            email="[email protected]",
            status=AccountStatus.UNINITIALIZED,
        )

        # Assert
        assert account.get_status() == AccountStatus.UNINITIALIZED
        assert account.initialized_at is None


class TestTenantRelationshipIntegrity:
    """Test suite for tenant relationship integrity."""

    def test_account_current_tenant_id_property(self) -> None:
        """Test the current_tenant_id property."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
        )
        tenant = Tenant(name="Test Tenant")
        tenant.id = str(uuid4())

        # Act - with tenant
        account._current_tenant = tenant
        tenant_id = account.current_tenant_id

        # Assert
        assert tenant_id == tenant.id

        # Act - without tenant
        account._current_tenant = None
        tenant_id_none = account.current_tenant_id

        # Assert
        assert tenant_id_none is None

    def test_current_tenant_is_read_only(self) -> None:
        """current_tenant has no setter, so tenant switching cannot open a hidden session."""
        account = Account(name="Test User", email="[email protected]")
        tenant = Tenant(name="Test Tenant")

        with pytest.raises(AttributeError):
            account.current_tenant = tenant  # type: ignore[misc]

    @pytest.mark.parametrize("sqlite_session", [(Account, Tenant, TenantAccountJoin)], indirect=True)
    def test_set_current_tenant_with_session_uses_caller_session(self, sqlite_session: Session) -> None:
        account = Account(name="Test User", email="[email protected]")
        account.id = str(uuid4())
        tenant = Tenant(name="Test Tenant")
        tenant.id = str(uuid4())
        decoy_tenant = Tenant(name="Decoy Tenant")
        decoy_tenant.id = str(uuid4())
        sqlite_session.add_all(
            [
                account,
                tenant,
                decoy_tenant,
                TenantAccountJoin(
                    tenant_id=tenant.id,
                    account_id=account.id,
                    role=TenantAccountRole.OWNER,
                ),
                TenantAccountJoin(
                    tenant_id=decoy_tenant.id,
                    account_id=account.id,
                    role=TenantAccountRole.NORMAL,
                ),
            ]
        )
        sqlite_session.flush()

        account.set_current_tenant_with_session(tenant, session=sqlite_session)

        assert account.current_tenant is tenant
        assert account.role == TenantAccountRole.OWNER

    @pytest.mark.parametrize("sqlite_session", [(Account, Tenant, TenantAccountJoin)], indirect=True)
    def test_set_tenant_id_with_session_uses_caller_session(self, sqlite_session: Session) -> None:
        account = Account(name="Test User", email="[email protected]")
        account.id = str(uuid4())
        decoy_account = Account(name="Decoy User", email="[email protected]")
        decoy_account.id = str(uuid4())
        tenant = Tenant(name="Test Tenant")
        tenant.id = str(uuid4())
        sqlite_session.add_all(
            [
                account,
                decoy_account,
                tenant,
                TenantAccountJoin(
                    tenant_id=tenant.id,
                    account_id=account.id,
                    role=TenantAccountRole.ADMIN,
                ),
                TenantAccountJoin(
                    tenant_id=tenant.id,
                    account_id=decoy_account.id,
                    role=TenantAccountRole.OWNER,
                ),
            ]
        )
        sqlite_session.flush()

        account.set_tenant_id_with_session(tenant.id, session=sqlite_session)

        assert account.current_tenant is tenant
        assert account.role == TenantAccountRole.ADMIN


class TestAccountRolePermissions:
    @pytest.fixture(autouse=True)
    def _rbac_disabled(self, config_overrides: Callable[..., None]) -> None:
        config_overrides(RBAC_ENABLED=False)

    """Test suite for account role permissions."""

    def test_is_admin_or_owner_with_admin_role(self) -> None:
        """Test is_admin_or_owner property with admin role."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
        )
        account.role = TenantAccountRole.ADMIN

        assert account.is_admin_or_owner

    def test_is_admin_or_owner_with_rbac_enabled(self, config_overrides: Callable[..., None]) -> None:
        account = Account(name="Test User", email="[email protected]")
        account.role = TenantAccountRole.NORMAL

        config_overrides(RBAC_ENABLED=True)
        assert account.is_admin_or_owner

    def test_is_admin_or_owner_with_owner_role(self) -> None:
        """Test is_admin_or_owner property with owner role."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
        )
        account.role = TenantAccountRole.OWNER

        # Act & Assert
        assert account.is_admin_or_owner

    def test_is_admin_or_owner_with_normal_role(self) -> None:
        """Test is_admin_or_owner property with normal role."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
        )
        account.role = TenantAccountRole.NORMAL

        # Act & Assert
        assert not account.is_admin_or_owner

    def test_is_admin_property(self) -> None:
        """Test is_admin property."""
        # Arrange
        admin_account = Account(name="Admin", email="[email protected]")
        admin_account.role = TenantAccountRole.ADMIN

        owner_account = Account(name="Owner", email="[email protected]")
        owner_account.role = TenantAccountRole.OWNER

        assert admin_account.is_admin
        assert not owner_account.is_admin

    def test_is_admin_with_rbac_enabled(self, config_overrides: Callable[..., None]) -> None:
        account = Account(name="Test User", email="[email protected]")
        account.role = TenantAccountRole.NORMAL

        config_overrides(RBAC_ENABLED=True)
        assert account.is_admin

    def test_has_edit_permission_with_editing_roles(self) -> None:
        """Test has_edit_permission property with roles that have edit permission."""
        # Arrange
        roles_with_edit = [
            TenantAccountRole.OWNER,
            TenantAccountRole.ADMIN,
            TenantAccountRole.EDITOR,
        ]

        for role in roles_with_edit:
            account = Account(name="Test User", email=f"test_{role}@example.com")
            account.role = role

            assert account.has_edit_permission, f"Role {role} should have edit permission"

    def test_has_edit_permission_with_rbac_enabled(self, config_overrides: Callable[..., None]) -> None:
        account = Account(name="Test User", email="[email protected]")
        account.role = TenantAccountRole.NORMAL

        config_overrides(RBAC_ENABLED=True)
        assert account.has_edit_permission

    def test_has_edit_permission_without_editing_roles(self) -> None:
        """Test has_edit_permission property with roles that don't have edit permission."""
        # Arrange
        roles_without_edit = [
            TenantAccountRole.NORMAL,
            TenantAccountRole.DATASET_OPERATOR,
        ]

        for role in roles_without_edit:
            account = Account(name="Test User", email=f"test_{role}@example.com")
            account.role = role

            assert not account.has_edit_permission, f"Role {role} should not have edit permission"

    def test_is_dataset_editor_property(self) -> None:
        """Test is_dataset_editor property."""
        # Arrange
        dataset_roles = [
            TenantAccountRole.OWNER,
            TenantAccountRole.ADMIN,
            TenantAccountRole.EDITOR,
            TenantAccountRole.DATASET_OPERATOR,
        ]

        for role in dataset_roles:
            account = Account(name="Test User", email=f"test_{role}@example.com")
            account.role = role

            assert account.is_dataset_editor, f"Role {role} should have dataset edit permission"

        # Test normal role doesn't have dataset edit permission
        normal_account = Account(name="Normal User", email="[email protected]")
        normal_account.role = TenantAccountRole.NORMAL
        assert not normal_account.is_dataset_editor

    def test_is_dataset_editor_with_rbac_enabled(self, config_overrides: Callable[..., None]) -> None:
        account = Account(name="Test User", email="[email protected]")
        account.role = TenantAccountRole.NORMAL

        config_overrides(RBAC_ENABLED=True)
        assert account.is_dataset_editor

    def test_is_dataset_operator_property(self) -> None:
        """Test is_dataset_operator property."""
        # Arrange
        dataset_operator = Account(name="Dataset Operator", email="[email protected]")
        dataset_operator.role = TenantAccountRole.DATASET_OPERATOR

        normal_account = Account(name="Normal User", email="[email protected]")
        normal_account.role = TenantAccountRole.NORMAL

        assert dataset_operator.is_dataset_operator
        assert not normal_account.is_dataset_operator

    def test_is_dataset_operator_with_rbac_enabled(self, config_overrides: Callable[..., None]) -> None:
        account = Account(name="Test User", email="[email protected]")
        account.role = TenantAccountRole.NORMAL

        config_overrides(RBAC_ENABLED=True)
        assert account.is_dataset_operator

    def test_current_role_property(self) -> None:
        """Test current_role property."""
        # Arrange
        account = Account(name="Test User", email="[email protected]")
        account.role = TenantAccountRole.EDITOR

        # Act
        current_role = account.current_role

        # Assert
        assert current_role == TenantAccountRole.EDITOR


class TestTenantAccountJoinModel:
    """Test suite for TenantAccountJoin model."""

    def test_tenant_account_join_creation(self) -> None:
        """Test creating a TenantAccountJoin record."""
        # Arrange
        tenant_id = str(uuid4())
        account_id = str(uuid4())

        # Act
        join = TenantAccountJoin(
            tenant_id=tenant_id,
            account_id=account_id,
            role=TenantAccountRole.NORMAL,
            current=True,
        )

        # Assert
        assert join.tenant_id == tenant_id
        assert join.account_id == account_id
        assert join.role == TenantAccountRole.NORMAL
        assert join.current is True

    def test_tenant_account_join_default_values(self) -> None:
        """Test default values for TenantAccountJoin."""
        # Arrange
        tenant_id = str(uuid4())
        account_id = str(uuid4())

        # Act
        join = TenantAccountJoin(
            tenant_id=tenant_id,
            account_id=account_id,
        )

        # Assert
        assert join.current is False  # Default value
        assert join.role == "normal"  # Default value
        assert join.invited_by is None  # Default value

    def test_tenant_account_join_with_invited_by(self) -> None:
        """Test TenantAccountJoin with invited_by field."""
        # Arrange
        tenant_id = str(uuid4())
        account_id = str(uuid4())
        inviter_id = str(uuid4())

        # Act
        join = TenantAccountJoin(
            tenant_id=tenant_id,
            account_id=account_id,
            role=TenantAccountRole.EDITOR,
            invited_by=inviter_id,
        )

        # Assert
        assert join.invited_by == inviter_id


class TestTenantModel:
    """Test suite for Tenant model."""

    def test_tenant_creation(self) -> None:
        """Test creating a Tenant."""
        # Arrange & Act
        tenant = Tenant(name="Test Workspace")

        # Assert
        assert tenant.name == "Test Workspace"
        assert tenant.status == "normal"  # Default value
        assert tenant.plan == "basic"  # Default value

    def test_tenant_custom_config_dict_property(self) -> None:
        """Test custom_config_dict property getter."""
        # Arrange
        tenant = Tenant(name="Test Workspace")
        tenant.custom_config = '{"remove_webapp_brand": true, "replace_webapp_logo": "value"}'

        # Act
        result = tenant.custom_config_dict

        # Assert
        assert result["remove_webapp_brand"] is True
        assert result["replace_webapp_logo"] == "value"

    def test_tenant_custom_config_dict_property_empty(self) -> None:
        """Test custom_config_dict property with empty config."""
        # Arrange
        tenant = Tenant(name="Test Workspace")
        tenant.custom_config = None

        # Act
        result = tenant.custom_config_dict

        # Assert
        assert result == {}

    def test_tenant_custom_config_dict_setter(self) -> None:
        """Test custom_config_dict property setter."""
        # Arrange
        tenant = Tenant(name="Test Workspace")
        config: TenantCustomConfigDict = {"remove_webapp_brand": True, "replace_webapp_logo": "value"}

        # Act
        tenant.custom_config_dict = config

        # Assert
        assert tenant.custom_config == '{"remove_webapp_brand": true, "replace_webapp_logo": "value"}'


class TestTenantStatusEnum:
    """Test suite for TenantStatus enum."""

    def test_tenant_status_enum_values(self) -> None:
        """Test TenantStatus enum values."""
        # Arrange & Act
        from models.account import TenantStatus

        # Assert
        assert TenantStatus.NORMAL == "normal"
        assert TenantStatus.ARCHIVE == "archive"


class TestAccountIntegration:
    """Integration tests for Account model with related models."""

    def test_account_with_multiple_tenants(self) -> None:
        """Test account associated with multiple tenants."""
        # Arrange
        account = Account(name="Multi-Tenant User", email="[email protected]")
        account.id = str(uuid4())

        tenant1_id = str(uuid4())
        tenant2_id = str(uuid4())

        join1 = TenantAccountJoin(
            tenant_id=tenant1_id,
            account_id=account.id,
            role=TenantAccountRole.OWNER,
            current=True,
        )

        join2 = TenantAccountJoin(
            tenant_id=tenant2_id,
            account_id=account.id,
            role=TenantAccountRole.NORMAL,
            current=False,
        )

        # Assert - verify the joins are created correctly
        assert join1.account_id == account.id
        assert join2.account_id == account.id
        assert join1.current is True
        assert join2.current is False

    def test_account_last_login_tracking(self) -> None:
        """Test account last login tracking."""
        # Arrange
        account = Account(name="Test User", email="[email protected]")
        login_time = datetime.now(UTC)
        login_ip = "192.168.1.1"

        # Act
        account.last_login_at = login_time
        account.last_login_ip = login_ip

        # Assert
        assert account.last_login_at == login_time
        assert account.last_login_ip == login_ip

    def test_account_initialization_tracking(self) -> None:
        """Test account initialization tracking."""
        # Arrange
        account = Account(
            name="Test User",
            email="[email protected]",
            status=AccountStatus.PENDING,
        )

        # Act - simulate initialization
        account.status = AccountStatus.ACTIVE
        account.initialized_at = datetime.now(UTC)

        # Assert
        assert account.get_status() == AccountStatus.ACTIVE
        assert account.initialized_at is not None