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,9 +156,9 @@ module Gitlab
156 # DELETE /projects/:id/members/:user_id 156 # DELETE /projects/:id/members/:user_id
157 delete ":id/members/:user_id" do 157 delete ":id/members/:user_id" do
158 authorize! :admin_project, user_project 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 else 162 else
163 {:message => "Access revoked", :id => params[:user_id].to_i} 163 {:message => "Access revoked", :id => params[:user_id].to_i}
164 end 164 end
@@ -205,6 +205,9 @@ module Gitlab @@ -205,6 +205,9 @@ module Gitlab
205 if @hook.save 205 if @hook.save
206 present @hook, with: Entities::Hook 206 present @hook, with: Entities::Hook
207 else 207 else
  208 + if @hook.errors[:url].present?
  209 + error!("Invalid url given", 422)
  210 + end
208 not_found! 211 not_found!
209 end 212 end
210 end 213 end
@@ -227,6 +230,9 @@ module Gitlab @@ -227,6 +230,9 @@ module Gitlab
227 if @hook.update_attributes attrs 230 if @hook.update_attributes attrs
228 present @hook, with: Entities::Hook 231 present @hook, with: Entities::Hook
229 else 232 else
  233 + if @hook.errors[:url].present?
  234 + error!("Invalid url given", 422)
  235 + end
230 not_found! 236 not_found!
231 end 237 end
232 end 238 end
@@ -281,6 +287,7 @@ module Gitlab @@ -281,6 +287,7 @@ module Gitlab
281 # PUT /projects/:id/repository/branches/:branch/protect 287 # PUT /projects/:id/repository/branches/:branch/protect
282 put ":id/repository/branches/:branch/protect" do 288 put ":id/repository/branches/:branch/protect" do
283 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] } 289 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
  290 + not_found! unless @branch
284 protected = user_project.protected_branches.find_by_name(@branch.name) 291 protected = user_project.protected_branches.find_by_name(@branch.name)
285 292
286 unless protected 293 unless protected
@@ -299,6 +306,7 @@ module Gitlab @@ -299,6 +306,7 @@ module Gitlab
299 # PUT /projects/:id/repository/branches/:branch/unprotect 306 # PUT /projects/:id/repository/branches/:branch/unprotect
300 put ":id/repository/branches/:branch/unprotect" do 307 put ":id/repository/branches/:branch/unprotect" do
301 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] } 308 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
  309 + not_found! unless @branch
302 protected = user_project.protected_branches.find_by_name(@branch.name) 310 protected = user_project.protected_branches.find_by_name(@branch.name)
303 311
304 if protected 312 if protected
spec/requests/api/projects_spec.rb
@@ -144,6 +144,17 @@ describe Gitlab::API do @@ -144,6 +144,17 @@ describe Gitlab::API do
144 json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1' 144 json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
145 json_response['protected'].should == true 145 json_response['protected'].should == true
146 end 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 end 158 end
148 159
149 describe "PUT /projects/:id/repository/branches/:branch/unprotect" do 160 describe "PUT /projects/:id/repository/branches/:branch/unprotect" do
@@ -155,6 +166,17 @@ describe Gitlab::API do @@ -155,6 +166,17 @@ describe Gitlab::API do
155 json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1' 166 json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
156 json_response['protected'].should == false 167 json_response['protected'].should == false
157 end 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 end 180 end
159 181
160 describe "GET /projects/:id/members" do 182 describe "GET /projects/:id/members" do
@@ -182,6 +204,11 @@ describe Gitlab::API do @@ -182,6 +204,11 @@ describe Gitlab::API do
182 json_response['email'].should == user.email 204 json_response['email'].should == user.email
183 json_response['access_level'].should == UsersProject::MASTER 205 json_response['access_level'].should == UsersProject::MASTER
184 end 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 end 212 end
186 213
187 describe "POST /projects/:id/members" do 214 describe "POST /projects/:id/members" do
@@ -262,6 +289,12 @@ describe Gitlab::API do @@ -262,6 +289,12 @@ describe Gitlab::API do
262 delete api("/projects/#{project.id}/members/#{user3.id}", user) 289 delete api("/projects/#{project.id}/members/#{user3.id}", user)
263 }.to_not change { UsersProject.count }.by(1) 290 }.to_not change { UsersProject.count }.by(1)
264 end 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 end 298 end
266 299
267 describe "DELETE /projects/:id/members/:user_id" do 300 describe "DELETE /projects/:id/members/:user_id" do
@@ -313,6 +346,11 @@ describe Gitlab::API do @@ -313,6 +346,11 @@ describe Gitlab::API do
313 post api("/projects/#{project.id}/hooks", user) 346 post api("/projects/#{project.id}/hooks", user)
314 response.status.should == 400 347 response.status.should == 400
315 end 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 end 354 end
317 355
318 describe "PUT /projects/:id/hooks/:hook_id" do 356 describe "PUT /projects/:id/hooks/:hook_id" do
@@ -332,6 +370,11 @@ describe Gitlab::API do @@ -332,6 +370,11 @@ describe Gitlab::API do
332 put api("/projects/#{project.id}/hooks/#{hook.id}", user) 370 put api("/projects/#{project.id}/hooks/#{hook.id}", user)
333 response.status.should == 400 371 response.status.should == 400
334 end 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 end 378 end
336 379
337 describe "DELETE /projects/:id/hooks" do 380 describe "DELETE /projects/:id/hooks" do