account_batch_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Form::AccountBatch do
  4. let(:account_batch) { described_class.new }
  5. describe '#save' do
  6. subject { account_batch.save }
  7. let(:account) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
  8. let(:account_ids) { [] }
  9. let(:query) { Account.none }
  10. before do
  11. account_batch.assign_attributes(
  12. action: action,
  13. current_account: account,
  14. account_ids: account_ids,
  15. query: query,
  16. select_all_matching: select_all_matching
  17. )
  18. end
  19. context 'when action is "suspend"' do
  20. let(:action) { 'suspend' }
  21. let(:target_account) { Fabricate(:account) }
  22. let(:target_account2) { Fabricate(:account) }
  23. before do
  24. Fabricate(:report, target_account: target_account)
  25. Fabricate(:report, target_account: target_account2)
  26. end
  27. context 'when accounts are passed as account_ids' do
  28. let(:select_all_matching) { '0' }
  29. let(:account_ids) { [target_account.id, target_account2.id] }
  30. it 'suspends the expected users and closes open reports' do
  31. expect { subject }
  32. .to change_account_suspensions
  33. .and change_open_reports_for_accounts
  34. end
  35. end
  36. context 'when accounts are passed as a query' do
  37. let(:select_all_matching) { '1' }
  38. let(:query) { Account.where(id: [target_account.id, target_account2.id]) }
  39. it 'suspends the expected users and closes open reports' do
  40. expect { subject }
  41. .to change_account_suspensions
  42. .and change_open_reports_for_accounts
  43. end
  44. end
  45. private
  46. def change_account_suspensions
  47. change { relevant_account_suspension_statuses }
  48. .from([false, false])
  49. .to([true, true])
  50. end
  51. def change_open_reports_for_accounts
  52. change(relevant_account_unresolved_reports, :count)
  53. .from(2)
  54. .to(0)
  55. end
  56. def relevant_account_unresolved_reports
  57. Report.unresolved.where(target_account: [target_account, target_account2])
  58. end
  59. def relevant_account_suspension_statuses
  60. [target_account.reload, target_account2.reload].map(&:suspended?)
  61. end
  62. end
  63. end
  64. end