account_action_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::AccountAction do
  4. let(:account_action) { described_class.new }
  5. describe '#save!' do
  6. subject { account_action.save! }
  7. let(:account) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
  8. let(:target_account) { Fabricate(:account) }
  9. let(:type) { 'disable' }
  10. before do
  11. account_action.assign_attributes(
  12. type: type,
  13. current_account: account,
  14. target_account: target_account
  15. )
  16. end
  17. context 'when type is "disable"' do
  18. let(:type) { 'disable' }
  19. it 'disable user' do
  20. subject
  21. expect(target_account.user).to be_disabled
  22. end
  23. end
  24. context 'when type is "silence"' do
  25. let(:type) { 'silence' }
  26. it 'silences account' do
  27. subject
  28. expect(target_account).to be_silenced
  29. end
  30. end
  31. context 'when type is "suspend"' do
  32. let(:type) { 'suspend' }
  33. it 'suspends account' do
  34. subject
  35. expect(target_account).to be_suspended
  36. end
  37. it 'queues Admin::SuspensionWorker by 1' do
  38. expect do
  39. subject
  40. end.to change { Admin::SuspensionWorker.jobs.size }.by 1
  41. end
  42. end
  43. context 'when type is invalid' do
  44. let(:type) { 'whatever' }
  45. it 'raises an invalid record error' do
  46. expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
  47. end
  48. end
  49. context 'when type is not given' do
  50. let(:type) { '' }
  51. it 'raises an invalid record error' do
  52. expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
  53. end
  54. end
  55. it 'sends email to target account user', :inline_jobs do
  56. emails = capture_emails { subject }
  57. expect(emails).to contain_exactly(
  58. have_attributes(
  59. to: contain_exactly(target_account.user.email)
  60. )
  61. )
  62. end
  63. it 'sends notification, log the action, and closes other reports', :aggregate_failures do
  64. other_report = Fabricate(:report, target_account: target_account)
  65. expect { subject }
  66. .to (change(Admin::ActionLog.where(action: type), :count).by 1)
  67. .and(change { other_report.reload.action_taken? }.from(false).to(true))
  68. expect(LocalNotificationWorker).to have_enqueued_sidekiq_job(target_account.id, anything, 'AccountWarning', 'moderation_warning')
  69. end
  70. end
  71. describe '#report' do
  72. subject { account_action.report }
  73. context 'with report_id.present?' do
  74. before do
  75. account_action.report_id = Fabricate(:report).id
  76. end
  77. it 'returns Report' do
  78. expect(subject).to be_instance_of Report
  79. end
  80. end
  81. context 'with !report_id.present?' do
  82. it 'returns nil' do
  83. expect(subject).to be_nil
  84. end
  85. end
  86. end
  87. describe '#with_report?' do
  88. subject { account_action.with_report? }
  89. context 'with !report.nil?' do
  90. before do
  91. account_action.report_id = Fabricate(:report).id
  92. end
  93. it 'returns true' do
  94. expect(subject).to be true
  95. end
  96. end
  97. context 'with !(!report.nil?)' do
  98. it 'returns false' do
  99. expect(subject).to be false
  100. end
  101. end
  102. end
  103. describe '.types_for_account' do
  104. subject { described_class.types_for_account(account) }
  105. context 'when Account.local?' do
  106. let(:account) { Fabricate(:account, domain: nil) }
  107. it 'returns ["none", "disable", "sensitive", "silence", "suspend"]' do
  108. expect(subject).to eq %w(none disable sensitive silence suspend)
  109. end
  110. end
  111. context 'with !account.local?' do
  112. let(:account) { Fabricate(:account, domain: 'hoge.com') }
  113. it 'returns ["sensitive", "silence", "suspend"]' do
  114. expect(subject).to eq %w(sensitive silence suspend)
  115. end
  116. end
  117. end
  118. end