summaryrefslogtreecommitdiff
path: root/spec/requests
diff options
context:
space:
mode:
Diffstat (limited to 'spec/requests')
-rw-r--r--spec/requests/api/merge_requests_spec.rb25
-rw-r--r--spec/requests/ci/api/builds_spec.rb115
-rw-r--r--spec/requests/ci/api/commits_spec.rb65
-rw-r--r--spec/requests/ci/api/forks_spec.rb59
-rw-r--r--spec/requests/ci/api/projects_spec.rb267
-rw-r--r--spec/requests/ci/api/runners_spec.rb83
-rw-r--r--spec/requests/ci/api/triggers_spec.rb78
-rw-r--r--spec/requests/ci/builds_spec.rb18
-rw-r--r--spec/requests/ci/commits_spec.rb17
9 files changed, 715 insertions, 12 deletions
diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb
index 942768fa254..35b3d3e296a 100644
--- a/spec/requests/api/merge_requests_spec.rb
+++ b/spec/requests/api/merge_requests_spec.rb
@@ -2,11 +2,12 @@ require "spec_helper"
describe API::API, api: true do
include ApiHelpers
+ let(:base_time) { Time.now }
let(:user) { create(:user) }
let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
- let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test") }
- let!(:merge_request_closed) { create(:merge_request, state: "closed", author: user, assignee: user, source_project: project, target_project: project, title: "Closed test") }
- let!(:merge_request_merged) { create(:merge_request, state: "merged", author: user, assignee: user, source_project: project, target_project: project, title: "Merged test") }
+ let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test", created_at: base_time) }
+ let!(:merge_request_closed) { create(:merge_request, state: "closed", author: user, assignee: user, source_project: project, target_project: project, title: "Closed test", created_at: base_time + 1.seconds) }
+ let!(:merge_request_merged) { create(:merge_request, state: "merged", author: user, assignee: user, source_project: project, target_project: project, title: "Merged test", created_at: base_time + 2.seconds) }
let!(:note) { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "a comment on a MR") }
let!(:note2) { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "another comment on a MR") }
@@ -74,8 +75,8 @@ describe API::API, api: true do
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
- expect(json_response.last['id']).to eq(@mr_earlier.id)
- expect(json_response.first['id']).to eq(@mr_later.id)
+ response_dates = json_response.map{ |merge_request| merge_request['created_at'] }
+ expect(response_dates).to eq(response_dates.sort)
end
it "should return an array of merge_requests in descending order" do
@@ -83,8 +84,8 @@ describe API::API, api: true do
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
- expect(json_response.first['id']).to eq(@mr_later.id)
- expect(json_response.last['id']).to eq(@mr_earlier.id)
+ response_dates = json_response.map{ |merge_request| merge_request['created_at'] }
+ expect(response_dates).to eq(response_dates.sort.reverse)
end
it "should return an array of merge_requests ordered by updated_at" do
@@ -92,17 +93,17 @@ describe API::API, api: true do
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
- expect(json_response.last['id']).to eq(@mr_earlier.id)
- expect(json_response.first['id']).to eq(@mr_later.id)
+ response_dates = json_response.map{ |merge_request| merge_request['updated_at'] }
+ expect(response_dates).to eq(response_dates.sort.reverse)
end
it "should return an array of merge_requests ordered by created_at" do
- get api("/projects/#{project.id}/merge_requests?sort=created_at", user)
+ get api("/projects/#{project.id}/merge_requests?order_by=created_at&sort=asc", user)
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
- expect(json_response.last['id']).to eq(@mr_earlier.id)
- expect(json_response.first['id']).to eq(@mr_later.id)
+ response_dates = json_response.map{ |merge_request| merge_request['created_at'] }
+ expect(response_dates).to eq(response_dates.sort)
end
end
end
diff --git a/spec/requests/ci/api/builds_spec.rb b/spec/requests/ci/api/builds_spec.rb
new file mode 100644
index 00000000000..c25d1823306
--- /dev/null
+++ b/spec/requests/ci/api/builds_spec.rb
@@ -0,0 +1,115 @@
+require 'spec_helper'
+
+describe Ci::API::API do
+ include ApiHelpers
+
+ let(:runner) { FactoryGirl.create(:ci_runner, tag_list: ["mysql", "ruby"]) }
+ let(:project) { FactoryGirl.create(:ci_project) }
+
+ describe "Builds API for runners" do
+ let(:shared_runner) { FactoryGirl.create(:ci_runner, token: "SharedRunner") }
+ let(:shared_project) { FactoryGirl.create(:ci_project, name: "SharedProject") }
+
+ before do
+ FactoryGirl.create :ci_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(:ci_commit, project: project)
+ commit.create_builds
+ build = commit.builds.first
+
+ post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
+
+ expect(response.status).to eq(201)
+ expect(json_response['sha']).to eq(build.sha)
+ expect(runner.reload.platform).to eq("darwin")
+ end
+
+ it "should return 404 error if no pending build found" do
+ post ci_api("/builds/register"), token: runner.token
+
+ expect(response.status).to eq(404)
+ end
+
+ it "should return 404 error if no builds for specific runner" do
+ commit = FactoryGirl.create(:ci_commit, project: shared_project)
+ FactoryGirl.create(:ci_build, commit: commit, status: 'pending' )
+
+ post ci_api("/builds/register"), token: runner.token
+
+ expect(response.status).to eq(404)
+ end
+
+ it "should return 404 error if no builds for shared runner" do
+ commit = FactoryGirl.create(:ci_commit, project: project)
+ FactoryGirl.create(:ci_build, commit: commit, status: 'pending' )
+
+ post ci_api("/builds/register"), token: shared_runner.token
+
+ expect(response.status).to eq(404)
+ end
+
+ it "returns options" do
+ commit = FactoryGirl.create(:ci_commit, project: project)
+ commit.create_builds
+
+ post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
+
+ expect(response.status).to eq(201)
+ expect(json_response["options"]).to eq({ "image" => "ruby:2.1", "services" => ["postgres"] })
+ end
+
+ it "returns variables" do
+ commit = FactoryGirl.create(:ci_commit, project: project)
+ commit.create_builds
+ project.variables << Ci::Variable.new(key: "SECRET_KEY", value: "secret_value")
+
+ post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
+
+ expect(response.status).to eq(201)
+ expect(json_response["variables"]).to eq([
+ { "key" => "DB_NAME", "value" => "postgres", "public" => true },
+ { "key" => "SECRET_KEY", "value" => "secret_value", "public" => false },
+ ])
+ end
+
+ it "returns variables for triggers" do
+ trigger = FactoryGirl.create(:ci_trigger, project: project)
+ commit = FactoryGirl.create(:ci_commit, project: project)
+
+ trigger_request = FactoryGirl.create(:ci_trigger_request_with_variables, commit: commit, trigger: trigger)
+ commit.create_builds(trigger_request)
+ project.variables << Ci::Variable.new(key: "SECRET_KEY", value: "secret_value")
+
+ post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
+
+ expect(response.status).to eq(201)
+ expect(json_response["variables"]).to eq([
+ { "key" => "DB_NAME", "value" => "postgres", "public" => true },
+ { "key" => "SECRET_KEY", "value" => "secret_value", "public" => false },
+ { "key" => "TRIGGER_KEY", "value" => "TRIGGER_VALUE", "public" => false },
+ ])
+ end
+ end
+
+ describe "PUT /builds/:id" do
+ let(:commit) { FactoryGirl.create(:ci_commit, project: project)}
+ let(:build) { FactoryGirl.create(:ci_build, commit: commit, runner_id: runner.id) }
+
+ it "should update a running build" do
+ build.run!
+ put ci_api("/builds/#{build.id}"), token: runner.token
+ expect(response.status).to eq(200)
+ end
+
+ it 'Should not override trace information when no trace is given' do
+ build.run!
+ build.update!(trace: 'hello_world')
+ put ci_api("/builds/#{build.id}"), token: runner.token
+ expect(build.reload.trace).to eq 'hello_world'
+ end
+ end
+ end
+end
diff --git a/spec/requests/ci/api/commits_spec.rb b/spec/requests/ci/api/commits_spec.rb
new file mode 100644
index 00000000000..e89b6651499
--- /dev/null
+++ b/spec/requests/ci/api/commits_spec.rb
@@ -0,0 +1,65 @@
+require 'spec_helper'
+
+describe Ci::API::API, 'Commits' do
+ include ApiHelpers
+
+ let(:project) { FactoryGirl.create(:ci_project) }
+ let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
+
+ let(:options) do
+ {
+ project_token: project.token,
+ project_id: project.id
+ }
+ end
+
+ describe "GET /commits" do
+ before { commit }
+
+ it "should return commits per project" do
+ get ci_api("/commits"), options
+
+ expect(response.status).to eq(200)
+ expect(json_response.count).to eq(1)
+ expect(json_response.first["project_id"]).to eq(project.id)
+ expect(json_response.first["sha"]).to eq(commit.sha)
+ end
+ end
+
+ describe "POST /commits" do
+ let(:data) do
+ {
+ "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",
+ }
+ }
+ ],
+ ci_yaml_file: gitlab_ci_yaml
+ }
+ end
+
+ it "should create a build" do
+ post ci_api("/commits"), options.merge(data: data)
+
+ expect(response.status).to eq(201)
+ expect(json_response['sha']).to eq("da1560886d4f094c3e6c9ef40349f7d38b5d27d7")
+ end
+
+ it "should return 400 error if no data passed" do
+ post ci_api("/commits"), options
+
+ expect(response.status).to eq(400)
+ expect(json_response['message']).to eq("400 (Bad request) \"data\" not given")
+ end
+ end
+end
diff --git a/spec/requests/ci/api/forks_spec.rb b/spec/requests/ci/api/forks_spec.rb
new file mode 100644
index 00000000000..37fa1e82f25
--- /dev/null
+++ b/spec/requests/ci/api/forks_spec.rb
@@ -0,0 +1,59 @@
+require 'spec_helper'
+
+describe Ci::API::API do
+ include ApiHelpers
+
+ let(:project) { FactoryGirl.create(:ci_project) }
+ let(:private_token) { create(:user).private_token }
+
+ let(:options) do
+ {
+ private_token: private_token,
+ url: GitlabCi.config.gitlab_ci.url
+ }
+ end
+
+ before do
+ stub_gitlab_calls
+ end
+
+
+ describe "POST /forks" do
+ let(:project_info) do
+ {
+ project_id: project.gitlab_id,
+ project_token: project.token,
+ data: {
+ id: create(:empty_project).id,
+ name_with_namespace: "Gitlab.org / Underscore",
+ path_with_namespace: "gitlab-org/underscore",
+ default_branch: "master",
+ ssh_url_to_repo: "git@example.com:gitlab-org/underscore"
+ }
+ }
+ end
+
+ context "with valid info" do
+ before do
+ options.merge!(project_info)
+ end
+
+ it "should create a project with valid data" do
+ post ci_api("/forks"), options
+ expect(response.status).to eq(201)
+ expect(json_response['name']).to eq("Gitlab.org / Underscore")
+ end
+ end
+
+ context "with invalid project info" do
+ before do
+ options.merge!({})
+ end
+
+ it "should error with invalid data" do
+ post ci_api("/forks"), options
+ expect(response.status).to eq(400)
+ end
+ end
+ end
+end
diff --git a/spec/requests/ci/api/projects_spec.rb b/spec/requests/ci/api/projects_spec.rb
new file mode 100644
index 00000000000..2adae52e79e
--- /dev/null
+++ b/spec/requests/ci/api/projects_spec.rb
@@ -0,0 +1,267 @@
+require 'spec_helper'
+
+describe Ci::API::API do
+ include ApiHelpers
+
+ let(:gitlab_url) { GitlabCi.config.gitlab_ci.url }
+ let(:user) { create(:user) }
+ let(:private_token) { user.private_token }
+
+ let(:options) do
+ {
+ private_token: private_token,
+ url: gitlab_url
+ }
+ end
+
+ before do
+ stub_gitlab_calls
+ end
+
+ 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(:ci_project) }
+ let!(:project2) { FactoryGirl.create(:ci_project) }
+
+ before do
+ project1.gl_project.team << [user, :developer]
+ project2.gl_project.team << [user, :developer]
+ end
+
+ it "should return all projects on the CI instance" do
+ get ci_api("/projects"), options
+ expect(response.status).to eq(200)
+ expect(json_response.count).to eq(2)
+ expect(json_response.first["id"]).to eq(project1.id)
+ expect(json_response.last["id"]).to eq(project2.id)
+ end
+ end
+
+ describe "GET /projects/owned" do
+ let!(:gl_project1) {FactoryGirl.create(:empty_project, namespace: user.namespace)}
+ let!(:gl_project2) {FactoryGirl.create(:empty_project, namespace: user.namespace)}
+ let!(:project1) { FactoryGirl.create(:ci_project, gl_project: gl_project1) }
+ let!(:project2) { FactoryGirl.create(:ci_project, gl_project: gl_project2) }
+
+ before do
+ project1.gl_project.team << [user, :developer]
+ project2.gl_project.team << [user, :developer]
+ end
+
+ it "should return all projects on the CI instance" do
+ get ci_api("/projects/owned"), options
+
+ expect(response.status).to eq(200)
+ expect(json_response.count).to eq(2)
+ end
+ end
+ end
+
+ describe "POST /projects/:project_id/webhooks" do
+ let!(:project) { FactoryGirl.create(:ci_project) }
+
+ context "Valid Webhook URL" do
+ let!(:webhook) { { web_hook: "http://example.com/sth/1/ala_ma_kota" } }
+
+ before do
+ options.merge!(webhook)
+ end
+
+ it "should create webhook for specified project" do
+ project.gl_project.team << [user, :master]
+ post ci_api("/projects/#{project.id}/webhooks"), options
+ expect(response.status).to eq(201)
+ expect(json_response["url"]).to eq(webhook[:web_hook])
+ end
+
+ it "fails to create webhook for non existsing project" do
+ post ci_api("/projects/non-existant-id/webhooks"), options
+ expect(response.status).to eq(404)
+ end
+
+ it "non-manager is not authorized" do
+ post ci_api("/projects/#{project.id}/webhooks"), options
+ expect(response.status).to eq(401)
+ end
+ end
+
+ context "Invalid Webhook URL" do
+ let!(:webhook) { { web_hook: "ala_ma_kota" } }
+
+ before do
+ options.merge!(webhook)
+ end
+
+ it "fails to create webhook for not valid url" do
+ project.gl_project.team << [user, :master]
+ post ci_api("/projects/#{project.id}/webhooks"), options
+ expect(response.status).to eq(400)
+ end
+ end
+
+ context "Missed web_hook parameter" do
+ it "fails to create webhook for not provided url" do
+ project.gl_project.team << [user, :master]
+ post ci_api("/projects/#{project.id}/webhooks"), options
+ expect(response.status).to eq(400)
+ end
+ end
+ end
+
+ describe "GET /projects/:id" do
+ let!(:project) { FactoryGirl.create(:ci_project) }
+
+ before do
+ project.gl_project.team << [user, :developer]
+ end
+
+ context "with an existing project" do
+ it "should retrieve the project info" do
+ get ci_api("/projects/#{project.id}"), options
+ expect(response.status).to eq(200)
+ expect(json_response['id']).to eq(project.id)
+ end
+ end
+
+ context "with a non-existing project" do
+ it "should return 404 error if project not found" do
+ get ci_api("/projects/non_existent_id"), options
+ expect(response.status).to eq(404)
+ end
+ end
+ end
+
+ describe "PUT /projects/:id" do
+ let!(:project) { FactoryGirl.create(:ci_project) }
+ let!(:project_info) { { name: "An updated name!" } }
+
+ before do
+ options.merge!(project_info)
+ end
+
+ it "should update a specific project's information" do
+ project.gl_project.team << [user, :master]
+ put ci_api("/projects/#{project.id}"), options
+ expect(response.status).to eq(200)
+ expect(json_response["name"]).to eq(project_info[:name])
+ end
+
+ it "fails to update a non-existing project" do
+ put ci_api("/projects/non-existant-id"), options
+ expect(response.status).to eq(404)
+ end
+
+ it "non-manager is not authorized" do
+ put ci_api("/projects/#{project.id}"), options
+ expect(response.status).to eq(401)
+ end
+ end
+
+ describe "DELETE /projects/:id" do
+ let!(:project) { FactoryGirl.create(:ci_project) }
+
+ it "should delete a specific project" do
+ project.gl_project.team << [user, :master]
+ delete ci_api("/projects/#{project.id}"), options
+ expect(response.status).to eq(200)
+ expect { project.reload }.to raise_error
+ end
+
+ it "non-manager is not authorized" do
+ delete ci_api("/projects/#{project.id}"), options
+ expect(response.status).to eq(401)
+ end
+
+ it "is getting not found error" do
+ delete ci_api("/projects/not-existing_id"), options
+ expect(response.status).to eq(404)
+ end
+ end
+
+ describe "POST /projects" do
+ let(:project_info) do
+ {
+ name: "My project",
+ gitlab_id: 1,
+ path: "testing/testing",
+ ssh_url_to_repo: "ssh://example.com/testing/testing.git"
+ }
+ end
+
+ 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 ci_api("/projects"), options
+ expect(response.status).to eq(201)
+ expect(json_response['name']).to eq(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 ci_api("/projects"), options
+ expect(response.status).to eq(400)
+ end
+ end
+
+ describe "POST /projects/:id/runners/:id" do
+ let(:project) { FactoryGirl.create(:ci_project) }
+ let(:runner) { FactoryGirl.create(:ci_runner) }
+
+ it "should add the project to the runner" do
+ project.gl_project.team << [user, :master]
+ post ci_api("/projects/#{project.id}/runners/#{runner.id}"), options
+ expect(response.status).to eq(201)
+
+ project.reload
+ expect(project.runners.first.id).to eq(runner.id)
+ end
+
+ it "should fail if it tries to link a non-existing project or runner" do
+ post ci_api("/projects/#{project.id}/runners/non-existing"), options
+ expect(response.status).to eq(404)
+
+ post ci_api("/projects/non-existing/runners/#{runner.id}"), options
+ expect(response.status).to eq(404)
+ end
+
+ it "non-manager is not authorized" do
+ allow_any_instance_of(User).to receive(:can_manage_project?).and_return(false)
+ post ci_api("/projects/#{project.id}/runners/#{runner.id}"), options
+ expect(response.status).to eq(401)
+ end
+ end
+
+ describe "DELETE /projects/:id/runners/:id" do
+ let(:project) { FactoryGirl.create(:ci_project) }
+ let(:runner) { FactoryGirl.create(:ci_runner) }
+
+ it "should remove the project from the runner" do
+ project.gl_project.team << [user, :master]
+ post ci_api("/projects/#{project.id}/runners/#{runner.id}"), options
+
+ expect(project.runners).to be_present
+ delete ci_api("/projects/#{project.id}/runners/#{runner.id}"), options
+ expect(response.status).to eq(200)
+
+ project.reload
+ expect(project.runners).to be_empty
+ end
+
+ it "non-manager is not authorized" do
+ delete ci_api("/projects/#{project.id}/runners/#{runner.id}"), options
+ expect(response.status).to eq(401)
+ end
+ end
+ end
+end
diff --git a/spec/requests/ci/api/runners_spec.rb b/spec/requests/ci/api/runners_spec.rb
new file mode 100644
index 00000000000..11dc089e1f5
--- /dev/null
+++ b/spec/requests/ci/api/runners_spec.rb
@@ -0,0 +1,83 @@
+require 'spec_helper'
+
+describe Ci::API::API do
+ include ApiHelpers
+ include StubGitlabCalls
+
+ before do
+ stub_gitlab_calls
+ end
+
+ describe "GET /runners" do
+ let(:gitlab_url) { GitlabCi.config.gitlab_ci.url }
+ let(:private_token) { create(:user).private_token }
+ let(:options) do
+ {
+ private_token: private_token,
+ url: gitlab_url
+ }
+ end
+
+ before do
+ 5.times { FactoryGirl.create(:ci_runner) }
+ end
+
+ it "should retrieve a list of all runners" do
+ get ci_api("/runners", nil), options
+ expect(response.status).to eq(200)
+ expect(json_response.count).to eq(5)
+ expect(json_response.last).to have_key("id")
+ expect(json_response.last).to have_key("token")
+ end
+ end
+
+ describe "POST /runners/register" do
+ describe "should create a runner if token provided" do
+ before { post ci_api("/runners/register"), token: GitlabCi::REGISTRATION_TOKEN }
+
+ it { expect(response.status).to eq(201) }
+ end
+
+ describe "should create a runner with description" do
+ before { post ci_api("/runners/register"), token: GitlabCi::REGISTRATION_TOKEN, description: "server.hostname" }
+
+ it { expect(response.status).to eq(201) }
+ it { expect(Ci::Runner.first.description).to eq("server.hostname") }
+ end
+
+ describe "should create a runner with tags" do
+ before { post ci_api("/runners/register"), token: GitlabCi::REGISTRATION_TOKEN, tag_list: "tag1, tag2" }
+
+ it { expect(response.status).to eq(201) }
+ it { expect(Ci::Runner.first.tag_list.sort).to eq(["tag1", "tag2"]) }
+ end
+
+ describe "should create a runner if project token provided" do
+ let(:project) { FactoryGirl.create(:ci_project) }
+ before { post ci_api("/runners/register"), token: project.token }
+
+ it { expect(response.status).to eq(201) }
+ it { expect(project.runners.size).to eq(1) }
+ end
+
+ it "should return 403 error if token is invalid" do
+ post ci_api("/runners/register"), token: 'invalid'
+
+ expect(response.status).to eq(403)
+ end
+
+ it "should return 400 error if no token" do
+ post ci_api("/runners/register")
+
+ expect(response.status).to eq(400)
+ end
+ end
+
+ describe "DELETE /runners/delete" do
+ let!(:runner) { FactoryGirl.create(:ci_runner) }
+ before { delete ci_api("/runners/delete"), token: runner.token }
+
+ it { expect(response.status).to eq(200) }
+ it { expect(Ci::Runner.count).to eq(0) }
+ end
+end
diff --git a/spec/requests/ci/api/triggers_spec.rb b/spec/requests/ci/api/triggers_spec.rb
new file mode 100644
index 00000000000..ff6fdbdd6f1
--- /dev/null
+++ b/spec/requests/ci/api/triggers_spec.rb
@@ -0,0 +1,78 @@
+require 'spec_helper'
+
+describe Ci::API::API do
+ include ApiHelpers
+
+ describe 'POST /projects/:project_id/refs/:ref/trigger' do
+ let!(:trigger_token) { 'secure token' }
+ let!(:project) { FactoryGirl.create(:ci_project) }
+ let!(:project2) { FactoryGirl.create(:ci_project) }
+ let!(:trigger) { FactoryGirl.create(:ci_trigger, project: project, token: trigger_token) }
+ let(:options) do
+ {
+ token: trigger_token
+ }
+ end
+
+ context 'Handles errors' do
+ it 'should return bad request if token is missing' do
+ post ci_api("/projects/#{project.id}/refs/master/trigger")
+ expect(response.status).to eq(400)
+ end
+
+ it 'should return not found if project is not found' do
+ post ci_api('/projects/0/refs/master/trigger'), options
+ expect(response.status).to eq(404)
+ end
+
+ it 'should return unauthorized if token is for different project' do
+ post ci_api("/projects/#{project2.id}/refs/master/trigger"), options
+ expect(response.status).to eq(401)
+ end
+ end
+
+ context 'Have a commit' do
+ before do
+ @commit = FactoryGirl.create(:ci_commit, project: project)
+ end
+
+ it 'should create builds' do
+ post ci_api("/projects/#{project.id}/refs/master/trigger"), options
+ expect(response.status).to eq(201)
+ @commit.builds.reload
+ expect(@commit.builds.size).to eq(2)
+ end
+
+ it 'should return bad request with no builds created if there\'s no commit for that ref' do
+ post ci_api("/projects/#{project.id}/refs/other-branch/trigger"), options
+ expect(response.status).to eq(400)
+ expect(json_response['message']).to eq('No builds created')
+ end
+
+ context 'Validates variables' do
+ let(:variables) do
+ { 'TRIGGER_KEY' => 'TRIGGER_VALUE' }
+ end
+
+ it 'should validate variables to be a hash' do
+ post ci_api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: 'value')
+ expect(response.status).to eq(400)
+ expect(json_response['message']).to eq('variables needs to be a hash')
+ end
+
+ it 'should validate variables needs to be a map of key-valued strings' do
+ post ci_api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: { key: %w(1 2) })
+ expect(response.status).to eq(400)
+ expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
+ end
+
+ it 'create trigger request with variables' do
+ post ci_api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: variables)
+ expect(response.status).to eq(201)
+ @commit.builds.reload
+ expect(@commit.builds.first.trigger_request.variables).to eq(variables)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/requests/ci/builds_spec.rb b/spec/requests/ci/builds_spec.rb
new file mode 100644
index 00000000000..998c386ead4
--- /dev/null
+++ b/spec/requests/ci/builds_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+describe "Builds" do
+ before do
+ @project = FactoryGirl.create :ci_project
+ @commit = FactoryGirl.create :ci_commit, project: @project
+ @build = FactoryGirl.create :ci_build, commit: @commit
+ end
+
+ describe "GET /:project/builds/:id/status.json" do
+ before do
+ get status_ci_project_build_path(@project, @build), format: :json
+ end
+
+ it { expect(response.status).to eq(200) }
+ it { expect(response.body).to include(@build.sha) }
+ end
+end
diff --git a/spec/requests/ci/commits_spec.rb b/spec/requests/ci/commits_spec.rb
new file mode 100644
index 00000000000..fb317670339
--- /dev/null
+++ b/spec/requests/ci/commits_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe "Commits" do
+ before do
+ @project = FactoryGirl.create :ci_project
+ @commit = FactoryGirl.create :ci_commit, project: @project
+ end
+
+ describe "GET /:project/refs/:ref_name/commits/:id/status.json" do
+ before do
+ get status_ci_project_ref_commits_path(@project, @commit.ref, @commit.sha), format: :json
+ end
+
+ it { expect(response.status).to eq(200) }
+ it { expect(response.body).to include(@commit.sha) }
+ end
+end