~cytrogen/masto-fe

ref: 178e151019cc1b0d5a537543e7d2f6b4712b9fd4 masto-fe/spec/controllers/admin/reports/actions_controller_spec.rb -rw-r--r-- 4.9 KiB
178e1510 — Claire Merge commit '55e7c08a83547424024bac311d5459cb82cf6dae' into glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# frozen_string_literal: true

require 'rails_helper'

describe Admin::Reports::ActionsController do
  render_views

  let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }

  before do
    sign_in user, scope: :user
  end

  describe 'POST #preview' do
    let(:report) { Fabricate(:report) }

    before do
      post :preview, params: { :report_id => report.id, action => '' }
    end

    context 'when the action is "suspend"' do
      let(:action) { 'suspend' }

      it 'returns http success' do
        expect(response).to have_http_status(200)
      end
    end

    context 'when the action is "silence"' do
      let(:action) { 'silence' }

      it 'returns http success' do
        expect(response).to have_http_status(200)
      end
    end

    context 'when the action is "delete"' do
      let(:action) { 'delete' }

      it 'returns http success' do
        expect(response).to have_http_status(200)
      end
    end

    context 'when the action is "mark_as_sensitive"' do
      let(:action) { 'mark_as_sensitive' }

      it 'returns http success' do
        expect(response).to have_http_status(200)
      end
    end
  end

  describe 'POST #create' do
    let(:target_account) { Fabricate(:account) }
    let(:statuses)       { [Fabricate(:status, account: target_account), Fabricate(:status, account: target_account)] }
    let!(:media)         { Fabricate(:media_attachment, account: target_account, status: statuses[0]) }
    let(:report)         { Fabricate(:report, target_account: target_account, status_ids: statuses.map(&:id)) }
    let(:text)           { 'hello' }
    let(:common_params) do
      { report_id: report.id, text: text }
    end

    shared_examples 'common behavior' do
      it 'closes the report and redirects' do
        expect { subject }.to mark_report_action_taken.and create_target_account_strike

        expect(report.target_account.strikes.last.text).to eq text
        expect(response).to redirect_to(admin_reports_path)
      end

      context 'when text is unset' do
        let(:common_params) do
          { report_id: report.id }
        end

        it 'closes the report and redirects' do
          expect { subject }.to mark_report_action_taken.and create_target_account_strike

          expect(report.target_account.strikes.last.text).to eq ''
          expect(response).to redirect_to(admin_reports_path)
        end
      end

      def mark_report_action_taken
        change { report.reload.action_taken? }.from(false).to(true)
      end

      def create_target_account_strike
        change { report.target_account.strikes.count }.by(1)
      end
    end

    shared_examples 'all action types' do
      context 'when the action is "suspend"' do
        let(:action) { 'suspend' }

        it_behaves_like 'common behavior'

        it 'suspends the target account' do
          expect { subject }.to change { report.target_account.reload.suspended? }.from(false).to(true)
        end
      end

      context 'when the action is "silence"' do
        let(:action) { 'silence' }

        it_behaves_like 'common behavior'

        it 'suspends the target account' do
          expect { subject }.to change { report.target_account.reload.silenced? }.from(false).to(true)
        end
      end

      context 'when the action is "delete"' do
        let(:action) { 'delete' }

        it_behaves_like 'common behavior'
      end

      context 'when the action is "mark_as_sensitive"' do
        let(:action) { 'mark_as_sensitive' }
        let(:statuses) { [media_attached_status, media_attached_deleted_status] }

        let!(:status) { Fabricate(:status, account: target_account) }
        let(:media_attached_status) { Fabricate(:status, account: target_account) }
        let!(:media_attachment) { Fabricate(:media_attachment, account: target_account, status: media_attached_status) }
        let(:media_attached_deleted_status) { Fabricate(:status, account: target_account, deleted_at: 1.day.ago) }
        let!(:media_attachment2) { Fabricate(:media_attachment, account: target_account, status: media_attached_deleted_status) }
        let(:last_media_attached_status) { Fabricate(:status, account: target_account) }
        let!(:last_media_attachment) { Fabricate(:media_attachment, account: target_account, status: last_media_attached_status) }
        let!(:last_status) { Fabricate(:status, account: target_account) }

        it_behaves_like 'common behavior'

        it 'marks the non-deleted as sensitive' do
          subject
          expect(media_attached_status.reload.sensitive).to be true
        end
      end
    end

    context 'with action as submit button' do
      subject { post :create, params: common_params.merge({ action => '' }) }

      it_behaves_like 'all action types'
    end

    context 'with moderation action as an extra field' do
      subject { post :create, params: common_params.merge({ moderation_action: action }) }

      it_behaves_like 'all action types'
    end
  end
end