BigW Consortium Gitlab

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

3
describe API::ProjectHooks, 'ProjectHooks' do
4 5
  let(:user) { create(:user) }
  let(:user3) { create(:user) }
6
  let!(:project) { create(:empty_project, creator_id: user.id, namespace: user.namespace) }
7 8
  let!(:hook) do
    create(:project_hook,
9 10 11
           :all_events_enabled,
           project: project,
           url: 'http://example.com',
12 13
           enable_ssl_verification: true)
  end
14 15 16 17 18 19 20 21

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

  describe "GET /projects/:id/hooks" do
    context "authorized user" do
22
      it "returns project hooks" do
23 24
        get api("/projects/#{project.id}/hooks", user)

25
        expect(response).to have_http_status(200)
26
        expect(json_response).to be_an Array
27
        expect(response).to include_pagination_headers
28 29
        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)
Toon Claes committed
35
        expect(json_response.first['job_events']).to eq(true)
36
        expect(json_response.first['pipeline_events']).to eq(true)
37
        expect(json_response.first['wiki_page_events']).to eq(true)
38
        expect(json_response.first['enable_ssl_verification']).to eq(true)
39 40 41 42
      end
    end

    context "unauthorized user" do
43
      it "does not access project hooks" do
44
        get api("/projects/#{project.id}/hooks", user3)
45

46
        expect(response).to have_http_status(403)
47 48 49 50 51 52
      end
    end
  end

  describe "GET /projects/:id/hooks/:hook_id" do
    context "authorized user" do
53
      it "returns a project hook" do
54
        get api("/projects/#{project.id}/hooks/#{hook.id}", user)
55

56
        expect(response).to have_http_status(200)
57
        expect(json_response['url']).to eq(hook.url)
58 59 60 61 62
        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)
Toon Claes committed
63
        expect(json_response['job_events']).to eq(hook.build_events)
64
        expect(json_response['pipeline_events']).to eq(hook.pipeline_events)
65
        expect(json_response['wiki_page_events']).to eq(hook.wiki_page_events)
66
        expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
67 68
      end

69
      it "returns a 404 error if hook id is not available" do
70
        get api("/projects/#{project.id}/hooks/1234", user)
71

72
        expect(response).to have_http_status(404)
73 74 75 76
      end
    end

    context "unauthorized user" do
77
      it "does not access an existing hook" do
78
        get api("/projects/#{project.id}/hooks/#{hook.id}", user3)
79
        expect(response).to have_http_status(403)
80 81 82
      end
    end

83
    it "returns a 404 error if hook id is not available" do
84
      get api("/projects/#{project.id}/hooks/1234", user)
85
      expect(response).to have_http_status(404)
86 87 88 89
    end
  end

  describe "POST /projects/:id/hooks" do
90
    it "adds hook to project" do
91
      expect do
92
        post api("/projects/#{project.id}/hooks", user),
93 94
          url: "http://example.com", issues_events: true, wiki_page_events: true,
          job_events: true
95
      end.to change {project.hooks.count}.by(1)
96

97
      expect(response).to have_http_status(201)
98 99 100 101 102 103
      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)
104
      expect(json_response['job_events']).to eq(true)
105
      expect(json_response['pipeline_events']).to eq(false)
106
      expect(json_response['wiki_page_events']).to eq(true)
107
      expect(json_response['enable_ssl_verification']).to eq(true)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      expect(json_response).not_to include('token')
    end

    it "adds the token without including it in the response" do
      token = "secret token"

      expect do
        post api("/projects/#{project.id}/hooks", user), url: "http://example.com", token: token
      end.to change {project.hooks.count}.by(1)

      expect(response).to have_http_status(201)
      expect(json_response["url"]).to eq("http://example.com")
      expect(json_response).not_to include("token")

      hook = project.hooks.find(json_response["id"])

      expect(hook.url).to eq("http://example.com")
      expect(hook.token).to eq(token)
126 127
    end

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

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

  describe "PUT /projects/:id/hooks/:hook_id" do
140
    it "updates an existing project hook" do
141
      put api("/projects/#{project.id}/hooks/#{hook.id}", user),
142 143
        url: 'http://example.org', push_events: false, job_events: true

144
      expect(response).to have_http_status(200)
145
      expect(json_response['url']).to eq('http://example.org')
146 147 148 149 150
      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)
Toon Claes committed
151
      expect(json_response['job_events']).to eq(hook.build_events)
152
      expect(json_response['pipeline_events']).to eq(hook.pipeline_events)
153
      expect(json_response['wiki_page_events']).to eq(hook.wiki_page_events)
154
      expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
155 156 157 158 159 160 161 162 163 164 165 166 167
    end

    it "adds the token without including it in the response" do
      token = "secret token"

      put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: "http://example.org", token: token

      expect(response).to have_http_status(200)
      expect(json_response["url"]).to eq("http://example.org")
      expect(json_response).not_to include("token")

      expect(hook.reload.url).to eq("http://example.org")
      expect(hook.reload.token).to eq(token)
168 169
    end

170
    it "returns 404 error if hook id not found" do
171
      put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org'
172
      expect(response).to have_http_status(404)
173 174
    end

175
    it "returns 400 error if url is not given" do
176
      put api("/projects/#{project.id}/hooks/#{hook.id}", user)
177
      expect(response).to have_http_status(400)
178 179
    end

180
    it "returns a 422 error if url is not valid" do
181
      put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: 'ftp://example.com'
182
      expect(response).to have_http_status(422)
183 184 185 186
    end
  end

  describe "DELETE /projects/:id/hooks/:hook_id" do
187
    it "deletes hook from project" do
188
      expect do
189 190
        delete api("/projects/#{project.id}/hooks/#{hook.id}", user)

191 192
        expect(response).to have_http_status(204)
      end.to change {project.hooks.count}.by(-1)
193 194
    end

195
    it "returns a 404 error when deleting non existent hook" do
196
      delete api("/projects/#{project.id}/hooks/42", user)
197
      expect(response).to have_http_status(404)
198 199
    end

200
    it "returns a 404 error if hook id not given" do
201
      delete api("/projects/#{project.id}/hooks", user)
202

203
      expect(response).to have_http_status(404)
204
    end
205

206
    it "returns a 404 if a user attempts to delete project hooks he/she does not own" do
207
      test_user = create(:user)
208
      other_project = create(:empty_project)
209 210 211
      other_project.team << [test_user, :master]

      delete api("/projects/#{other_project.id}/hooks/#{hook.id}", test_user)
212
      expect(response).to have_http_status(404)
213 214
      expect(WebHook.exists?(hook.id)).to be_truthy
    end
215 216
  end
end