diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-07-05 15:06:02 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-07-05 15:06:02 +0300 |
commit | 162dd6cb2b9b93ef188ecacc285f476660fcb90c (patch) | |
tree | f37375ccd518cd7e9c74a89331af52a01cadfbb9 | |
parent | 4b74c55b1572da19d78fa6982edf54916d09eec3 (diff) | |
download | gitlab-ci-162dd6cb2b9b93ef188ecacc285f476660fcb90c.tar.gz |
Api rspec
-rw-r--r-- | spec/requests/builds_spec.rb | 38 | ||||
-rw-r--r-- | spec/requests/runners_spec.rb | 19 | ||||
-rw-r--r-- | spec/support/api_helpers.rb | 34 |
3 files changed, 91 insertions, 0 deletions
diff --git a/spec/requests/builds_spec.rb b/spec/requests/builds_spec.rb new file mode 100644 index 0000000..3e06de1 --- /dev/null +++ b/spec/requests/builds_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe API::API do + include ApiHelpers + + let(:runner) { FactoryGirl.create(:runner) } + let(:project) { FactoryGirl.create(:project) } + + 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 + build = FactoryGirl.create(:build, project_id: project.id, 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(:build) { FactoryGirl.create(:build, project_id: project.id) } + + it "should update a build" do + put api("/builds/#{build.id}"), token: runner.token + response.status.should == 200 + end + end +end diff --git a/spec/requests/runners_spec.rb b/spec/requests/runners_spec.rb new file mode 100644 index 0000000..a8d1f4f --- /dev/null +++ b/spec/requests/runners_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe API::API do + include ApiHelpers + + describe "POST /runners/register" do + it "should create a runner if token provided" do + post api("/runners/register"), token: GitlabCi::RunnersToken, 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 diff --git a/spec/support/api_helpers.rb b/spec/support/api_helpers.rb new file mode 100644 index 0000000..ec9a326 --- /dev/null +++ b/spec/support/api_helpers.rb @@ -0,0 +1,34 @@ +module ApiHelpers + # Public: Prepend a request path with the path to the API + # + # path - Path to append + # user - User object - If provided, automatically appends private_token query + # string for authenticated requests + # + # Examples + # + # >> api('/issues') + # => "/api/v2/issues" + # + # >> api('/issues', User.last) + # => "/api/v2/issues?private_token=..." + # + # >> api('/issues?foo=bar', User.last) + # => "/api/v2/issues?foo=bar&private_token=..." + # + # Returns the relative path to the requested API resource + def api(path, user = nil) + "/api/#{API::API.version}#{path}" + + + # Normalize query string + (path.index('?') ? '' : '?') + + + # Append private_token if given a User object + (user.respond_to?(:private_token) ? + "&private_token=#{user.private_token}" : "") + end + + def json_response + JSON.parse(response.body) + end +end |