actions_controller_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Admin::Reports::ActionsController do
  4. render_views
  5. let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  6. before do
  7. sign_in user, scope: :user
  8. end
  9. describe 'POST #preview' do
  10. let(:report) { Fabricate(:report) }
  11. before do
  12. post :preview, params: { :report_id => report.id, action => '' }
  13. end
  14. context 'when the action is "suspend"' do
  15. let(:action) { 'suspend' }
  16. it 'returns http success' do
  17. expect(response).to have_http_status(200)
  18. end
  19. end
  20. context 'when the action is "silence"' do
  21. let(:action) { 'silence' }
  22. it 'returns http success' do
  23. expect(response).to have_http_status(200)
  24. end
  25. end
  26. context 'when the action is "delete"' do
  27. let(:action) { 'delete' }
  28. it 'returns http success' do
  29. expect(response).to have_http_status(200)
  30. end
  31. end
  32. context 'when the action is "mark_as_sensitive"' do
  33. let(:action) { 'mark_as_sensitive' }
  34. it 'returns http success' do
  35. expect(response).to have_http_status(200)
  36. end
  37. end
  38. end
  39. describe 'POST #create' do
  40. let(:target_account) { Fabricate(:account) }
  41. let(:statuses) { [Fabricate(:status, account: target_account), Fabricate(:status, account: target_account)] }
  42. let(:report) { Fabricate(:report, target_account: target_account, status_ids: statuses.map(&:id)) }
  43. let(:text) { 'hello' }
  44. let(:common_params) do
  45. { report_id: report.id, text: text }
  46. end
  47. before do
  48. _media = Fabricate(:media_attachment, account: target_account, status: statuses[0])
  49. end
  50. shared_examples 'common behavior' do
  51. it 'closes the report and redirects' do
  52. expect { subject }.to mark_report_action_taken.and create_target_account_strike
  53. expect(report.target_account.strikes.last.text).to eq text
  54. expect(response).to redirect_to(admin_reports_path)
  55. end
  56. context 'when text is unset' do
  57. let(:common_params) do
  58. { report_id: report.id }
  59. end
  60. it 'closes the report and redirects' do
  61. expect { subject }.to mark_report_action_taken.and create_target_account_strike
  62. expect(report.target_account.strikes.last.text).to eq ''
  63. expect(response).to redirect_to(admin_reports_path)
  64. end
  65. end
  66. def mark_report_action_taken
  67. change { report.reload.action_taken? }.from(false).to(true)
  68. end
  69. def create_target_account_strike
  70. change { report.target_account.strikes.count }.by(1)
  71. end
  72. end
  73. shared_examples 'all action types' do
  74. context 'when the action is "suspend"' do
  75. let(:action) { 'suspend' }
  76. it_behaves_like 'common behavior'
  77. it 'suspends the target account' do
  78. expect { subject }.to change { report.target_account.reload.suspended? }.from(false).to(true)
  79. end
  80. end
  81. context 'when the action is "silence"' do
  82. let(:action) { 'silence' }
  83. it_behaves_like 'common behavior'
  84. it 'suspends the target account' do
  85. expect { subject }.to change { report.target_account.reload.silenced? }.from(false).to(true)
  86. end
  87. end
  88. context 'when the action is "delete"' do
  89. let(:action) { 'delete' }
  90. it_behaves_like 'common behavior'
  91. end
  92. context 'when the action is "mark_as_sensitive"' do
  93. let(:action) { 'mark_as_sensitive' }
  94. let(:statuses) { [media_attached_status, media_attached_deleted_status] }
  95. let(:media_attached_status) { Fabricate(:status, account: target_account) }
  96. let(:media_attached_deleted_status) { Fabricate(:status, account: target_account, deleted_at: 1.day.ago) }
  97. let(:last_media_attached_status) { Fabricate(:status, account: target_account) }
  98. before do
  99. _last_media_attachment = Fabricate(:media_attachment, account: target_account, status: last_media_attached_status)
  100. _last_status = Fabricate(:status, account: target_account)
  101. _media_attachment = Fabricate(:media_attachment, account: target_account, status: media_attached_status)
  102. _media_attachment2 = Fabricate(:media_attachment, account: target_account, status: media_attached_deleted_status)
  103. _status = Fabricate(:status, account: target_account)
  104. end
  105. it_behaves_like 'common behavior'
  106. it 'marks the non-deleted as sensitive' do
  107. subject
  108. expect(media_attached_status.reload.sensitive).to be true
  109. end
  110. end
  111. end
  112. context 'with action as submit button' do
  113. subject { post :create, params: common_params.merge({ action => '' }) }
  114. it_behaves_like 'all action types'
  115. end
  116. context 'with moderation action as an extra field' do
  117. subject { post :create, params: common_params.merge({ moderation_action: action }) }
  118. it_behaves_like 'all action types'
  119. end
  120. end
  121. end