summaryrefslogtreecommitdiff
path: root/spec/models/hooks
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-01-30 15:40:04 -0500
committerRobert Speicher <rspeicher@gmail.com>2015-01-30 15:51:10 -0500
commitab6f7164e03139889c09a6a207e9df3481e57b3b (patch)
treec1c107612a9db89bbdc4f10c222bfcb4f5b44119 /spec/models/hooks
parentc47328948b5fff218c68279260a57ab6b03e7423 (diff)
downloadgitlab-ce-ab6f7164e03139889c09a6a207e9df3481e57b3b.tar.gz
Make the structure of spec/models match app/models
Diffstat (limited to 'spec/models/hooks')
-rw-r--r--spec/models/hooks/project_hook_spec.rb36
-rw-r--r--spec/models/hooks/service_hook_spec.rb24
-rw-r--r--spec/models/hooks/system_hook_spec.rb100
-rw-r--r--spec/models/hooks/web_hook_spec.rb74
4 files changed, 234 insertions, 0 deletions
diff --git a/spec/models/hooks/project_hook_spec.rb b/spec/models/hooks/project_hook_spec.rb
new file mode 100644
index 00000000000..4e0d50d7f3f
--- /dev/null
+++ b/spec/models/hooks/project_hook_spec.rb
@@ -0,0 +1,36 @@
+# == Schema Information
+#
+# Table name: web_hooks
+#
+# id :integer not null, primary key
+# url :string(255)
+# project_id :integer
+# created_at :datetime
+# updated_at :datetime
+# 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
+# tag_push_events :boolean default(FALSE)
+#
+
+require 'spec_helper'
+
+describe ProjectHook do
+ describe '.push_hooks' do
+ it 'should return hooks for push events only' do
+ hook = create(:project_hook, push_events: true)
+ hook2 = create(:project_hook, push_events: false)
+ expect(ProjectHook.push_hooks).to eq([hook])
+ end
+ end
+
+ describe '.tag_push_hooks' do
+ it 'should return hooks for tag push events only' do
+ hook = create(:project_hook, tag_push_events: true)
+ hook2 = create(:project_hook, tag_push_events: false)
+ expect(ProjectHook.tag_push_hooks).to eq([hook])
+ end
+ end
+end
diff --git a/spec/models/hooks/service_hook_spec.rb b/spec/models/hooks/service_hook_spec.rb
new file mode 100644
index 00000000000..6ec82438dfe
--- /dev/null
+++ b/spec/models/hooks/service_hook_spec.rb
@@ -0,0 +1,24 @@
+# == Schema Information
+#
+# Table name: web_hooks
+#
+# id :integer not null, primary key
+# url :string(255)
+# project_id :integer
+# created_at :datetime
+# updated_at :datetime
+# 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
+# tag_push_events :boolean default(FALSE)
+#
+
+require "spec_helper"
+
+describe ServiceHook do
+ describe "Associations" do
+ it { should belong_to :service }
+ end
+end
diff --git a/spec/models/hooks/system_hook_spec.rb b/spec/models/hooks/system_hook_spec.rb
new file mode 100644
index 00000000000..8deb732de9c
--- /dev/null
+++ b/spec/models/hooks/system_hook_spec.rb
@@ -0,0 +1,100 @@
+# == Schema Information
+#
+# Table name: web_hooks
+#
+# id :integer not null, primary key
+# url :string(255)
+# project_id :integer
+# created_at :datetime
+# updated_at :datetime
+# 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
+# tag_push_events :boolean default(FALSE)
+#
+
+require "spec_helper"
+
+describe SystemHook do
+ describe "execute" do
+ before(:each) do
+ @system_hook = create(:system_hook)
+ WebMock.stub_request(:post, @system_hook.url)
+ end
+
+ it "project_create hook" do
+ Projects::CreateService.new(create(:user), name: 'empty').execute
+ WebMock.should have_requested(:post, @system_hook.url).with(body: /project_create/).once
+ end
+
+ it "project_destroy hook" do
+ user = create(:user)
+ project = create(:empty_project, namespace: user.namespace)
+ Projects::DestroyService.new(project, user, {}).execute
+ WebMock.should have_requested(:post, @system_hook.url).with(body: /project_destroy/).once
+ end
+
+ it "user_create hook" do
+ create(:user)
+ WebMock.should have_requested(:post, @system_hook.url).with(body: /user_create/).once
+ end
+
+ it "user_destroy hook" do
+ user = create(:user)
+ user.destroy
+ WebMock.should have_requested(:post, @system_hook.url).with(body: /user_destroy/).once
+ end
+
+ it "project_create hook" do
+ user = create(:user)
+ project = create(:project)
+ project.team << [user, :master]
+ WebMock.should have_requested(:post, @system_hook.url).with(body: /user_add_to_team/).once
+ end
+
+ it "project_destroy hook" do
+ user = create(:user)
+ project = create(:project)
+ project.team << [user, :master]
+ project.project_members.destroy_all
+ WebMock.should have_requested(:post, @system_hook.url).with(body: /user_remove_from_team/).once
+ end
+
+ it 'group create hook' do
+ create(:group)
+ WebMock.should have_requested(:post, @system_hook.url).with(
+ body: /group_create/
+ ).once
+ end
+
+ it 'group destroy hook' do
+ group = create(:group)
+ group.destroy
+ WebMock.should have_requested(:post, @system_hook.url).with(
+ body: /group_destroy/
+ ).once
+ end
+
+ it 'group member create hook' do
+ group = create(:group)
+ user = create(:user)
+ group.add_user(user, Gitlab::Access::MASTER)
+ WebMock.should have_requested(:post, @system_hook.url).with(
+ body: /user_add_to_group/
+ ).once
+ end
+
+ it 'group member destroy hook' do
+ group = create(:group)
+ user = create(:user)
+ group.add_user(user, Gitlab::Access::MASTER)
+ group.group_members.destroy_all
+ WebMock.should have_requested(:post, @system_hook.url).with(
+ body: /user_remove_from_group/
+ ).once
+ end
+
+ end
+end
diff --git a/spec/models/hooks/web_hook_spec.rb b/spec/models/hooks/web_hook_spec.rb
new file mode 100644
index 00000000000..e9c04ee89cb
--- /dev/null
+++ b/spec/models/hooks/web_hook_spec.rb
@@ -0,0 +1,74 @@
+# == Schema Information
+#
+# Table name: web_hooks
+#
+# id :integer not null, primary key
+# url :string(255)
+# project_id :integer
+# created_at :datetime
+# updated_at :datetime
+# 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
+# tag_push_events :boolean default(FALSE)
+#
+
+require 'spec_helper'
+
+describe ProjectHook do
+ describe "Associations" do
+ it { should belong_to :project }
+ end
+
+ describe "Mass assignment" do
+ end
+
+ describe "Validations" do
+ it { should validate_presence_of(:url) }
+
+ context "url format" do
+ it { should allow_value("http://example.com").for(:url) }
+ it { should allow_value("https://excample.com").for(:url) }
+ it { should allow_value("http://test.com/api").for(:url) }
+ it { should allow_value("http://test.com/api?key=abc").for(:url) }
+ it { should allow_value("http://test.com/api?key=abc&type=def").for(:url) }
+
+ it { should_not allow_value("example.com").for(:url) }
+ it { should_not allow_value("ftp://example.com").for(:url) }
+ it { should_not allow_value("herp-and-derp").for(:url) }
+ end
+ end
+
+ describe "execute" do
+ before(:each) do
+ @project_hook = create(:project_hook)
+ @project = create(:project)
+ @project.hooks << [@project_hook]
+ @data = { before: 'oldrev', after: 'newrev', ref: 'ref'}
+
+ WebMock.stub_request(:post, @project_hook.url)
+ end
+
+ it "POSTs to the web hook URL" do
+ @project_hook.execute(@data)
+ WebMock.should have_requested(:post, @project_hook.url).once
+ end
+
+ it "POSTs the data as JSON" do
+ json = @data.to_json
+
+ @project_hook.execute(@data)
+ WebMock.should have_requested(:post, @project_hook.url).with(body: json).once
+ end
+
+ it "catches exceptions" do
+ WebHook.should_receive(:post).and_raise("Some HTTP Post error")
+
+ lambda {
+ @project_hook.execute(@data)
+ }.should raise_error
+ end
+ end
+end