system_hook_spec.rb 4.11 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 23
  describe "execute" do
    before(:each) do
24
      @system_hook = create(:system_hook)
Valeriy Sizov's avatar
Valeriy Sizov committed
25 26 27 28
      WebMock.stub_request(:post, @system_hook.url)
    end

    it "project_create hook" do
29
      Projects::CreateService.new(create(:user), name: 'empty').execute
30 31
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
        body: /project_create/,
32
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
33
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
34
    end
35

Valeriy Sizov's avatar
Valeriy Sizov committed
36
    it "project_destroy hook" do
37 38
      user = create(:user)
      project = create(:empty_project, namespace: user.namespace)
39
      Projects::DestroyService.new(project, user, {}).pending_delete!
40 41
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
        body: /project_destroy/,
42
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
43
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
44 45 46
    end

    it "user_create hook" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
47
      create(:user)
48 49
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
        body: /user_create/,
50
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
51
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
52
    end
53

Valeriy Sizov's avatar
Valeriy Sizov committed
54
    it "user_destroy hook" do
55
      user = create(:user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
56
      user.destroy
57 58
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
        body: /user_destroy/,
59
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
60
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
61
    end
62

Valeriy Sizov's avatar
Valeriy Sizov committed
63
    it "project_create hook" do
64 65
      user = create(:user)
      project = create(:project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
66
      project.team << [user, :master]
67
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
68
        body: /user_add_to_team/,
69
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
70
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
71
    end
72

Valeriy Sizov's avatar
Valeriy Sizov committed
73
    it "project_destroy hook" do
74 75
      user = create(:user)
      project = create(:project)
76
      project.team << [user, :master]
77
      project.project_members.destroy_all
78
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
79
        body: /user_remove_from_team/,
80
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
81
      ).once
Valeriy Sizov's avatar
Valeriy Sizov committed
82
    end
83 84 85

    it 'group create hook' do
      create(:group)
86
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
87
        body: /group_create/,
88
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
89 90 91 92 93 94
      ).once
    end

    it 'group destroy hook' do
      group = create(:group)
      group.destroy
95
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
96
        body: /group_destroy/,
97
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
98 99 100 101 102 103
      ).once
    end

    it 'group member create hook' do
      group = create(:group)
      user = create(:user)
104
      group.add_master(user)
105
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
106
        body: /user_add_to_group/,
107
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
108 109 110 111 112 113
      ).once
    end

    it 'group member destroy hook' do
      group = create(:group)
      user = create(:user)
114
      group.add_master(user)
115
      group.group_members.destroy_all
116
      expect(WebMock).to have_requested(:post, @system_hook.url).with(
117
        body: /user_remove_from_group/,
118
        headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'System Hook' }
119 120 121
      ).once
    end

Valeriy Sizov's avatar
Valeriy Sizov committed
122 123
  end
end