API: status code 403 returned if new project would exceed limit

When the project limit is reached the user is not allowed to create new ones.
Instead of error code 404 the status code 403 (Forbidden) is returned with error
message via API.
This commit is contained in:
Sebastian Ziebell 2013-02-14 15:51:56 +01:00
parent 6fc3263e15
commit 6df02adc7a
3 changed files with 17 additions and 1 deletions

View file

@ -155,7 +155,7 @@ class Project < ActiveRecord::Base
def check_limit def check_limit
unless creator.can_create_project? unless creator.can_create_project?
errors[:base] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it") errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it")
end end
rescue rescue
errors[:base] << ("Can't check your ability to create project") errors[:base] << ("Can't check your ability to create project")

View file

@ -58,6 +58,9 @@ module Gitlab
if @project.saved? if @project.saved?
present @project, with: Entities::Project present @project, with: Entities::Project
else else
if @project.errors[:limit_reached].present?
error!(@project.errors[:limit_reached], 403)
end
not_found! not_found!
end end
end end

View file

@ -41,6 +41,11 @@ describe Gitlab::API do
expect { post api("/projects", user) }.to_not change {Project.count} expect { post api("/projects", user) }.to_not change {Project.count}
end end
it "should return a 400 error if name not given" do
post api("/projects", user)
response.status.should == 400
end
it "should respond with 201 on success" do it "should respond with 201 on success" do
post api("/projects", user), name: 'foo' post api("/projects", user), name: 'foo'
response.status.should == 201 response.status.should == 201
@ -51,6 +56,14 @@ describe Gitlab::API do
response.status.should == 400 response.status.should == 400
end end
it "should return a 403 error if project limit reached" do
(1..user.projects_limit).each do |p|
post api("/projects", user), name: "foo#{p}"
end
post api("/projects", user), name: 'bar'
response.status.should == 403
end
it "should assign attributes to project" do it "should assign attributes to project" do
project = attributes_for(:project, { project = attributes_for(:project, {
description: Faker::Lorem.sentence, description: Faker::Lorem.sentence,