summaryrefslogtreecommitdiff
path: root/spec/gitlab_post_receive_spec.rb
blob: 5a9892a13c02257f238b28bcd94d6074f1974d2c (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
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(:gitlab_post_receive) { GitlabPostReceive.new(repo_path, actor, wrongly_encoded_changes) }
  let(:message) { "test " * 10 + "message " * 10 }

  before do
    GitlabConfig.any_instance.stub(repos_path: repository_path)
    GitlabNet.any_instance.stub(broadcast_message: { "message" => message })
  end

  describe "#exec" do

    before do
      GitlabConfig.any_instance.stub(redis_command: %w(env -i redis-cli))
      allow(gitlab_post_receive).to receive(:system).and_return(true)
    end

    it "resets the GL_ID environment variable" do
      ENV["GL_ID"] = actor

      gitlab_post_receive.exec

      expect(ENV["GL_ID"]).to be_nil
    end

    it "prints the broadcast message" do
      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

      gitlab_post_receive.exec
    end

    it "pushes a Sidekiq job onto the queue" do
      expect(gitlab_post_receive).to receive(:system).with(
        *[
          *%w(env -i redis-cli rpush resque:gitlab:queue:post_receive), 
          %Q/{"class":"PostReceive","args":["#{repo_path}","#{actor}",#{base64_changes.inspect}]}/,
          { err: "/dev/null", out: "/dev/null" }
        ]
      ).and_return(true)

      gitlab_post_receive.exec
    end

    context "when the redis command succeeds" do

      before do
        allow(gitlab_post_receive).to receive(:system).and_return(true)
      end

      it "returns true" do
        expect(gitlab_post_receive.exec).to eq(true)
      end
    end

    context "when the redis command fails" do

      before do
        allow(gitlab_post_receive).to receive(:system).and_return(false)
        allow($?).to receive(:exitstatus).and_return(nil)
      end

      it "returns false" do
        expect(gitlab_post_receive.exec).to eq(false)
      end
    end
  end
end