minor improvements and fixed specs

This commit is contained in:
Zeger-Jan van de Weg 2016-03-18 19:11:25 +01:00
parent 7342a4566c
commit 1d77822816
6 changed files with 37 additions and 32 deletions

View file

@ -118,9 +118,7 @@ module API
end
def authorize!(action, subject)
unless abilities.allowed?(current_user, action, subject)
forbidden!
end
forbidden! unless abilities.allowed?(current_user, action, subject)
end
def authorize_push_project

View file

@ -200,7 +200,8 @@ module API
# DELETE /projects/:id/issues/:issue_id
delete ":id/issues/:issue_id" do
issue = user_project.issues.find(params[:issue_id])
!JLJsdf sdfijsf current_user.can?(:remove_issue, issue)
authorize!(:remove_issue, issue)
issue = user_project.issues.find(params[:issue_id])
issue.destroy

View file

@ -106,9 +106,9 @@ module API
# id (required) - The ID of the project
# merge_request_id (required) - The MR id
delete ":id/merge_requests/:merge_request_id" do
authenticated_as_admin!
merge_request = user_project.merge_requests.find(params[:merge_request_id])
authorize!(:remove_merge_request, merge_request)
merge_request.destroy
present merge_request, with: Entities::MergeRequest

View file

@ -164,7 +164,7 @@ describe Projects::MergeRequestsController do
expect(response.status).to eq 404
end
context "user is an admin" do
context "user is an admin or owner" do
before do
user.admin = true
user.save

View file

@ -6,7 +6,7 @@ describe API::API, api: true do
let(:non_member) { create(:user) }
let(:author) { create(:author) }
let(:assignee) { create(:assignee) }
let(:admin) { create(:admin) }
let(:admin) { create(:user, :admin) }
let!(:project) { create(:project, :public, namespace: user.namespace ) }
let!(:closed_issue) do
create :closed_issue,
@ -469,16 +469,18 @@ describe API::API, api: true do
end
describe "DELETE /projects/:id/issues/:issue_id" do
it "should reject non admins form deleting an issue" do
delete api("/projects/#{project.id}/issues/#{issue.id}", user)
expect(response.status).to eq(403)
it "should reject a non member from deleting an issue" do
delete api("/projects/#{project.id}/issues/#{issue.id}", non_member)
expect(response.status).to be(403)
end
it "should reject a developer from deleting an issue" do
delete api("/projects/#{project.id}/issues/#{issue.id}", author)
expect(response.status).to be(403)
end
it "deletes the issue if an admin requests it" do
user.admin = true
user.save
delete api("/projects/#{project.id}/issues/#{issue.id}", user)
delete api("/projects/#{project.id}/issues/#{issue.id}", admin)
expect(response.status).to eq(200)
expect(json_response['state']).to eq 'opened'
end

View file

@ -4,7 +4,9 @@ describe API::API, api: true do
include ApiHelpers
let(:base_time) { Time.now }
let(:user) { create(:user) }
let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
let(:admin) { create(:user, :admin) }
let(:non_member) { create(:user) }
let!(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test", created_at: base_time) }
let!(:merge_request_closed) { create(:merge_request, state: "closed", author: user, assignee: user, source_project: project, target_project: project, title: "Closed test", created_at: base_time + 1.second) }
let!(:merge_request_merged) { create(:merge_request, state: "merged", author: user, assignee: user, source_project: project, target_project: project, title: "Merged test", created_at: base_time + 2.seconds) }
@ -316,21 +318,23 @@ describe API::API, api: true do
end
describe "DELETE /projects/:id/merge_request/:merge_request_id" do
it "rejects non admin users from deletions" do
delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user)
expect(response.status).to eq(403)
end
it "let's Admins delete a merge request" do
user.admin = true
user.save
it "owners can destroy" do
delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user)
expect(response.status).to eq(200)
end
it "let's Admins and owners delete a merge request" do
delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}", admin)
expect(response.status).to eq(200)
expect(json_response['id']).to eq merge_request.id
end
it "rejects removal from other users" do
delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}", non_member)
expect(response.status).to eq(404)
end
end
describe "PUT /projects/:id/merge_requests/:merge_request_id to close MR" do