From 86bd11cbd8796adc31849aa02317604abb1576e2 Mon Sep 17 00:00:00 2001 From: Valeriy Sizov Date: Thu, 19 Jul 2012 00:24:37 +0300 Subject: [PATCH] System Hooks: rspec --- app/controllers/admin/hooks_controller.rb | 12 +++++++++--- spec/factories.rb | 10 ++++++++++ spec/models/system_hook_spec.rb | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ spec/requests/admin/admin_hooks_spec.rb | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 spec/models/system_hook_spec.rb create mode 100644 spec/requests/admin/admin_hooks_spec.rb diff --git a/app/controllers/admin/hooks_controller.rb b/app/controllers/admin/hooks_controller.rb index 446d4ea..7f832fd 100644 --- a/app/controllers/admin/hooks_controller.rb +++ b/app/controllers/admin/hooks_controller.rb @@ -28,9 +28,15 @@ class Admin::HooksController < ApplicationController def test - @hook = @project.hooks.find(params[:id]) - commits = @project.commits(@project.default_branch, nil, 3) - data = @project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{@project.default_branch}", current_user) + @hook = SystemHook.find(params[:hook_id]) + data = { + event_name: "project_create", + name: "Ruby", + path: "ruby", + project_id: 1, + owner_name: "Someone", + owner_email: "example@gitlabhq.com" + } @hook.execute(data) redirect_to :back diff --git a/spec/factories.rb b/spec/factories.rb index d657055..ab2ca46 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -7,6 +7,12 @@ Factory.add(:project, Project) do |obj| obj.code = 'LGT' end +Factory.add(:project_without_owner, Project) do |obj| + obj.name = Faker::Internet.user_name + obj.path = 'gitlabhq' + obj.code = 'LGT' +end + Factory.add(:public_project, Project) do |obj| obj.name = Faker::Internet.user_name obj.path = 'gitlabhq' @@ -64,6 +70,10 @@ Factory.add(:project_hook, ProjectHook) do |obj| obj.url = Faker::Internet.uri("http") end +Factory.add(:system_hook, SystemHook) do |obj| + obj.url = Faker::Internet.uri("http") +end + Factory.add(:wiki, Wiki) do |obj| obj.title = Faker::Lorem.sentence obj.content = Faker::Lorem.sentence diff --git a/spec/models/system_hook_spec.rb b/spec/models/system_hook_spec.rb new file mode 100644 index 0000000..661ba6b --- /dev/null +++ b/spec/models/system_hook_spec.rb @@ -0,0 +1,61 @@ +require "spec_helper" + +describe SystemHook do + describe "execute" do + before(:each) do + @system_hook = Factory :system_hook + WebMock.stub_request(:post, @system_hook.url) + end + + it "project_create hook" do + user = Factory :user + with_resque do + project = Factory :project_without_owner, :owner => user + end + WebMock.should have_requested(:post, @system_hook.url).with(body: /project_create/).once + end + + it "project_destroy hook" do + project = Factory :project + with_resque do + project.destroy + end + WebMock.should have_requested(:post, @system_hook.url).with(body: /project_destroy/).once + end + + it "user_create hook" do + with_resque do + Factory :user + end + WebMock.should have_requested(:post, @system_hook.url).with(body: /user_create/).once + end + + it "user_destroy hook" do + user = Factory :user + with_resque do + user.destroy + end + WebMock.should have_requested(:post, @system_hook.url).with(body: /user_destroy/).once + end + + it "project_create hook" do + user = Factory :user + project = Factory :project + with_resque do + project.users << user + end + WebMock.should have_requested(:post, @system_hook.url).with(body: /user_add_to_team/).once + end + + it "project_destroy hook" do + user = Factory :user + project = Factory :project + project.users << user + with_resque do + project.users_projects.clear + end + WebMock.should have_requested(:post, @system_hook.url).with(body: /user_remove_from_team/).once + end + end + +end diff --git a/spec/requests/admin/admin_hooks_spec.rb b/spec/requests/admin/admin_hooks_spec.rb new file mode 100644 index 0000000..e8a345b --- /dev/null +++ b/spec/requests/admin/admin_hooks_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +describe "Admin::Hooks" do + before do + @project = Factory :project, + :name => "LeGiT", + :code => "LGT" + login_as :admin + + @system_hook = Factory :system_hook + + end + + describe "GET /admin/hooks" do + it "should be ok" do + visit admin_root_path + within ".main_menu" do + click_on "Hooks" + end + current_path.should == admin_hooks_path + end + + it "should have hooks list" do + visit admin_hooks_path + page.should have_content(@system_hook.url) + end + end + + describe "New Hook" do + before do + @url = Faker::Internet.uri("http") + visit admin_hooks_path + fill_in "hook_url", :with => @url + expect { click_button "Add System Hook" }.to change(SystemHook, :count).by(1) + end + + it "should open new hook popup" do + page.current_path.should == admin_hooks_path + page.should have_content(@url) + end + end + + describe "Test" do + before do + WebMock.stub_request(:post, @system_hook.url) + visit admin_hooks_path + click_link "Test Hook" + end + + it { page.current_path.should == admin_hooks_path } + end + +end -- libgit2 0.21.2