From c305eb31aa1cf1aec24b907e0db1d7b2084400dc Mon Sep 17 00:00:00 2001
From: Sebastian Ziebell <sebastian.ziebell@asquera.de>
Date: Thu, 14 Feb 2013 16:55:33 +0100
Subject: [PATCH] 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                | 14 +++++++---
 spec/requests/api/projects_spec.rb | 43 ++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/lib/api/projects.rb b/lib/api/projects.rb
index 87653f0445..cf3e8257a7 100644
--- a/lib/api/projects.rb
+++ b/lib/api/projects.rb
@@ -156,9 +156,9 @@ module Gitlab
       #   DELETE /projects/:id/members/:user_id
       delete ":id/members/:user_id" do
         authorize! :admin_project, user_project
-        users_project = user_project.users_projects.find_by_user_id params[:user_id]
-        unless users_project.nil?
-          users_project.destroy
+        team_member = user_project.users_projects.find_by_user_id(params[:user_id])
+        unless team_member.nil?
+          team_member.destroy
         else
           {:message => "Access revoked", :id => params[:user_id].to_i}
         end
@@ -205,6 +205,9 @@ module Gitlab
         if @hook.save
           present @hook, with: Entities::Hook
         else
+          if @hook.errors[:url].present?
+            error!("Invalid url given", 422)
+          end
           not_found!
         end
       end
@@ -227,6 +230,9 @@ module Gitlab
         if @hook.update_attributes attrs
           present @hook, with: Entities::Hook
         else
+          if @hook.errors[:url].present?
+            error!("Invalid url given", 422)
+          end
           not_found!
         end
       end
@@ -281,6 +287,7 @@ module Gitlab
       #   PUT /projects/:id/repository/branches/:branch/protect
       put ":id/repository/branches/:branch/protect" do
         @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
+        not_found! unless @branch
         protected = user_project.protected_branches.find_by_name(@branch.name)
 
         unless protected
@@ -299,6 +306,7 @@ module Gitlab
       #   PUT /projects/:id/repository/branches/:branch/unprotect
       put ":id/repository/branches/:branch/unprotect" do
         @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
+        not_found! unless @branch
         protected = user_project.protected_branches.find_by_name(@branch.name)
 
         if protected
diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb
index b635307884..96f58dde19 100644
--- a/spec/requests/api/projects_spec.rb
+++ b/spec/requests/api/projects_spec.rb
@@ -144,6 +144,17 @@ describe Gitlab::API do
       json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
       json_response['protected'].should == true
     end
+
+    it "should return a 404 error if branch not found" do
+      put api("/projects/#{project.id}/repository/branches/unknown/protect", user)
+      response.status.should == 404
+    end
+
+    it "should return success when protect branch again" do
+      put api("/projects/#{project.id}/repository/branches/new_design/protect", user)
+      put api("/projects/#{project.id}/repository/branches/new_design/protect", user)
+      response.status.should == 200
+    end
   end
 
   describe "PUT /projects/:id/repository/branches/:branch/unprotect" do
@@ -155,6 +166,17 @@ describe Gitlab::API do
       json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
       json_response['protected'].should == false
     end
+
+    it "should return success when unprotect branch" do
+      put api("/projects/#{project.id}/repository/branches/unknown/unprotect", user)
+      response.status.should == 404
+    end
+
+    it "should return success when unprotect branch again" do
+      put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)
+      put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)
+      response.status.should == 200
+    end
   end
 
   describe "GET /projects/:id/members" do
@@ -182,6 +204,11 @@ describe Gitlab::API do
       json_response['email'].should == user.email
       json_response['access_level'].should == UsersProject::MASTER
     end
+
+    it "should return a 404 error if user id not found" do
+      get api("/projects/#{project.id}/members/1234", user)
+      response.status.should == 404
+    end
   end
 
   describe "POST /projects/:id/members" do
@@ -262,6 +289,12 @@ describe Gitlab::API do
         delete api("/projects/#{project.id}/members/#{user3.id}", user)
       }.to_not change { UsersProject.count }.by(1)
     end
+
+    it "should return 200 if team member already removed" do
+      delete api("/projects/#{project.id}/members/#{user3.id}", user)
+      delete api("/projects/#{project.id}/members/#{user3.id}", user)
+      response.status.should == 200
+    end
   end
 
   describe "DELETE /projects/:id/members/:user_id" do
@@ -313,6 +346,11 @@ describe Gitlab::API do
       post api("/projects/#{project.id}/hooks", user)
       response.status.should == 400
     end
+
+    it "should return a 422 error if url not valid" do
+      post api("/projects/#{project.id}/hooks", user), "url" => "ftp://example.com"
+      response.status.should == 422
+    end
   end
 
   describe "PUT /projects/:id/hooks/:hook_id" do
@@ -332,6 +370,11 @@ describe Gitlab::API do
       put api("/projects/#{project.id}/hooks/#{hook.id}", user)
       response.status.should == 400
     end
+
+    it "should return a 422 error if url is not valid" do
+      put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: 'ftp://example.com'
+      response.status.should == 422
+    end
   end
 
   describe "DELETE /projects/:id/hooks" do
-- 
2.30.9