BigW Consortium Gitlab

Commit 9544f903 by Sebastian Ziebell

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
parent 818caf0b
...@@ -4,6 +4,15 @@ module Gitlab ...@@ -4,6 +4,15 @@ module Gitlab
before { authenticate! } before { authenticate! }
resource :projects do 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 # Get a projects list for authenticated user
# #
# Example Request: # Example Request:
...@@ -36,6 +45,7 @@ module Gitlab ...@@ -36,6 +45,7 @@ module Gitlab
# Example Request # Example Request
# POST /projects # POST /projects
post do post do
error!("Name is required", 400) if !params.has_key? :name
attrs = attributes_for_keys [:name, attrs = attributes_for_keys [:name,
:description, :description,
:default_branch, :default_branch,
...@@ -43,6 +53,7 @@ module Gitlab ...@@ -43,6 +53,7 @@ module Gitlab
:wall_enabled, :wall_enabled,
:merge_requests_enabled, :merge_requests_enabled,
:wiki_enabled] :wiki_enabled]
@project = ::Projects::CreateContext.new(current_user, attrs).execute @project = ::Projects::CreateContext.new(current_user, attrs).execute
if @project.saved? if @project.saved?
present @project, with: Entities::Project present @project, with: Entities::Project
...@@ -106,10 +117,7 @@ module Gitlab ...@@ -106,10 +117,7 @@ module Gitlab
@member = team_member.user @member = team_member.user
present @member, with: Entities::ProjectMember, project: user_project present @member, with: Entities::ProjectMember, project: user_project
else else
if team_member.errors[:project_access].any? handle_project_member_errors team_member.errors
error!(team_member.errors[:project_access], 422)
end
not_found!
end end
end end
...@@ -132,10 +140,7 @@ module Gitlab ...@@ -132,10 +140,7 @@ module Gitlab
@member = team_member.user @member = team_member.user
present @member, with: Entities::ProjectMember, project: user_project present @member, with: Entities::ProjectMember, project: user_project
else else
if team_member.errors[:project_access].any? handle_project_member_errors team_member.errors
error!(team_member.errors[:project_access], 422)
end
not_found!
end end
end end
...@@ -210,8 +215,9 @@ module Gitlab ...@@ -210,8 +215,9 @@ module Gitlab
@hook = user_project.hooks.find(params[:hook_id]) @hook = user_project.hooks.find(params[:hook_id])
authorize! :admin_project, user_project 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 if @hook.update_attributes attrs
present @hook, with: Entities::Hook present @hook, with: Entities::Hook
else else
......
...@@ -46,9 +46,9 @@ describe Gitlab::API do ...@@ -46,9 +46,9 @@ describe Gitlab::API do
response.status.should == 201 response.status.should == 201
end end
it "should respond with 404 on failure" do it "should respond with 400 if name is not given" do
post api("/projects", user) post api("/projects", user)
response.status.should == 404 response.status.should == 400
end end
it "should assign attributes to project" do it "should assign attributes to project" do
...@@ -237,6 +237,13 @@ describe Gitlab::API do ...@@ -237,6 +237,13 @@ describe Gitlab::API do
delete api("/projects/#{project.id}/members/#{user3.id}", user) delete api("/projects/#{project.id}/members/#{user3.id}", user)
}.to change { UsersProject.count }.by(-1) }.to change { UsersProject.count }.by(-1)
end 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 end
describe "DELETE /projects/:id/members/:user_id" do describe "DELETE /projects/:id/members/:user_id" do
...@@ -268,6 +275,11 @@ describe Gitlab::API do ...@@ -268,6 +275,11 @@ describe Gitlab::API do
response.status.should == 200 response.status.should == 200
json_response['url'].should == hook.url json_response['url'].should == hook.url
end 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 end
describe "POST /projects/:id/hooks" do describe "POST /projects/:id/hooks" do
...@@ -276,6 +288,7 @@ describe Gitlab::API do ...@@ -276,6 +288,7 @@ describe Gitlab::API do
post api("/projects/#{project.id}/hooks", user), post api("/projects/#{project.id}/hooks", user),
"url" => "http://example.com" "url" => "http://example.com"
}.to change {project.hooks.count}.by(1) }.to change {project.hooks.count}.by(1)
response.status.should == 200
end end
end end
...@@ -286,8 +299,17 @@ describe Gitlab::API do ...@@ -286,8 +299,17 @@ describe Gitlab::API do
response.status.should == 200 response.status.should == 200
json_response['url'].should == 'http://example.org' json_response['url'].should == 'http://example.org'
end end
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 describe "DELETE /projects/:id/hooks" do
it "should delete hook from project" do it "should delete hook from project" do
...@@ -295,6 +317,7 @@ describe Gitlab::API do ...@@ -295,6 +317,7 @@ describe Gitlab::API do
delete api("/projects/#{project.id}/hooks", user), delete api("/projects/#{project.id}/hooks", user),
hook_id: hook.id hook_id: hook.id
}.to change {project.hooks.count}.by(-1) }.to change {project.hooks.count}.by(-1)
response.status.should == 200
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment