The personal finance app for everyone (by everyone)
Ruby
Scope group sparklines to accounts the user can access (#4207)
GFR34295f3cd241
diff --git a/app/controllers/accountable_sparklines_controller.rb b/app/controllers/accountable_sparklines_controller.rbindex a58e804f6a..9b67ba4656 100644--- a/app/controllers/accountable_sparklines_controller.rb+++ b/app/controllers/accountable_sparklines_controller.rb@@ -19,8 +19,10 @@ class AccountableSparklinesController < ApplicationController@family ||= Current.familyend+ # Scope to the accounts the current user can access (owned or shared),+ # matching the account list and group total rendered in the sidebar.def account_scope- @account_scope ||= family.accounts.visible.where(accountable_type: @accountable.name)+ @account_scope ||= Current.accessible_accounts.visible.where(accountable_type: @accountable.name)enddef account_ids@@ -67,7 +69,19 @@ class AccountableSparklinesController < ApplicationController)end+ # The series is built from exactly these account rows, so the key carries a+ # digest of them. A share count/timestamp version can stay the same while+ # access changes (ownership moved by update_all, shares replaced with older+ # timestamps); the row digest changes whenever the accessible set does and+ # never repeats for a different set.def cache_key- family.build_cache_key("#{@accountable.name}_sparkline_#{Account::Chartable::SPARKLINE_CACHE_VERSION}", invalidate_on_data_updates: true)+ family.build_cache_key(+ "#{@accountable.name}_sparkline_#{Account::Chartable::SPARKLINE_CACHE_VERSION}_#{account_rows_digest}",+ invalidate_on_data_updates: true+ )+ end++ def account_rows_digest+ Digest::SHA256.hexdigest(account_identity_rows.map { |row| row.join(":") }.sort.join(","))endenddiff --git a/test/controllers/accountable_sparklines_controller_test.rb b/test/controllers/accountable_sparklines_controller_test.rbindex 4dfd678341..a998027be6 100644--- a/test/controllers/accountable_sparklines_controller_test.rb+++ b/test/controllers/accountable_sparklines_controller_test.rb@@ -26,6 +26,117 @@ class AccountableSparklinesControllerTest < ActionDispatch::IntegrationTestassert_select "p.font-mono", count: 0end+ test "group sparkline only includes accounts the user can access" do+ member = users(:family_member)+ sign_in member+ Rails.cache.clear++ requested_account_ids = nil+ Balance::ChartSeriesBuilder.expects(:new).with do |args|+ requested_account_ids = args[:account_ids]+ end.returns(stub(balance_series: empty_series))++ get accountable_sparkline_url("depository")++ assert_response :success+ assert_includes requested_account_ids, accounts(:depository).id+ assert_not_includes requested_account_ids, accounts(:connected).id+ assert_equal member.accessible_accounts.visible.where(accountable_type: "Depository").pluck(:id).sort,+ requested_account_ids.sort+ end++ test "group sparkline cache is not shared between users with different access" do+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)++ get accountable_sparkline_url("depository")+ assert_response :success++ sign_in users(:family_member)++ requested_account_ids = nil+ Balance::ChartSeriesBuilder.expects(:new).with do |args|+ requested_account_ids = args[:account_ids]+ end.returns(stub(balance_series: empty_series))++ get accountable_sparkline_url("depository")++ assert_response :success+ assert_not_nil requested_account_ids, "member must not be served the admin's cached series"+ assert_not_includes requested_account_ids, accounts(:connected).id+ end++ test "group sparkline cache is invalidated when account sharing changes" do+ member = users(:family_member)+ sign_in member+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)++ get accountable_sparkline_url("depository")+ assert_response :success++ AccountShare.create!(account: accounts(:connected), user: member, permission: "read_only", include_in_finances: true)++ requested_account_ids = nil+ Balance::ChartSeriesBuilder.expects(:new).with do |args|+ requested_account_ids = args[:account_ids]+ end.returns(stub(balance_series: empty_series))++ get accountable_sparkline_url("depository")++ assert_response :success+ assert_includes requested_account_ids, accounts(:connected).id+ end++ test "group sparkline cache is invalidated when account ownership moves without a share change" do+ member = users(:family_member)+ sign_in member+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)++ get accountable_sparkline_url("depository")+ assert_response :success++ # Same write as User#reassign_owned_accounts!: no share row and no account+ # timestamp changes, but the member now owns (and can see) the account.+ Account.where(id: accounts(:connected).id).update_all(owner_id: member.id)++ requested_account_ids = nil+ Balance::ChartSeriesBuilder.expects(:new).with do |args|+ requested_account_ids = args[:account_ids]+ end.returns(stub(balance_series: empty_series))++ get accountable_sparkline_url("depository")++ assert_response :success+ assert_includes requested_account_ids, accounts(:connected).id+ end++ test "group sparkline cache is invalidated when a share is replaced without a newer timestamp" do+ member = users(:family_member)+ sign_in member+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ shared_at = 1.hour.ago+ AccountShare.where(user: member).update_all(updated_at: shared_at)++ get accountable_sparkline_url("depository")+ assert_response :success++ # Share count and newest updated_at stay the same, the accessible set does not.+ AccountShare.where(user: member, account: accounts(:credit_card)).delete_all+ AccountShare.insert_all([+ { account_id: accounts(:connected).id, user_id: member.id, permission: "read_only",+ include_in_finances: true, created_at: shared_at, updated_at: shared_at }+ ])++ requested_account_ids = nil+ Balance::ChartSeriesBuilder.expects(:new).with do |args|+ requested_account_ids = args[:account_ids]+ end.returns(stub(balance_series: empty_series))++ get accountable_sparkline_url("depository")++ assert_response :success+ assert_includes requested_account_ids, accounts(:connected).id+ end+test "linked investment sparkline does not load full account records" doAccountProvider.create!(account: accounts(:investment),@@ -46,6 +157,10 @@ class AccountableSparklinesControllerTest < ActionDispatch::IntegrationTestendprivate+ def empty_series+ Series.new(start_date: 1.day.ago.to_date, end_date: Date.current, interval: "1 day", values: [])+ end+def capture_sql_queriesqueries = []callback = lambda do |_name, _started, _finished, _unique_id, payload|