summaryrefslogtreecommitdiff
path: root/spec/controllers/autocomplete_controller_spec.rb
blob: b40f647644dffabc08aae484a3ac458230844e9a (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
require 'spec_helper'

describe AutocompleteController do
  let!(:project) { create(:empty_project) }
  let!(:user) { create(:user) }

  context 'GET users' do
    let!(:user2) { create(:user) }
    let!(:non_member) { create(:user) }

    context 'project members' do
      before do
        sign_in(user)
        project.add_master(user)
      end

      describe 'GET #users with project ID' do
        before do
          get(:users, project_id: project.id)
        end

        let(:body) { JSON.parse(response.body) }

        it { expect(body).to be_kind_of(Array) }
        it { expect(body.size).to eq 2 }
        it { expect(body.map { |u| u["username"] }).to include(user.username) }
      end

      describe 'GET #users with unknown project' do
        before do
          get(:users, project_id: 'unknown')
        end

        it { expect(response).to have_http_status(404) }
      end
    end

    context 'group members' do
      let(:group) { create(:group) }

      before do
        sign_in(user)
        group.add_owner(user)
      end

      let(:body) { JSON.parse(response.body) }

      describe 'GET #users with group ID' do
        before do
          get(:users, group_id: group.id)
        end

        it { expect(body).to be_kind_of(Array) }
        it { expect(body.size).to eq 1 }
        it { expect(body.first["username"]).to eq user.username }
      end

      describe 'GET #users with unknown group ID' do
        before do
          get(:users, group_id: 'unknown')
        end

        it { expect(response).to have_http_status(404) }
      end
    end

    context 'non-member login for public project' do
      let!(:project) { create(:project, :public) }

      before do
        sign_in(non_member)
        project.add_master(user)
      end

      let(:body) { JSON.parse(response.body) }

      describe 'GET #users with project ID' do
        before do
          get(:users, project_id: project.id, current_user: true)
        end

        it { expect(body).to be_kind_of(Array) }
        it { expect(body.size).to eq 3 }
        it { expect(body.map { |u| u['username'] }).to include(user.username, non_member.username) }
      end
    end

    context 'all users' do
      before do
        sign_in(user)
        get(:users)
      end

      let(:body) { JSON.parse(response.body) }

      it { expect(body).to be_kind_of(Array) }
      it { expect(body.size).to eq User.count }
    end

    context 'limited users per page' do
      let(:per_page) { 2 }

      before do
        sign_in(user)
        get(:users, per_page: per_page)
      end

      let(:body) { JSON.parse(response.body) }

      it { expect(body).to be_kind_of(Array) }
      it { expect(body.size).to eq per_page }
    end

    context 'unauthenticated user' do
      let(:public_project) { create(:project, :public) }
      let(:body) { JSON.parse(response.body) }

      describe 'GET #users with public project' do
        before do
          public_project.add_guest(user)
          get(:users, project_id: public_project.id)
        end

        it { expect(body).to be_kind_of(Array) }
        it { expect(body.size).to eq 2 }
      end

      describe 'GET #users with project' do
        before do
          get(:users, project_id: project.id)
        end

        it { expect(response).to have_http_status(404) }
      end

      describe 'GET #users with unknown project' do
        before do
          get(:users, project_id: 'unknown')
        end

        it { expect(response).to have_http_status(404) }
      end

      describe 'GET #users with inaccessible group' do
        before do
          project.add_guest(user)
          get(:users, group_id: user.namespace.id)
        end

        it { expect(response).to have_http_status(404) }
      end

      describe 'GET #users with no project' do
        before do
          get(:users)
        end

        it { expect(body).to be_kind_of(Array) }
        it { expect(body.size).to eq 0 }
      end

      describe 'GET #users with todo filter' do
        it 'gives an array of users' do
          get :users, todo_filter: true

          expect(response.status).to eq 200
          expect(body).to be_kind_of(Array)
        end
      end
    end

    context 'author of issuable included' do
      let(:body) { JSON.parse(response.body) }

      context 'authenticated' do
        before do
          sign_in(user)
        end

        it 'includes the author' do
          get(:users, author_id: non_member.id)

          expect(body.first["username"]).to eq non_member.username
        end

        it 'rejects non existent user ids' do
          get(:users, author_id: 99999)

          expect(body.collect { |u| u['id'] }).not_to include(99999)
        end
      end

      context 'without authenticating' do
        it 'returns empty result' do
          get(:users, author_id: non_member.id)

          expect(body).to be_empty
        end
      end
    end

    context 'skip_users parameter included' do
      before do
        sign_in(user)
      end

      it 'skips the user IDs passed' do
        get(:users, skip_users: [user, user2].map(&:id))

        other_user_ids    = [non_member, project.owner, project.creator].map(&:id)
        response_user_ids = JSON.parse(response.body).map { |user| user['id'] }

        expect(response_user_ids).to contain_exactly(*other_user_ids)
      end
    end
  end

  context 'GET projects' do
    let(:authorized_project) { create(:project) }
    let(:authorized_search_project) { create(:project, name: 'rugged') }

    before do
      sign_in(user)
      project.add_master(user)
    end

    context 'authorized projects' do
      before do
        authorized_project.add_master(user)
      end

      describe 'GET #projects with project ID' do
        before do
          get(:projects, project_id: project.id)
        end

        let(:body) { JSON.parse(response.body) }

        it do
          expect(body).to be_kind_of(Array)
          expect(body.size).to eq 2

          expect(body.first['id']).to eq 0
          expect(body.first['name_with_namespace']).to eq 'No project'

          expect(body.last['id']).to eq authorized_project.id
          expect(body.last['name_with_namespace']).to eq authorized_project.name_with_namespace
        end
      end
    end

    context 'authorized projects and search' do
      before do
        authorized_project.add_master(user)
        authorized_search_project.add_master(user)
      end

      describe 'GET #projects with project ID and search' do
        before do
          get(:projects, project_id: project.id, search: 'rugged')
        end

        let(:body) { JSON.parse(response.body) }

        it do
          expect(body).to be_kind_of(Array)
          expect(body.size).to eq 2

          expect(body.last['id']).to eq authorized_search_project.id
          expect(body.last['name_with_namespace']).to eq authorized_search_project.name_with_namespace
        end
      end
    end

    context 'authorized projects apply limit' do
      before do
        authorized_project2 = create(:project)
        authorized_project3 = create(:project)

        authorized_project.add_master(user)
        authorized_project2.add_master(user)
        authorized_project3.add_master(user)

        stub_const 'MoveToProjectFinder::PAGE_SIZE', 2
      end

      describe 'GET #projects with project ID' do
        before do
          get(:projects, project_id: project.id)
        end

        let(:body) { JSON.parse(response.body) }

        it do
          expect(body).to be_kind_of(Array)
          expect(body.size).to eq 3 # Of a total of 4
        end
      end
    end

    context 'authorized projects with offset' do
      before do
        authorized_project2 = create(:project)
        authorized_project3 = create(:project)

        authorized_project.add_master(user)
        authorized_project2.add_master(user)
        authorized_project3.add_master(user)
      end

      describe 'GET #projects with project ID and offset_id' do
        before do
          get(:projects, project_id: project.id, offset_id: authorized_project.id)
        end

        let(:body) { JSON.parse(response.body) }

        it do
          expect(body.detect { |item| item['id'] == 0 }).to be_nil # 'No project' is not there
          expect(body.detect { |item| item['id'] == authorized_project.id }).to be_nil # Offset project is not there either
        end
      end
    end

    context 'authorized projects without admin_issue ability' do
      before(:each) do
        authorized_project.add_guest(user)

        expect(user.can?(:admin_issue, authorized_project)).to eq(false)
      end

      describe 'GET #projects with project ID' do
        before do
          get(:projects, project_id: project.id)
        end

        let(:body) { JSON.parse(response.body) }

        it do
          expect(body).to be_kind_of(Array)
          expect(body.size).to eq 1 # 'No project'

          expect(body.first['id']).to eq 0
        end
      end
    end
  end
end