Skip to content
Snippets Groups Projects
Commit 5cc0cfb1 authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Create specs for build triggers

parent ae8f755f
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -5,7 +5,7 @@ FactoryGirl.define do
factory :trigger_request_with_variables do
variables do
{
KEY: 'VALUE'
TRIGGER_KEY: 'TRIGGER_VALUE'
}
end
end
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
@@ -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) }
Loading
Loading
@@ -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
Loading
Loading
@@ -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 }
Loading
Loading
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
Loading
Loading
@@ -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
Loading
Loading
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment