BigW Consortium Gitlab

system_hooks_spec.rb 2.66 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::SystemHooks, api: true  do
4 5 6 7 8 9 10 11 12 13
  include ApiHelpers

  let(:user) { create(:user) }
  let(:admin) { create(:admin) }
  let!(:hook) { create(:system_hook, url: "http://example.com") }

  before { stub_request(:post, hook.url) }

  describe "GET /hooks" do
    context "when no user" do
14
      it "returns authentication error" do
15
        get api("/hooks")
16

17
        expect(response).to have_http_status(401)
18 19 20 21
      end
    end

    context "when not an admin" do
22
      it "returns forbidden error" do
23
        get api("/hooks", user)
24

25
        expect(response).to have_http_status(403)
26 27 28 29
      end
    end

    context "when authenticated as admin" do
30
      it "returns an array of hooks" do
31
        get api("/hooks", admin)
32

33
        expect(response).to have_http_status(200)
34 35
        expect(json_response).to be_an Array
        expect(json_response.first['url']).to eq(hook.url)
36 37
        expect(json_response.first['push_events']).to be true
        expect(json_response.first['tag_push_events']).to be false
38 39 40 41 42
      end
    end
  end

  describe "POST /hooks" do
43
    it "creates new hook" do
44
      expect do
45
        post api("/hooks", admin), url: 'http://example.com'
46
      end.to change { SystemHook.count }.by(1)
47 48
    end

49
    it "responds with 400 if url not given" do
50
      post api("/hooks", admin)
51

52
      expect(response).to have_http_status(400)
53 54
    end

55 56 57 58 59 60
    it "responds with 400 if url is invalid" do
      post api("/hooks", admin), url: 'hp://mep.mep'

      expect(response).to have_http_status(400)
    end

61
    it "does not create new hook without url" do
62
      expect do
63
        post api("/hooks", admin)
64
      end.not_to change { SystemHook.count }
65
    end
66

67
    it 'sets default values for events' do
68 69 70 71 72 73
      post api('/hooks', admin), url: 'http://mep.mep', enable_ssl_verification: true

      expect(response).to have_http_status(201)
      expect(json_response['enable_ssl_verification']).to be true
      expect(json_response['tag_push_events']).to be false
    end
74 75 76
  end

  describe "GET /hooks/:id" do
77
    it "returns hook by id" do
78
      get api("/hooks/#{hook.id}", admin)
79
      expect(response).to have_http_status(200)
80
      expect(json_response['event_name']).to eq('project_create')
81 82
    end

83
    it "returns 404 on failure" do
84
      get api("/hooks/404", admin)
85
      expect(response).to have_http_status(404)
86 87 88 89
    end
  end

  describe "DELETE /hooks/:id" do
90
    it "deletes a hook" do
91
      expect do
92
        delete api("/hooks/#{hook.id}", admin)
93
      end.to change { SystemHook.count }.by(-1)
94 95
    end

96 97 98 99
    it 'returns 404 if the system hook does not exist' do
      delete api('/hooks/12345', admin)

      expect(response).to have_http_status(404)
100 101 102
    end
  end
end