diff options
-rw-r--r-- | lib/api/projects.rb | 58 | ||||
-rw-r--r-- | spec/requests/projects_spec.rb | 174 |
2 files changed, 130 insertions, 102 deletions
diff --git a/lib/api/projects.rb b/lib/api/projects.rb index c482f4a..f2334a5 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -4,22 +4,6 @@ module API before { authenticate! } resource :projects do - # Retrieve info for a Gitlab CI project - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id - get ":id" do - project = Project.find(params[:id]) - - if current_user.can_access_project?(project.gitlab_id) - present project, with: Entities::Project - else - unauthorized! - end - end - # Retrieve all Gitlab CI projects that the user has access to # # Example Request: @@ -37,7 +21,6 @@ module API # Example Request: # GET /projects/owned get "owned" do - byebug gitlab_projects = Project.from_gitlab(current_user, nil, nil, :owned) ids = gitlab_projects.map { |project| project.id } @@ -45,6 +28,22 @@ module API present projects, with: Entities::Project end + # Retrieve info for a Gitlab CI project + # + # Parameters: + # id (required) - The ID of a project + # Example Request: + # GET /projects/:id + get ":id" do + project = Project.find(params[:id]) + + if current_user.can_access_project?(project.gitlab_id) + present project, with: Entities::Project + else + unauthorized! + end + end + # Create Gitlab CI project using Gitlab project info # # Parameters: @@ -131,16 +130,16 @@ module API # Example Request: # POST /projects/:id/runners/:runner_id post ":id/runners/:runner_id" do - unauthorized! unless current_user.can_access_project?(params[:id]) + project = Project.find_by_id(params[:id]) + runner = Runner.find_by_id(params[:runner_id]) - project_exists = Project.exists?(params[:id]) - runner_exists = Runner.exists?(params[:runner_id]) + not_found! if project.blank? or runner.blank? - not_found! if project_exists.blank? or runner_exists.blank? + unauthorized! unless current_user.can_access_project?(project.gitlab_id) options = { - :project_id => params[:id], - :runner_id => params[:runner_id] + :project_id => project.id, + :runner_id => runner.id } runner_project = RunnerProject.new(options) @@ -161,16 +160,15 @@ module API # Example Request: # DELETE /projects/:id/runners/:runner_id delete ":id/runners/:runner_id" do - unauthorized! unless current_user.can_access_project?(params[:id]) - - project_exists = Project.exists?(params[:id]) - runner_exists = Runner.exists?(params[:runner_id]) + project = Project.find_by_id(params[:id]) + runner = Runner.find_by_id(params[:runner_id]) - not_found! if project_exists.blank? or runner_exists.blank? + not_found! if project.blank? or runner.blank? + unauthorized! unless current_user.can_access_project?(project.gitlab_id) options = { - :project_id => params[:id], - :runner_id => params[:runner_id] + :project_id => project.id, + :runner_id => runner.id } runner_project = RunnerProject.where(options).first diff --git a/spec/requests/projects_spec.rb b/spec/requests/projects_spec.rb index dd55938..1aea2c3 100644 --- a/spec/requests/projects_spec.rb +++ b/spec/requests/projects_spec.rb @@ -22,7 +22,7 @@ describe API::API do to_return( :status => 200, :headers => {"Content-Type" => "application/json"}, - :body => { :url => "http://myurl" }.to_json) + :body => { :id => 1, :url => "http://myurl" }.to_json) end context "requests for scoped projects" do @@ -48,7 +48,6 @@ describe API::API do end it "should return all projects on the CI instance" do - byebug get api("/projects"), options response.status.should == 200 json_response.count.should == 2 @@ -72,6 +71,7 @@ describe API::API do it "should return all projects on the CI instance" do get api("/projects/owned"), options + response.status.should == 200 json_response.count.should == 1 json_response.first["id"].should == project2.id @@ -79,23 +79,71 @@ describe API::API do end end - describe "GET /projects/:id" do - let(:project) { FactoryGirl.create(:project) } + context "with valid access privileges" do + let(:gitlab_project_url) { + File.join(gitlab_url, Network::API_PREFIX, "projects", "#{project.gitlab_id}.json") } - context "with an existing project" do - it "should retrieve the project info" do - get api("/projects/#{project.id}"), options - response.status.should == 200 - json_response['id'].should == project.id + before do + # stub authentication endpoint for owned projects + stub_request(:get, gitlab_project_url). + with(:query => { :private_token => private_token }). + to_return( + :status => 200, + :headers => {"Content-Type" => "application/json"}, + :body => project.to_json) + end + + describe "GET /projects/:id" do + let(:project) { FactoryGirl.create(:project) } + + context "with an existing project" do + it "should retrieve the project info" do + get api("/projects/#{project.id}"), options + response.status.should == 200 + json_response['id'].should == project.id + end + end + + context "with a non-existing project" do + it "should return 404 error if project not found" do + get api("/projects/non_existent_id"), options + response.status.should == 404 + end end end - context "with a non-existing project" do - it "should return 404 error if project not found" do - get api("/projects/non_existent_id"), options + describe "PUT /projects/:id" do + let(:project) { FactoryGirl.create(:project) } + let(:project_info) { {:name => "An updated name!" } } + + before do + options.merge!(project_info) + end + + it "should update a specific project's information" do + put api("/projects/#{project.id}"), options + response.status.should == 200 + json_response["name"].should == project_info[:name] + end + + it "fails to update a non-existing project" do + put api("/projects/non-existant-id"), options response.status.should == 404 end end + + describe "DELETE /projects/:id" do + let(:project) { FactoryGirl.create(:project) } + + before { project } + + it "should delete a specific project" do + delete api("/projects/#{project.id}"), options + response.status.should == 200 + + expect { project.reload }.to raise_error + end + end end describe "POST /projects" do @@ -132,77 +180,59 @@ describe API::API do response.status.should == 400 end end - end - describe "PUT /projects/:id" do - let(:project) { FactoryGirl.create(:project) } - let(:project_info) { {:name => "An updated name!" } } + context "for project runners" do + let(:gitlab_project_url) { + File.join(gitlab_url, Network::API_PREFIX, "projects", "#{project.gitlab_id}.json") } - before do - options.merge!(project_info) - end - - it "should update a specific project's information" do - put api("/projects/#{project.id}"), options - response.status.should == 200 - json_response["name"].should == project_info[:name] - end - - it "fails to update a non-existing project" do - put api("/projects/non-existant-id"), options - response.status.should == 404 - end - end - - describe "DELETE /projects/:id" do - let(:project) { FactoryGirl.create(:project) } - - before { project } - - it "should delete a specific project" do - delete api("/projects/#{project.id}"), options - response.status.should == 200 - - expect { project.reload }.to raise_error - end - end + before do + # stub authentication endpoint for owned projects + stub_request(:get, gitlab_project_url). + with(:query => { :private_token => private_token }). + to_return( + :status => 200, + :headers => {"Content-Type" => "application/json"}, + :body => project.to_json) + end - describe "POST /projects/:id/runners/:id" do - let(:project) { FactoryGirl.create(:project) } - let(:runner) { FactoryGirl.create(:runner) } + describe "POST /projects/:id/runners/:id" do + let(:project) { FactoryGirl.create(:project) } + let(:runner) { FactoryGirl.create(:runner) } - it "should add the project to the runner" do - post api("/projects/#{project.id}/runners/#{runner.id}"), options - response.status.should == 201 + it "should add the project to the runner" do + post api("/projects/#{project.id}/runners/#{runner.id}"), options + response.status.should == 201 - project.reload - project.runners.first.id.should == runner.id - end + project.reload + project.runners.first.id.should == runner.id + end - it "should fail if it tries to link a non-existing project or runner" do - post api("/projects/#{project.id}/runners/non-existing"), options - response.status.should == 404 + it "should fail if it tries to link a non-existing project or runner" do + post api("/projects/#{project.id}/runners/non-existing"), options + response.status.should == 404 - post api("/projects/non-existing/runners/#{runner.id}"), options - response.status.should == 404 - end - end + post api("/projects/non-existing/runners/#{runner.id}"), options + response.status.should == 404 + end + end - describe "DELETE /projects/:id/runners/:id" do - let(:project) { FactoryGirl.create(:project) } - let(:runner) { FactoryGirl.create(:runner) } + describe "DELETE /projects/:id/runners/:id" do + let(:project) { FactoryGirl.create(:project) } + let(:runner) { FactoryGirl.create(:runner) } - before do - post api("/projects/#{project.id}/runners/#{runner.id}"), options - end + before do + post api("/projects/#{project.id}/runners/#{runner.id}"), options + end - it "should remove the project from the runner" do - project.runners.should be_present - delete api("/projects/#{project.id}/runners/#{runner.id}"), options - response.status.should == 200 + it "should remove the project from the runner" do + project.runners.should be_present + delete api("/projects/#{project.id}/runners/#{runner.id}"), options + response.status.should == 200 - project.reload - project.runners.should be_empty + project.reload + project.runners.should be_empty + end + end end end end |