Files
sure/test/controllers/settings/preferences_controller_test.rb
T
e5750a6c09 feat(budgets): add per-user personal budgets with strict isolation (#2891)
* feat(budgets): add per-user personal budgets with strict isolation

Families can now opt into personal budgets (toggleable via family
settings): each family member gets their own budget for a given
period instead of sharing a single family-wide budget.

- Add families.personal_budgets flag and budgets.user_id, with
  partial unique indexes so shared budgets (user_id IS NULL) and
  personal budgets (user_id IS NOT NULL) can't collide.
- Budget.find_or_bootstrap scopes lookup/creation by user when the
  family has personal_budgets enabled.
- Scope most_recent_initialized_budget (used to seed a new budget
  from the prior period) by user_id so one user's copy-forward never
  bleeds into another user's budget.
- budgets.user_id cascades on user deletion so personal budgets don't
  outlive their owner.

* feat(budgets): enforce user-specific budget ownership and cascade deletion

* feat(budgets): display user name for personal budgets in budget card on the plan section

* feat(budgets): enhance personal budgets display for admins with preview feature indication

* feat(budgets): enforce user-specific budget and category visibility for personal budgets

* feat(budgets): create budget section titles and add translations notice in preferences

* feat(budgets): let household and personal budgets coexist with sharing

Previously enabling personal_budgets made the shared household budget
unreachable. Budget.find_or_bootstrap now takes an explicit household:
flag so both can be resolved independently for the same period, with a
new household_budget_enabled family setting to opt out of the household
side and keep personal budgets only.

Adds a BudgetShare model (read_only/read_write) so a member can grant
another family member access to their personal budget, enforced via
Budget#viewable_by?/editable_by? across BudgetsController,
BudgetCategoriesController, PlansController, and the read-only API.
Preferences gains a Budget sharing card (gated on preview access like
the rest of the personal budgets UI) and an owner switcher pill (
Household / mine / shared-with-me) appears on the budget page and the
Plan hub card.

Also fixes personal budgets showing the same "actual spending" as the
household budget: actual spending/income now scope to the budget
owner's own accounts instead of the viewer's full accessible set,
via a new accounts: override on IncomeStatement.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat(budgets): enhance budget switcher with icons and improved styling

* feat(budgets): remove user name display from budget card and header

* feat(budgets): remove unique index on taggable_type and taggable_id in taggings

* feat(budgets): enhance budget sharing functionality and improve UI elements

* Collapse personal budget migrations

---------

Signed-off-by: JulienGourmet <69808509+jubbakka@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: sure-admin <sure-admin@splashblot.com>
2026-08-18 08:36:46 +02:00

83 lines
2.8 KiB
Ruby

require "test_helper"
class Settings::PreferencesControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in users(:family_admin)
end
test "get" do
get settings_preferences_url
assert_response :success
end
test "group moniker uses group currencies copy and hides legacy currency field" do
users(:family_admin).family.update!(moniker: "Group")
get settings_preferences_url
assert_response :success
assert_includes response.body, "Group Currencies"
assert_includes response.body, "your group"
assert_select "select[name='user[family_attributes][currency]']", count: 0
end
test "renders preview features toggle for non-admin users too" do
sign_in users(:family_member)
get settings_preferences_url
assert_response :success
assert_includes response.body, "Enable preview features"
end
test "update toggles preview_features_enabled on" do
user = users(:family_admin)
assert_not user.preview_features_enabled?
patch settings_preferences_url, params: { user: { preview_features_enabled: "1" } }
assert_redirected_to settings_preferences_url
assert user.reload.preview_features_enabled?
end
test "update toggles preview_features_enabled off" do
user = users(:family_admin)
user.update!(preferences: (user.preferences || {}).merge("preview_features_enabled" => true))
assert user.preview_features_enabled?
patch settings_preferences_url, params: { user: { preview_features_enabled: "0" } }
assert_redirected_to settings_preferences_url
assert_not user.reload.preview_features_enabled?
end
test "household budget toggle and sharing card only render once personal_budgets is on" do
user = users(:family_admin)
user.update!(preferences: (user.preferences || {}).merge("preview_features_enabled" => true))
get settings_preferences_url
assert_response :success
assert_not_includes response.body, I18n.t("settings.preferences.show.household_budget_enabled")
assert_not_includes response.body, I18n.t("settings.preferences.show.budget_sharing_title")
user.family.update!(personal_budgets: true)
get settings_preferences_url
assert_response :success
assert_includes response.body, I18n.t("settings.preferences.show.household_budget_enabled")
assert_includes response.body, I18n.t("settings.preferences.show.budget_sharing_title")
end
test "hides the sharing card when personal_budgets is on but preview features are off" do
user = users(:family_admin)
user.family.update!(personal_budgets: true)
assert_not user.preview_features_enabled?
get settings_preferences_url
assert_response :success
assert_not_includes response.body, I18n.t("settings.preferences.show.household_budget_enabled")
assert_not_includes response.body, I18n.t("settings.preferences.show.budget_sharing_title")
end
end