reports_spec.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Reports' do
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:scopes) { 'admin:read:reports admin:write:reports' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  9. describe 'GET /api/v1/admin/reports' do
  10. subject do
  11. get '/api/v1/admin/reports', headers: headers, params: params
  12. end
  13. let(:params) { {} }
  14. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  15. it_behaves_like 'forbidden for wrong role', ''
  16. context 'when there are no reports' do
  17. it 'returns an empty list' do
  18. subject
  19. expect(response)
  20. .to have_http_status(200)
  21. expect(response.content_type)
  22. .to start_with('application/json')
  23. expect(response.parsed_body)
  24. .to be_empty
  25. end
  26. end
  27. context 'when there are reports' do
  28. let!(:reporter) { Fabricate(:account) }
  29. let!(:spammer) { Fabricate(:account) }
  30. let(:expected_response) do
  31. scope.map do |report|
  32. hash_including({
  33. id: report.id.to_s,
  34. action_taken: report.action_taken?,
  35. category: report.category,
  36. comment: report.comment,
  37. account: hash_including(id: report.account.id.to_s),
  38. target_account: hash_including(id: report.target_account.id.to_s),
  39. statuses: report.statuses,
  40. rules: report.rules,
  41. forwarded: report.forwarded,
  42. })
  43. end
  44. end
  45. let(:scope) { Report.unresolved }
  46. before do
  47. Fabricate(:report)
  48. Fabricate(:report, target_account: spammer)
  49. Fabricate(:report, account: reporter, target_account: spammer)
  50. Fabricate(:report, action_taken_at: 4.days.ago, account: reporter)
  51. Fabricate(:report, action_taken_at: 20.days.ago)
  52. end
  53. it 'returns all unresolved reports' do
  54. subject
  55. expect(response)
  56. .to have_http_status(200)
  57. expect(response.content_type)
  58. .to start_with('application/json')
  59. expect(response.parsed_body)
  60. .to match_array(expected_response)
  61. end
  62. context 'with resolved param' do
  63. let(:params) { { resolved: true } }
  64. let(:scope) { Report.resolved }
  65. it 'returns only the resolved reports' do
  66. subject
  67. expect(response.parsed_body).to match_array(expected_response)
  68. end
  69. end
  70. context 'with account_id param' do
  71. let(:params) { { account_id: reporter.id } }
  72. let(:scope) { Report.unresolved.where(account: reporter) }
  73. it 'returns all unresolved reports filed by the specified account' do
  74. subject
  75. expect(response.parsed_body).to match_array(expected_response)
  76. end
  77. end
  78. context 'with target_account_id param' do
  79. let(:params) { { target_account_id: spammer.id } }
  80. let(:scope) { Report.unresolved.where(target_account: spammer) }
  81. it 'returns all unresolved reports targeting the specified account' do
  82. subject
  83. expect(response.parsed_body).to match_array(expected_response)
  84. end
  85. end
  86. context 'with limit param' do
  87. let(:params) { { limit: 1 } }
  88. it 'returns only the requested number of reports' do
  89. subject
  90. expect(response.parsed_body.size).to eq(1)
  91. end
  92. end
  93. end
  94. end
  95. describe 'GET /api/v1/admin/reports/:id' do
  96. subject do
  97. get "/api/v1/admin/reports/#{report.id}", headers: headers
  98. end
  99. let(:report) { Fabricate(:report) }
  100. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  101. it_behaves_like 'forbidden for wrong role', ''
  102. it 'returns the requested report content', :aggregate_failures do
  103. subject
  104. expect(response).to have_http_status(200)
  105. expect(response.content_type)
  106. .to start_with('application/json')
  107. expect(response.parsed_body).to include(
  108. {
  109. id: report.id.to_s,
  110. action_taken: report.action_taken?,
  111. category: report.category,
  112. comment: report.comment,
  113. account: a_hash_including(id: report.account.id.to_s),
  114. target_account: a_hash_including(id: report.target_account.id.to_s),
  115. statuses: report.statuses,
  116. rules: report.rules,
  117. forwarded: report.forwarded,
  118. }
  119. )
  120. end
  121. end
  122. describe 'PUT /api/v1/admin/reports/:id' do
  123. subject do
  124. put "/api/v1/admin/reports/#{report.id}", headers: headers, params: params
  125. end
  126. let!(:report) { Fabricate(:report, category: :other) }
  127. let(:params) { { category: 'spam' } }
  128. it 'updates the report category', :aggregate_failures do
  129. expect { subject }
  130. .to change { report.reload.category }.from('other').to('spam')
  131. .and create_an_action_log
  132. expect(response).to have_http_status(200)
  133. expect(response.content_type)
  134. .to start_with('application/json')
  135. report.reload
  136. expect(response.parsed_body).to include(
  137. {
  138. id: report.id.to_s,
  139. action_taken: report.action_taken?,
  140. category: report.category,
  141. comment: report.comment,
  142. account: a_hash_including(id: report.account.id.to_s),
  143. target_account: a_hash_including(id: report.target_account.id.to_s),
  144. statuses: report.statuses,
  145. rules: report.rules,
  146. forwarded: report.forwarded,
  147. }
  148. )
  149. end
  150. end
  151. describe 'POST #resolve' do
  152. subject do
  153. post "/api/v1/admin/reports/#{report.id}/resolve", headers: headers
  154. end
  155. let(:report) { Fabricate(:report, action_taken_at: nil) }
  156. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  157. it_behaves_like 'forbidden for wrong role', ''
  158. it 'marks report as resolved', :aggregate_failures do
  159. expect { subject }
  160. .to change { report.reload.unresolved? }.from(true).to(false)
  161. .and create_an_action_log
  162. expect(response).to have_http_status(200)
  163. expect(response.content_type)
  164. .to start_with('application/json')
  165. end
  166. end
  167. describe 'POST #reopen' do
  168. subject do
  169. post "/api/v1/admin/reports/#{report.id}/reopen", headers: headers
  170. end
  171. let(:report) { Fabricate(:report, action_taken_at: 10.days.ago) }
  172. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  173. it_behaves_like 'forbidden for wrong role', ''
  174. it 'marks report as unresolved', :aggregate_failures do
  175. expect { subject }
  176. .to change { report.reload.unresolved? }.from(false).to(true)
  177. .and create_an_action_log
  178. expect(response).to have_http_status(200)
  179. expect(response.content_type)
  180. .to start_with('application/json')
  181. end
  182. end
  183. describe 'POST #assign_to_self' do
  184. subject do
  185. post "/api/v1/admin/reports/#{report.id}/assign_to_self", headers: headers
  186. end
  187. let(:report) { Fabricate(:report) }
  188. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  189. it_behaves_like 'forbidden for wrong role', ''
  190. it 'assigns report to the requesting user', :aggregate_failures do
  191. expect { subject }
  192. .to change { report.reload.assigned_account_id }.from(nil).to(user.account.id)
  193. .and create_an_action_log
  194. expect(response).to have_http_status(200)
  195. expect(response.content_type)
  196. .to start_with('application/json')
  197. end
  198. end
  199. describe 'POST #unassign' do
  200. subject do
  201. post "/api/v1/admin/reports/#{report.id}/unassign", headers: headers
  202. end
  203. let(:report) { Fabricate(:report, assigned_account_id: user.account.id) }
  204. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  205. it_behaves_like 'forbidden for wrong role', ''
  206. it 'unassigns report from assignee', :aggregate_failures do
  207. expect { subject }
  208. .to change { report.reload.assigned_account_id }.from(user.account.id).to(nil)
  209. .and create_an_action_log
  210. expect(response).to have_http_status(200)
  211. expect(response.content_type)
  212. .to start_with('application/json')
  213. end
  214. end
  215. private
  216. def create_an_action_log
  217. change(Admin::ActionLog, :count).by(1)
  218. end
  219. end