BigW Consortium Gitlab

project_hooks_spec.rb 6.6 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::API, 'ProjectHooks', api: true do
4 5 6
  include ApiHelpers
  let(:user) { create(:user) }
  let(:user3) { create(:user) }
7
  let!(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
8 9 10 11 12 13 14
  let!(:hook) do
    create(:project_hook,
           project: project, url: "http://example.com",
           push_events: true, merge_requests_events: true, tag_push_events: true,
           issues_events: true, note_events: true, build_events: true,
           enable_ssl_verification: true)
  end
15 16 17 18 19 20 21 22

  before do
    project.team << [user, :master]
    project.team << [user3, :developer]
  end

  describe "GET /projects/:id/hooks" do
    context "authorized user" do
23
      it "returns project hooks" do
24
        get api("/projects/#{project.id}/hooks", user)
25
        expect(response).to have_http_status(200)
26

27 28 29
        expect(json_response).to be_an Array
        expect(json_response.count).to eq(1)
        expect(json_response.first['url']).to eq("http://example.com")
30 31 32 33 34
        expect(json_response.first['issues_events']).to eq(true)
        expect(json_response.first['push_events']).to eq(true)
        expect(json_response.first['merge_requests_events']).to eq(true)
        expect(json_response.first['tag_push_events']).to eq(true)
        expect(json_response.first['note_events']).to eq(true)
35
        expect(json_response.first['build_events']).to eq(true)
36
        expect(json_response.first['enable_ssl_verification']).to eq(true)
37 38 39 40
      end
    end

    context "unauthorized user" do
41
      it "does not access project hooks" do
42
        get api("/projects/#{project.id}/hooks", user3)
43
        expect(response).to have_http_status(403)
44 45 46 47 48 49
      end
    end
  end

  describe "GET /projects/:id/hooks/:hook_id" do
    context "authorized user" do
50
      it "returns a project hook" do
51
        get api("/projects/#{project.id}/hooks/#{hook.id}", user)
52
        expect(response).to have_http_status(200)
53
        expect(json_response['url']).to eq(hook.url)
54 55 56 57 58 59
        expect(json_response['issues_events']).to eq(hook.issues_events)
        expect(json_response['push_events']).to eq(hook.push_events)
        expect(json_response['merge_requests_events']).to eq(hook.merge_requests_events)
        expect(json_response['tag_push_events']).to eq(hook.tag_push_events)
        expect(json_response['note_events']).to eq(hook.note_events)
        expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
60 61
      end

62
      it "returns a 404 error if hook id is not available" do
63
        get api("/projects/#{project.id}/hooks/1234", user)
64
        expect(response).to have_http_status(404)
65 66 67 68
      end
    end

    context "unauthorized user" do
69
      it "does not access an existing hook" do
70
        get api("/projects/#{project.id}/hooks/#{hook.id}", user3)
71
        expect(response).to have_http_status(403)
72 73 74
      end
    end

75
    it "returns a 404 error if hook id is not available" do
76
      get api("/projects/#{project.id}/hooks/1234", user)
77
      expect(response).to have_http_status(404)
78 79 80 81
    end
  end

  describe "POST /projects/:id/hooks" do
82
    it "adds hook to project" do
83 84 85
      expect do
        post api("/projects/#{project.id}/hooks", user), url: "http://example.com", issues_events: true
      end.to change {project.hooks.count}.by(1)
86
      expect(response).to have_http_status(201)
87 88 89 90 91 92
      expect(json_response['url']).to eq('http://example.com')
      expect(json_response['issues_events']).to eq(true)
      expect(json_response['push_events']).to eq(true)
      expect(json_response['merge_requests_events']).to eq(false)
      expect(json_response['tag_push_events']).to eq(false)
      expect(json_response['note_events']).to eq(false)
93
      expect(json_response['build_events']).to eq(false)
94
      expect(json_response['enable_ssl_verification']).to eq(true)
95 96
    end

97
    it "returns a 400 error if url not given" do
98
      post api("/projects/#{project.id}/hooks", user)
99
      expect(response).to have_http_status(400)
100 101
    end

102
    it "returns a 422 error if url not valid" do
103
      post api("/projects/#{project.id}/hooks", user), "url" => "ftp://example.com"
104
      expect(response).to have_http_status(422)
105 106 107 108
    end
  end

  describe "PUT /projects/:id/hooks/:hook_id" do
109
    it "updates an existing project hook" do
110
      put api("/projects/#{project.id}/hooks/#{hook.id}", user),
111
        url: 'http://example.org', push_events: false
112
      expect(response).to have_http_status(200)
113
      expect(json_response['url']).to eq('http://example.org')
114 115 116 117 118 119
      expect(json_response['issues_events']).to eq(hook.issues_events)
      expect(json_response['push_events']).to eq(false)
      expect(json_response['merge_requests_events']).to eq(hook.merge_requests_events)
      expect(json_response['tag_push_events']).to eq(hook.tag_push_events)
      expect(json_response['note_events']).to eq(hook.note_events)
      expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
120 121
    end

122
    it "returns 404 error if hook id not found" do
123
      put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org'
124
      expect(response).to have_http_status(404)
125 126
    end

127
    it "returns 400 error if url is not given" do
128
      put api("/projects/#{project.id}/hooks/#{hook.id}", user)
129
      expect(response).to have_http_status(400)
130 131
    end

132
    it "returns a 422 error if url is not valid" do
133
      put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: 'ftp://example.com'
134
      expect(response).to have_http_status(422)
135 136 137 138
    end
  end

  describe "DELETE /projects/:id/hooks/:hook_id" do
139
    it "deletes hook from project" do
140
      expect do
141
        delete api("/projects/#{project.id}/hooks/#{hook.id}", user)
142
      end.to change {project.hooks.count}.by(-1)
143
      expect(response).to have_http_status(200)
144 145
    end

146
    it "returns success when deleting hook" do
147
      delete api("/projects/#{project.id}/hooks/#{hook.id}", user)
148
      expect(response).to have_http_status(200)
149 150
    end

151
    it "returns a 404 error when deleting non existent hook" do
152
      delete api("/projects/#{project.id}/hooks/42", user)
153
      expect(response).to have_http_status(404)
154 155
    end

156
    it "returns a 405 error if hook id not given" do
157
      delete api("/projects/#{project.id}/hooks", user)
158
      expect(response).to have_http_status(405)
159
    end
160

161
    it "returns a 404 if a user attempts to delete project hooks he/she does not own" do
162 163 164 165 166
      test_user = create(:user)
      other_project = create(:project)
      other_project.team << [test_user, :master]

      delete api("/projects/#{other_project.id}/hooks/#{hook.id}", test_user)
167
      expect(response).to have_http_status(404)
168 169
      expect(WebHook.exists?(hook.id)).to be_truthy
    end
170 171
  end
end