Files
WYGIWYH/app/apps/export_app/tests.py
T
Asish Kumar a41b32420a feat(export): back up split expenses
Export and restore left split events out, so moving to a new instance or
restoring a backup lost every event, its people, expenses, payments and
the links to the members' own transactions.

- Export has a "Split Expenses" option, on by default. It writes
  split_events, split_participants, split_expenses, split_expense_shares
  and split_payments. Like the bank sync option, it warns that the file
  carries something sensitive: the events' share links, kept so the links
  people already have keep working after a restore.
- split_transaction_links goes along only when transactions are exported
  too, the same way sub-transactions follow their parents, so a restore
  never points at transactions missing from the backup.
- Restore reads the new files after users, currencies and transactions,
  in dependency order. Everything is matched by id, except the event's
  currency (by name, like DCA strategies) and the linked transaction,
  which is looked up across every user: restore runs as the admin, who
  can't see the other members' transactions.
- Guests' usernames and password hashes are restored, so they can still
  sign in through the share link.
- Attachments stay out, as transaction attachments do: they're files,
  not rows.
2026-12-05 05:46:57 +05:30

401 lines
17 KiB
Python

import io
from datetime import date
import zipfile
from decimal import Decimal
from django.contrib.auth import get_user_model
from django.db import connection
from django.contrib.messages import get_messages
from django.test import TestCase
from django.urls import reverse
from django.test.utils import CaptureQueriesContext
from apps.accounts.models import Account, InvestmentClass
from apps.common.middleware.thread_local import delete_current_user, write_current_user
from apps.currencies.models import Currency
from apps.export_app.resources.accounts import AccountResource, InvestmentClassResource
from apps.export_app.resources.splits import (
SplitEventResource,
SplitExpenseResource,
SplitExpenseShareResource,
SplitParticipantResource,
SplitPaymentResource,
SplitTransactionLinkResource,
)
from apps.export_app.resources.transactions import (
InstallmentPlanResource,
RecurringTransactionResource,
TransactionCategoyResource,
TransactionTagResource,
QuickTransactionResource,
SubTransactionResource,
TransactionEntityResource,
TransactionResource,
)
from apps.export_app.views import process_imports
from apps.splits.models import (
SplitEvent,
SplitExpense,
SplitExpenseShare,
SplitParticipant,
SplitPayment,
SplitTransactionLink,
)
from apps.transactions.models import (
InstallmentPlan,
QuickTransaction,
SubTransaction,
Transaction,
TransactionCategory,
TransactionEntity,
TransactionTag,
)
class InvestmentRestoreTests(TestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(email="u@test.com", password="pw")
write_current_user(self.user)
def tearDown(self):
delete_current_user()
def test_round_trip_keeps_asset_class_and_income_links(self):
brl = Currency.objects.create(code="BRL", name="Real", decimal_places=2)
stocks = InvestmentClass.objects.create(name="Stocks", target=Decimal("60"))
broker = TransactionEntity.objects.create(name="Broker")
account = Account.objects.create(
name="PETR4", currency=brl, is_investment=True, investment_class=stocks
)
account.income_entities.add(broker)
backup = io.BytesIO()
with zipfile.ZipFile(backup, "w") as z:
for resource, name in (
(AccountResource, "accounts"),
(InvestmentClassResource, "investment_classes"),
(TransactionEntityResource, "transactions_entities"),
):
z.writestr(f"{name}.csv", resource().export().csv)
Account.all_objects.all().delete()
InvestmentClass.all_objects.all().delete()
TransactionEntity.all_objects.all().delete()
backup.seek(0)
process_imports(None, {"zip_file": backup})
account = Account.all_objects.get(name="PETR4")
self.assertEqual(account.investment_class.name, "Stocks")
self.assertEqual(account.investment_class.target, Decimal("60"))
self.assertEqual([e.name for e in account.income_entities.all()], ["Broker"])
class SubTransactionBackupTests(TestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(email="s@test.com", password="pw")
write_current_user(self.user)
self.addCleanup(delete_current_user)
account = Account.objects.create(
name="A", currency=Currency.objects.create(code="USD", name="USD", decimal_places=2)
)
self.transaction = Transaction.objects.create(
account=account, type="EX", date=date(2030, 1, 10), amount=5
)
self.plan = InstallmentPlan.objects.create(
account=account, type="EX", description="Laptop", number_of_installments=2,
start_date=date(2030, 1, 10), installment_amount=100,
)
SubTransaction.objects.create(transaction=self.transaction, name="Fee", amount="1.25")
SubTransaction.objects.create(transaction=self.transaction, name="Item", amount="3.75")
SubTransaction.objects.create(installment_plan=self.plan, name="Principal", amount=100)
lunch = QuickTransaction.objects.create(
name="Lunch", account=account, type="EX", amount=Decimal("13.90")
)
SubTransaction.objects.create(quick_transaction=lunch, name="Meal", amount="11.90")
SubTransaction.objects.create(quick_transaction=lunch, name="Tip", amount=2)
def _parts(self):
return sorted(
SubTransaction.objects.values_list(
"name", "transaction", "installment_plan", "quick_transaction__name", "amount"
)
)
def test_round_trip_puts_parts_back_on_their_parents(self):
before = self._parts()
backup = io.BytesIO()
with zipfile.ZipFile(backup, "w") as z:
for resource, name in (
(TransactionResource(), "transactions"),
(InstallmentPlanResource(), "installment_plans"),
(QuickTransactionResource(), "quick_transactions"),
(SubTransactionResource(), "sub_transactions"),
):
z.writestr(f"{name}.csv", resource.export().csv)
SubTransaction.objects.all().delete()
QuickTransaction.all_objects.all().delete()
backup.seek(0)
process_imports(None, {"zip_file": backup})
self.assertEqual(self._parts(), before)
def test_exports_only_the_parts_of_exported_parents(self):
dataset = SubTransactionResource(parents=["installment_plan"]).export()
self.assertEqual([row["name"] for row in dataset.dict], ["Principal"])
class MultiUserRestoreTests(TestCase):
"""Export and restore run as the admin; other users' private names must survive (#691)."""
def setUp(self):
User = get_user_model()
self.admin = User.objects.create_superuser(email="admin@x.com", password="pw")
self.bob = User.objects.create_user(email="bob@x.com", password="pw")
usd = Currency.objects.create(code="USD", name="USD", decimal_places=2)
for user in (self.admin, self.bob):
write_current_user(user)
account = Account.objects.create(name="Checking", currency=usd)
food = TransactionCategory.objects.create(name="Food")
transaction = Transaction.objects.create(
account=account, type="EX", date=date(2030, 1, 1), amount=1,
category=food, description="lunch",
)
transaction.tags.add(TransactionTag.objects.create(name="groceries"))
InstallmentPlan.objects.create(
account=account, type="EX", description="TV", number_of_installments=1,
start_date=date(2030, 1, 1), installment_amount=10, category=food,
)
write_current_user(self.admin)
self.addCleanup(delete_current_user)
def _state(self):
return sorted(
(t.account.owner.email, t.category.owner.email, [g.owner.email for g in t.tags(manager="all_objects").all()])
for t in Transaction.userless_all_objects.all()
) + sorted(
(p.account.owner.email, p.category.owner.email)
for p in InstallmentPlan.all_objects.all()
)
def test_round_trip_keeps_each_users_categories_and_tags(self):
before = self._state()
backup = io.BytesIO()
with zipfile.ZipFile(backup, "w") as z:
for resource, name in (
(TransactionCategoyResource(), "transactions_categories"),
(TransactionTagResource(), "transactions_tags"),
(TransactionResource(), "transactions"),
(InstallmentPlanResource(), "installment_plans"),
):
z.writestr(f"{name}.csv", resource.export().csv)
Transaction.userless_all_objects.all().delete()
InstallmentPlan.all_objects.all().delete()
TransactionCategory.all_objects.all().delete()
TransactionTag.all_objects.all().delete()
backup.seek(0)
process_imports(None, {"zip_file": backup})
self.assertEqual(self._state(), before)
def test_missing_names_are_created_for_the_rows_owner(self):
csv = TransactionResource().export().csv
Transaction.userless_all_objects.all().delete()
InstallmentPlan.all_objects.all().delete()
TransactionCategory.all_objects.all().delete()
TransactionTag.all_objects.all().delete()
process_imports(None, {"transactions": io.BytesIO(csv.encode())})
self.assertEqual(
sorted(TransactionCategory.all_objects.values_list("owner__email", flat=True)),
["admin@x.com", "bob@x.com"],
)
self.assertEqual(
sorted(TransactionTag.all_objects.values_list("owner__email", flat=True)),
["admin@x.com", "bob@x.com"],
)
def test_export_queries_do_not_grow_with_rows(self):
# A few queries per row timed big exports out (#692)
def export_queries():
with CaptureQueriesContext(connection) as queries:
TransactionResource().export()
return len(queries)
few = export_queries()
template = Transaction.userless_all_objects.first()
for _ in range(5):
copy = Transaction.all_objects.create(
account=template.account, type="EX", date=date(2030, 1, 2), amount=1,
category=template.category, owner=template.owner,
)
copy.tags.set(template.tags(manager="all_objects").all())
self.assertEqual(export_queries(), few)
class SplitBackupTests(TestCase):
"""Export and restore run as the admin; split events hold several users' data."""
def setUp(self):
User = get_user_model()
self.admin = User.objects.create_superuser(email="admin@x.com", password="pw")
bob = User.objects.create_user(email="bob@x.com", password="pw")
usd = Currency.objects.create(code="USD", name="US Dollar", decimal_places=2)
write_current_user(bob)
self.addCleanup(delete_current_user)
account = Account.objects.create(name="Checking", currency=usd)
hotel = Transaction.objects.create(
account=account, type="EX", date=date(2030, 1, 5), amount=90,
description="Hotel",
)
event = SplitEvent.objects.create(
name="Beach Trip", currency=usd, creator=bob, is_archived=True
)
bob_p = SplitParticipant.objects.create(event=event, user=bob, name="Bob")
SplitParticipant.objects.create(
event=event, user=self.admin, name="Admin", is_active=False
)
charlie = SplitParticipant.objects.create(
event=event, name="Charlie", is_external=True
)
charlie.set_external_password("secret", username="chaz")
expense = SplitExpense.objects.create(
event=event, paid_by=bob_p, created_by_participant=charlie,
description="Hotel", amount=Decimal("90.00"), date=date(2030, 1, 5),
split_type=SplitExpense.SplitType.SHARES, notes="Two nights",
)
SplitExpenseShare.objects.create(
expense=expense, participant=bob_p, amount=Decimal("60.00"),
percentage=Decimal("66.6667"), shares=Decimal("2"),
)
SplitExpenseShare.objects.create(
expense=expense, participant=charlie, amount=Decimal("30.00"),
percentage=Decimal("33.3333"), shares=Decimal("1"),
)
payment = SplitPayment.objects.create(
event=event, from_participant=charlie, to_participant=bob_p,
amount=Decimal("12.50"), date=date(2030, 1, 6), notes="Part of it",
)
SplitTransactionLink.objects.create(
event=event, user=bob, transaction=hotel,
kind=SplitTransactionLink.Kind.EXPENSE, expense=expense,
)
SplitTransactionLink.objects.create(
event=event, user=bob, transaction=hotel,
kind=SplitTransactionLink.Kind.PAYMENT_IN, payment=payment,
counterparty=charlie,
)
# Restore runs as the admin, who can't see Bob's transactions
write_current_user(self.admin)
def _state(self):
return [
sorted(SplitEvent.objects.values_list(
"name", "currency__name", "creator__email", "share_token",
"allow_external_access", "is_archived",
)),
sorted(SplitParticipant.objects.values_list(
"name", "user__email", "is_external", "is_active",
"external_username", "external_password_hash",
)),
sorted(SplitExpense.objects.values_list(
"description", "paid_by__name", "created_by_participant__name",
"amount", "date", "split_type", "notes",
)),
sorted(SplitExpenseShare.objects.values_list(
"expense__description", "participant__name", "amount", "percentage", "shares"
)),
sorted(SplitPayment.objects.values_list(
"from_participant__name", "to_participant__name", "amount", "date", "notes"
)),
sorted(SplitTransactionLink.objects.values_list(
"user__email", "transaction__description", "kind",
"expense__description", "payment__notes", "counterparty__name",
)),
]
def test_round_trip_restores_events_and_links_to_each_users_transactions(self):
before = self._state()
backup = io.BytesIO()
with zipfile.ZipFile(backup, "w") as z:
for resource, name in (
(TransactionResource(), "transactions"),
(SplitEventResource(), "split_events"),
(SplitParticipantResource(), "split_participants"),
(SplitExpenseResource(), "split_expenses"),
(SplitExpenseShareResource(), "split_expense_shares"),
(SplitPaymentResource(), "split_payments"),
(SplitTransactionLinkResource(), "split_transaction_links"),
):
z.writestr(f"{name}.csv", resource.export().csv)
SplitExpense.objects.all().delete()
SplitPayment.objects.all().delete()
SplitEvent.objects.all().delete()
Transaction.userless_all_objects.all().delete()
backup.seek(0)
process_imports(None, {"zip_file": backup})
self.assertEqual(self._state(), before)
# The share link and the guest's password keep working
charlie = SplitParticipant.objects.get(name="Charlie")
self.assertTrue(charlie.check_external_password("secret"))
self.assertIsNotNone(charlie.external_claimed_at)
def _exported_files(self, data):
self.client.force_login(self.admin)
response = self.client.post(reverse("export_form"), data, HTTP_HX_REQUEST="true")
with zipfile.ZipFile(io.BytesIO(response.content)) as z:
return set(z.namelist())
def test_export_includes_split_expenses(self):
files = self._exported_files({"splits": "on", "transactions": "on"})
self.assertLessEqual(
{
"split_events.csv",
"split_participants.csv",
"split_expenses.csv",
"split_expense_shares.csv",
"split_payments.csv",
"split_transaction_links.csv",
},
files,
)
def test_transaction_links_are_left_out_without_transactions(self):
files = self._exported_files({"splits": "on"})
self.assertIn("split_events.csv", files)
self.assertNotIn("split_transaction_links.csv", files)
class ExportViewTests(TestCase):
def setUp(self):
self.client.force_login(
get_user_model().objects.create_superuser(email="a@test.com", password="pw")
)
def test_nothing_selected_warns_instead_of_downloading(self):
response = self.client.post(reverse("export_form"), {}, HTTP_HX_REQUEST="true")
self.assertEqual(response.status_code, 204)
self.assertEqual(
[m.level_tag for m in get_messages(response.wsgi_request)], ["warning"]
)
def test_non_superuser_is_refused_not_sent_back_to_login(self):
# The login page sends signed-in users back to ?next=, so redirecting there looped
self.client.force_login(
get_user_model().objects.create_user(email="b@test.com", password="pw")
)
self.assertEqual(self.client.get(reverse("export_index")).status_code, 403)
def test_restore_without_files_shows_the_error_in_the_form(self):
response = self.client.post(reverse("restore_form"), {}, HTTP_HX_REQUEST="true")
self.assertContains(response, "Please upload either a ZIP file or at least one CSV file")
self.assertContains(response, 'id="restore-form"')