BigW Consortium Gitlab

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

3
describe API::API, api: true  do
4 5 6 7 8 9 10 11 12 13 14 15
  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
      it "should return authentication error" do
        get api("/hooks")
16
        expect(response.status).to eq(401)
17 18 19 20 21 22
      end
    end

    context "when not an admin" do
      it "should return forbidden error" do
        get api("/hooks", user)
23
        expect(response.status).to eq(403)
24 25 26 27 28 29
      end
    end

    context "when authenticated as admin" do
      it "should return an array of hooks" do
        get api("/hooks", admin)
30 31 32
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['url']).to eq(hook.url)
33 34 35 36 37 38
      end
    end
  end

  describe "POST /hooks" do
    it "should create new hook" do
39
      expect do
40
        post api("/hooks", admin), url: 'http://example.com'
41
      end.to change { SystemHook.count }.by(1)
42 43 44 45
    end

    it "should respond with 400 if url not given" do
      post api("/hooks", admin)
46
      expect(response.status).to eq(400)
47 48 49
    end

    it "should not create new hook without url" do
50
      expect do
51
        post api("/hooks", admin)
52
      end.to_not change { SystemHook.count }
53 54 55 56 57 58
    end
  end

  describe "GET /hooks/:id" do
    it "should return hook by id" do
      get api("/hooks/#{hook.id}", admin)
59 60
      expect(response.status).to eq(200)
      expect(json_response['event_name']).to eq('project_create')
61 62 63 64
    end

    it "should return 404 on failure" do
      get api("/hooks/404", admin)
65
      expect(response.status).to eq(404)
66 67 68 69 70
    end
  end

  describe "DELETE /hooks/:id" do
    it "should delete a hook" do
71
      expect do
72
        delete api("/hooks/#{hook.id}", admin)
73
      end.to change { SystemHook.count }.by(-1)
74 75 76 77
    end

    it "should return success if hook id not found" do
      delete api("/hooks/12345", admin)
78
      expect(response.status).to eq(200)
79 80 81
    end
  end
end