|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Status actions' do |
| 6 | + let(:role) { UserRole.find_by(name: 'Moderator') } |
| 7 | + let(:user) { Fabricate(:user, role: role) } |
| 8 | + let(:scopes) { 'admin:write admin:write:statuses' } |
| 9 | + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) } |
| 10 | + let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } |
| 11 | + let(:mailer) { instance_double(ActionMailer::MessageDelivery, deliver_later!: nil) } |
| 12 | + |
| 13 | + before do |
| 14 | + allow(UserMailer).to receive(:warning).with(status.account.user, anything).and_return(mailer) |
| 15 | + end |
| 16 | + |
| 17 | + shared_examples 'a successful notification delivery' do |
| 18 | + it 'notifies the user about the action taken' do |
| 19 | + subject |
| 20 | + |
| 21 | + expect(UserMailer).to have_received(:warning).with(status.account.user, anything).once |
| 22 | + expect(mailer).to have_received(:deliver_later!).once |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + shared_examples 'a successful logged action' do |action_type, target_type| |
| 27 | + it 'logs action' do |
| 28 | + subject |
| 29 | + |
| 30 | + log_item = Admin::ActionLog.where(action: action_type).last |
| 31 | + |
| 32 | + expect(log_item).to be_present |
| 33 | + expect(log_item.account_id).to eq(user.account_id) |
| 34 | + expect(log_item.target_id).to eq(target_type == :status ? status.id : report.id) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + describe 'POST /api/v1/admin/statuses/:id/action' do |
| 39 | + subject do |
| 40 | + post "/api/v1/admin/statuses/#{status.id}/action", headers: headers, params: params |
| 41 | + end |
| 42 | + |
| 43 | + let(:account) { Fabricate(:account, domain: nil) } # local account for email notification |
| 44 | + let(:media) { Fabricate(:media_attachment) } |
| 45 | + let(:status) { Fabricate(:status, media_attachments: [media], account: account) } |
| 46 | + |
| 47 | + context 'with type of delete' do |
| 48 | + let(:params) { { type: 'delete' } } |
| 49 | + |
| 50 | + it_behaves_like 'forbidden for wrong scope', 'admin:read:statuses' |
| 51 | + it_behaves_like 'forbidden for wrong scope', 'write:statuses' # non-admin scope |
| 52 | + it_behaves_like 'forbidden for wrong role', '' |
| 53 | + it_behaves_like 'a successful logged action', :destroy, :status |
| 54 | + |
| 55 | + it 'returns http success' do |
| 56 | + subject |
| 57 | + |
| 58 | + expect(response).to have_http_status(200) |
| 59 | + end |
| 60 | + |
| 61 | + it 'deletes the status' do |
| 62 | + expect { subject }.to change { Status.find_by(id: status.id) }.from(status).to(nil) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + context 'with type of sensitive' do |
| 67 | + let(:params) { { type: 'sensitive' } } |
| 68 | + |
| 69 | + it_behaves_like 'forbidden for wrong scope', 'admin:read:statuses' |
| 70 | + it_behaves_like 'forbidden for wrong scope', 'write:statuses' # non-admin scope |
| 71 | + it_behaves_like 'forbidden for wrong role', '' |
| 72 | + it_behaves_like 'a successful logged action', :update, :status |
| 73 | + |
| 74 | + it 'returns http success' do |
| 75 | + subject |
| 76 | + |
| 77 | + expect(response).to have_http_status(200) |
| 78 | + end |
| 79 | + |
| 80 | + it 'marks the status as sensitive' do |
| 81 | + expect { subject }.to change { status.reload.sensitive? }.from(false).to(true) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'with no type' do |
| 86 | + let(:params) { {} } |
| 87 | + |
| 88 | + it 'returns http unprocessable entity' do |
| 89 | + subject |
| 90 | + |
| 91 | + expect(response).to have_http_status(422) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + context 'with invalid type' do |
| 96 | + let(:params) { { type: 'invalid' } } |
| 97 | + |
| 98 | + it 'returns http unprocessable entity' do |
| 99 | + subject |
| 100 | + |
| 101 | + expect(response).to have_http_status(422) |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + context 'with notification delivery' do |
| 106 | + let(:params) { { type: 'delete', send_email_notification: true } } |
| 107 | + |
| 108 | + it_behaves_like 'a successful notification delivery' |
| 109 | + end |
| 110 | + |
| 111 | + context 'with report' do |
| 112 | + let(:report) { Fabricate(:report) } |
| 113 | + let(:params) { { type: 'delete', report_id: report.id } } |
| 114 | + |
| 115 | + it_behaves_like 'a successful logged action', :resolve, :report |
| 116 | + |
| 117 | + it 'resolves report' do |
| 118 | + expect { subject }.to change { report.reload.unresolved? }.from(true).to(false) |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments