123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- # frozen_string_literal: true
- require 'rails_helper'
- RSpec.describe 'Reports' do
- let(:role) { UserRole.find_by(name: 'Admin') }
- let(:user) { Fabricate(:user, role: role) }
- let(:scopes) { 'admin:read:reports admin:write:reports' }
- let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
- let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
- describe 'GET /api/v1/admin/reports' do
- subject do
- get '/api/v1/admin/reports', headers: headers, params: params
- end
- let(:params) { {} }
- it_behaves_like 'forbidden for wrong scope', 'write:statuses'
- it_behaves_like 'forbidden for wrong role', ''
- context 'when there are no reports' do
- it 'returns an empty list' do
- subject
- expect(response)
- .to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body)
- .to be_empty
- end
- end
- context 'when there are reports' do
- let!(:reporter) { Fabricate(:account) }
- let!(:spammer) { Fabricate(:account) }
- let(:expected_response) do
- scope.map do |report|
- hash_including({
- id: report.id.to_s,
- action_taken: report.action_taken?,
- category: report.category,
- comment: report.comment,
- account: hash_including(id: report.account.id.to_s),
- target_account: hash_including(id: report.target_account.id.to_s),
- statuses: report.statuses,
- rules: report.rules,
- forwarded: report.forwarded,
- })
- end
- end
- let(:scope) { Report.unresolved }
- before do
- Fabricate(:report)
- Fabricate(:report, target_account: spammer)
- Fabricate(:report, account: reporter, target_account: spammer)
- Fabricate(:report, action_taken_at: 4.days.ago, account: reporter)
- Fabricate(:report, action_taken_at: 20.days.ago)
- end
- it 'returns all unresolved reports' do
- subject
- expect(response)
- .to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body)
- .to match_array(expected_response)
- end
- context 'with resolved param' do
- let(:params) { { resolved: true } }
- let(:scope) { Report.resolved }
- it 'returns only the resolved reports' do
- subject
- expect(response.parsed_body).to match_array(expected_response)
- end
- end
- context 'with account_id param' do
- let(:params) { { account_id: reporter.id } }
- let(:scope) { Report.unresolved.where(account: reporter) }
- it 'returns all unresolved reports filed by the specified account' do
- subject
- expect(response.parsed_body).to match_array(expected_response)
- end
- end
- context 'with target_account_id param' do
- let(:params) { { target_account_id: spammer.id } }
- let(:scope) { Report.unresolved.where(target_account: spammer) }
- it 'returns all unresolved reports targeting the specified account' do
- subject
- expect(response.parsed_body).to match_array(expected_response)
- end
- end
- context 'with limit param' do
- let(:params) { { limit: 1 } }
- it 'returns only the requested number of reports' do
- subject
- expect(response.parsed_body.size).to eq(1)
- end
- end
- end
- end
- describe 'GET /api/v1/admin/reports/:id' do
- subject do
- get "/api/v1/admin/reports/#{report.id}", headers: headers
- end
- let(:report) { Fabricate(:report) }
- it_behaves_like 'forbidden for wrong scope', 'write:statuses'
- it_behaves_like 'forbidden for wrong role', ''
- it 'returns the requested report content', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body).to include(
- {
- id: report.id.to_s,
- action_taken: report.action_taken?,
- category: report.category,
- comment: report.comment,
- account: a_hash_including(id: report.account.id.to_s),
- target_account: a_hash_including(id: report.target_account.id.to_s),
- statuses: report.statuses,
- rules: report.rules,
- forwarded: report.forwarded,
- }
- )
- end
- end
- describe 'PUT /api/v1/admin/reports/:id' do
- subject do
- put "/api/v1/admin/reports/#{report.id}", headers: headers, params: params
- end
- let!(:report) { Fabricate(:report, category: :other) }
- let(:params) { { category: 'spam' } }
- it 'updates the report category', :aggregate_failures do
- expect { subject }
- .to change { report.reload.category }.from('other').to('spam')
- .and create_an_action_log
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- report.reload
- expect(response.parsed_body).to include(
- {
- id: report.id.to_s,
- action_taken: report.action_taken?,
- category: report.category,
- comment: report.comment,
- account: a_hash_including(id: report.account.id.to_s),
- target_account: a_hash_including(id: report.target_account.id.to_s),
- statuses: report.statuses,
- rules: report.rules,
- forwarded: report.forwarded,
- }
- )
- end
- end
- describe 'POST #resolve' do
- subject do
- post "/api/v1/admin/reports/#{report.id}/resolve", headers: headers
- end
- let(:report) { Fabricate(:report, action_taken_at: nil) }
- it_behaves_like 'forbidden for wrong scope', 'write:statuses'
- it_behaves_like 'forbidden for wrong role', ''
- it 'marks report as resolved', :aggregate_failures do
- expect { subject }
- .to change { report.reload.unresolved? }.from(true).to(false)
- .and create_an_action_log
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- end
- end
- describe 'POST #reopen' do
- subject do
- post "/api/v1/admin/reports/#{report.id}/reopen", headers: headers
- end
- let(:report) { Fabricate(:report, action_taken_at: 10.days.ago) }
- it_behaves_like 'forbidden for wrong scope', 'write:statuses'
- it_behaves_like 'forbidden for wrong role', ''
- it 'marks report as unresolved', :aggregate_failures do
- expect { subject }
- .to change { report.reload.unresolved? }.from(false).to(true)
- .and create_an_action_log
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- end
- end
- describe 'POST #assign_to_self' do
- subject do
- post "/api/v1/admin/reports/#{report.id}/assign_to_self", headers: headers
- end
- let(:report) { Fabricate(:report) }
- it_behaves_like 'forbidden for wrong scope', 'write:statuses'
- it_behaves_like 'forbidden for wrong role', ''
- it 'assigns report to the requesting user', :aggregate_failures do
- expect { subject }
- .to change { report.reload.assigned_account_id }.from(nil).to(user.account.id)
- .and create_an_action_log
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- end
- end
- describe 'POST #unassign' do
- subject do
- post "/api/v1/admin/reports/#{report.id}/unassign", headers: headers
- end
- let(:report) { Fabricate(:report, assigned_account_id: user.account.id) }
- it_behaves_like 'forbidden for wrong scope', 'write:statuses'
- it_behaves_like 'forbidden for wrong role', ''
- it 'unassigns report from assignee', :aggregate_failures do
- expect { subject }
- .to change { report.reload.assigned_account_id }.from(user.account.id).to(nil)
- .and create_an_action_log
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- end
- end
- private
- def create_an_action_log
- change(Admin::ActionLog, :count).by(1)
- end
- end
|