Files
WYGIWYH/app/apps/api/tests/test_object_permissions.py
T
Herculino Trotta 4754f7e022 feat(api): serve a native MCP server at /mcp
Off unless MCP_ENABLED=true. Runs inside the web app (stateless Streamable
HTTP, JSON responses), so no extra container or supervisord program.

- Tools run the REST views in-process as the caller, so permissions,
  sharing, validation and transaction rules stay the API's.
- 11 curated tools with compact output, plus search_api/call_api to reach
  every other API endpoint from the OpenAPI schema.
- OAuth protected-resource metadata (RFC 9728) and a WWW-Authenticate hint
  on 401s, so MCP clients find the existing OAuth server and DCR.
- GET /api/transactions/summary/: totals per currency and type, optionally
  grouped by category, tag, entity, account or month.

API changes found while testing it:

- Writes no longer need Django model permissions, which nothing granted;
  access now matches the web UI (signed-in user, scoped querysets, owner-only
  edits of shared objects). API tokens and OAuth apps can now write for
  regular users.
- Account group owner is read-only; it could be set to another user.
- Reject negative installment, recurring, DCA and exchange-rate amounts, a
  zero recurrence interval, and performance on non-investment accounts.
- Accounts can be archived; group_id/exchange_currency_id are optional;
  category accepts null; currency prefix/suffix keep their spaces.
- Transactions filter by tags and entities.
- OpenAPI: separate request schemas (COMPONENT_SPLIT_REQUEST) and real
  descriptions for the viewsets and DCA chart actions.
2026-11-15 16:29:06 -03:00

144 lines
4.8 KiB
Python

"""Object-level ownership on the SharedObject API viewsets.
Any signed-in user may write through the API, and for SharedObject the
viewset's queryset includes other people's public and shared-with-them objects,
so the object-level check is what keeps those read-only.
Reads are unaffected -- they stay governed by SharedObjectManager.
"""
from datetime import date
from decimal import Decimal
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from rest_framework import status
from rest_framework.test import APIClient
from apps.accounts.models import Account
from apps.currencies.models import Currency
from apps.dca.models import DCAEntry, DCAStrategy
from apps.transactions.models import TransactionCategory
@override_settings(
STORAGES={
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
},
},
WHITENOISE_AUTOREFRESH=True,
DEMO=False,
)
class SharedObjectAPIPermissionTests(TestCase):
"""The attacker is an ordinary signed-in user who can see the objects."""
def setUp(self):
User = get_user_model()
self.owner = User.objects.create_user(
email="owner@test.com", password="testpass123"
)
self.attacker = User.objects.create_user(
email="attacker@test.com", password="testpass123"
)
self.currency = Currency.objects.create(
code="USD", name="US Dollar", decimal_places=2
)
self.api = APIClient()
self.api.force_authenticate(user=self.attacker)
def test_cannot_modify_public_account(self):
account = Account.all_objects.create(
name="Public account",
currency=self.currency,
owner=self.owner,
visibility="public",
)
response = self.api.patch(
f"/api/accounts/{account.id}/", {"name": "HIJACKED"}, format="json"
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
account.refresh_from_db()
self.assertEqual(account.name, "Public account")
def test_cannot_delete_account_shared_with_them(self):
account = Account.all_objects.create(
name="Shared account",
currency=self.currency,
owner=self.owner,
visibility="private",
)
account.shared_with.add(self.attacker)
response = self.api.delete(f"/api/accounts/{account.id}/")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertTrue(Account.all_objects.filter(pk=account.pk).exists())
def test_cannot_delete_public_category(self):
category = TransactionCategory.all_objects.create(
name="Public category", owner=self.owner, visibility="public"
)
response = self.api.delete(f"/api/categories/{category.id}/")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertTrue(TransactionCategory.all_objects.filter(pk=category.pk).exists())
def test_cannot_delete_entry_on_public_strategy(self):
strategy = DCAStrategy.all_objects.create(
name="Public strategy",
owner=self.owner,
visibility="public",
target_currency=self.currency,
payment_currency=self.currency,
)
entry = DCAEntry.objects.create(
strategy=strategy,
date=date(2025, 1, 1),
amount_paid=Decimal("100"),
amount_received=Decimal("1"),
)
response = self.api.delete(f"/api/dca/entries/{entry.id}/")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertTrue(DCAEntry.objects.filter(pk=entry.pk).exists())
def test_reads_of_shared_objects_still_work(self):
account = Account.all_objects.create(
name="Public account",
currency=self.currency,
owner=self.owner,
visibility="public",
)
response = self.api.get(f"/api/accounts/{account.id}/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_owner_can_still_write_their_own_objects(self):
owner_client = APIClient()
owner = self.owner
owner_client.force_authenticate(user=owner)
account = Account.all_objects.create(
name="Own account",
currency=self.currency,
owner=owner,
visibility="private",
)
response = owner_client.patch(
f"/api/accounts/{account.id}/", {"name": "Renamed"}, format="json"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
account.refresh_from_db()
self.assertEqual(account.name, "Renamed")