OSDN Git Service

Refactor API classes. So api classes like Gitlab::Issues become API::Issues
[wvm/gitlab.git] / spec / requests / api / system_hooks_spec.rb
1 require 'spec_helper'
2
3 describe API::API do
4   include ApiHelpers
5
6   let(:user) { create(:user) }
7   let(:admin) { create(:admin) }
8   let!(:hook) { create(:system_hook, url: "http://example.com") }
9
10   before { stub_request(:post, hook.url) }
11
12   describe "GET /hooks" do
13     context "when no user" do
14       it "should return authentication error" do
15         get api("/hooks")
16         response.status.should == 401
17       end
18     end
19
20     context "when not an admin" do
21       it "should return forbidden error" do
22         get api("/hooks", user)
23         response.status.should == 403
24       end
25     end
26
27     context "when authenticated as admin" do
28       it "should return an array of hooks" do
29         get api("/hooks", admin)
30         response.status.should == 200
31         json_response.should be_an Array
32         json_response.first['url'].should == hook.url
33       end
34     end
35   end
36
37   describe "POST /hooks" do
38     it "should create new hook" do
39       expect {
40         post api("/hooks", admin), url: 'http://example.com'
41       }.to change { SystemHook.count }.by(1)
42     end
43
44     it "should respond with 400 if url not given" do
45       post api("/hooks", admin)
46       response.status.should == 400
47     end
48
49     it "should not create new hook without url" do
50       expect {
51         post api("/hooks", admin)
52       }.to_not change { SystemHook.count }
53     end
54   end
55
56   describe "GET /hooks/:id" do
57     it "should return hook by id" do
58       get api("/hooks/#{hook.id}", admin)
59       response.status.should == 200
60       json_response['event_name'].should == 'project_create'
61     end
62
63     it "should return 404 on failure" do
64       get api("/hooks/404", admin)
65       response.status.should == 404
66     end
67   end
68
69   describe "DELETE /hooks/:id" do
70     it "should delete a hook" do
71       expect {
72         delete api("/hooks/#{hook.id}", admin)
73       }.to change { SystemHook.count }.by(-1)
74     end
75
76     it "should return success if hook id not found" do
77       delete api("/hooks/12345", admin)
78       response.status.should == 200
79     end
80   end
81 end