summaryrefslogtreecommitdiff
path: root/spec/gitlab_post_receive_spec.rb
blob: d0618309c365f7a88fa7d04ff0d6447c8886e3d8 (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
# coding: utf-8
require 'spec_helper'
require 'gitlab_post_receive'

describe GitlabPostReceive do
  let(:repository_path) { "/home/git/repositories" }
  let(:repo_name) { 'dzaporozhets/gitlab-ci' }
  let(:actor) { 'key-123' }
  let(:changes) { "123456 789012 refs/heads/tést\n654321 210987 refs/tags/tag" }
  let(:wrongly_encoded_changes) { changes.encode("ISO-8859-1").force_encoding("UTF-8") }
  let(:base64_changes) { Base64.encode64(wrongly_encoded_changes) }
  let(:repo_path) { File.join(repository_path, repo_name) + ".git" }
  let(:gl_repository) { "project-1" }
  let(:push_options) { [] }
  let(:gitlab_post_receive) { GitlabPostReceive.new(gl_repository, repo_path, actor, wrongly_encoded_changes, push_options) }
  let(:broadcast_message) { "test " * 10 + "message " * 10 }
  let(:enqueued_at) { Time.new(2016, 6, 23, 6, 59) }
  let(:new_merge_request_urls) do
    [{
      'branch_name' => 'new_branch',
      'url' => 'http://localhost/dzaporozhets/gitlab-ci/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch',
      'new_merge_request' => true
    }]
  end
  let(:existing_merge_request_urls) do
    [{
      'branch_name' => 'feature_branch',
      'url' => 'http://localhost/dzaporozhets/gitlab-ci/merge_requests/1',
      'new_merge_request' => false
    }]
  end

  before do
    $logger = double('logger').as_null_object # Global vars are bad
    allow_any_instance_of(GitlabConfig).to receive(:repos_path).and_return(repository_path)
  end

  describe "#exec" do
    let(:response) { { 'reference_counter_decreased' => true } }

    it 'calls the api to notify the execution of the hook' do
      expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)

      expect(gitlab_post_receive.exec).to eq(true)
    end

    context 'merge request urls and broadcast messages' do
      let(:response) do
        {
          'reference_counter_decreased' => true,
          'merge_request_urls' => new_merge_request_urls,
          'broadcast_message' => broadcast_message
        }
      end

      it 'prints the merge request urls and broadcast message' do
        expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
        assert_broadcast_message_printed(gitlab_post_receive)
        assert_new_mr_printed(gitlab_post_receive)

        expect(gitlab_post_receive.exec).to eq(true)
      end

      context 'when contains long url string at end' do
        let(:broadcast_message) { "test " * 10 + "message " * 10 + "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url" }

        it 'doesnt truncate url' do
          expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
          assert_broadcast_message_printed_keep_long_url_end(gitlab_post_receive)
          assert_new_mr_printed(gitlab_post_receive)

          expect(gitlab_post_receive.exec).to eq(true)
        end
      end

      context 'when contains long url string at start' do
        let(:broadcast_message) { "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url " + "test " * 10 + "message " * 11}

        it 'doesnt truncate url' do
          expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
          assert_broadcast_message_printed_keep_long_url_start(gitlab_post_receive)
          assert_new_mr_printed(gitlab_post_receive)

          expect(gitlab_post_receive.exec).to eq(true)
        end
      end

      context 'when contains long url string in middle' do
        let(:broadcast_message) { "test " * 11 + "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url " + "message " * 11}

        it 'doesnt truncate url' do
          expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
          assert_broadcast_message_printed_keep_long_url_middle(gitlab_post_receive)
          assert_new_mr_printed(gitlab_post_receive)

          expect(gitlab_post_receive.exec).to eq(true)
        end
      end
    end

    context 'when warnings are present' do
      let(:response) do
        {
          'reference_counter_decreased' => true,
          'warnings' => 'My warning message'
        }
      end

      it 'treats the warning as a broadcast message' do
        expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
        expect(gitlab_post_receive).to receive(:print_formatted_alert_message).with("WARNINGS:\nMy warning message")
        expect(gitlab_post_receive.exec).to eq(true)
      end
    end

    context 'when redirected message available' do
      let(:message) { "This is a redirected message" }
      let(:response) do
        {
          'reference_counter_decreased' => true,
          'redirected_message' => message
        }
      end

      it 'prints redirected message' do
        expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
        assert_redirected_message_printed(gitlab_post_receive)
        expect(gitlab_post_receive.exec).to eq(true)
      end

      context 'when project created message is available' do
        let(:message) { "This is a created project message" }
        let(:response) do
          {
            'reference_counter_decreased' => true,
            'project_created_message' => message
          }
        end

        it 'prints project created message' do
          expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)

          assert_project_created_message_printed(gitlab_post_receive)

          expect(gitlab_post_receive.exec).to be true
        end
      end
    end
  end

  private

  def assert_new_mr_printed(gitlab_post_receive)
    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "To create a merge request for new_branch, visit:"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "  http://localhost/dzaporozhets/gitlab-ci/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).ordered
  end

  def assert_existing_mr_printed(gitlab_post_receive)
    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "View merge request for feature_branch:"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "  http://localhost/dzaporozhets/gitlab-ci/merge_requests/1"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).ordered
  end

  def assert_broadcast_message_printed(gitlab_post_receive)
    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "========================================================================"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "   test test test test test test test test test test message message"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "    message message message message message message message message"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "========================================================================"
    ).ordered
  end

  def assert_redirected_message_printed(gitlab_post_receive)
    expect(gitlab_post_receive).to receive(:puts).with("This is a redirected message")
  end

  def assert_project_created_message_printed(gitlab_post_receive)
    expect(gitlab_post_receive).to receive(:puts).with("This is a created project message")
  end

  def assert_broadcast_message_printed_keep_long_url_end(gitlab_post_receive)
    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "========================================================================"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "   test test test test test test test test test test message message"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "    message message message message message message message message"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "========================================================================"
    ).ordered
  end

  def assert_broadcast_message_printed_keep_long_url_start(gitlab_post_receive)
    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "========================================================================"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "   test test test test test test test test test test message message"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "    message message message message message message message message"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "                                message"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "========================================================================"
    ).ordered
  end

  def assert_broadcast_message_printed_keep_long_url_middle(gitlab_post_receive)
    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "========================================================================"
    ).ordered
    expect(gitlab_post_receive).to receive(:puts).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "         test test test test test test test test test test test"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "    message message message message message message message message"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).with(
      "                        message message message"
    ).ordered

    expect(gitlab_post_receive).to receive(:puts).ordered
    expect(gitlab_post_receive).to receive(:puts).with(
      "========================================================================"
    ).ordered
  end
end