Adding a project hook returns status code 400 if url is not given

When adding a project hook a url must be specified or a 400 error code is returned

* Specs added to check status code on handling project hooks
* refactored code, extracted a method
This commit is contained in:
Sebastian Ziebell 2013-02-08 16:33:15 +01:00
parent 818caf0b5d
commit 9544f90389
2 changed files with 41 additions and 12 deletions

View file

@ -4,6 +4,15 @@ module Gitlab
before { authenticate! }
resource :projects do
helpers do
def handle_project_member_errors(errors)
if errors[:project_access].any?
error!(errors[:project_access], 422)
end
not_found!
end
end
# Get a projects list for authenticated user
#
# Example Request:
@ -36,6 +45,7 @@ module Gitlab
# Example Request
# POST /projects
post do
error!("Name is required", 400) if !params.has_key? :name
attrs = attributes_for_keys [:name,
:description,
:default_branch,
@ -43,6 +53,7 @@ module Gitlab
:wall_enabled,
:merge_requests_enabled,
:wiki_enabled]
@project = ::Projects::CreateContext.new(current_user, attrs).execute
if @project.saved?
present @project, with: Entities::Project
@ -106,10 +117,7 @@ module Gitlab
@member = team_member.user
present @member, with: Entities::ProjectMember, project: user_project
else
if team_member.errors[:project_access].any?
error!(team_member.errors[:project_access], 422)
end
not_found!
handle_project_member_errors team_member.errors
end
end
@ -132,10 +140,7 @@ module Gitlab
@member = team_member.user
present @member, with: Entities::ProjectMember, project: user_project
else
if team_member.errors[:project_access].any?
error!(team_member.errors[:project_access], 422)
end
not_found!
handle_project_member_errors team_member.errors
end
end
@ -210,8 +215,9 @@ module Gitlab
@hook = user_project.hooks.find(params[:hook_id])
authorize! :admin_project, user_project
attrs = attributes_for_keys [:url]
error!("Url not given", 400) if !params.has_key? :url
attrs = attributes_for_keys [:url]
if @hook.update_attributes attrs
present @hook, with: Entities::Hook
else

View file

@ -46,9 +46,9 @@ describe Gitlab::API do
response.status.should == 201
end
it "should respond with 404 on failure" do
it "should respond with 400 if name is not given" do
post api("/projects", user)
response.status.should == 404
response.status.should == 400
end
it "should assign attributes to project" do
@ -237,6 +237,13 @@ describe Gitlab::API do
delete api("/projects/#{project.id}/members/#{user3.id}", user)
}.to change { UsersProject.count }.by(-1)
end
it "should return 200 if team member is not part of a project" do
delete api("/projects/#{project.id}/members/#{user3.id}", user)
expect {
delete api("/projects/#{project.id}/members/#{user3.id}", user)
}.to_not change { UsersProject.count }.by(1)
end
end
describe "DELETE /projects/:id/members/:user_id" do
@ -268,6 +275,11 @@ describe Gitlab::API do
response.status.should == 200
json_response['url'].should == hook.url
end
it "should return a 404 error if hook id is not available" do
get api("/projects/#{project.id}/hooks/1234", user)
response.status.should == 404
end
end
describe "POST /projects/:id/hooks" do
@ -276,6 +288,7 @@ describe Gitlab::API do
post api("/projects/#{project.id}/hooks", user),
"url" => "http://example.com"
}.to change {project.hooks.count}.by(1)
response.status.should == 200
end
end
@ -286,8 +299,17 @@ describe Gitlab::API do
response.status.should == 200
json_response['url'].should == 'http://example.org'
end
it "should return 404 error if hook id is not found" do
put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org'
response.status.should == 404
end
it "should return 400 error if url is not given" do
put api("/projects/#{project.id}/hooks/#{hook.id}", user)
response.status.should == 400
end
end
describe "DELETE /projects/:id/hooks" do
it "should delete hook from project" do
@ -295,6 +317,7 @@ describe Gitlab::API do
delete api("/projects/#{project.id}/hooks", user),
hook_id: hook.id
}.to change {project.hooks.count}.by(-1)
response.status.should == 200
end
end