summaryrefslogtreecommitdiff
path: root/spec/requests/api
diff options
context:
space:
mode:
Diffstat (limited to 'spec/requests/api')
-rw-r--r--spec/requests/api/builds_spec.rb79
-rw-r--r--spec/requests/api/projects_spec.rb180
-rw-r--r--spec/requests/api/runners_spec.rb54
3 files changed, 313 insertions, 0 deletions
diff --git a/spec/requests/api/builds_spec.rb b/spec/requests/api/builds_spec.rb
new file mode 100644
index 0000000..0fe2313
--- /dev/null
+++ b/spec/requests/api/builds_spec.rb
@@ -0,0 +1,79 @@
+require 'spec_helper'
+
+describe API::API do
+ include ApiHelpers
+
+ let(:runner) { FactoryGirl.create(:runner) }
+ let(:project) { FactoryGirl.create(:project) }
+
+ describe "Builds API for runners" do
+ before do
+ FactoryGirl.create :runner_project, project_id: project.id, runner_id: runner.id
+ end
+
+ describe "POST /builds/register" do
+ it "should start a build" do
+ commit = FactoryGirl.create(:commit, project: project)
+ build = FactoryGirl.create(:build, commit: commit, status: 'pending' )
+
+ post api("/builds/register"), token: runner.token
+
+ response.status.should == 201
+ json_response['sha'].should == build.sha
+ end
+
+ it "should return 404 error if no pending build found" do
+ post api("/builds/register"), token: runner.token
+
+ response.status.should == 404
+ end
+ end
+
+ describe "PUT /builds/:id" do
+ let(:commit) { FactoryGirl.create(:commit, project: project)}
+ let(:build) { FactoryGirl.create(:build, commit: commit, runner_id: runner.id) }
+
+ it "should update a running build" do
+ build.run!
+ put api("/builds/#{build.id}"), token: runner.token
+ response.status.should == 200
+ end
+ end
+ end
+
+ describe "POST /builds" do
+ let(:data) {
+ {
+ "before" => "95790bf891e76fee5e1747ab589903a6a1f80f22",
+ "after" => "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
+ "ref" => "refs/heads/master",
+ "commits" => [
+ {
+ "id" => "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
+ "message" => "Update Catalan translation to e38cb41.",
+ "timestamp" => "2011-12-12T14:27:31+02:00",
+ "url" => "http://localhost/diaspora/commits/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
+ "author" => {
+ "name" => "Jordi Mallach",
+ "email" => "jordi@softcatala.org",
+ }
+ }
+ ]
+ }
+ }
+
+ it "should create a build" do
+ post api("/builds"), project_id: project.id, data: data, project_token: project.token
+
+ response.status.should == 201
+ json_response['sha'].should == "da1560886d4f094c3e6c9ef40349f7d38b5d27d7"
+ end
+
+ it "should return 400 error if no data passed" do
+ post api("/builds"), project_id: project.id, project_token: project.token
+
+ response.status.should == 400
+ json_response['message'].should == "400 (Bad request) \"data\" not given"
+ end
+ end
+end
diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb
new file mode 100644
index 0000000..bb2f1cb
--- /dev/null
+++ b/spec/requests/api/projects_spec.rb
@@ -0,0 +1,180 @@
+require 'spec_helper'
+
+describe API::API do
+ include ApiHelpers
+
+ let(:gitlab_url) { GitlabCi.config.gitlab_server_urls.first }
+ let(:auth_opts) {
+ {
+ :email => "test@test.com",
+ :password => "123456"
+ }
+ }
+ let(:private_token) { Network.new.authenticate(gitlab_url, auth_opts)["private_token"] }
+
+ let(:options) {
+ {
+ :private_token => private_token,
+ :url => gitlab_url
+ }
+ }
+
+ before {
+ stub_gitlab_calls
+ }
+
+ context "requests for scoped projects" do
+ # NOTE: These ids are tied to the actual projects on demo.gitlab.com
+ describe "GET /projects" do
+ let!(:project1) { FactoryGirl.create(:project, :name => "gitlabhq", :gitlab_id => 3) }
+ let!(:project2) { FactoryGirl.create(:project, :name => "gitlab-ci", :gitlab_id => 4) }
+
+ it "should return all projects on the CI instance" do
+ get api("/projects"), options
+ response.status.should == 200
+ json_response.count.should == 2
+ json_response.first["id"].should == project1.id
+ json_response.last["id"].should == project2.id
+ end
+ end
+
+ describe "GET /projects/owned" do
+ # NOTE: This user doesn't own any of these projects on demo.gitlab.com
+ let!(:project1) { FactoryGirl.create(:project, :name => "gitlabhq", :gitlab_id => 3) }
+ let!(:project2) { FactoryGirl.create(:project, :name => "random-project", :gitlab_id => 9898) }
+
+ it "should return all projects on the CI instance" do
+ get api("/projects/owned"), options
+
+ response.status.should == 200
+ json_response.count.should == 0
+ end
+ end
+ 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
+
+ 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) }
+
+ 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
+
+ describe "POST /projects" do
+ let(:project_info) {
+ {
+ :name => "My project",
+ :gitlab_id => 1,
+ :gitlab_url => "http://example.com/testing/testing",
+ :ssh_url_to_repo => "ssh://example.com/testing/testing.git"
+ }
+ }
+
+ let(:invalid_project_info) { {} }
+
+ context "with valid project info" do
+ before do
+ options.merge!(project_info)
+ end
+
+ it "should create a project with valid data" do
+ post api("/projects"), options
+ response.status.should == 201
+ json_response['name'].should == project_info[:name]
+ end
+ end
+
+ context "with invalid project info" do
+ before do
+ options.merge!(invalid_project_info)
+ end
+
+ it "should error with invalid data" do
+ post api("/projects"), options
+ response.status.should == 400
+ end
+ end
+
+ 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
+
+ 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
+
+ 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) }
+
+ 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
+
+ project.reload
+ project.runners.should be_empty
+ end
+ end
+ end
+end
diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb
new file mode 100644
index 0000000..bf0b658
--- /dev/null
+++ b/spec/requests/api/runners_spec.rb
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+describe API::API do
+ include ApiHelpers
+ include StubGitlabCalls
+
+ before {
+ stub_gitlab_calls
+ }
+
+ describe "GET /runners" do
+ let(:gitlab_url) { GitlabCi.config.gitlab_server_urls.first }
+ let(:auth_opts) {
+ {
+ :email => "test@test.com",
+ :password => "123456"
+ }
+ }
+
+ let(:private_token) { Network.new.authenticate(gitlab_url, auth_opts)["private_token"] }
+ let(:options) {
+ {
+ :private_token => private_token,
+ :url => gitlab_url
+ }
+ }
+
+ before do
+ 5.times { FactoryGirl.create(:runner) }
+ end
+
+ it "should retrieve a list of all runners" do
+ get api("/runners"), options
+ response.status.should == 200
+ json_response.count.should == 5
+ json_response.last.should have_key("id")
+ json_response.last.should have_key("token")
+ end
+ end
+
+ describe "POST /runners/register" do
+ it "should create a runner if token provided" do
+ post api("/runners/register"), token: GitlabCi::REGISTRATION_TOKEN, public_key: 'sha-rsa ....'
+
+ response.status.should == 201
+ end
+
+ it "should return 403 error if no token" do
+ post api("/runners/register")
+
+ response.status.should == 403
+ end
+ end
+end