Commit 86c58687 authored by Robert Schilling's avatar Robert Schilling

Return 204 for delete endpoints

parent 7733f285
......@@ -68,7 +68,7 @@ gem 'gollum-rugged_adapter', '~> 0.4.2', require: false
gem 'github-linguist', '~> 4.7.0', require: 'linguist'
# API
gem 'grape', '~> 0.18.0'
gem 'grape', '~> 0.19.0'
gem 'grape-entity', '~> 0.6.0'
gem 'rack-cors', '~> 0.4.0', require: 'rack/cors'
......
......@@ -304,7 +304,7 @@ GEM
multi_json (~> 1.11)
os (~> 0.9)
signet (~> 0.7)
grape (0.18.0)
grape (0.19.1)
activesupport
builder
hashie (>= 2.1.0)
......@@ -353,8 +353,8 @@ GEM
json (~> 1.8)
multi_xml (>= 0.5.2)
httpclient (2.8.2)
i18n (0.8.0)
ice_nine (0.11.1)
i18n (0.8.1)
ice_nine (0.11.2)
influxdb (0.2.3)
cause
json
......@@ -417,7 +417,7 @@ GEM
minitest (5.7.0)
mousetrap-rails (1.4.6)
multi_json (1.12.1)
multi_xml (0.5.5)
multi_xml (0.6.0)
multipart-post (2.0.0)
mustermann (0.4.0)
tool (~> 0.2)
......@@ -758,7 +758,7 @@ GEM
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
thor (0.19.4)
thread_safe (0.3.5)
thread_safe (0.3.6)
tilt (2.0.6)
timecop (0.8.1)
timfel-krb5-auth (0.8.3)
......@@ -886,7 +886,7 @@ DEPENDENCIES
gollum-rugged_adapter (~> 0.4.2)
gon (~> 6.1.0)
google-api-client (~> 0.8.6)
grape (~> 0.18.0)
grape (~> 0.19.0)
grape-entity (~> 0.6.0)
haml_lint (~> 0.21.0)
hamlit (~> 2.6.1)
......@@ -1011,4 +1011,4 @@ DEPENDENCIES
wikicloth (= 0.8.1)
BUNDLED WITH
1.14.3
1.14.4
......@@ -83,7 +83,6 @@ module API
unauthorized! unless award.user == current_user || current_user.admin?
award.destroy
present award, with: Entities::AwardEmoji
end
end
end
......
......@@ -127,9 +127,7 @@ module API
service = ::Boards::Lists::DestroyService.new(user_project, current_user)
if service.execute(list)
present list, with: Entities::List
else
unless service.execute(list)
render_api_error!({ error: 'List could not be deleted!' }, 400)
end
end
......
......@@ -124,11 +124,7 @@ module API
result = DeleteBranchService.new(user_project, current_user).
execute(params[:branch])
if result[:status] == :success
{
branch: params[:branch]
}
else
if result[:status] != :success
render_api_error!(result[:message], result[:return_code])
end
end
......
......@@ -91,7 +91,7 @@ module API
delete ':id' do
message = find_message
present message.destroy, with: Entities::BroadcastMessage
message.destroy
end
end
end
......
......@@ -79,7 +79,7 @@ module API
environment = user_project.environments.find(params[:environment_id])
present environment.destroy, with: Entities::Environment
environment.destroy
end
end
end
......
......@@ -118,10 +118,7 @@ module API
file_params = declared_params(include_missing: false)
result = ::Files::DestroyService.new(user_project, current_user, commit_params(file_params)).execute
if result[:status] == :success
status(200)
commit_response(file_params)
else
if result[:status] != :success
render_api_error!(result[:message], 400)
end
end
......
module API
class Labels < Grape::API
include PaginationParams
before { authenticate! }
params do
......@@ -56,7 +56,7 @@ module API
label = user_project.labels.find_by(title: params[:name])
not_found!('Label') unless label
present label.destroy, with: Entities::Label, current_user: current_user, project: user_project
label.destroy
end
desc 'Update an existing label. At least one optional parameter is required.' do
......
......@@ -93,24 +93,10 @@ module API
end
delete ":id/members/:user_id" do
source = find_source(source_type, params[:id])
# Ensure that memeber exists
source.members.find_by!(user_id: params[:user_id])
# This is to ensure back-compatibility but find_by! should be used
# in that casse in 9.0!
member = source.members.find_by(user_id: params[:user_id])
# This is to ensure back-compatibility but this should be removed in
# favor of find_by! in 9.0!
not_found!("Member: user_id:#{params[:user_id]}") if source_type == 'group' && member.nil?
# This is to ensure back-compatibility but 204 behavior should be used
# for all DELETE endpoints in 9.0!
if member.nil?
{ message: "Access revoked", id: params[:user_id].to_i }
else
::Members::DestroyService.new(source, current_user, declared_params).execute
present member.user, with: Entities::Member, member: member
end
::Members::DestroyService.new(source, current_user, declared_params).execute
end
end
end
......
......@@ -132,8 +132,6 @@ module API
authorize! :admin_note, note
::Notes::DestroyService.new(user_project, current_user).execute(note)
present note, with: Entities::Note
end
end
end
......
......@@ -90,12 +90,9 @@ module API
requires :hook_id, type: Integer, desc: 'The ID of the hook to delete'
end
delete ":id/hooks/:hook_id" do
begin
present user_project.hooks.destroy(params[:hook_id]), with: Entities::ProjectHook
rescue
# ProjectHook can raise Error if hook_id not found
not_found!("Error deleting hook #{params[:hook_id]}")
end
hook = user_project.hooks.find(params.delete(:hook_id))
hook.destroy
end
end
end
......
......@@ -353,7 +353,6 @@ module API
not_found!('Group Link') unless link
link.destroy
no_content!
end
desc 'Upload a file'
......
......@@ -78,9 +78,8 @@ module API
delete ':id' do
runner = get_runner(params[:id])
authenticate_delete_runner!(runner)
runner.destroy!
present runner, with: Entities::Runner
runner.destroy!
end
end
......@@ -136,8 +135,6 @@ module API
forbidden!("Only one project associated with the runner. Please remove the runner instead") if runner.projects.count == 1
runner_project.destroy
present runner, with: Entities::Runner
end
end
......
......@@ -654,9 +654,7 @@ module API
hash.merge!(key => nil)
end
if service.update_attributes(attrs.merge(active: false))
true
else
unless service.update_attributes(attrs.merge(active: false))
render_api_error!('400 Bad Request', 400)
end
end
......
......@@ -118,9 +118,10 @@ module API
delete ':id' do
snippet = snippets_for_current_user.find_by(id: params.delete(:id))
return not_found!('Snippet') unless snippet
authorize! :destroy_personal_snippet, snippet
snippet.destroy
no_content!
end
desc 'Get a raw snippet' do
......
......@@ -66,7 +66,7 @@ module API
hook = SystemHook.find_by(id: params[:id])
not_found!('System hook') unless hook
present hook.destroy, with: Entities::Hook
hook.destroy
end
end
end
......
......@@ -66,11 +66,7 @@ module API
result = ::Tags::DestroyService.new(user_project, current_user).
execute(params[:tag_name])
if result[:status] == :success
{
tag_name: params[:tag_name]
}
else
if result[:status] != :success
render_api_error!(result[:message], result[:return_code])
end
end
......
......@@ -93,8 +93,6 @@ module API
return not_found!('Trigger') unless trigger
trigger.destroy
present trigger, with: Entities::Trigger
end
end
end
......
......@@ -236,7 +236,7 @@ module API
key = user.keys.find_by(id: params[:key_id])
not_found!('Key') unless key
present key.destroy, with: Entities::SSHKey
key.destroy
end
desc 'Add an email address to a specified user. Available only for admins.' do
......@@ -422,7 +422,7 @@ module API
key = current_user.keys.find_by(id: params[:key_id])
not_found!('Key') unless key
present key.destroy, with: Entities::SSHKey
key.destroy
end
desc "Get the currently authenticated user's email addresses" do
......
......@@ -81,10 +81,9 @@ module API
end
delete ':id/variables/:key' do
variable = user_project.variables.find_by(key: params[:key])
return not_found!('Variable') unless variable
present variable.destroy, with: Entities::Variable
variable.destroy
end
end
end
......
......@@ -200,7 +200,7 @@ describe API::AccessRequests, api: true do
expect do
delete api("/#{source_type.pluralize}/#{source.id}/access_requests/#{access_requester.id}", access_requester)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change { source.requesters.count }.by(-1)
end
end
......@@ -210,7 +210,7 @@ describe API::AccessRequests, api: true do
expect do
delete api("/#{source_type.pluralize}/#{source.id}/access_requests/#{access_requester.id}", master)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change { source.requesters.count }.by(-1)
end
......
......@@ -242,9 +242,9 @@ describe API::AwardEmoji, api: true do
it 'deletes the award' do
expect do
delete api("/projects/#{project.id}/issues/#{issue.id}/award_emoji/#{award_emoji.id}", user)
end.to change { issue.award_emoji.count }.from(1).to(0)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change { issue.award_emoji.count }.from(1).to(0)
end
it 'returns a 404 error when the award emoji can not be found' do
......@@ -258,9 +258,9 @@ describe API::AwardEmoji, api: true do
it 'deletes the award' do
expect do
delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}/award_emoji/#{downvote.id}", user)
end.to change { merge_request.award_emoji.count }.from(1).to(0)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change { merge_request.award_emoji.count }.from(1).to(0)
end
it 'returns a 404 error when note id not found' do
......@@ -277,9 +277,9 @@ describe API::AwardEmoji, api: true do
it 'deletes the award' do
expect do
delete api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji/#{award.id}", user)
end.to change { snippet.award_emoji.count }.from(1).to(0)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change { snippet.award_emoji.count }.from(1).to(0)
end
end
end
......@@ -290,9 +290,9 @@ describe API::AwardEmoji, api: true do
it 'deletes the award' do
expect do
delete api("/projects/#{project.id}/issues/#{issue.id}/notes/#{note.id}/award_emoji/#{rocket.id}", user)
end.to change { note.award_emoji.count }.from(1).to(0)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change { note.award_emoji.count }.from(1).to(0)
end
end
end
......@@ -195,8 +195,7 @@ describe API::Boards, api: true do
it "deletes the list if an admin requests it" do
delete api("#{base_url}/#{dev_list.id}", owner)
expect(response).to have_http_status(200)
expect(json_response['position']).to eq(1)
expect(response).to have_http_status(204)
end
end
end
......
......@@ -325,15 +325,14 @@ describe API::Branches, api: true do
it "removes branch" do
delete api("/projects/#{project.id}/repository/branches/#{branch_name}", user)
expect(response).to have_http_status(200)
expect(json_response['branch']).to eq(branch_name)
expect(response).to have_http_status(204)
end
it "removes a branch with dots in the branch name" do
delete api("/projects/#{project.id}/repository/branches/with.1.2.3", user)
expect(response).to have_http_status(200)
expect(json_response['branch']).to eq("with.1.2.3")
expect(response).to have_http_status(204)
end
it 'returns 404 if branch not exists' do
......
......@@ -174,8 +174,11 @@ describe API::BroadcastMessages, api: true do
end
it 'deletes the broadcast message for admins' do
expect { delete api("/broadcast_messages/#{message.id}", admin) }
.to change { BroadcastMessage.count }.by(-1)
expect do
delete api("/broadcast_messages/#{message.id}", admin)
expect(response).to have_http_status(204)
end.to change { BroadcastMessage.count }.by(-1)
end
end
end
......@@ -116,6 +116,8 @@ describe API::DeployKeys, api: true do
it 'should delete existing key' do
expect do
delete api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin)
expect(response).to have_http_status(204)
end.to change{ project.deploy_keys.count }.by(-1)
end
......
......@@ -122,7 +122,7 @@ describe API::Environments, api: true do
it 'returns a 200 for an existing environment' do
delete api("/projects/#{project.id}/environments/#{environment.id}", user)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end
it 'returns a 404 for non existing id' do
......
......@@ -201,11 +201,7 @@ describe API::Files, api: true do
it "deletes existing file in project repo" do
delete api("/projects/#{project.id}/repository/files", user), valid_params
expect(response).to have_http_status(200)
expect(json_response['file_path']).to eq(file_path)
last_commit = project.repository.commit.raw
expect(last_commit.author_email).to eq(user.email)
expect(last_commit.author_name).to eq(user.name)
expect(response).to have_http_status(204)
end
it "returns a 400 bad request if no params given" do
......@@ -228,10 +224,7 @@ describe API::Files, api: true do
delete api("/projects/#{project.id}/repository/files", user), valid_params
expect(response).to have_http_status(200)
last_commit = project.repository.commit.raw
expect(last_commit.author_email).to eq(author_email)
expect(last_commit.author_name).to eq(author_name)
expect(response).to have_http_status(204)
end
end
end
......
......@@ -467,7 +467,7 @@ describe API::Groups, api: true do
it "removes group" do
delete api("/groups/#{group1.id}", user1)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end
it "does not remove a group if not an owner" do
......@@ -496,7 +496,7 @@ describe API::Groups, api: true do
it "removes any existing group" do
delete api("/groups/#{group2.id}", admin)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end
it "does not remove a non existing group" do
......
......@@ -1175,8 +1175,8 @@ describe API::Issues, api: true do
it "deletes the issue if an admin requests it" do
delete api("/projects/#{project.id}/issues/#{issue.id}", owner)
expect(response).to have_http_status(200)
expect(json_response['state']).to eq 'opened'
expect(response).to have_http_status(204)
end
end
......
......@@ -175,9 +175,10 @@ describe API::Labels, api: true do
end
describe 'DELETE /projects/:id/labels' do
it 'returns 200 for existing label' do
it 'returns 204 for existing label' do
delete api("/projects/#{project.id}/labels", user), name: 'label1'
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end
it 'returns 404 for non existing label' do
......
......@@ -263,18 +263,18 @@ describe API::Members, api: true do
expect do
delete api("/#{source_type.pluralize}/#{source.id}/members/#{developer.id}", developer)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change { source.members.count }.by(-1)
end
end
context 'when authenticated as a master/owner' do
context 'and member is a requester' do
it "returns #{source_type == 'project' ? 200 : 404}" do
it 'returns 404' do
expect do
delete api("/#{source_type.pluralize}/#{source.id}/members/#{access_requester.id}", master)
expect(response).to have_http_status(source_type == 'project' ? 200 : 404)
expect(response).to have_http_status(404)
end.not_to change { source.requesters.count }
end
end
......@@ -283,15 +283,15 @@ describe API::Members, api: true do
expect do
delete api("/#{source_type.pluralize}/#{source.id}/members/#{developer.id}", master)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change { source.members.count }.by(-1)
end
end
it "returns #{source_type == 'project' ? 200 : 404} if member does not exist" do
it 'returns 404 if member does not exist' do
delete api("/#{source_type.pluralize}/#{source.id}/members/123", master)
expect(response).to have_http_status(source_type == 'project' ? 200 : 404)
expect(response).to have_http_status(404)
end
end
end
......
......@@ -411,7 +411,7 @@ describe API::MergeRequests, api: true do
it "destroys the merge request owners can destroy" do
delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end
end
end
......
......@@ -373,7 +373,7 @@ describe API::Notes, api: true do
delete api("/projects/#{project.id}/issues/#{issue.id}/"\
"notes/#{issue_note.id}", user)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
# Check if note is really deleted
delete api("/projects/#{project.id}/issues/#{issue.id}/"\
"notes/#{issue_note.id}", user)
......@@ -392,7 +392,7 @@ describe API::Notes, api: true do
delete api("/projects/#{project.id}/snippets/#{snippet.id}/"\
"notes/#{snippet_note.id}", user)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
# Check if note is really deleted
delete api("/projects/#{project.id}/snippets/#{snippet.id}/"\
"notes/#{snippet_note.id}", user)
......@@ -412,7 +412,7 @@ describe API::Notes, api: true do
delete api("/projects/#{project.id}/merge_requests/"\
"#{merge_request.id}/notes/#{merge_request_note.id}", user)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
# Check if note is really deleted
delete api("/projects/#{project.id}/merge_requests/"\
"#{merge_request.id}/notes/#{merge_request_note.id}", user)
......
......@@ -183,13 +183,9 @@ describe API::ProjectHooks, 'ProjectHooks', api: true do
it "deletes hook from project" do
expect do
delete api("/projects/#{project.id}/hooks/#{hook.id}", user)
end.to change {project.hooks.count}.by(-1)
expect(response).to have_http_status(200)
end
it "returns success when deleting hook" do
delete api("/projects/#{project.id}/hooks/#{hook.id}", user)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end.to change {project.hooks.count}.by(-1)
end
it "returns a 404 error when deleting non existent hook" do
......
......@@ -189,7 +189,7 @@ describe API::ProjectSnippets, api: true do
delete api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
end
it 'returns 404 for invalid snippet id' do
......@@ -212,7 +212,7 @@ describe API::ProjectSnippets, api: true do
end
it 'returns 404 for invalid snippet id' do
delete api("/projects/#{snippet.project.id}/snippets/1234", admin)
get api("/projects/#{snippet.project.id}/snippets/1234", admin)
expect(response).to have_http_status(404)
expect(json_response['message']).to eq('404 Snippet Not Found')
......
......@@ -820,8 +820,9 @@ describe API::Projects, api: true do
it 'deletes existing project snippet' do
expect do
delete api("/projects/#{project.id}/snippets/#{snippet.id}", user)
expect(response).to have_http_status(204)
end.to change { Snippet.count }.by(-1)
expect(response).to have_http_status(200)
end
it 'returns 404 when deleting unknown snippet id' do
......@@ -905,8 +906,10 @@ describe API::Projects, api: true do
project_fork_target.reload
expect(project_fork_target.forked_from_project).not_to be_nil
expect(project_fork_target.forked?).to be_truthy
delete api("/projects/#{project_fork_target.id}/fork", admin)
expect(response).to have_http_status(200)
expect(response).to have_http_status(204)
project_fork_target.reload
expect(project_fork_target.forked_from_project).to be_nil
expect(project_fork_target.forked?).not_to be_truthy
......
......@@ -277,8 +277,9 @@ describe API::Runners, api: true do
it 'deletes runner' do
expect do
delete api("/runners/#{shared_runner.id}", admin)
expect(response).to have_http_status(204)
end.to change{ Ci::Runner.shared.count }.by(-1)
expect(response).to have_http_status(200)
end
end
......@@ -286,15 +287,17 @@ describe API::Runners, api: true do
it 'deletes unused runner' do
expect do
delete api("/runners/#{unused_specific_runner.id}", admin)
expect(response).to have_http_status(204)
end.to change{ Ci::Runner.specific.count }.by(-1)
expect(response).to have_http_status(200)
end
it 'deletes used runner' do
expect do
delete api("/runners/#{specific_runner.id}", admin)
expect(response).to have_http_status(204)
end.to change{ Ci::Runner.specific.count }.by(-1)
expect(response).to have_http_status(200)
end
end
......@@ -327,8 +330,9 @@ describe API::Runners, api: true do
it 'deletes runner for one owned project' do
expect do
delete api("/runners/#{specific_runner.id}", user)
expect(response).to have_http_status(204)
end.to change{ Ci::Runner.specific.count }.by(-1)
expect(response).to have_http_status(200)
end
end
end
......@@ -457,8 +461,9 @@ describe API::Runners, api: true do
it "disables project's runner" do
expect do
delete api("/projects/#{project.id}/runners/#{two_projects_runner.id}", user)
expect(response).to have_http_status(204)
end.to change{ project.runners.count }.by(-1)
expect(response).to have_http_status(200)
end
end
......
......@@ -55,7 +55,7 @@ describe API::Services, api: true do
it "deletes #{service}" do
delete api("/projects/#{project.id}/services/#{dashed_service}", user)