diff options
Diffstat (limited to 'spec/requests/api/project_hooks_spec.rb')
-rw-r--r-- | spec/requests/api/project_hooks_spec.rb | 247 |
1 files changed, 35 insertions, 212 deletions
diff --git a/spec/requests/api/project_hooks_spec.rb b/spec/requests/api/project_hooks_spec.rb index 26e0adc11b3..2d925620a91 100644 --- a/spec/requests/api/project_hooks_spec.rb +++ b/spec/requests/api/project_hooks_spec.rb @@ -3,10 +3,10 @@ require 'spec_helper' RSpec.describe API::ProjectHooks, 'ProjectHooks' do - let(:user) { create(:user) } - let(:user3) { create(:user) } - let!(:project) { create(:project, creator_id: user.id, namespace: user.namespace) } - let!(:hook) do + let_it_be(:user) { create(:user) } + let_it_be(:user3) { create(:user) } + let_it_be(:project) { create(:project, creator_id: user.id, namespace: user.namespace) } + let_it_be_with_refind(:hook) do create(:project_hook, :all_events_enabled, project: project, @@ -15,232 +15,55 @@ RSpec.describe API::ProjectHooks, 'ProjectHooks' do push_events_branch_filter: 'master') end - before do + before_all do project.add_maintainer(user) project.add_developer(user3) end - describe "GET /projects/:id/hooks" do - context "authorized user" do - it "returns project hooks" do - get api("/projects/#{project.id}/hooks", user) + it_behaves_like 'web-hook API endpoints', '/projects/:id' do + let(:unauthorized_user) { user3 } - expect(response).to have_gitlab_http_status(:ok) - expect(json_response).to be_an Array - expect(response).to include_pagination_headers - expect(json_response.count).to eq(1) - expect(json_response.first['url']).to eq("http://example.com") - expect(json_response.first['issues_events']).to eq(true) - expect(json_response.first['confidential_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) - expect(json_response.first['confidential_note_events']).to eq(true) - expect(json_response.first['job_events']).to eq(true) - expect(json_response.first['pipeline_events']).to eq(true) - expect(json_response.first['wiki_page_events']).to eq(true) - expect(json_response.first['deployment_events']).to eq(true) - expect(json_response.first['releases_events']).to eq(true) - expect(json_response.first['enable_ssl_verification']).to eq(true) - expect(json_response.first['push_events_branch_filter']).to eq('master') - expect(json_response.first['alert_status']).to eq('executable') - expect(json_response.first['disabled_until']).to be_nil - end + def scope + project.hooks end - context "unauthorized user" do - it "does not access project hooks" do - get api("/projects/#{project.id}/hooks", user3) - - expect(response).to have_gitlab_http_status(:forbidden) - end - end - end - - describe "GET /projects/:id/hooks/:hook_id" do - context "authorized user" do - it "returns a project hook" do - get api("/projects/#{project.id}/hooks/#{hook.id}", user) - - expect(response).to have_gitlab_http_status(:ok) - expect(json_response['url']).to eq(hook.url) - expect(json_response['issues_events']).to eq(hook.issues_events) - expect(json_response['confidential_issues_events']).to eq(hook.confidential_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['confidential_note_events']).to eq(hook.confidential_note_events) - expect(json_response['job_events']).to eq(hook.job_events) - expect(json_response['pipeline_events']).to eq(hook.pipeline_events) - expect(json_response['wiki_page_events']).to eq(hook.wiki_page_events) - expect(json_response['releases_events']).to eq(hook.releases_events) - expect(json_response['deployment_events']).to eq(true) - expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification) - expect(json_response['alert_status']).to eq(hook.alert_status.to_s) - expect(json_response['disabled_until']).to be_nil - end - - it "returns a 404 error if hook id is not available" do - get api("/projects/#{project.id}/hooks/#{non_existing_record_id}", user) - - expect(response).to have_gitlab_http_status(:not_found) - end - end - - context "unauthorized user" do - it "does not access an existing hook" do - get api("/projects/#{project.id}/hooks/#{hook.id}", user3) - expect(response).to have_gitlab_http_status(:forbidden) - end - end - end - - describe "POST /projects/:id/hooks" do - it "adds hook to project" do - expect do - post(api("/projects/#{project.id}/hooks", user), - params: { url: "http://example.com", issues_events: true, - confidential_issues_events: true, wiki_page_events: true, - job_events: true, deployment_events: true, releases_events: true, - push_events_branch_filter: 'some-feature-branch' }) - end.to change {project.hooks.count}.by(1) - - expect(response).to have_gitlab_http_status(:created) - expect(json_response['url']).to eq('http://example.com') - expect(json_response['issues_events']).to eq(true) - expect(json_response['confidential_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) - expect(json_response['confidential_note_events']).to eq(nil) - expect(json_response['job_events']).to eq(true) - expect(json_response['pipeline_events']).to eq(false) - expect(json_response['wiki_page_events']).to eq(true) - expect(json_response['deployment_events']).to eq(true) - expect(json_response['releases_events']).to eq(true) - expect(json_response['enable_ssl_verification']).to eq(true) - expect(json_response['push_events_branch_filter']).to eq('some-feature-branch') - expect(json_response).not_to include('token') + def collection_uri + "/projects/#{project.id}/hooks" end - it "adds the token without including it in the response" do - token = "secret token" - - expect do - post api("/projects/#{project.id}/hooks", user), params: { url: "http://example.com", token: token } - end.to change {project.hooks.count}.by(1) - - expect(response).to have_gitlab_http_status(:created) - 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) + def match_collection_schema + match_response_schema('public_api/v4/project_hooks') end - it "returns a 400 error if url not given" do - post api("/projects/#{project.id}/hooks", user) - expect(response).to have_gitlab_http_status(:bad_request) + def hook_uri(hook_id = hook.id) + "/projects/#{project.id}/hooks/#{hook_id}" end - it "returns a 422 error if url not valid" do - post api("/projects/#{project.id}/hooks", user), params: { url: "ftp://example.com" } - expect(response).to have_gitlab_http_status(:unprocessable_entity) + def match_hook_schema + match_response_schema('public_api/v4/project_hook') end - it "returns a 422 error if branch filter is not valid" do - post api("/projects/#{project.id}/hooks", user), params: { url: "http://example.com", push_events_branch_filter: '~badbranchname/' } - expect(response).to have_gitlab_http_status(:unprocessable_entity) + def event_names + %i[ + push_events + tag_push_events + merge_requests_events + issues_events + confidential_issues_events + note_events + confidential_note_events + pipeline_events + wiki_page_events + job_events + deployment_events + releases_events + ] end - end - - describe "PUT /projects/:id/hooks/:hook_id" do - it "updates an existing project hook" do - put api("/projects/#{project.id}/hooks/#{hook.id}", user), - params: { url: 'http://example.org', push_events: false, job_events: true } - expect(response).to have_gitlab_http_status(:ok) - expect(json_response['url']).to eq('http://example.org') - expect(json_response['issues_events']).to eq(hook.issues_events) - expect(json_response['confidential_issues_events']).to eq(hook.confidential_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['confidential_note_events']).to eq(hook.confidential_note_events) - expect(json_response['job_events']).to eq(hook.job_events) - expect(json_response['pipeline_events']).to eq(hook.pipeline_events) - expect(json_response['wiki_page_events']).to eq(hook.wiki_page_events) - expect(json_response['releases_events']).to eq(hook.releases_events) - expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification) + let(:default_values) do + { push_events: true, confidential_note_events: nil } end - it "adds the token without including it in the response" do - token = "secret token" - - put api("/projects/#{project.id}/hooks/#{hook.id}", user), params: { url: "http://example.org", token: token } - - expect(response).to have_gitlab_http_status(:ok) - 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) - end - - it "returns 404 error if hook id not found" do - put api("/projects/#{project.id}/hooks/#{non_existing_record_id}", user), params: { url: 'http://example.org' } - expect(response).to have_gitlab_http_status(:not_found) - end - - it "returns 400 error if url is not given" do - put api("/projects/#{project.id}/hooks/#{hook.id}", user) - expect(response).to have_gitlab_http_status(:bad_request) - end - - it "returns a 422 error if url is not valid" do - put api("/projects/#{project.id}/hooks/#{hook.id}", user), params: { url: 'ftp://example.com' } - expect(response).to have_gitlab_http_status(:unprocessable_entity) - end - end - - describe "DELETE /projects/:id/hooks/:hook_id" do - it "deletes hook from project" do - expect do - delete api("/projects/#{project.id}/hooks/#{hook.id}", user) - - expect(response).to have_gitlab_http_status(:no_content) - end.to change {project.hooks.count}.by(-1) - end - - it "returns a 404 error when deleting non existent hook" do - delete api("/projects/#{project.id}/hooks/42", user) - expect(response).to have_gitlab_http_status(:not_found) - end - - it "returns a 404 error if hook id not given" do - delete api("/projects/#{project.id}/hooks", user) - - expect(response).to have_gitlab_http_status(:not_found) - end - - it "returns a 404 if a user attempts to delete project hooks they do not own" do - test_user = create(:user) - other_project = create(:project) - other_project.add_maintainer(test_user) - - delete api("/projects/#{other_project.id}/hooks/#{hook.id}", test_user) - expect(response).to have_gitlab_http_status(:not_found) - expect(WebHook.exists?(hook.id)).to be_truthy - end - - it_behaves_like '412 response' do - let(:request) { api("/projects/#{project.id}/hooks/#{hook.id}", user) } - end + it_behaves_like 'web-hook API endpoints with branch-filter', '/projects/:id' end end |