system_hook_spec.rb 3.23 KB
Newer Older
Valeriy Sizov's avatar
Valeriy Sizov committed
1 2
require "spec_helper"

Douwe Maan's avatar
Douwe Maan committed
3
describe SystemHook, models: true do
Valeriy Sizov's avatar
Valeriy Sizov committed
4
  describe "execute" do
5 6 7 8 9 10 11
    let(:system_hook) { create(:system_hook) }
    let(:user)        { create(:user) }
    let(:project)     { create(:project, namespace: user.namespace) }
    let(:group)       { create(:group) }

    before do
      WebMock.stub_request(:post, system_hook.url)
Valeriy Sizov's avatar
Valeriy Sizov committed
12 13 14
    end

    it "project_create hook" do
15 16
      Projects::CreateService.new(user, name: 'empty').execute
      expect(WebMock).to have_requested(:post, system_hook.url).with(
17
        body: /project_create/,
18
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
19
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
20
    end
21

Valeriy Sizov's avatar
Valeriy Sizov committed
22
    it "project_destroy hook" do
Stan Hu's avatar
Stan Hu committed
23
      Projects::DestroyService.new(project, user, {}).async_execute
24 25

      expect(WebMock).to have_requested(:post, system_hook.url).with(
26
        body: /project_destroy/,
27
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
28
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
29 30 31
    end

    it "user_create hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
32
      create(:user)
33

34
      expect(WebMock).to have_requested(:post, system_hook.url).with(
35
        body: /user_create/,
36
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
37
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
38
    end
39

Valeriy Sizov's avatar
Valeriy Sizov committed
40
    it "user_destroy hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41
      user.destroy
42 43

      expect(WebMock).to have_requested(:post, system_hook.url).with(
44
        body: /user_destroy/,
45
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
46
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
47
    end
48

Valeriy Sizov's avatar
Valeriy Sizov committed
49
    it "project_create hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
50
      project.team << [user, :master]
51 52

      expect(WebMock).to have_requested(:post, system_hook.url).with(
53
        body: /user_add_to_team/,
54
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
55
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
56
    end
57

Valeriy Sizov's avatar
Valeriy Sizov committed
58
    it "project_destroy hook" do
59
      project.team << [user, :master]
60
      project.project_members.destroy_all
61 62

      expect(WebMock).to have_requested(:post, system_hook.url).with(
63
        body: /user_remove_from_team/,
64
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
65
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
66
    end
67 68 69

    it 'group create hook' do
      create(:group)
70 71

      expect(WebMock).to have_requested(:post, system_hook.url).with(
72
        body: /group_create/,
73
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
74 75 76 77 78
      ).once
    end

    it 'group destroy hook' do
      group.destroy
79 80

      expect(WebMock).to have_requested(:post, system_hook.url).with(
81
        body: /group_destroy/,
82
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
83 84 85 86
      ).once
    end

    it 'group member create hook' do
87
      group.add_master(user)
88 89

      expect(WebMock).to have_requested(:post, system_hook.url).with(
90
        body: /user_add_to_group/,
91
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
92 93 94 95
      ).once
    end

    it 'group member destroy hook' do
96
      group.add_master(user)
97
      group.group_members.destroy_all
98 99

      expect(WebMock).to have_requested(:post, system_hook.url).with(
100
        body: /user_remove_from_group/,
101
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
102 103
      ).once
    end
Valeriy Sizov's avatar
Valeriy Sizov committed
104 105
  end
end