summaryrefslogtreecommitdiff
path: root/spec/features/projects_spec.rb
blob: db64f84aa76c6919ddfaf5e185c146c32f5b3e3a (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Project' do
  include ProjectForksHelper
  include MobileHelpers

  describe 'template' do
    let(:user) { create(:user) }

    before do
      sign_in user
      visit new_project_path
    end

    shared_examples 'creates from template' do |template, sub_template_tab = nil|
      let(:selected_template) { page.find('.project-fields-form .selected-template') }

      choose_template_selector = '.choose-template'
      template_option_selector = '.template-option'
      template_name_selector = '.description strong'

      it "is created from template", :js do
        click_link 'Create from template'
        find(".project-template #{sub_template_tab}").click if sub_template_tab
        find("label[for=#{template.name}]").click
        fill_in("project_name", with: template.name)

        page.within '#content-body' do
          click_button "Create project"
        end

        expect(page).to have_content template.name
      end

      it 'is created using keyboard navigation', :js do
        click_link 'Create from template'

        first_template = first(template_option_selector)
        first_template_name = first_template.find(template_name_selector).text
        first_template.find(choose_template_selector).click

        expect(selected_template).to have_text(first_template_name)

        click_button "Change template"
        find("#built-in").click

        # Jumps down 1 template, skipping the `preview` buttons
        2.times do
          page.send_keys :tab
        end

        # Ensure the template with focus is selected
        project_name = "project from template"
        focused_template = page.find(':focus').ancestor(template_option_selector)
        focused_template_name = focused_template.find(template_name_selector).text
        focused_template.find(choose_template_selector).send_keys :enter
        fill_in "project_name", with: project_name

        expect(selected_template).to have_text(focused_template_name)

        page.within '#content-body' do
          click_button "Create project"
        end

        expect(page).to have_content project_name
      end
    end

    context 'create with project template' do
      it_behaves_like 'creates from template', Gitlab::ProjectTemplate.find(:rails)
    end

    context 'create with sample data template' do
      it_behaves_like 'creates from template', Gitlab::SampleDataTemplate.find(:sample)
    end
  end

  describe 'shows tip about push to create git command' do
    let(:user)    { create(:user) }

    before do
      sign_in user
      visit new_project_path
    end

    it 'shows the command in a popover', :js do
      click_link 'Show command'

      expect(page).to have_css('.popover #push-to-create-tip')
      expect(page).to have_content 'Private projects can be created in your personal namespace with:'
    end
  end

  describe 'description' do
    let(:project) { create(:project, :repository) }
    let(:path)    { project_path(project) }

    before do
      sign_in(project.first_owner)
    end

    it 'parses Markdown' do
      project.update_attribute(:description, 'This is **my** project')
      visit path
      expect(page).to have_css('.home-panel-description > .home-panel-description-markdown > p > strong')
    end

    it 'passes through html-pipeline' do
      project.update_attribute(:description, 'This project is the :poop:')
      visit path
      expect(page).to have_css('.home-panel-description > .home-panel-description-markdown > p > gl-emoji')
    end

    it 'sanitizes unwanted tags' do
      project.update_attribute(:description, "```\ncode\n```")
      visit path
      expect(page).not_to have_css('.home-panel-description code')
    end

    it 'permits `rel` attribute on links' do
      project.update_attribute(:description, 'https://google.com/')
      visit path
      expect(page).to have_css('.home-panel-description a[rel]')
    end

    context 'read more', :js do
      let(:read_more_selector)         { '.read-more-container' }
      let(:read_more_trigger_selector) { '.home-panel-home-desc .js-read-more-trigger' }

      it 'does not display "read more" link on desktop breakpoint' do
        project.update_attribute(:description, 'This is **my** project')
        visit path

        expect(find(read_more_trigger_selector, visible: false)).not_to be_visible
      end

      it 'displays "read more" link on mobile breakpoint' do
        project.update_attribute(:description, 'This is **my** project')
        visit path
        resize_screen_xs

        find(read_more_trigger_selector).click

        expect(page).to have_css('.home-panel-description .is-expanded')
      end
    end

    context 'page description' do
      before do
        project.update_attribute(:description, '**Lorem** _ipsum_ dolor sit [amet](https://example.com)')
        visit path
      end

      it_behaves_like 'page meta description', 'Lorem ipsum dolor sit amet'
    end
  end

  describe 'project topics' do
    let(:project) { create(:project, :repository) }
    let(:path)    { project_path(project) }

    before do
      sign_in(project.first_owner)
      visit path
    end

    it 'shows project topics' do
      project.update_attribute(:topic_list, 'topic1')

      visit path

      expect(page).to have_selector('[data-testid="project_topic_list"]')
      expect(page).to have_link('topic1', href: topic_explore_projects_path(topic_name: 'topic1'))
    end

    it 'shows up to 3 project topics' do
      project.update_attribute(:topic_list, 'topic1, topic2, topic3, topic4')

      visit path

      expect(page).to have_selector('[data-testid="project_topic_list"]')
      expect(page).to have_link('topic1', href: topic_explore_projects_path(topic_name: 'topic1'))
      expect(page).to have_link('topic2', href: topic_explore_projects_path(topic_name: 'topic2'))
      expect(page).to have_link('topic3', href: topic_explore_projects_path(topic_name: 'topic3'))
      expect(page).to have_content('+ 1 more')
    end
  end

  describe 'copy clone URL to clipboard', :js do
    let(:project) { create(:project, :repository) }
    let(:path)    { project_path(project) }

    before do
      sign_in(project.first_owner)
      visit path
    end

    context 'desktop component' do
      it 'shows on md and larger breakpoints' do
        expect(find('.git-clone-holder')).to be_visible
        expect(find('.mobile-git-clone', visible: false)).not_to be_visible
      end
    end

    context 'mobile component' do
      it 'shows mobile component on sm and smaller breakpoints' do
        resize_screen_xs
        expect(find('.mobile-git-clone')).to be_visible
        expect(find('.git-clone-holder', visible: false)).not_to be_visible
      end
    end
  end

  describe 'showing information about source of a project fork' do
    let(:user) { create(:user) }
    let(:base_project) { create(:project, :public, :repository) }
    let(:forked_project) { fork_project(base_project, user, repository: true) }

    before do
      sign_in user
    end

    it 'shows a link to the source project when it is available', :sidekiq_might_not_need_inline do
      visit project_path(forked_project)

      expect(page).to have_content('Forked from')
      expect(page).to have_link(base_project.full_name)
    end

    it 'does not contain fork network information for the root project' do
      forked_project

      visit project_path(base_project)

      expect(page).not_to have_content('In fork network of')
      expect(page).not_to have_content('Forked from')
    end

    it 'does not show the name of the deleted project when the source was deleted', :sidekiq_might_not_need_inline do
      forked_project
      Projects::DestroyService.new(base_project, base_project.first_owner).execute

      visit project_path(forked_project)

      expect(page).to have_content('Forked from an inaccessible project')
    end

    context 'a fork of a fork' do
      let(:fork_of_fork) { fork_project(forked_project, user, repository: true) }

      it 'links to the base project if the source project is removed', :sidekiq_might_not_need_inline do
        fork_of_fork
        Projects::DestroyService.new(forked_project, user).execute

        visit project_path(fork_of_fork)

        expect(page).to have_content("Forked from")
        expect(page).to have_link(base_project.full_name)
      end
    end
  end

  describe 'when the project repository is disabled', :js do
    let(:user)    { create(:user) }
    let(:project) { create(:project, :repository_disabled, :repository, namespace: user.namespace) }

    before do
      sign_in(user)
      project.add_maintainer(user)
      visit project_path(project)
    end

    it 'does not show an error' do
      wait_for_requests

      expect(page).not_to have_selector('[data-testid="alert-danger"]')
    end
  end

  describe 'removal', :js do
    let(:user)    { create(:user) }
    let(:project) { create(:project, namespace: user.namespace) }

    before do
      sign_in(user)
      project.add_maintainer(user)
      visit edit_project_path(project)
    end

    it 'focuses on the confirmation field' do
      click_button 'Delete project'

      expect(page).to have_selector '#confirm_name_input:focus'
    end

    it 'deletes a project', :sidekiq_inline do
      expect { remove_with_confirm('Delete project', project.path_with_namespace, 'Yes, delete project') }.to change { Project.count }.by(-1)
      expect(page).to have_content "Project '#{project.full_name}' is in the process of being deleted."
      expect(Project.all.count).to be_zero
      expect(project.issues).to be_empty
      expect(project.merge_requests).to be_empty
    end
  end

  describe 'tree view (default view is set to Files)', :js do
    let(:user) { create(:user, project_view: 'files') }
    let(:project) { create(:forked_project_with_submodules) }

    before do
      project.add_maintainer(user)
      sign_in user
      visit project_path(project)
    end

    it 'has working links to files' do
      click_link('PROCESS.md')

      expect(page).to have_selector('.file-holder')
    end

    it 'has working links to directories' do
      click_link('encoding')

      expect(page).to have_selector('.breadcrumb-item', text: 'encoding')
    end

    it 'has working links to submodules' do
      click_link('645f6c4c')

      expect(page).to have_selector('.qa-branches-select', text: '645f6c4c82fd3f5e06f67134450a570b795e55a6') # rubocop:disable QA/SelectorUsage
    end

    context 'for signed commit on default branch', :js do
      before do
        project.change_head('33f3729a45c02fc67d00adb1b8bca394b0e761d9')
      end

      it 'displays a GPG badge' do
        visit project_path(project)
        wait_for_requests

        expect(page).not_to have_selector '.gpg-status-box.js-loading-gpg-badge'
        expect(page).to have_selector '.gpg-status-box.invalid'
      end
    end

    context 'for subgroups', :js do
      let(:group) { create(:group) }
      let(:subgroup) { create(:group, parent: group) }
      let(:project) { create(:project, :repository, group: subgroup) }

      it 'renders tree table without errors' do
        wait_for_requests

        expect(page).to have_selector('.tree-item')
        expect(page).not_to have_selector('[data-testid="alert-danger"]')
      end

      context 'for signed commit' do
        before do
          repository = project.repository
          repository.write_ref("refs/heads/#{project.default_branch}", '33f3729a45c02fc67d00adb1b8bca394b0e761d9')
          repository.expire_branches_cache
        end

        it 'displays a GPG badge' do
          visit project_path(project)
          wait_for_requests

          expect(page).not_to have_selector '.gpg-status-box.js-loading-gpg-badge'
          expect(page).to have_selector '.gpg-status-box.invalid'
        end
      end
    end
  end

  describe 'activity view' do
    let(:user) { create(:user, project_view: 'activity') }
    let(:project) { create(:project, :repository) }

    before do
      project.add_maintainer(user)
      sign_in user
      visit project_path(project)
    end

    it 'loads activity', :js do
      expect(page).to have_selector('.event-item')
    end
  end

  context 'content is not cached after signing out', :js do
    let(:user) { create(:user, project_view: 'activity') }
    let(:project) { create(:project, :repository) }

    it 'does not load activity', :js do
      project.add_maintainer(user)
      sign_in(user)
      visit project_path(project)
      sign_out(user)

      page.evaluate_script('window.history.back()')

      expect(page).not_to have_selector('.event-item')
    end
  end

  describe 'edit' do
    let(:user) { create(:user) }
    let(:project) { create(:project, :public) }
    let(:path) { edit_project_path(project) }

    before do
      project.add_maintainer(user)
      sign_in(user)
      visit path
    end

    it_behaves_like 'dirty submit form', [{ form: '.js-general-settings-form', input: 'input[name="project[name]"]' },
                                          { form: '.rspec-merge-request-settings', input: '#project_printing_merge_request_link_enabled' }]
  end

  describe 'view for a user without an access to a repo' do
    let(:project) { create(:project, :repository) }
    let(:user) { create(:user) }

    it 'does not contain default branch information in its content' do
      default_branch = 'merge-commit-analyze-side-branch'

      project.add_guest(user)
      project.change_head(default_branch)

      sign_in(user)
      visit project_path(project)

      lines_with_default_branch = page.html.lines.select { |line| line.include?(default_branch) }
      expect(lines_with_default_branch).to eq([])
    end
  end

  def remove_with_confirm(button_text, confirm_with, confirm_button_text = 'Confirm')
    click_button button_text
    fill_in 'confirm_name_input', with: confirm_with
    click_button confirm_button_text
  end
end