summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/commit_trailers_filter_spec.rb
blob: 896f3beb7c27f96fbaa8f4808fd89603a0cd4ec8 (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
# frozen_string_literal: true

require 'spec_helper'
require 'ffaker'

RSpec.describe Banzai::Filter::CommitTrailersFilter, feature_category: :source_code_management do
  include FilterSpecHelper
  include CommitTrailersSpecHelper

  let(:secondary_email)     { create(:email, :confirmed) }
  let(:user)                { create(:user) }

  let(:trailer)             { "#{FFaker::Lorem.word}-by:" }

  let(:commit_message)      { trailer_line(trailer, user.name, user.email) }
  let(:commit_message_html) { commit_html(commit_message) }

  context 'detects' do
    let(:email) { FFaker::Internet.email }

    context 'trailers in the form of *-by' do
      where(:commit_trailer) do
        ["#{FFaker::Lorem.word}-by:", "#{FFaker::Lorem.word}-BY:", "#{FFaker::Lorem.word}-By:"]
      end

      with_them do
        let(:trailer) { commit_trailer }

        it 'replaces users with links' do
          doc = filter(commit_message_html)

          expect_to_have_user_link_with_avatar(doc, user: user, trailer: trailer)
        end
      end
    end

    it 'trailers prefixed with whitespaces' do
      message_html = commit_html("\n\r  #{commit_message}")

      doc = filter(message_html)

      expect_to_have_user_link_with_avatar(doc, user: user, trailer: trailer)
    end

    it 'GitLab users via a secondary email' do
      _, message_html = build_commit_message(
        trailer: trailer,
        name: secondary_email.user.name,
        email: secondary_email.email
      )

      doc = filter(message_html)

      expect_to_have_user_link_with_avatar(
        doc,
        user: secondary_email.user,
        trailer: trailer,
        email: secondary_email.email
      )
    end

    context 'non GitLab users' do
      shared_examples 'mailto links' do
        it 'replaces them with mailto links' do
          _, message_html = build_commit_message(
            trailer: trailer,
            name: FFaker::Name.name,
            email: email
          )

          doc = filter(message_html)

          expect_to_have_mailto_link_with_avatar(doc, email: email, trailer: trailer)
        end
      end

      context 'when Gravatar is disabled' do
        before do
          stub_application_setting(gravatar_enabled: false)
        end

        it_behaves_like 'mailto links'
      end

      context 'when Gravatar is enabled' do
        before do
          stub_application_setting(gravatar_enabled: true)
        end

        it_behaves_like 'mailto links'
      end
    end

    it 'multiple trailers in the same message' do
      different_trailer = "#{FFaker::Lorem.word}-by:"
      message = commit_html %(
        #{commit_message}
        #{trailer_line(different_trailer, FFaker::Name.name, email)}
      )

      doc = filter(message)

      expect_to_have_user_link_with_avatar(doc, user: user, trailer: trailer)
      expect_to_have_mailto_link_with_avatar(doc, email: email, trailer: different_trailer)
    end

    context 'special names' do
      where(:name) do
        [
          'John S. Doe',
          'L33t H@x0r'
        ]
      end

      with_them do
        it do
          message, message_html = build_commit_message(
            trailer: trailer,
            name: name,
            email: email
          )

          doc = filter(message_html)

          expect_to_have_mailto_link_with_avatar(doc, email: email, trailer: trailer)
          expect(doc.text).to match Regexp.escape(message)
        end
      end
    end
  end

  context "ignores" do
    it 'commit messages without trailers' do
      exp = message = commit_html(Array.new(5) { FFaker::Lorem.sentence }.join("\n"))
      doc = filter(message)

      expect(doc.to_html).to match Regexp.escape(exp)
    end

    it 'trailers without emails' do
      exp = message = commit_html(Array.new(5) { 'Merged-By:' }.join("\n"))
      doc = filter(message)

      expect(doc.to_html).to match Regexp.escape(exp)
    end

    it 'trailers that are inline the commit message body' do
      message = commit_html %(
        #{FFaker::Lorem.sentence} #{commit_message} #{FFaker::Lorem.sentence}
      )

      doc = filter(message)

      expect(doc.css('a').size).to eq 0
    end
  end

  context "structure" do
    it 'starts with two newlines to separate with actual commit message' do
      doc = filter(commit_message_html)

      expect(doc.xpath('pre').text).to start_with("\n\n")
    end

    it 'preserves the commit trailer structure' do
      doc = filter(commit_message_html)

      expect_to_have_user_link_with_avatar(doc, user: user, trailer: trailer)
      expect(doc.text).to match Regexp.escape(commit_message)
    end

    it 'preserves the original name used in the commit message' do
      message, message_html = build_commit_message(
        trailer: trailer,
        name: FFaker::Name.name,
        email: user.email
      )

      doc = filter(message_html)

      expect_to_have_user_link_with_avatar(doc, user: user, trailer: trailer)
      expect(doc.text).to match Regexp.escape(message)
    end

    it 'preserves the original email used in the commit message' do
      message, message_html = build_commit_message(
        trailer: trailer,
        name: secondary_email.user.name,
        email: secondary_email.email
      )

      doc = filter(message_html)

      expect_to_have_user_link_with_avatar(
        doc,
        user: secondary_email.user,
        trailer: trailer,
        email: secondary_email.email
      )
      expect(doc.text).to match Regexp.escape(message)
    end

    it 'only replaces trailer lines not the full commit message' do
      commit_body = FFaker::Lorem.paragraph
      message = commit_html %(
        #{commit_body}
        #{commit_message}
      )

      doc = filter(message)

      expect_to_have_user_link_with_avatar(doc, user: user, trailer: trailer)
      expect(doc.text).to include(commit_body)
    end

    context 'with Gitlab-hosted avatars in commit trailers' do
      # Because commit trailers are contained within markdown,
      # any path-only link will automatically be prefixed
      # with the path of its repository.
      # See: "build_relative_path" in "lib/banzai/filter/relative_link_filter.rb"
      let(:user_with_avatar) { create(:user, :public_email, :with_avatar, username: 'foobar') }

      it 'returns a full path for avatar urls' do
        _, message_html = build_commit_message(
          trailer: trailer,
          name: user_with_avatar.name,
          email: user_with_avatar.email
        )

        doc = filter(message_html)
        expected = "#{Gitlab.config.gitlab.url}#{user_with_avatar.avatar_url}"

        expect(doc.css('img')[0].attr('src')).to start_with expected
      end
    end
  end
end