Commit c305eb31aa1cf1aec24b907e0db1d7b2084400dc

Authored by Sebastian Ziebell
1 parent 6df02adc

API: tests that check status codes for project branches and hooks

Status code 422 (Unprocessable Entity) returned if invalid url is given when creating
or updating a project hook.
lib/api/projects.rb
... ... @@ -156,9 +156,9 @@ module Gitlab
156 156 # DELETE /projects/:id/members/:user_id
157 157 delete ":id/members/:user_id" do
158 158 authorize! :admin_project, user_project
159   - users_project = user_project.users_projects.find_by_user_id params[:user_id]
160   - unless users_project.nil?
161   - users_project.destroy
  159 + team_member = user_project.users_projects.find_by_user_id(params[:user_id])
  160 + unless team_member.nil?
  161 + team_member.destroy
162 162 else
163 163 {:message => "Access revoked", :id => params[:user_id].to_i}
164 164 end
... ... @@ -205,6 +205,9 @@ module Gitlab
205 205 if @hook.save
206 206 present @hook, with: Entities::Hook
207 207 else
  208 + if @hook.errors[:url].present?
  209 + error!("Invalid url given", 422)
  210 + end
208 211 not_found!
209 212 end
210 213 end
... ... @@ -227,6 +230,9 @@ module Gitlab
227 230 if @hook.update_attributes attrs
228 231 present @hook, with: Entities::Hook
229 232 else
  233 + if @hook.errors[:url].present?
  234 + error!("Invalid url given", 422)
  235 + end
230 236 not_found!
231 237 end
232 238 end
... ... @@ -281,6 +287,7 @@ module Gitlab
281 287 # PUT /projects/:id/repository/branches/:branch/protect
282 288 put ":id/repository/branches/:branch/protect" do
283 289 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
  290 + not_found! unless @branch
284 291 protected = user_project.protected_branches.find_by_name(@branch.name)
285 292  
286 293 unless protected
... ... @@ -299,6 +306,7 @@ module Gitlab
299 306 # PUT /projects/:id/repository/branches/:branch/unprotect
300 307 put ":id/repository/branches/:branch/unprotect" do
301 308 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
  309 + not_found! unless @branch
302 310 protected = user_project.protected_branches.find_by_name(@branch.name)
303 311  
304 312 if protected
... ...
spec/requests/api/projects_spec.rb
... ... @@ -144,6 +144,17 @@ describe Gitlab::API do
144 144 json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
145 145 json_response['protected'].should == true
146 146 end
  147 +
  148 + it "should return a 404 error if branch not found" do
  149 + put api("/projects/#{project.id}/repository/branches/unknown/protect", user)
  150 + response.status.should == 404
  151 + end
  152 +
  153 + it "should return success when protect branch again" do
  154 + put api("/projects/#{project.id}/repository/branches/new_design/protect", user)
  155 + put api("/projects/#{project.id}/repository/branches/new_design/protect", user)
  156 + response.status.should == 200
  157 + end
147 158 end
148 159  
149 160 describe "PUT /projects/:id/repository/branches/:branch/unprotect" do
... ... @@ -155,6 +166,17 @@ describe Gitlab::API do
155 166 json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
156 167 json_response['protected'].should == false
157 168 end
  169 +
  170 + it "should return success when unprotect branch" do
  171 + put api("/projects/#{project.id}/repository/branches/unknown/unprotect", user)
  172 + response.status.should == 404
  173 + end
  174 +
  175 + it "should return success when unprotect branch again" do
  176 + put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)
  177 + put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)
  178 + response.status.should == 200
  179 + end
158 180 end
159 181  
160 182 describe "GET /projects/:id/members" do
... ... @@ -182,6 +204,11 @@ describe Gitlab::API do
182 204 json_response['email'].should == user.email
183 205 json_response['access_level'].should == UsersProject::MASTER
184 206 end
  207 +
  208 + it "should return a 404 error if user id not found" do
  209 + get api("/projects/#{project.id}/members/1234", user)
  210 + response.status.should == 404
  211 + end
185 212 end
186 213  
187 214 describe "POST /projects/:id/members" do
... ... @@ -262,6 +289,12 @@ describe Gitlab::API do
262 289 delete api("/projects/#{project.id}/members/#{user3.id}", user)
263 290 }.to_not change { UsersProject.count }.by(1)
264 291 end
  292 +
  293 + it "should return 200 if team member already removed" do
  294 + delete api("/projects/#{project.id}/members/#{user3.id}", user)
  295 + delete api("/projects/#{project.id}/members/#{user3.id}", user)
  296 + response.status.should == 200
  297 + end
265 298 end
266 299  
267 300 describe "DELETE /projects/:id/members/:user_id" do
... ... @@ -313,6 +346,11 @@ describe Gitlab::API do
313 346 post api("/projects/#{project.id}/hooks", user)
314 347 response.status.should == 400
315 348 end
  349 +
  350 + it "should return a 422 error if url not valid" do
  351 + post api("/projects/#{project.id}/hooks", user), "url" => "ftp://example.com"
  352 + response.status.should == 422
  353 + end
316 354 end
317 355  
318 356 describe "PUT /projects/:id/hooks/:hook_id" do
... ... @@ -332,6 +370,11 @@ describe Gitlab::API do
332 370 put api("/projects/#{project.id}/hooks/#{hook.id}", user)
333 371 response.status.should == 400
334 372 end
  373 +
  374 + it "should return a 422 error if url is not valid" do
  375 + put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: 'ftp://example.com'
  376 + response.status.should == 422
  377 + end
335 378 end
336 379  
337 380 describe "DELETE /projects/:id/hooks" do
... ...