Commit 86bd11cbd8796adc31849aa02317604abb1576e2

Authored by Valeriy Sizov
1 parent 655418be

System Hooks: rspec

app/controllers/admin/hooks_controller.rb
... ... @@ -28,9 +28,15 @@ class Admin::HooksController < ApplicationController
28 28  
29 29  
30 30 def test
31   - @hook = @project.hooks.find(params[:id])
32   - commits = @project.commits(@project.default_branch, nil, 3)
33   - data = @project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{@project.default_branch}", current_user)
  31 + @hook = SystemHook.find(params[:hook_id])
  32 + data = {
  33 + event_name: "project_create",
  34 + name: "Ruby",
  35 + path: "ruby",
  36 + project_id: 1,
  37 + owner_name: "Someone",
  38 + owner_email: "example@gitlabhq.com"
  39 + }
34 40 @hook.execute(data)
35 41  
36 42 redirect_to :back
... ...
spec/factories.rb
... ... @@ -7,6 +7,12 @@ Factory.add(:project, Project) do |obj|
7 7 obj.code = 'LGT'
8 8 end
9 9  
  10 +Factory.add(:project_without_owner, Project) do |obj|
  11 + obj.name = Faker::Internet.user_name
  12 + obj.path = 'gitlabhq'
  13 + obj.code = 'LGT'
  14 +end
  15 +
10 16 Factory.add(:public_project, Project) do |obj|
11 17 obj.name = Faker::Internet.user_name
12 18 obj.path = 'gitlabhq'
... ... @@ -64,6 +70,10 @@ Factory.add(:project_hook, ProjectHook) do |obj|
64 70 obj.url = Faker::Internet.uri("http")
65 71 end
66 72  
  73 +Factory.add(:system_hook, SystemHook) do |obj|
  74 + obj.url = Faker::Internet.uri("http")
  75 +end
  76 +
67 77 Factory.add(:wiki, Wiki) do |obj|
68 78 obj.title = Faker::Lorem.sentence
69 79 obj.content = Faker::Lorem.sentence
... ...
spec/models/system_hook_spec.rb 0 → 100644
... ... @@ -0,0 +1,61 @@
  1 +require "spec_helper"
  2 +
  3 +describe SystemHook do
  4 + describe "execute" do
  5 + before(:each) do
  6 + @system_hook = Factory :system_hook
  7 + WebMock.stub_request(:post, @system_hook.url)
  8 + end
  9 +
  10 + it "project_create hook" do
  11 + user = Factory :user
  12 + with_resque do
  13 + project = Factory :project_without_owner, :owner => user
  14 + end
  15 + WebMock.should have_requested(:post, @system_hook.url).with(body: /project_create/).once
  16 + end
  17 +
  18 + it "project_destroy hook" do
  19 + project = Factory :project
  20 + with_resque do
  21 + project.destroy
  22 + end
  23 + WebMock.should have_requested(:post, @system_hook.url).with(body: /project_destroy/).once
  24 + end
  25 +
  26 + it "user_create hook" do
  27 + with_resque do
  28 + Factory :user
  29 + end
  30 + WebMock.should have_requested(:post, @system_hook.url).with(body: /user_create/).once
  31 + end
  32 +
  33 + it "user_destroy hook" do
  34 + user = Factory :user
  35 + with_resque do
  36 + user.destroy
  37 + end
  38 + WebMock.should have_requested(:post, @system_hook.url).with(body: /user_destroy/).once
  39 + end
  40 +
  41 + it "project_create hook" do
  42 + user = Factory :user
  43 + project = Factory :project
  44 + with_resque do
  45 + project.users << user
  46 + end
  47 + WebMock.should have_requested(:post, @system_hook.url).with(body: /user_add_to_team/).once
  48 + end
  49 +
  50 + it "project_destroy hook" do
  51 + user = Factory :user
  52 + project = Factory :project
  53 + project.users << user
  54 + with_resque do
  55 + project.users_projects.clear
  56 + end
  57 + WebMock.should have_requested(:post, @system_hook.url).with(body: /user_remove_from_team/).once
  58 + end
  59 + end
  60 +
  61 +end
... ...
spec/requests/admin/admin_hooks_spec.rb 0 → 100644
... ... @@ -0,0 +1,53 @@
  1 +require 'spec_helper'
  2 +
  3 +describe "Admin::Hooks" do
  4 + before do
  5 + @project = Factory :project,
  6 + :name => "LeGiT",
  7 + :code => "LGT"
  8 + login_as :admin
  9 +
  10 + @system_hook = Factory :system_hook
  11 +
  12 + end
  13 +
  14 + describe "GET /admin/hooks" do
  15 + it "should be ok" do
  16 + visit admin_root_path
  17 + within ".main_menu" do
  18 + click_on "Hooks"
  19 + end
  20 + current_path.should == admin_hooks_path
  21 + end
  22 +
  23 + it "should have hooks list" do
  24 + visit admin_hooks_path
  25 + page.should have_content(@system_hook.url)
  26 + end
  27 + end
  28 +
  29 + describe "New Hook" do
  30 + before do
  31 + @url = Faker::Internet.uri("http")
  32 + visit admin_hooks_path
  33 + fill_in "hook_url", :with => @url
  34 + expect { click_button "Add System Hook" }.to change(SystemHook, :count).by(1)
  35 + end
  36 +
  37 + it "should open new hook popup" do
  38 + page.current_path.should == admin_hooks_path
  39 + page.should have_content(@url)
  40 + end
  41 + end
  42 +
  43 + describe "Test" do
  44 + before do
  45 + WebMock.stub_request(:post, @system_hook.url)
  46 + visit admin_hooks_path
  47 + click_link "Test Hook"
  48 + end
  49 +
  50 + it { page.current_path.should == admin_hooks_path }
  51 + end
  52 +
  53 +end
... ...