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

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

require 'spec_helper'

RSpec.describe Projects::AutocompleteSourcesController do
  let_it_be(:group, reload: true) { create(:group) }
  let_it_be(:private_group) { create(:group, :private) }
  let_it_be(:project) { create(:project, namespace: group) }
  let_it_be(:public_project) { create(:project, :public, group: group) }
  let_it_be(:development) { create(:label, project: project, name: 'Development') }
  let_it_be(:private_issue) { create(:labeled_issue, project: project, labels: [development]) }
  let_it_be(:private_work_item) { create(:work_item, project: project) }
  let_it_be(:issue) { create(:labeled_issue, project: public_project, labels: [development]) }
  let_it_be(:work_item) { create(:work_item, project: public_project, id: 1, iid: 100) }
  let_it_be(:user) { create(:user) }

  def members_by_username(username)
    json_response.find { |member| member['username'] == username }
  end

  describe 'GET commands' do
    before do
      group.add_owner(user)
    end

    context 'with a public project' do
      shared_examples 'issuable commands' do
        it 'returns empty array when no user logged in' do
          get :commands, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to eq([])
        end

        it 'raises an error when no target type specified' do
          sign_in(user)

          expect { get :commands, format: :json, params: { namespace_id: group.path, project_id: project.path } }
            .to raise_error(ActionController::ParameterMissing)
        end

        it 'returns an array of commands' do
          sign_in(user)

          get :commands, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type, type_id: issuable_iid }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to be_present
        end
      end

      context 'with an issue' do
        let(:issuable_type) { issue.class.name }
        let(:issuable_iid) { issue.iid }

        it_behaves_like 'issuable commands'
      end

      context 'with work items' do
        let(:issuable_type) { work_item.class.name }
        let(:issuable_iid) { work_item.iid }

        it_behaves_like 'issuable commands'
      end

      context 'with merge request' do
        let(:merge_request) { create(:merge_request, target_project: public_project, source_project: public_project) }
        let(:issuable_type) { merge_request.class.name }
        let(:issuable_iid) { merge_request.iid }

        it_behaves_like 'issuable commands'
      end
    end
  end

  describe 'GET labels' do
    before do
      group.add_owner(user)
      sign_in(user)
    end

    shared_examples 'label commands' do
      it 'raises an error when no target type specified' do
        expect { get :labels, format: :json, params: { namespace_id: group.path, project_id: project.path } }
          .to raise_error(ActionController::ParameterMissing)
      end

      it 'returns an array of labels' do
        get :labels, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issuable_type }

        expect(json_response).to be_a(Array)
        expect(json_response.count).to eq(1)
        expect(json_response[0]['title']).to eq('Development')
      end
    end

    context 'with issues' do
      let(:issuable_type) { issue.class.name }
      let(:issuable_iid) { issue.iid }

      it_behaves_like 'label commands'
    end

    context 'with work items' do
      let(:issuable_type) { work_item.class.name }
      let(:issuable_iid) { work_item.iid }

      it_behaves_like 'label commands'
    end
  end

  describe 'GET members' do
    let_it_be(:invited_private_member) { create(:user) }
    let_it_be(:issue) { create(:labeled_issue, project: public_project, labels: [development], author: user) }
    let_it_be(:work_item) { create(:work_item, project: public_project, author: user) }

    before_all do
      create(:project_group_link, group: private_group, project: public_project)
      group.add_owner(user)
      private_group.add_developer(invited_private_member)
    end

    context 'when logged in' do
      before do
        sign_in(user)
      end

      it 'returns 400 when no target type specified' do
        expect { get :members, format: :json, params: { namespace_id: group.path, project_id: project.path } }
          .to raise_error(ActionController::ParameterMissing)
      end

      shared_examples 'all members are returned' do
        before do
          stub_feature_flags(disable_all_mention: false)
        end

        it 'returns an array of member object' do
          get :members, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type }

          expect(members_by_username('all').symbolize_keys).to include(
            username: 'all',
            name: 'All Project and Group Members',
            count: 2)

          expect(members_by_username(group.full_path).symbolize_keys).to include(
            type: group.class.name,
            name: group.full_name,
            avatar_url: group.avatar_url,
            count: 1)

          expect(members_by_username(user.username).symbolize_keys).to include(
            type: user.class.name,
            name: user.name,
            avatar_url: user.avatar_url)

          expect(members_by_username(invited_private_member.username).symbolize_keys).to include(
            type: invited_private_member.class.name,
            name: invited_private_member.name,
            avatar_url: invited_private_member.avatar_url)
        end

        context 'when `disable_all_mention` FF is enabled' do
          before do
            stub_feature_flags(disable_all_mention: true)
          end

          it 'does not return the all mention user' do
            get :members, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type }

            expect(json_response).not_to include(a_hash_including(
              { username: 'all', name: 'All Project and Group Members' }))
          end
        end
      end

      context 'with issue' do
        let(:issuable_type) { issue.class.name }

        it_behaves_like 'all members are returned'
      end

      context 'with work item' do
        let(:issuable_type) { work_item.class.name }

        it_behaves_like 'all members are returned'
      end
    end

    context 'when anonymous' do
      shared_examples 'private project is inaccessible' do
        it 'redirects to login page for private project' do
          get :members, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issuable_type }

          expect(response).to redirect_to new_user_session_path
        end
      end

      shared_examples 'only public members are returned for public project' do
        before do
          stub_feature_flags(disable_all_mention: false)
        end

        it 'only returns public members' do
          get :members, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type }

          expect(members_by_username('all').symbolize_keys).to include(
            username: 'all',
            name: 'All Project and Group Members',
            count: 1)

          expect(members_by_username(user.username).symbolize_keys).to include(
            type: user.class.name,
            name: user.name,
            avatar_url: user.avatar_url)
        end

        context 'when `disable_all_mention` FF is enabled' do
          before do
            stub_feature_flags(disable_all_mention: true)
          end

          it 'does not return the all mention user' do
            get :members, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type }

            expect(json_response).not_to include(a_hash_including(
              { username: 'all', name: 'All Project and Group Members' }))
          end
        end
      end

      context 'with issue' do
        it_behaves_like 'private project is inaccessible' do
          let(:issuable_type) { private_issue.class.name }
        end

        it_behaves_like 'only public members are returned for public project' do
          let(:issuable_type) { issue.class.name }
        end
      end

      context 'with work item' do
        it_behaves_like 'private project is inaccessible' do
          let(:issuable_type) { private_work_item.class.name }
        end

        it_behaves_like 'only public members are returned for public project' do
          let(:issuable_type) { work_item.class.name }
        end
      end
    end
  end

  describe 'GET milestones' do
    let(:group) { create(:group, :public) }
    let(:project) { create(:project, :public, namespace: group) }
    let!(:project_milestone) { create(:milestone, project: project) }
    let!(:group_milestone) { create(:milestone, group: group) }

    before do
      sign_in(user)
    end

    it 'lists milestones' do
      group.add_owner(user)

      get :milestones, format: :json, params: { namespace_id: group.path, project_id: project.path }

      milestone_titles = json_response.map { |milestone| milestone["title"] }
      expect(milestone_titles).to match_array([project_milestone.title, group_milestone.title])
    end

    context 'when user cannot read project issues and merge requests' do
      it 'renders 404' do
        project.project_feature.update!(issues_access_level: ProjectFeature::PRIVATE)
        project.project_feature.update!(merge_requests_access_level: ProjectFeature::PRIVATE)

        get :milestones, format: :json, params: { namespace_id: group.path, project_id: project.path }

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe 'GET contacts' do
    let_it_be(:contact_1) { create(:contact, group: group) }
    let_it_be(:contact_2) { create(:contact, group: group) }

    before do
      sign_in(user)
    end

    context 'when feature flag is enabled' do
      context 'when a group has contact relations enabled' do
        before do
          create(:crm_settings, group: group, enabled: true)
        end

        context 'when a user can read contacts' do
          it 'lists contacts' do
            group.add_developer(user)

            get :contacts, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issue.class.name }

            emails = json_response.map { |contact_data| contact_data["email"] }
            expect(emails).to match_array([contact_1.email, contact_2.email])
          end
        end

        context 'when a user can not read contacts' do
          it 'renders 404' do
            get :contacts, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issue.class.name }

            expect(response).to have_gitlab_http_status(:not_found)
          end
        end
      end

      context 'when a group has contact relations disabled' do
        it 'renders 404' do
          group.add_developer(user)

          get :contacts, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issue.class.name }

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end
    end
  end
end