Welcome to mirror list, hosted at ThFree Co, Russian Federation.

moderate_user_service_spec.rb « abuse_reports « admin « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e08db2b6123ab938df7b787c5da42a566689d2a (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Admin::AbuseReports::ModerateUserService, feature_category: :instance_resiliency do
  let_it_be_with_reload(:abuse_report) { create(:abuse_report) }
  let_it_be_with_reload(:similar_abuse_report) do
    create(:abuse_report, user: abuse_report.user, category: abuse_report.category)
  end

  let(:action) { 'ban_user' }
  let(:close) { true }
  let(:reason) { 'spam' }
  let(:params) { { user_action: action, close: close, reason: reason, comment: 'obvious spam' } }
  let_it_be(:admin) { create(:admin) }

  let(:service) { described_class.new(abuse_report, admin, params) }

  describe '#execute', :enable_admin_mode do
    subject { service.execute }

    shared_examples 'returns an error response' do |error|
      it 'returns an error response' do
        expect(subject.status).to eq :error
        expect(subject.message).to eq error
      end
    end

    shared_examples 'closes the report' do
      it 'closes the report' do
        expect { subject }.to change { abuse_report.closed? }.from(false).to(true)
      end

      context 'when similar open reports for the user exist' do
        it 'closes the similar report' do
          expect { subject }.to change { similar_abuse_report.reload.closed? }.from(false).to(true)
        end
      end
    end

    shared_examples 'does not close the report' do
      it 'does not close the report' do
        subject
        expect(abuse_report.closed?).to be(false)
      end

      context 'when similar open reports for the user exist' do
        it 'does not close the similar report' do
          subject
          expect(similar_abuse_report.reload.closed?).to be(false)
        end
      end
    end

    shared_examples 'does not record an event' do
      it 'does not record an event' do
        expect { subject }.not_to change { abuse_report.events.count }
      end
    end

    shared_examples 'records an event' do |action:|
      it 'records the event', :aggregate_failures do
        expect { subject }.to change { abuse_report.events.count }.by(1)

        expect(abuse_report.events.last).to have_attributes(
          action: action,
          user: admin,
          reason: reason,
          comment: params[:comment]
        )
      end

      it 'returns the event success message' do
        expect(subject.message).to eq(abuse_report.events.last.success_message)
      end
    end

    context 'when invalid parameters are given' do
      describe 'invalid user' do
        describe 'when no user is given' do
          let_it_be(:admin) { nil }

          it_behaves_like 'returns an error response', 'Admin is required'
        end

        describe 'when given user is no admin' do
          let_it_be(:admin) { create(:user) }

          it_behaves_like 'returns an error response', 'Admin is required'
        end
      end

      describe 'invalid action' do
        describe 'when no action is given' do
          let(:action) { '' }
          let(:close) { 'false' }

          it_behaves_like 'returns an error response', 'Action is required'
        end

        describe 'when unknown action is given' do
          let(:action) { 'unknown' }
          let(:close) { 'false' }

          it_behaves_like 'returns an error response', 'Action is required'
        end
      end

      describe 'invalid reason' do
        let(:reason) { '' }

        it 'sets the reason to `other`' do
          subject

          expect(abuse_report.events.last).to have_attributes(reason: 'other')
        end
      end
    end

    describe 'when banning the user' do
      it 'calls the Users::BanService' do
        expect_next_instance_of(Users::BanService, admin) do |service|
          expect(service).to receive(:execute).with(abuse_report.user).and_return(status: :success)
        end

        subject
      end

      context 'when closing the report' do
        it_behaves_like 'closes the report'
        it_behaves_like 'records an event', action: 'ban_user_and_close_report'
      end

      context 'when not closing the report' do
        let(:close) { 'false' }

        it_behaves_like 'does not close the report'
        it_behaves_like 'records an event', action: 'ban_user'
      end

      context 'when banning the user fails' do
        before do
          allow_next_instance_of(Users::BanService, admin) do |service|
            allow(service).to receive(:execute).with(abuse_report.user)
              .and_return(status: :error, message: 'Banning the user failed')
          end
        end

        it_behaves_like 'returns an error response', 'Banning the user failed'
        it_behaves_like 'does not close the report'
        it_behaves_like 'does not record an event'
      end
    end

    describe 'when blocking the user' do
      let(:action) { 'block_user' }

      it 'calls the Users::BlockService' do
        expect_next_instance_of(Users::BlockService, admin) do |service|
          expect(service).to receive(:execute).with(abuse_report.user).and_return(status: :success)
        end

        subject
      end

      context 'when closing the report' do
        it_behaves_like 'closes the report'
        it_behaves_like 'records an event', action: 'block_user_and_close_report'
      end

      context 'when not closing the report' do
        let(:close) { 'false' }

        it_behaves_like 'does not close the report'
        it_behaves_like 'records an event', action: 'block_user'
      end

      context 'when blocking the user fails' do
        before do
          allow_next_instance_of(Users::BlockService, admin) do |service|
            allow(service).to receive(:execute).with(abuse_report.user)
              .and_return(status: :error, message: 'Blocking the user failed')
          end
        end

        it_behaves_like 'returns an error response', 'Blocking the user failed'
        it_behaves_like 'does not close the report'
        it_behaves_like 'does not record an event'
      end
    end

    describe 'when deleting the user' do
      let(:action) { 'delete_user' }

      it 'calls the delete_async method' do
        expect(abuse_report.user).to receive(:delete_async).with(deleted_by: admin)
        subject
      end

      context 'when closing the report' do
        it_behaves_like 'closes the report'
        it_behaves_like 'records an event', action: 'delete_user_and_close_report'
      end

      context 'when not closing the report' do
        let(:close) { 'false' }

        it_behaves_like 'does not close the report'
        it_behaves_like 'records an event', action: 'delete_user'
      end
    end

    describe 'when only closing the report' do
      let(:action) { '' }

      it_behaves_like 'closes the report'
      it_behaves_like 'records an event', action: 'close_report'

      context 'when report is already closed' do
        before do
          abuse_report.closed!
        end

        it_behaves_like 'returns an error response', 'Report already closed'
        it_behaves_like 'does not record an event'
      end
    end
  end
end