Commit 9544f9038981b881b539419be72276b2b2fd079f

Authored by Sebastian Ziebell
1 parent 818caf0b

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
lib/api/projects.rb
... ... @@ -4,6 +4,15 @@ module Gitlab
4 4 before { authenticate! }
5 5  
6 6 resource :projects do
  7 + helpers do
  8 + def handle_project_member_errors(errors)
  9 + if errors[:project_access].any?
  10 + error!(errors[:project_access], 422)
  11 + end
  12 + not_found!
  13 + end
  14 + end
  15 +
7 16 # Get a projects list for authenticated user
8 17 #
9 18 # Example Request:
... ... @@ -36,6 +45,7 @@ module Gitlab
36 45 # Example Request
37 46 # POST /projects
38 47 post do
  48 + error!("Name is required", 400) if !params.has_key? :name
39 49 attrs = attributes_for_keys [:name,
40 50 :description,
41 51 :default_branch,
... ... @@ -43,6 +53,7 @@ module Gitlab
43 53 :wall_enabled,
44 54 :merge_requests_enabled,
45 55 :wiki_enabled]
  56 +
46 57 @project = ::Projects::CreateContext.new(current_user, attrs).execute
47 58 if @project.saved?
48 59 present @project, with: Entities::Project
... ... @@ -106,10 +117,7 @@ module Gitlab
106 117 @member = team_member.user
107 118 present @member, with: Entities::ProjectMember, project: user_project
108 119 else
109   - if team_member.errors[:project_access].any?
110   - error!(team_member.errors[:project_access], 422)
111   - end
112   - not_found!
  120 + handle_project_member_errors team_member.errors
113 121 end
114 122 end
115 123  
... ... @@ -132,10 +140,7 @@ module Gitlab
132 140 @member = team_member.user
133 141 present @member, with: Entities::ProjectMember, project: user_project
134 142 else
135   - if team_member.errors[:project_access].any?
136   - error!(team_member.errors[:project_access], 422)
137   - end
138   - not_found!
  143 + handle_project_member_errors team_member.errors
139 144 end
140 145 end
141 146  
... ... @@ -210,8 +215,9 @@ module Gitlab
210 215 @hook = user_project.hooks.find(params[:hook_id])
211 216 authorize! :admin_project, user_project
212 217  
213   - attrs = attributes_for_keys [:url]
  218 + error!("Url not given", 400) if !params.has_key? :url
214 219  
  220 + attrs = attributes_for_keys [:url]
215 221 if @hook.update_attributes attrs
216 222 present @hook, with: Entities::Hook
217 223 else
... ...
spec/requests/api/projects_spec.rb
... ... @@ -46,9 +46,9 @@ describe Gitlab::API do
46 46 response.status.should == 201
47 47 end
48 48  
49   - it "should respond with 404 on failure" do
  49 + it "should respond with 400 if name is not given" do
50 50 post api("/projects", user)
51   - response.status.should == 404
  51 + response.status.should == 400
52 52 end
53 53  
54 54 it "should assign attributes to project" do
... ... @@ -237,6 +237,13 @@ describe Gitlab::API do
237 237 delete api("/projects/#{project.id}/members/#{user3.id}", user)
238 238 }.to change { UsersProject.count }.by(-1)
239 239 end
  240 +
  241 + it "should return 200 if team member is not part of a project" do
  242 + delete api("/projects/#{project.id}/members/#{user3.id}", user)
  243 + expect {
  244 + delete api("/projects/#{project.id}/members/#{user3.id}", user)
  245 + }.to_not change { UsersProject.count }.by(1)
  246 + end
240 247 end
241 248  
242 249 describe "DELETE /projects/:id/members/:user_id" do
... ... @@ -268,6 +275,11 @@ describe Gitlab::API do
268 275 response.status.should == 200
269 276 json_response['url'].should == hook.url
270 277 end
  278 +
  279 + it "should return a 404 error if hook id is not available" do
  280 + get api("/projects/#{project.id}/hooks/1234", user)
  281 + response.status.should == 404
  282 + end
271 283 end
272 284  
273 285 describe "POST /projects/:id/hooks" do
... ... @@ -276,6 +288,7 @@ describe Gitlab::API do
276 288 post api("/projects/#{project.id}/hooks", user),
277 289 "url" => "http://example.com"
278 290 }.to change {project.hooks.count}.by(1)
  291 + response.status.should == 200
279 292 end
280 293 end
281 294  
... ... @@ -286,8 +299,17 @@ describe Gitlab::API do
286 299 response.status.should == 200
287 300 json_response['url'].should == 'http://example.org'
288 301 end
289   - end
290 302  
  303 + it "should return 404 error if hook id is not found" do
  304 + put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org'
  305 + response.status.should == 404
  306 + end
  307 +
  308 + it "should return 400 error if url is not given" do
  309 + put api("/projects/#{project.id}/hooks/#{hook.id}", user)
  310 + response.status.should == 400
  311 + end
  312 + end
291 313  
292 314 describe "DELETE /projects/:id/hooks" do
293 315 it "should delete hook from project" do
... ... @@ -295,6 +317,7 @@ describe Gitlab::API do
295 317 delete api("/projects/#{project.id}/hooks", user),
296 318 hook_id: hook.id
297 319 }.to change {project.hooks.count}.by(-1)
  320 + response.status.should == 200
298 321 end
299 322 end
300 323  
... ...