system_hook_spec.rb 3.87 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: web_hooks
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5 6 7
#  id                    :integer          not null, primary key
#  url                   :string(255)
#  project_id            :integer
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
8 9
#  created_at            :datetime
#  updated_at            :datetime
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10 11 12 13 14
#  type                  :string(255)      default("ProjectHook")
#  service_id            :integer
#  push_events           :boolean          default(TRUE), not null
#  issues_events         :boolean          default(FALSE), not null
#  merge_requests_events :boolean          default(FALSE), not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15
#  tag_push_events       :boolean          default(FALSE)
Stan Hu's avatar
Stan Hu committed
16
#  note_events           :boolean          default(FALSE), not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17 18
#

Valeriy Sizov's avatar
Valeriy Sizov committed
19 20
require "spec_helper"

Douwe Maan's avatar
Douwe Maan committed
21
describe SystemHook, models: true do
Valeriy Sizov's avatar
Valeriy Sizov committed
22
  describe "execute" do
23 24 25 26 27 28 29
    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
30 31 32
    end

    it "project_create hook" do
33 34
      Projects::CreateService.new(user, name: 'empty').execute
      expect(WebMock).to have_requested(:post, system_hook.url).with(
35
        body: /project_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 "project_destroy hook" do
41
      Projects::DestroyService.new(project, user, {}).pending_delete!
42 43

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

    it "user_create hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
50
      create(:user)
51 52
      
      expect(WebMock).to have_requested(:post, system_hook.url).with(
53
        body: /user_create/,
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 "user_destroy hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59
      user.destroy
60 61

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

Valeriy Sizov's avatar
Valeriy Sizov committed
67
    it "project_create hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
68
      project.team << [user, :master]
69 70

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

Valeriy Sizov's avatar
Valeriy Sizov committed
76
    it "project_destroy hook" do
77
      project.team << [user, :master]
78
      project.project_members.destroy_all
79 80

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

    it 'group create hook' do
      create(:group)
88 89

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

    it 'group destroy hook' do
      group.destroy
97 98

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

    it 'group member create hook' do
105
      group.add_master(user)
106 107

      expect(WebMock).to have_requested(:post, system_hook.url).with(
108
        body: /user_add_to_group/,
109
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
110 111 112 113
      ).once
    end

    it 'group member destroy hook' do
114
      group.add_master(user)
115
      group.group_members.destroy_all
116 117

      expect(WebMock).to have_requested(:post, system_hook.url).with(
118
        body: /user_remove_from_group/,
119
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
120 121
      ).once
    end
Valeriy Sizov's avatar
Valeriy Sizov committed
122 123
  end
end