Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

23.1 KB
Raw Blame
import uuid

import pytest

from httpx import AsyncClient
from sqlalchemy import delete
from sqlalchemy.ext.asyncio import AsyncSession

from app.models.app_settings import AppSetting
from app.models.user import User
from app.core.auth import get_jwt_strategy
from app.core.config import get_settings


pytestmark = pytest.mark.asyncio


class TestAdminUserCRUD:
    """Test admin user CRUD operations."""

    async def test_list_users_as_admin(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.get("/api/admin/users", headers=admin_auth_headers)
        assert response.status_code == 200
        data = response.json()
        assert "items" in data
        assert "total" in data
        assert data["total"] >= 1
        emails = [u["email"] for u in data["items"]]
        assert "[email protected]" in emails

    async def test_list_users_search(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.get(
            "/api/admin/users", params={"search": "admin"}, headers=admin_auth_headers
        )
        assert response.status_code == 200
        data = response.json()
        assert all("admin" in u["email"] for u in data["items"])

    async def test_create_user(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.post(
            "/api/admin/users",
            json={
                "email": "[email protected]",
                "password": "password123",
                "is_superuser": False,
                "preferences": {"language": "en", "currency_display": "USD"},
            },
            headers=admin_auth_headers,
        )
        assert response.status_code == 201
        data = response.json()
        assert data["email"] == "[email protected]"
        assert data["is_active"] is True
        assert data["is_superuser"] is False

    async def test_create_user_forbidden_when_local_auth_disabled(
        self,
        client: AsyncClient,
        test_superuser: User,
        oidc_only_settings,
    ):
        token = await get_jwt_strategy().write_token(test_superuser)
        response = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers={"Authorization": f"Bearer {token}"},
        )

        assert response.status_code == 403
        assert response.json()["detail"] == "LOCAL_AUTH_DISABLED"

    async def test_admin_password_update_forbidden_but_profile_update_allowed(
        self,
        client: AsyncClient,
        test_superuser: User,
        oidc_only_settings,
    ):
        headers = {
            "Authorization": f"Bearer {await get_jwt_strategy().write_token(test_superuser)}"
        }
        password_response = await client.patch(
            f"/api/admin/users/{test_superuser.id}",
            json={"password": "newpassword456"},
            headers=headers,
        )
        profile_response = await client.patch(
            f"/api/admin/users/{test_superuser.id}",
            json={"preferences": {"language": "de"}},
            headers=headers,
        )

        assert password_response.status_code == 403
        assert password_response.json()["detail"] == "LOCAL_AUTH_DISABLED"
        assert profile_response.status_code == 200
        assert profile_response.json()["preferences"]["language"] == "de"

    async def test_create_user_seeds_defaults(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User
    ):
        # Regression for #154: admin-created users were left without default
        # categories, rules, or a wallet because on_after_register bails when
        # called programmatically (request=None).
        create_resp = await client.post(
            "/api/admin/users",
            json={
                "email": "[email protected]",
                "password": "password123",
                "preferences": {"language": "en", "currency_display": "USD"},
            },
            headers=admin_auth_headers,
        )
        assert create_resp.status_code == 201

        login_resp = await client.post(
            "/api/auth/login",
            data={"username": "[email protected]", "password": "password123"},
        )
        token = login_resp.json()["access_token"]
        user_headers = {"Authorization": f"Bearer {token}"}

        cats = (await client.get("/api/categories", headers=user_headers)).json()
        rules = (await client.get("/api/rules", headers=user_headers)).json()
        accounts = (await client.get("/api/accounts", headers=user_headers)).json()

        assert len(cats) > 0, "admin-created user should have default categories"
        assert len(rules) > 0, "admin-created user should have default rules"
        assert len(accounts) == 1, "admin-created user should have a default wallet"

    async def test_create_duplicate_user(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        # Create first
        await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers=admin_auth_headers,
        )
        # Try duplicate
        response = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 400

    async def test_get_user(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.get(
            f"/api/admin/users/{test_superuser.id}", headers=admin_auth_headers
        )
        assert response.status_code == 200
        assert response.json()["email"] == "[email protected]"

    async def test_get_user_not_found(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        fake_id = uuid.uuid4()
        response = await client.get(
            f"/api/admin/users/{fake_id}", headers=admin_auth_headers
        )
        assert response.status_code == 404

    async def test_update_user(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        # Create a user to update
        create_resp = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers=admin_auth_headers,
        )
        user_id = create_resp.json()["id"]

        response = await client.patch(
            f"/api/admin/users/{user_id}",
            json={"is_active": False},
            headers=admin_auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["is_active"] is False

    async def test_delete_user(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        # Create a user to delete
        create_resp = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers=admin_auth_headers,
        )
        user_id = create_resp.json()["id"]

        response = await client.delete(
            f"/api/admin/users/{user_id}", headers=admin_auth_headers
        )
        assert response.status_code == 204

        # Verify deleted
        get_resp = await client.get(
            f"/api/admin/users/{user_id}", headers=admin_auth_headers
        )
        assert get_resp.status_code == 404

    async def test_update_user_email_conflict(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        # Create two users
        resp_a = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers=admin_auth_headers,
        )
        await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers=admin_auth_headers,
        )
        user_a_id = resp_a.json()["id"]

        # Try to update user A's email to user B's email
        response = await client.patch(
            f"/api/admin/users/{user_a_id}",
            json={"email": "[email protected]"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 400
        assert "email already exists" in response.json()["detail"].lower()

    async def test_update_user_password(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        # Create a user
        create_resp = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "oldpassword123"},
            headers=admin_auth_headers,
        )
        user_id = create_resp.json()["id"]

        # Update the password
        response = await client.patch(
            f"/api/admin/users/{user_id}",
            json={"password": "newpassword456"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 200

        # Login with the new password
        login_resp = await client.post(
            "/api/auth/login",
            data={"username": "[email protected]", "password": "newpassword456"},
            headers={"Content-Type": "application/x-www-form-urlencoded"},
        )
        assert login_resp.status_code == 200
        assert "access_token" in login_resp.json()

    async def test_update_user_password_too_short(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        # Create a user
        create_resp = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers=admin_auth_headers,
        )
        user_id = create_resp.json()["id"]

        # Try to update with a too-short password
        response = await client.patch(
            f"/api/admin/users/{user_id}",
            json={"password": "short"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 422


class TestAdminProtections:
    """Test access control and self-protection."""

    async def test_non_admin_gets_403(self, client: AsyncClient, auth_headers: dict, test_user: User):
        response = await client.get("/api/admin/users", headers=auth_headers)
        assert response.status_code == 403

    async def test_unauthenticated_gets_401(self, client: AsyncClient):
        response = await client.get("/api/admin/users")
        assert response.status_code == 401

    async def test_cannot_delete_self(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.delete(
            f"/api/admin/users/{test_superuser.id}", headers=admin_auth_headers
        )
        assert response.status_code == 400
        assert "own account" in response.json()["detail"].lower()

    async def test_cannot_demote_self(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.patch(
            f"/api/admin/users/{test_superuser.id}",
            json={"is_superuser": False},
            headers=admin_auth_headers,
        )
        assert response.status_code == 400
        assert "own admin" in response.json()["detail"].lower()

    async def test_cannot_deactivate_self(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.patch(
            f"/api/admin/users/{test_superuser.id}",
            json={"is_active": False},
            headers=admin_auth_headers,
        )
        assert response.status_code == 400
        assert "own account" in response.json()["detail"].lower()

    async def test_cannot_delete_last_superuser(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User
    ):
        # Create a second superuser and login as them
        create_resp = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "adminpass456", "is_superuser": True},
            headers=admin_auth_headers,
        )
        assert create_resp.status_code == 201

        # Login as admin2
        login_resp = await client.post(
            "/api/auth/login",
            data={"username": "[email protected]", "password": "adminpass456"},
            headers={"Content-Type": "application/x-www-form-urlencoded"},
        )
        assert login_resp.status_code == 200
        admin2_headers = {"Authorization": f"Bearer {login_resp.json()['access_token']}"}

        # admin2 deletes admin1 — should succeed because admin2 still exists
        response = await client.delete(
            f"/api/admin/users/{test_superuser.id}", headers=admin2_headers
        )
        assert response.status_code == 204

        # Now create a non-admin user
        create_resp2 = await client.post(
            "/api/admin/users",
            json={"email": "[email protected]", "password": "password123"},
            headers=admin2_headers,
        )
        assert create_resp2.status_code == 201
        assert create_resp2.json()["id"]

        # admin2 is now the only superuser — trying to delete them from another session
        # would fail with self-protection. Instead, test that deleting regular user works
        # but the last-superuser logic is intact by checking the count.
        # We can't easily test deleting the last superuser without self-protection kicking in,
        # but we verified above that deleting a superuser when another exists succeeds.


class TestRegistrationToggle:
    """Test the registration toggle functionality."""

    async def test_registration_status_public(self, client: AsyncClient):
        """Registration status endpoint should work without auth."""
        response = await client.get("/api/admin/registration-status")
        assert response.status_code == 200
        assert "enabled" in response.json()

    async def test_env_var_closes_registration_when_nothing_is_saved(
        self, client: AsyncClient, session: AsyncSession, monkeypatch
    ):
        # The database is shared by the worker, and another test may have
        # saved the toggle: start from nothing saved.
        await session.execute(delete(AppSetting).where(AppSetting.key == "registration_enabled"))
        await session.commit()
        monkeypatch.setattr(get_settings(), "registration_enabled", False)

        status_response = await client.get("/api/admin/registration-status")
        assert status_response.json()["enabled"] is False

        reg_response = await client.post(
            "/api/auth/register",
            json={"email": "[email protected]", "password": "password123"},
        )
        assert reg_response.status_code == 403

    async def test_disable_registration(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User, session: AsyncSession
    ):
        # Seed the setting if not exists
        setting = AppSetting(key="registration_enabled", value="true")
        session.add(setting)
        await session.commit()

        # Disable registration
        response = await client.patch(
            "/api/admin/settings/registration_enabled",
            json={"value": "false"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["value"] == "false"

        # Verify registration is blocked
        reg_response = await client.post(
            "/api/auth/register",
            json={"email": "[email protected]", "password": "password123"},
        )
        assert reg_response.status_code == 403

        # Re-enable for other tests
        await client.patch(
            "/api/admin/settings/registration_enabled",
            json={"value": "true"},
            headers=admin_auth_headers,
        )

    async def test_enable_registration(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User, session: AsyncSession
    ):
        # Ensure enabled
        setting = AppSetting(key="registration_enabled", value="true")
        await session.merge(setting)
        await session.commit()

        response = await client.get("/api/admin/registration-status")
        assert response.status_code == 200
        assert response.json()["enabled"] is True

    async def test_setting_not_configurable(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.patch(
            "/api/admin/settings/some_random_key",
            json={"value": "test"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 400

    async def test_get_setting(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User, session: AsyncSession
    ):
        setting = AppSetting(key="registration_enabled", value="true")
        await session.merge(setting)
        await session.commit()

        response = await client.get(
            "/api/admin/settings/registration_enabled", headers=admin_auth_headers
        )
        assert response.status_code == 200
        assert response.json()["key"] == "registration_enabled"

    async def test_get_setting_not_found(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.get(
            "/api/admin/settings/nonexistent", headers=admin_auth_headers
        )
        assert response.status_code == 404

    async def test_setting_invalid_value(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.patch(
            "/api/admin/settings/registration_enabled",
            json={"value": "maybe"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 400
        assert "invalid value" in response.json()["detail"].lower()


class TestUseProviderCategoriesToggle:
    """Admin-scope toggle that controls whether sync auto-maps provider
    (e.g. Pluggy) categories onto the user's seeded categories. Default is
    on; flipping off makes synced transactions arrive uncategorized so user
    Rules are the only source of truth."""

    async def test_get_unset_returns_404(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User
    ):
        # Defaults are not seeded as DB rows — they're returned by the helper.
        # The raw GET /settings/{key} endpoint reflects DB state so an unset
        # key is a 404. The frontend handles this and falls back to "true".
        response = await client.get(
            "/api/admin/settings/use_provider_categories", headers=admin_auth_headers
        )
        assert response.status_code == 404

    async def test_patch_to_false_persists(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User
    ):
        response = await client.patch(
            "/api/admin/settings/use_provider_categories",
            json={"value": "false"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["key"] == "use_provider_categories"
        assert response.json()["value"] == "false"

        # Round-trip
        get_resp = await client.get(
            "/api/admin/settings/use_provider_categories", headers=admin_auth_headers
        )
        assert get_resp.status_code == 200
        assert get_resp.json()["value"] == "false"

    async def test_patch_invalid_value_rejected(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User
    ):
        response = await client.patch(
            "/api/admin/settings/use_provider_categories",
            json={"value": "maybe"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 400
        # Allowed values appear in the error message so callers can self-correct
        body = response.json()["detail"].lower()
        assert "true" in body and "false" in body

    async def test_patch_back_to_true(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User
    ):
        # Disable first, then re-enable
        await client.patch(
            "/api/admin/settings/use_provider_categories",
            json={"value": "false"},
            headers=admin_auth_headers,
        )
        response = await client.patch(
            "/api/admin/settings/use_provider_categories",
            json={"value": "true"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["value"] == "true"

    async def test_non_admin_cannot_read(
        self, client: AsyncClient, auth_headers: dict, test_user: User, session: AsyncSession
    ):
        # Seed so a 200 would be possible if RBAC were broken
        setting = AppSetting(key="use_provider_categories", value="true")
        await session.merge(setting)
        await session.commit()
        response = await client.get(
            "/api/admin/settings/use_provider_categories", headers=auth_headers
        )
        assert response.status_code == 403

    async def test_non_admin_cannot_write(
        self, client: AsyncClient, auth_headers: dict, test_user: User
    ):
        response = await client.patch(
            "/api/admin/settings/use_provider_categories",
            json={"value": "false"},
            headers=auth_headers,
        )
        assert response.status_code == 403


class TestThemeSettings:
    """Test theme color settings and the public default-colors endpoint."""

    async def test_get_default_colors_empty(self, client: AsyncClient, clean_db):
        response = await client.get("/api/admin/default-colors")
        assert response.status_code == 200
        assert response.json() == {"light": None, "dark": None}

    async def test_get_default_colors_populated(self, client: AsyncClient, session: AsyncSession, clean_db):
        session.add_all([
            AppSetting(key="theme_color_light", value="#FFFFFF"),
            AppSetting(key="theme_color_dark", value="#000000"),
        ])
        await session.commit()

        response = await client.get("/api/admin/default-colors")
        assert response.status_code == 200
        assert response.json() == {"light": "#FFFFFF", "dark": "#000000"}

    async def test_update_theme_color_valid(self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User):
        response = await client.patch(
            "/api/admin/settings/theme_color_light",
            json={"value": "#6366F1"},
            headers=admin_auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["value"] == "#6366F1"

    @pytest.mark.parametrize("invalid_color", [
        "6366F1",      # missing #
        "#6366F",       # too short
        "#6366F11",     # too long
        "#GGGGGG",      # invalid hex chars
        "red",          # named color
        "#123",         # 3-digit hex (not allowed by regex)
    ])
    async def test_update_theme_color_invalid(
        self, client: AsyncClient, admin_auth_headers: dict, test_superuser: User, invalid_color: str
    ):
        response = await client.patch(
            "/api/admin/settings/theme_color_light",
            json={"value": invalid_color},
            headers=admin_auth_headers,
        )
        assert response.status_code == 400
        assert "invalid hex color code" in response.json()["detail"].lower()