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

3
describe API::ProjectHooks, 'ProjectHooks', api: true do
4 5 6
  include ApiHelpers
  let(:user) { create(:user) }
  let(:user3) { create(:user) }
7
  let!(:project) { create(:empty_project, creator_id: user.id, namespace: user.namespace) }
8 9
  let!(:hook) do
    create(:project_hook,
10 11 12
           :all_events_enabled,
           project: project,
           url: 'http://example.com',
13 14
           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
        expect(json_response).to be_an Array
28
        expect(response).to include_pagination_headers
29 30
        expect(json_response.count).to eq(1)
        expect(json_response.first['url']).to eq("http://example.com")
31 32 33 34 35
        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's avatar
Toon Claes committed
36
        expect(json_response.first['job_events']).to eq(true)
37
        expect(json_response.first['pipeline_events']).to eq(true)
38
        expect(json_response.first['wiki_page_events']).to eq(true)
39
        expect(json_response.first['enable_ssl_verification']).to eq(true)
40 41 42 43
      end
    end

    context "unauthorized user" do
44
      it "does not access project hooks" do
45
        get api("/projects/#{project.id}/hooks", user3)
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
        expect(response).to have_http_status(200)
56
        expect(json_response['url']).to eq(hook.url)
57 58 59 60 61
        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's avatar
Toon Claes committed
62
        expect(json_response['job_events']).to eq(hook.build_events)
63
        expect(json_response['pipeline_events']).to eq(hook.pipeline_events)
64
        expect(json_response['wiki_page_events']).to eq(hook.wiki_page_events)
65
        expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
66 67
      end

68
      it "returns a 404 error if hook id is not available" do
69
        get api("/projects/#{project.id}/hooks/1234", user)
70
        expect(response).to have_http_status(404)
71 72 73 74
      end
    end

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

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

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

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

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

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

  describe "PUT /projects/:id/hooks/:hook_id" do
137
    it "updates an existing project hook" do
138
      put api("/projects/#{project.id}/hooks/#{hook.id}", user),
139
        url: 'http://example.org', push_events: false
140
      expect(response).to have_http_status(200)
141
      expect(json_response['url']).to eq('http://example.org')
142 143 144 145 146
      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's avatar
Toon Claes committed
147
      expect(json_response['job_events']).to eq(hook.build_events)
148
      expect(json_response['pipeline_events']).to eq(hook.pipeline_events)
149
      expect(json_response['wiki_page_events']).to eq(hook.wiki_page_events)
150
      expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
151 152 153 154 155 156 157 158 159 160 161 162 163
    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)
164 165
    end

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

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

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

  describe "DELETE /projects/:id/hooks/:hook_id" do
183
    it "deletes hook from project" do
184
      expect do
185 186
        delete api("/projects/#{project.id}/hooks/#{hook.id}", user)

187 188
        expect(response).to have_http_status(204)
      end.to change {project.hooks.count}.by(-1)
189 190
    end

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

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

199
      expect(response).to have_http_status(404)
200
    end
201

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

      delete api("/projects/#{other_project.id}/hooks/#{hook.id}", test_user)
208
      expect(response).to have_http_status(404)
209 210
      expect(WebHook.exists?(hook.id)).to be_truthy
    end
211 212
  end
end