diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2015-08-21 14:10:18 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2015-08-21 14:49:36 +0100 |
commit | 5cc0cfb15d9a12737bc5e8d1710861af52c145d3 (patch) | |
tree | 6583390117060975101a97ad89da4b02dca49eef | |
parent | ae8f755fc81160b695e44bbfb00b1225527bf5f9 (diff) | |
download | gitlab-ci-5cc0cfb15d9a12737bc5e8d1710861af52c145d3.tar.gz |
Create specs for build triggers
-rw-r--r-- | spec/factories/trigger_requests.rb | 2 | ||||
-rw-r--r-- | spec/models/build_spec.rb | 44 | ||||
-rw-r--r-- | spec/models/commit_spec.rb | 49 | ||||
-rw-r--r-- | spec/models/trigger_spec.rb | 11 | ||||
-rw-r--r-- | spec/requests/api/builds_spec.rb | 17 | ||||
-rw-r--r-- | spec/requests/api/triggers_spec.rb | 78 |
6 files changed, 191 insertions, 10 deletions
diff --git a/spec/factories/trigger_requests.rb b/spec/factories/trigger_requests.rb index 3056f58..c85d102 100644 --- a/spec/factories/trigger_requests.rb +++ b/spec/factories/trigger_requests.rb @@ -5,7 +5,7 @@ FactoryGirl.define do factory :trigger_request_with_variables do variables do { - KEY: 'VALUE' + TRIGGER_KEY: 'TRIGGER_VALUE' } end end diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index ffa79a2..7333981 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -303,4 +303,48 @@ describe Build do it { should eq(98.29) } end end + + describe :variables do + context 'returns variables' do + subject { build.variables } + + let(:variables) { + [ + {key: :DB_NAME, value: 'postgres', public: true} + ] + } + + it { should eq(variables) } + + context 'and secure variables' do + let(:secure_variables) { + [ + {key: 'SECRET_KEY', value: 'secret_value', public: false} + ] + } + + before do + build.project.variables << Variable.new(key: 'SECRET_KEY', value: 'secret_value') + end + + it { should eq(variables + secure_variables) } + + context 'and trigger variables' do + let(:trigger) { FactoryGirl.create :trigger, project: project } + let(:trigger_request) { FactoryGirl.create :trigger_request_with_variables, commit: commit, trigger: trigger } + let(:trigger_variables) { + [ + {key: :TRIGGER_KEY, value: 'TRIGGER_VALUE', public: false} + ] + } + + before do + build.trigger_request = trigger_request + end + + it { should eq(variables + secure_variables + trigger_variables) } + end + end + end + end end diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb index dbb9a9e..e1a8172 100644 --- a/spec/models/commit_spec.rb +++ b/spec/models/commit_spec.rb @@ -21,6 +21,7 @@ describe Commit do let(:project) { FactoryGirl.create :project } let(:commit) { FactoryGirl.create :commit, project: project } let(:commit_with_project) { FactoryGirl.create :commit, project: project } + let(:config_processor) { GitlabCiYamlProcessor.new(gitlab_ci_yaml) } it { should belong_to(:project) } it { should have_many(:builds) } @@ -134,10 +135,11 @@ describe Commit do end describe :create_next_builds do - it "creates builds for next type" do - config_processor = GitlabCiYamlProcessor.new(gitlab_ci_yaml) + before do commit.stub(:config_processor).and_return(config_processor) + end + it "creates builds for next type" do commit.create_builds.should be_true commit.builds.reload commit.builds.size.should == 2 @@ -154,6 +156,49 @@ describe Commit do end end + describe :create_builds do + before do + commit.stub(:config_processor).and_return(config_processor) + end + + it 'creates builds' do + commit.create_builds.should be_true + commit.builds.reload + commit.builds.size.should == 2 + end + + context 'for build triggers' do + let(:trigger) { FactoryGirl.create :trigger, project: project } + let(:trigger_request) { FactoryGirl.create :trigger_request, commit: commit, trigger: trigger } + + it 'creates builds' do + commit.create_builds(trigger_request).should be_true + commit.builds.reload + commit.builds.size.should == 2 + end + + it 'rebuilds commit' do + commit.create_builds.should be_true + commit.builds.reload + commit.builds.size.should == 2 + + commit.create_builds(trigger_request).should be_true + commit.builds.reload + commit.builds.size.should == 4 + end + + it 'creates next builds' do + commit.create_builds(trigger_request).should be_true + commit.builds.reload + commit.builds.size.should == 2 + + commit.create_next_builds(trigger_request).should be_true + commit.builds.reload + commit.builds.size.should == 4 + end + end + end + describe "#finished_at" do let(:project) { FactoryGirl.create :project } let(:commit) { FactoryGirl.create :commit, project: project } diff --git a/spec/models/trigger_spec.rb b/spec/models/trigger_spec.rb index 1728c0f..bba638e 100644 --- a/spec/models/trigger_spec.rb +++ b/spec/models/trigger_spec.rb @@ -1,20 +1,17 @@ - require 'spec_helper' describe Trigger do let(:project) { FactoryGirl.create :project } - subject { FactoryGirl.create :trigger, project: project } - describe 'before_validation' do it 'should set an random token if none provided' do - project = FactoryGirl.create :trigger_without_token - project.token.should_not == "" + trigger = FactoryGirl.create :trigger_without_token, project: project + trigger.token.should_not be_nil end it 'should not set an random token if one provided' do - project = FactoryGirl.create :trigger - project.token.should == 'token' + trigger = FactoryGirl.create :trigger, project: project + trigger.token.should == 'token' end end end diff --git a/spec/requests/api/builds_spec.rb b/spec/requests/api/builds_spec.rb index a169f8e..746435a 100644 --- a/spec/requests/api/builds_spec.rb +++ b/spec/requests/api/builds_spec.rb @@ -74,6 +74,23 @@ describe API::API do {"key" => "SECRET_KEY", "value" => "secret_value", "public" => false}, ] end + + it "returns variables for triggers" do + trigger = FactoryGirl.create(:trigger, project: project) + trigger_request = FactoryGirl.create(:trigger_request_with_variables, commit: commit, trigger: trigger) + commit = FactoryGirl.create(:commit, project: project) + commit.create_builds(trigger_request) + project.variables << Variable.new(key: "SECRET_KEY", value: "secret_value") + + post api("/builds/register"), token: runner.token, info: {platform: :darwin} + + response.status.should == 201 + json_response["variables"].should == [ + {"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 diff --git a/spec/requests/api/triggers_spec.rb b/spec/requests/api/triggers_spec.rb new file mode 100644 index 0000000..6e56c4b --- /dev/null +++ b/spec/requests/api/triggers_spec.rb @@ -0,0 +1,78 @@ +require 'spec_helper' + +describe API::API do + include ApiHelpers + + describe 'POST /projects/:project_id/refs/:ref/trigger' do + let!(:trigger_token) { 'secure token' } + let!(:project) { FactoryGirl.create(:project) } + let!(:project2) { FactoryGirl.create(:project) } + let!(:trigger) { FactoryGirl.create(:trigger, project: project, token: trigger_token) } + let(:options) { + { + token: trigger_token + } + } + + context 'Handles errors' do + it 'should return bad request if token is missing' do + post api("/projects/#{project.id}/refs/master/trigger") + response.status.should == 400 + end + + it 'should return not found if project is not found' do + post api('/projects/0/refs/master/trigger'), options + response.status.should == 404 + end + + it 'should return unauthorized if token is for different project' do + post api("/projects/#{project2.id}/refs/master/trigger"), options + response.status.should == 401 + end + end + + context 'Have a commit' do + before do + @commit = FactoryGirl.create(:commit, project: project) + end + + it 'should create builds' do + post api("/projects/#{project.id}/refs/master/trigger"), options + response.status.should == 201 + @commit.builds.reload + @commit.builds.size.should == 2 + end + + it 'should return bad request with no builds created if there\'s no commit for that ref' do + post api("/projects/#{project.id}/refs/other-branch/trigger"), options + response.status.should == 400 + json_response['message'].should == 'No builds created' + end + + context 'Validates variables' do + let(:variables) { + {'TRIGGER_KEY' => 'TRIGGER_VALUE'} + } + + it 'should validate variables to be a hash' do + post api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: 'value') + response.status.should == 400 + json_response['message'].should == 'variables needs to be a hash' + end + + it 'should validate variables needs to be a map of key-valued strings' do + post api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: {key: %w(1 2)}) + response.status.should == 400 + json_response['message'].should == 'variables needs to be a map of key-valued strings' + end + + it 'create trigger request with variables' do + post api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: variables) + response.status.should == 201 + @commit.builds.reload + @commit.builds.first.trigger_request.variables.should == variables + end + end + end + end +end |