system_hook_spec.rb 3.39 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
    let(:system_hook) { create(:system_hook) }
    let(:user)        { create(:user) }
7
    let(:project)     { create(:empty_project, namespace: user.namespace) }
8
    let(:group)       { create(:group) }
9 10 11
    let(:params) do
      { name: 'John Doe', username: 'jduser', email: 'jg@example.com', password: 'mydummypass' }
    end
12 13 14

    before do
      WebMock.stub_request(:post, system_hook.url)
Valeriy Sizov's avatar
Valeriy Sizov committed
15 16 17
    end

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

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

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

    it "user_create hook" do
35
      Users::CreateService.new(nil, params).execute
36

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

Valeriy Sizov's avatar
Valeriy Sizov committed
43
    it "user_destroy hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
44
      user.destroy
45 46

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

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

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

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

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

    it 'group create hook' do
      create(:group)
73 74

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

    it 'group destroy hook' do
      group.destroy
82 83

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

    it 'group member create hook' do
90
      group.add_master(user)
91 92

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

    it 'group member destroy hook' do
99
      group.add_master(user)
100
      group.group_members.destroy_all
101 102

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