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

abuse_reports_finder_spec.rb « finders « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b641d0cb08254f98fbf4ed9fd5eb90c3022c42f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AbuseReportsFinder, feature_category: :insider_threat do
  let_it_be(:user_1) { create(:user) }
  let_it_be(:user_2) { create(:user) }

  let_it_be(:reporter_1) { create(:user) }
  let_it_be(:reporter_2) { create(:user) }

  let_it_be(:abuse_report_1) do
    create(:abuse_report, :open, category: 'spam', user: user_1, reporter: reporter_1, id: 1)
  end

  let_it_be(:abuse_report_2) do
    create(:abuse_report, :closed, category: 'phishing', user: user_2, reporter: reporter_2, id: 2)
  end

  let(:params) { {} }

  subject(:finder) { described_class.new(params).execute }

  describe '#execute' do
    context 'when params is empty' do
      it 'returns all abuse reports' do
        expect(finder).to match_array([abuse_report_1, abuse_report_2])
      end
    end

    shared_examples 'returns filtered reports' do |filter_field|
      it "returns abuse reports filtered by #{filter_field}_id" do
        expect(finder).to match_array(filtered_reports)
      end

      context "when no user has username = params[:#{filter_field}]" do
        before do
          allow(User).to receive_message_chain(:by_username, :pick)
            .with(params[filter_field])
            .with(:id)
            .and_return(nil)
        end

        it 'returns all abuse reports' do
          expect(finder).to match_array([abuse_report_1, abuse_report_2])
        end
      end
    end

    context 'when params[:user] is present' do
      it_behaves_like 'returns filtered reports', :user do
        let(:params) { { user: user_1.username } }
        let(:filtered_reports) { [abuse_report_1] }
      end
    end

    context 'when params[:reporter] is present' do
      it_behaves_like 'returns filtered reports', :reporter do
        let(:params) { { reporter: reporter_1.username } }
        let(:filtered_reports) { [abuse_report_1] }
      end
    end

    context 'when params[:status] = open' do
      let(:params) { { status: 'open' } }

      it 'returns only open abuse reports' do
        expect(finder).to match_array([abuse_report_1])
      end
    end

    context 'when params[:status] = closed' do
      let(:params) { { status: 'closed' } }

      it 'returns only closed abuse reports' do
        expect(finder).to match_array([abuse_report_2])
      end
    end

    context 'when params[:status] is not a valid status' do
      let(:params) { { status: 'partial' } }

      it 'defaults to returning open abuse reports' do
        expect(finder).to match_array([abuse_report_1])
      end
    end

    context 'when params[:category] is present' do
      let(:params) { { category: 'phishing' } }

      it 'returns abuse reports with the specified category' do
        expect(subject).to match_array([abuse_report_2])
      end
    end

    describe 'aggregating reports' do
      context 'when multiple open reports exist' do
        let(:params) { { status: 'open' } }

        # same category and user as abuse_report_1 -> will get aggregated
        let_it_be(:abuse_report_3) do
          create(:abuse_report, :open, category: abuse_report_1.category, user: abuse_report_1.user, id: 3)
        end

        # different category, but same user as abuse_report_1 -> won't get aggregated
        let_it_be(:abuse_report_4) do
          create(:abuse_report, :open, category: 'phishing', user: abuse_report_1.user, id: 4)
        end

        it 'aggregates open reports by user and category' do
          expect(finder).to match_array([abuse_report_1, abuse_report_4])
        end

        it 'sorts by aggregated_count in descending order and created_at in descending order' do
          expect(finder).to eq([abuse_report_1, abuse_report_4])
        end

        it 'returns count with aggregated reports' do
          expect(finder[0].count).to eq(2)
        end

        context 'when a different sorting attribute is given' do
          let(:params) { { status: 'open', sort: 'created_at_desc' } }

          it 'returns reports sorted by the specified sort attribute' do
            expect(subject).to eq([abuse_report_4, abuse_report_1])
          end
        end

        context 'when params[:sort] is invalid' do
          let(:params) { { status: 'open', sort: 'invalid' } }

          it 'sorts reports by aggregated_count in descending order' do
            expect(finder).to eq([abuse_report_1, abuse_report_4])
          end
        end
      end

      context 'when multiple closed reports exist' do
        let(:params) { { status: 'closed' } }

        # same user and category as abuse_report_2 -> won't get aggregated
        let_it_be(:abuse_report_5) do
          create(:abuse_report, :closed, category: abuse_report_2.category, user: abuse_report_2.user, id: 5)
        end

        it 'does not aggregate closed reports' do
          expect(finder).to match_array([abuse_report_2, abuse_report_5])
        end

        it 'sorts reports by created_at in descending order' do
          expect(finder).to eq([abuse_report_5, abuse_report_2])
        end

        context 'when a different sorting attribute is given' do
          let(:params) { { status: 'closed', sort: 'created_at_asc' } }

          it 'returns reports sorted by the specified sort attribute' do
            expect(subject).to eq([abuse_report_2, abuse_report_5])
          end
        end

        context 'when params[:sort] is invalid' do
          let(:params) { { status: 'closed', sort: 'invalid' } }

          it 'sorts reports by created_at in descending order' do
            expect(finder).to eq([abuse_report_5, abuse_report_2])
          end
        end
      end
    end

    context 'when legacy view is enabled' do
      before do
        stub_feature_flags(abuse_reports_list: false)
      end

      context 'when params is empty' do
        it 'returns all abuse reports' do
          expect(subject).to match_array([abuse_report_1, abuse_report_2])
        end
      end

      context 'when params[:user_id] is present' do
        let(:params) { { user_id: user_1 } }

        it 'returns abuse reports for the specified user' do
          expect(subject).to match_array([abuse_report_1])
        end
      end

      context 'when sorting' do
        it 'returns reports sorted by id in descending order' do
          expect(subject).to match_array([abuse_report_2, abuse_report_1])
        end
      end

      context 'when any of the new filters are present such as params[:status]' do
        let(:params) { { status: 'open' } }

        it 'returns all abuse reports' do
          expect(subject).to match_array([abuse_report_1, abuse_report_2])
        end
      end
    end
  end
end