summaryrefslogtreecommitdiff
path: root/spec/features/projects/commits/user_browses_commits_spec.rb
blob: 35ed6620548fed185c37ae1a439c690875a8c1d5 (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
require 'spec_helper'

describe 'User browses commits' do
  include RepoHelpers

  let(:user) { create(:user) }
  let(:project) { create(:project, :repository, namespace: user.namespace) }

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

  it 'renders commit' do
    visit project_commit_path(project, sample_commit.id)

    expect(page).to have_content(sample_commit.message)
      .and have_content("Showing #{sample_commit.files_changed_count} changed files")
      .and have_content('Side-by-side')
  end

  it 'fill commit sha when click new tag from commit page' do
    visit project_commit_path(project, sample_commit.id)
    click_link 'Tag'

    expect(page).to have_selector("input[value='#{sample_commit.id}']", visible: false)
  end

  it 'renders inline diff button when click side-by-side diff button' do
    visit project_commit_path(project, sample_commit.id)
    find('#parallel-diff-btn').click

    expect(page).to have_content 'Inline'
  end

  it 'renders breadcrumbs on specific commit path' do
    visit project_commits_path(project, project.repository.root_ref + '/files/ruby/regex.rb', limit: 5)

    expect(page).to have_selector('ul.breadcrumb')
      .and have_selector('ul.breadcrumb a', count: 4)
  end

  it 'renders diff links to both the previous and current image' do
    visit project_commit_path(project, sample_image_commit.id)

    links = page.all('.file-actions a')
    expect(links[0]['href']).to match %r{blob/#{sample_image_commit.old_blob_id}}
    expect(links[1]['href']).to match %r{blob/#{sample_image_commit.new_blob_id}}
  end

  context 'when commit has ci status' do
    let(:pipeline) { create(:ci_pipeline, project: project, sha: sample_commit.id) }

    before do
      project.enable_ci

      create(:ci_build, pipeline: pipeline)

      allow_any_instance_of(Ci::Pipeline).to receive(:ci_yaml_file).and_return('')
    end

    it 'renders commit ci info' do
      visit project_commit_path(project, sample_commit.id)

      expect(page).to have_content "Pipeline ##{pipeline.id} pending"
    end
  end

  context 'primary email' do
    it 'finds a commit by a primary email' do
      user = create(:user, email: 'dmitriy.zaporozhets@gmail.com')

      visit(project_commit_path(project, sample_commit.id))

      check_author_link(sample_commit.author_email, user)
    end
  end

  context 'secondary email' do
    it 'finds a commit by a secondary email' do
      user =
        create(:user) do |user|
          create(:email, { user: user, email: 'dmitriy.zaporozhets@gmail.com' })
        end

      visit(project_commit_path(project, sample_commit.parent_id))

      check_author_link(sample_commit.author_email, user)
    end
  end

  context 'when the blob does not exist' do
    let(:commit) { create(:commit, project: project) }

    it 'shows a blank label' do
      allow_any_instance_of(Gitlab::Diff::File).to receive(:blob).and_return(nil)
      allow_any_instance_of(Gitlab::Diff::File).to receive(:raw_binary?).and_return(true)

      visit(project_commit_path(project, commit))

      expect(find('.diff-file-changes', visible: false)).to have_content('No file name available')
    end
  end

  describe 'commits list' do
    let(:visit_commits_page) do
      visit project_commits_path(project, project.repository.root_ref, limit: 5)
    end

    it 'searches commit', :js do
      visit_commits_page
      fill_in 'commits-search', with: 'submodules'

      expect(page).to have_content 'More submodules'
      expect(page).not_to have_content 'Change some files'
    end

    it 'renders commits atom feed' do
      visit_commits_page
      click_link('Commits feed')

      commit = project.repository.commit

      expect(response_headers['Content-Type']).to have_content("application/atom+xml")
      expect(body).to have_selector('title', text: "#{project.name}:master commits")
        .and have_selector('author email', text: commit.author_email)
        .and have_selector('entry summary', text: commit.description[0..10].delete("\r\n"))
    end

    context 'master branch' do
      before do
        visit_commits_page
      end

      it 'renders project commits' do
        commit = project.repository.commit

        expect(page).to have_content(project.name)
          .and have_content(commit.message[0..20])
          .and have_content(commit.short_id)
      end

      it 'does not render create merge request button' do
        expect(page).not_to have_link 'Create merge request'
      end

      context 'when click the compare tab' do
        before do
          click_link('Compare')
        end

        it 'does not render create merge request button' do
          expect(page).not_to have_link 'Create merge request'
        end
      end
    end

    context 'feature branch' do
      let(:visit_commits_page) do
        visit project_commits_path(project, 'feature')
      end

      context 'when project does not have open merge requests' do
        before do
          visit_commits_page
        end

        it 'renders project commits' do
          commit = project.repository.commit('0b4bc9a')

          expect(page).to have_content(project.name)
            .and have_content(commit.message[0..12])
            .and have_content(commit.short_id)
        end

        it 'renders create merge request button' do
          expect(page).to have_link 'Create merge request'
        end

        context 'when click the compare tab' do
          before do
            click_link('Compare')
          end

          it 'renders create merge request button' do
            expect(page).to have_link 'Create merge request'
          end
        end
      end

      context 'when project have open merge request' do
        let!(:merge_request) do
          create(
            :merge_request,
            title: 'Feature',
            source_project: project,
            source_branch: 'feature',
            target_branch: 'master',
            author: project.users.first
          )
        end

        before do
          visit_commits_page
        end

        it 'renders project commits' do
          commit = project.repository.commit('0b4bc9a')

          expect(page).to have_content(project.name)
            .and have_content(commit.message[0..12])
            .and have_content(commit.short_id)
        end

        it 'renders button to the merge request' do
          expect(page).not_to have_link 'Create merge request'
          expect(page).to have_link 'View open merge request', href: project_merge_request_path(project, merge_request)
        end

        context 'when click the compare tab' do
          before do
            click_link('Compare')
          end

          it 'renders button to the merge request' do
            expect(page).not_to have_link 'Create merge request'
            expect(page).to have_link 'View open merge request', href: project_merge_request_path(project, merge_request)
          end
        end
      end
    end
  end
end

private

def check_author_link(email, author)
  author_link = find('.commit-author-link')

  expect(author_link['href']).to eq(user_path(author))
  expect(author_link['title']).to eq(email)
  expect(find('.commit-author-name').text).to eq(author.name)
end