summaryrefslogtreecommitdiff
path: root/spec/helpers/commits_helper_spec.rb
blob: 86ed133e599307c6308720e141507908da19bc31 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe CommitsHelper do
  include ProjectForksHelper

  describe 'commit_author_link' do
    it 'escapes the author email' do
      commit = double(
        author: nil,
        author_name: 'Persistent XSS',
        author_email: 'my@email.com" onmouseover="alert(1)'
      )

      expect(helper.commit_author_link(commit))
        .not_to include('onmouseover="alert(1)"')
    end

    it 'escapes the author name' do
      user = build_stubbed(:user, name: 'Foo <script>alert("XSS")</script>')

      commit = double(author: user, author_name: '', author_email: '')

      expect(helper.commit_author_link(commit))
        .to include('Foo &lt;script&gt;')
      expect(helper.commit_author_link(commit, avatar: true))
        .to include('commit-author-name', 'js-user-link', 'Foo &lt;script&gt;')
    end
  end

  describe 'commit_committer_link' do
    it 'escapes the committer email' do
      commit = double(
        committer: nil,
        committer_name: 'Persistent XSS',
        committer_email: 'my@email.com" onmouseover="alert(1)'
      )

      expect(helper.commit_committer_link(commit))
        .not_to include('onmouseover="alert(1)"')
    end

    it 'escapes the committer name' do
      user = build_stubbed(:user, name: 'Foo <script>alert("XSS")</script>')

      commit = double(committer: user, committer_name: '', committer_email: '')

      expect(helper.commit_committer_link(commit))
        .to include('Foo &lt;script&gt;')
      expect(helper.commit_committer_link(commit, avatar: true))
        .to include('commit-committer-name', 'Foo &lt;script&gt;')
    end
  end

  describe '#view_file_button' do
    let(:project) { build(:project) }
    let(:path) { 'path/to/file' }
    let(:sha) { '1234567890' }

    subject do
      helper.view_file_button(sha, path, project)
    end

    it 'links to project files' do
      expect(subject).to have_link('1234567', href: helper.project_blob_path(project, "#{sha}/#{path}"))
    end
  end

  describe '#view_on_environment_button' do
    let(:project) { create(:project) }
    let(:environment) { create(:environment, external_url: 'http://example.com') }
    let(:path) { 'source/file.html' }
    let(:sha) { RepoHelpers.sample_commit.id }

    before do
      allow(environment).to receive(:external_url_for).with(path, sha).and_return('http://example.com/file.html')
    end

    it 'returns a link tag linking to the file in the environment' do
      html = helper.view_on_environment_button(sha, path, environment)
      node = Nokogiri::HTML.parse(html).at_css('a')

      expect(node[:title]).to eq('View on example.com')
      expect(node[:href]).to eq('http://example.com/file.html')
    end
  end

  describe '#commit_to_html' do
    let(:project) { create(:project, :repository) }
    let(:ref) { 'master' }
    let(:commit) { project.commit(ref) }

    it 'renders HTML representation of a commit' do
      assign(:project, project)
      allow(helper).to receive(:current_user).and_return(project.owner)

      expect(helper.commit_to_html(commit, ref, project)).to include('<div class="commit-content')
    end
  end

  describe 'commit_path' do
    it 'returns a persisted merge request commit path' do
      project = create(:project, :repository)
      persisted_merge_request = create(:merge_request, source_project: project, target_project: project)
      commit = project.repository.commit

      expect(helper.commit_path(persisted_merge_request.project, commit, merge_request: persisted_merge_request))
        .to eq(diffs_project_merge_request_path(project, persisted_merge_request, commit_id: commit.id))
    end

    it 'returns a non-persisted merge request commit path which commits still reside in the source project' do
      source_project = create(:project, :repository)
      target_project = create(:project, :repository)
      non_persisted_merge_request = build(:merge_request, source_project: source_project, target_project: target_project)
      commit = source_project.repository.commit

      expect(helper.commit_path(non_persisted_merge_request.project, commit, merge_request: non_persisted_merge_request))
        .to eq(project_commit_path(source_project, commit))
    end

    it 'returns a project commit path' do
      project = create(:project, :repository)
      commit = project.repository.commit

      expect(helper.commit_path(project, commit)).to eq(project_commit_path(project, commit))
    end
  end

  describe "#conditionally_paginate_diff_files" do
    let(:diffs_collection) { instance_double(Gitlab::Diff::FileCollection::Commit, diff_files: diff_files) }
    let(:diff_files) { Gitlab::Git::DiffCollection.new(files) }
    let(:page) { nil }

    let(:files) do
      Array.new(85).map do
        { too_large: false, diff: "" }
      end
    end

    let(:params) do
      {
        page: page
      }
    end

    subject { helper.conditionally_paginate_diff_files(diffs_collection, paginate: paginate) }

    before do
      allow(helper).to receive(:params).and_return(params)
    end

    context "pagination is enabled" do
      let(:paginate) { true }

      it "has been paginated" do
        expect(subject).to be_an(Array)
      end

      it "can change the number of items per page" do
        commits = helper.conditionally_paginate_diff_files(diffs_collection, paginate: paginate, per: 10)

        expect(commits).to be_an(Array)
        expect(commits.size).to eq(10)
      end

      context "page 1" do
        let(:page) { 1 }

        it "has 20 diffs" do
          expect(subject.size).to eq(75)
        end
      end

      context "page 2" do
        let(:page) { 2 }

        it "has the remaining 10 diffs" do
          expect(subject.size).to eq(10)
        end
      end
    end

    context "pagination is disabled" do
      let(:paginate) { false }

      it "returns a standard DiffCollection" do
        expect(subject).to be_a(Gitlab::Git::DiffCollection)
      end
    end
  end

  describe '#cherry_pick_projects_data' do
    let(:project) { create(:project, :repository) }
    let(:user) { create(:user, maintainer_projects: [project]) }
    let!(:forked_project) { fork_project(project, user, { namespace: user.namespace, repository: true }) }

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    it 'returns data for cherry picking into a project' do
      expect(helper.cherry_pick_projects_data(forked_project)).to match_array([
        { id: project.id.to_s, name: project.full_path, refsUrl: refs_project_path(project) },
        { id: forked_project.id.to_s, name: forked_project.full_path, refsUrl: refs_project_path(forked_project) }
      ])
    end

    context 'pick_into_project is disabled' do
      before do
        stub_feature_flags(pick_into_project: false)
      end

      it 'does not calculate target projects' do
        expect(helper.cherry_pick_projects_data(project)).to eq([])
      end
    end
  end

  describe "#commit_options_dropdown_data" do
    let(:project) { build(:project, :repository) }
    let(:commit) { build(:commit) }
    let(:user) { build(:user) }

    subject { helper.commit_options_dropdown_data(project, commit) }

    context "when user is logged in" do
      before do
        allow(helper).to receive(:can?).with(user, :push_code, project).and_return(true)
        allow(helper).to receive(:current_user).and_return(user)
      end

      it "returns data as expected" do
        is_expected.to eq standard_expected_data
      end

      context "when can not collaborate on project" do
        before do
          allow(helper).to receive(:can_collaborate_with_project?).with(project).and_return(false)
        end

        it "returns data as expected" do
          no_collaboration_values = {
            can_revert: 'false',
            can_cherry_pick: 'false'
          }

          is_expected.to eq standard_expected_data.merge(no_collaboration_values)
        end
      end

      context "when commit has already been reverted" do
        before do
          allow(commit).to receive(:has_been_reverted?).with(user).and_return(true)
        end

        it "returns data as expected" do
          is_expected.to eq standard_expected_data.merge({ can_revert: 'false' })
        end
      end
    end

    context "when user is not logged in" do
      before do
        allow(helper).to receive(:can?).with(nil, :push_code, project).and_return(false)
        allow(helper).to receive(:current_user).and_return(nil)
      end

      it "returns data as expected" do
        logged_out_values = {
          can_revert: '',
          can_cherry_pick: '',
          can_tag: 'false'
        }

        is_expected.to eq standard_expected_data.merge(logged_out_values)
      end
    end

    def standard_expected_data
      {
        new_project_tag_path: new_project_tag_path(project, ref: commit),
        email_patches_path: project_commit_path(project, commit, format: :patch),
        plain_diff_path: project_commit_path(project, commit, format: :diff),
        can_revert: 'true',
        can_cherry_pick: 'true',
        can_tag: 'true',
        can_email_patches: 'true'
      }
    end
  end
end