project_hooks_spec.rb 3.78 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3
require 'spec_helper'

describe Project, "Hooks" do
4
  let(:project) { create(:project) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5

6 7
  before do
    @key = create(:key, user: project.owner)
8
    @user = @key.user
9 10
    @key_id = @key.identifier
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
11

12 13
  describe "Post Receive Event" do
    it "should create push event" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14
      oldrev, newrev, ref = '00000000000000000000000000000000', 'newrev', 'refs/heads/master'
15 16 17
      data = project.post_receive_data(oldrev, newrev, ref, @user)

      project.observe_push(data)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18 19 20 21
      event = Event.last

      event.should_not be_nil
      event.project.should == project
22
      event.action.should == Event::PUSHED
23
      event.data.should == data
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
24 25 26
    end
  end

27
  describe "Project hooks" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28 29 30
    context "with no web hooks" do
      it "raises no errors" do
        lambda {
31
          project.execute_hooks({})
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
32 33 34 35 36 37
        }.should_not raise_error
      end
    end

    context "with web hooks" do
      before do
38 39
        @project_hook = create(:project_hook)
        @project_hook_2 = create(:project_hook)
40
        project.hooks << [@project_hook, @project_hook_2]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41 42 43

        stub_request(:post, @project_hook.url)
        stub_request(:post, @project_hook_2.url)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
44 45 46
      end

      it "executes multiple web hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
47 48
        @project_hook.should_receive(:async_execute).once
        @project_hook_2.should_receive(:async_execute).once
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
49

50
        project.trigger_post_receive('oldrev', 'newrev', 'refs/heads/master', @user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
51 52 53 54 55
      end
    end

    context "does not execute web hooks" do
      before do
56
        @project_hook = create(:project_hook)
57
        project.hooks << [@project_hook]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
58 59 60
      end

      it "when pushing a branch for the first time" do
61
        @project_hook.should_not_receive(:execute)
62
        project.trigger_post_receive('00000000000000000000000000000000', 'newrev', 'refs/heads/master', @user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
63 64 65
      end

      it "when pushing tags" do
66
        @project_hook.should_not_receive(:execute)
67
        project.trigger_post_receive('oldrev', 'newrev', 'refs/tags/v1.0.0', @user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
68 69 70 71 72 73 74 75 76
      end
    end

    context "when pushing new branches" do

    end

    context "when gathering commit data" do
      before do
77 78 79
        @oldrev, @newrev, @ref = project.repository.fresh_commits(2).last.sha,
          project.repository.fresh_commits(2).first.sha, 'refs/heads/master'
        @commit = project.repository.fresh_commits(2).first
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
80 81 82 83

        # Fill nil/empty attributes
        project.description = "This is a description"

84
        @data = project.post_receive_data(@oldrev, @newrev, @ref, @user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
85 86 87 88 89 90 91
      end

      subject { @data }

      it { should include(before: @oldrev) }
      it { should include(after: @newrev) }
      it { should include(ref: @ref) }
92 93
      it { should include(user_id: project.owner.id) }
      it { should include(user_name: project.owner.name) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
94 95 96 97 98

      context "with repository data" do
        subject { @data[:repository] }

        it { should include(name: project.name) }
99
        it { should include(url: project.url_to_repo) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        it { should include(description: project.description) }
        it { should include(homepage: project.web_url) }
      end

      context "with commits" do
        subject { @data[:commits] }

        it { should be_an(Array) }
        it { should have(1).element }

        context "the commit" do
          subject { @data[:commits].first }

          it { should include(id: @commit.id) }
          it { should include(message: @commit.safe_message) }
          it { should include(timestamp: @commit.date.xmlschema) }
116
          it { should include(url: "#{Gitlab.config.gitlab.url}/#{project.code}/commit/#{@commit.id}") }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
117 118 119 120 121 122 123 124 125 126 127 128

          context "with a author" do
            subject { @data[:commits].first[:author] }

            it { should include(name: @commit.author_name) }
            it { should include(email: @commit.author_email) }
          end
        end
      end
    end
  end
end