BigW Consortium Gitlab

post_receive_spec.rb 5.13 KB
Newer Older
1 2 3
require 'spec_helper'

describe PostReceive do
4 5 6
  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) }
7
  let(:gl_repository) { "project-#{project.id}" }
8 9
  let(:key) { create(:key, user: project.owner) }
  let(:key_id) { key.shell_id }
10

11 12 13 14
  let(:project) do
    create(:project, :repository, auto_cancel_pending_pipelines: 'disabled')
  end

15
  context "as a sidekiq worker" do
16
    it "responds to #perform" do
17
      expect(described_class.new).to respond_to(:perform)
18 19 20
    end
  end

21
  context 'with a non-existing project' do
22
    let(:gl_repository) { "project-123456789" }
23
    let(:error_message) do
24
      "Triggered hook for non-existing project with gl_repository \"#{gl_repository}\""
25 26 27 28
    end

    it "returns false and logs an error" do
      expect(Gitlab::GitLogger).to receive(:error).with("POST-RECEIVE: #{error_message}")
29
      expect(described_class.new.perform(gl_repository, key_id, base64_changes)).to be(false)
30 31 32
    end
  end

33 34 35 36
  describe "#process_project_changes" do
    before do
      allow_any_instance_of(Gitlab::GitPostReceive).to receive(:identify).and_return(project.owner)
    end
37

38 39 40
    context "branches" do
      let(:changes) { "123456 789012 refs/heads/tést" }

41
      it "calls GitTagPushService" do
42 43
        expect_any_instance_of(GitPushService).to receive(:execute).and_return(true)
        expect_any_instance_of(GitTagPushService).not_to receive(:execute)
44
        described_class.new.perform(gl_repository, key_id, base64_changes)
45 46 47 48 49 50
      end
    end

    context "tags" do
      let(:changes) { "123456 789012 refs/tags/tag" }

51
      it "calls GitTagPushService" do
52 53
        expect_any_instance_of(GitPushService).not_to receive(:execute)
        expect_any_instance_of(GitTagPushService).to receive(:execute).and_return(true)
54
        described_class.new.perform(gl_repository, key_id, base64_changes)
55 56 57 58 59 60
      end
    end

    context "merge-requests" do
      let(:changes) { "123456 789012 refs/merge-requests/123" }

61
      it "does not call any of the services" do
62 63
        expect_any_instance_of(GitPushService).not_to receive(:execute)
        expect_any_instance_of(GitTagPushService).not_to receive(:execute)
64
        described_class.new.perform(gl_repository, key_id, base64_changes)
65 66
      end
    end
67 68

    context "gitlab-ci.yml" do
69
      subject { described_class.new.perform(gl_repository, key_id, base64_changes) }
70

71
      context "creates a Ci::Pipeline for every change" do
72 73
        before do
          stub_ci_pipeline_to_return_yaml_file
74 75 76 77 78 79 80 81

          # TODO, don't stub private methods
          #
          allow_any_instance_of(Ci::CreatePipelineService)
            .to receive(:commit).and_return(OpenStruct.new(id: '123456'))

          allow_any_instance_of(Repository)
            .to receive(:branch_exists?).and_return(true)
82
        end
83

84
        it { expect { subject }.to change { Ci::Pipeline.count }.by(2) }
85 86
      end

87
      context "does not create a Ci::Pipeline" do
88 89 90
        before do
          stub_ci_pipeline_yaml_file(nil)
        end
91

92
        it { expect { subject }.not_to change { Ci::Pipeline.count } }
93 94
      end
    end
95

96 97 98
    context 'after project changes hooks' do
      let(:changes) { '123456 789012 refs/heads/tést' }
      let(:fake_hook_data) { Hash.new(event_name: 'repository_update') }
99

100 101 102 103 104 105
      before do
        allow_any_instance_of(Gitlab::DataBuilder::Repository).to receive(:update).and_return(fake_hook_data)
        # silence hooks so we can isolate
        allow_any_instance_of(Key).to receive(:post_create_hook).and_return(true)
        allow_any_instance_of(GitPushService).to receive(:execute).and_return(true)
      end
106

107 108
      it 'calls SystemHooksService' do
        expect_any_instance_of(SystemHooksService).to receive(:execute_hooks).with(fake_hook_data, :repository_update_hooks).and_return(true)
109

110
        described_class.new.perform(gl_repository, key_id, base64_changes)
111
      end
112 113 114
    end
  end

115
  context "webhook" do
116
    it "fetches the correct project" do
117
      expect(Project).to receive(:find_by).with(id: project.id.to_s)
118
      described_class.new.perform(gl_repository, key_id, base64_changes)
119
    end
120

121
    it "does not run if the author is not in the project" do
122 123 124
      allow_any_instance_of(Gitlab::GitPostReceive)
        .to receive(:identify_using_ssh_key)
        .and_return(nil)
125

126
      expect(project).not_to receive(:execute_hooks)
127

128
      expect(described_class.new.perform(gl_repository, key_id, base64_changes)).to be_falsey
129 130
    end

131
    it "asks the project to trigger all hooks" do
132
      allow(Project).to receive(:find_by).and_return(project)
133

134 135
      expect(project).to receive(:execute_hooks).twice
      expect(project).to receive(:execute_services).twice
136

137
      described_class.new.perform(gl_repository, key_id, base64_changes)
138 139 140
    end

    it "enqueues a UpdateMergeRequestsWorker job" do
141
      allow(Project).to receive(:find_by).and_return(project)
142

143
      expect(UpdateMergeRequestsWorker).to receive(:perform_async).with(project.id, project.owner.id, any_args)
144

145
      described_class.new.perform(gl_repository, key_id, base64_changes)
146 147 148
    end
  end
end