mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-05 21:51:23 +02:00
The Finance page was the only surface in Bambuddy that read its currency from a data row rather than the `currency` setting, and it fell back to EUR where every other page falls back to USD. One variable drives every amount on that page, so the personal balance, the cost-center budgets and the whole transaction list were wrong together on any install not set to euros. It now takes the configured currency from /settings/ui-flags, which is readable by anyone who can see Finance -- /settings needs SETTINGS_READ, which a cost_centers:read_own user does not have. The backend was the other half. Of the four places that settle on a currency, three wrote a hardcoded "EUR": the wallet the API mints on demand, the wallet a print charge mints when none exists, and the balance returned for a user with no wallet row at all. All four now go through one resolver, which lives beside the rest of the balance logic. The wallet's currency column is removed outright rather than merely ignored. An install has one currency and nothing here converts between them, so a per-wallet copy could only ever drift from the setting -- and a column nothing reads is a trap for whoever finds it next. A startup migration drops it on both SQLite and PostgreSQL, after the raw CREATE TABLE that would otherwise re-add it on an install whose finance tables predate the ORM. SQLite builds older than 3.35 have no DROP COLUMN and keep it, harmlessly, since it has a default and no reader. Saving settings now invalidates the ui-flags query too. Nothing did, so a changed currency sat behind that query's staleTime before showing up. The sponsor prompt's own EUR fallback is now USD, matching AppSettings.
40 lines
1.6 KiB
Python
40 lines
1.6 KiB
Python
"""Unit tests for how a balance is reported."""
|
|
|
|
import pytest
|
|
|
|
from backend.app.models.settings import Settings
|
|
from backend.app.schemas.settings import AppSettings as AppSettingsSchema
|
|
from backend.app.services.finance_balance import resolve_configured_currency
|
|
|
|
|
|
class TestConfiguredCurrency:
|
|
"""#3123: finance is not allowed its own idea of the currency.
|
|
|
|
Every other surface renders the ``currency`` app setting. Finance answered
|
|
from a per-wallet column instead, which three of its four writers filled
|
|
with a hardcoded "EUR", so an install configured for AUD reported euros.
|
|
The column is gone and this function is what replaced it.
|
|
"""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reads_the_configured_currency(self, db_session):
|
|
db_session.add(Settings(key="currency", value="AUD"))
|
|
await db_session.commit()
|
|
|
|
assert await resolve_configured_currency(db_session) == "AUD"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_falls_back_to_the_app_default_when_unset(self, db_session):
|
|
# The app default is USD, which is also what every frontend fallback
|
|
# uses. The old finance fallback said EUR, which is how an install
|
|
# that never touched the setting still showed euros.
|
|
assert await resolve_configured_currency(db_session) == AppSettingsSchema().currency
|
|
assert await resolve_configured_currency(db_session) == "USD"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_an_empty_setting_row_is_not_a_currency(self, db_session):
|
|
db_session.add(Settings(key="currency", value=""))
|
|
await db_session.commit()
|
|
|
|
assert await resolve_configured_currency(db_session) == "USD"
|