diff --git a/lib/api/projects.rb b/lib/api/projects.rb index e6df6b4e..f1e0f32e 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -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 diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 91632407..11c3d012 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -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 - 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