Commit 3578153d authored by Kamil Trzcinski's avatar Kamil Trzcinski

Fix triggers tests

parent 91cdb08d
...@@ -14,7 +14,7 @@ module API ...@@ -14,7 +14,7 @@ module API
post ":id/trigger/builds" do post ":id/trigger/builds" do
required_attributes! [:ref, :token] required_attributes! [:ref, :token]
project = Project.find_with_namespace(id) || Project.find_by(id: params[:id]) project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])
trigger = Ci::Trigger.find_by_token(params[:token].to_s) trigger = Ci::Trigger.find_by_token(params[:token].to_s)
not_found! unless project && trigger not_found! unless project && trigger
unauthorized! unless trigger.project == project unauthorized! unless trigger.project == project
......
...@@ -25,7 +25,7 @@ describe API::API do ...@@ -25,7 +25,7 @@ describe API::API do
end end
it 'should return not found if project is not found' do it 'should return not found if project is not found' do
post api('/projects/0/refs/master/trigger/builds'), options.merge(ref: 'master') post api('/projects/0/trigger/builds'), options.merge(ref: 'master')
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
...@@ -57,19 +57,19 @@ describe API::API do ...@@ -57,19 +57,19 @@ describe API::API do
end end
it 'should validate variables to be a hash' do it 'should validate variables to be a hash' do
post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: 'value', refs: 'master') post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: 'value', ref: 'master')
expect(response.status).to eq(400) expect(response.status).to eq(400)
expect(json_response['message']).to eq('variables needs to be a hash') expect(json_response['message']).to eq('variables needs to be a hash')
end end
it 'should validate variables needs to be a map of key-valued strings' do it 'should validate variables needs to be a map of key-valued strings' do
post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: { key: %w(1 2) }, refs: 'master') post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: { key: %w(1 2) }, ref: 'master')
expect(response.status).to eq(400) expect(response.status).to eq(400)
expect(json_response['message']).to eq('variables needs to be a map of key-valued strings') expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
end end
it 'create trigger request with variables' do it 'create trigger request with variables' do
post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: variables, refs: 'master') post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: variables, ref: 'master')
expect(response.status).to eq(201) expect(response.status).to eq(201)
commit.builds.reload commit.builds.reload
expect(commit.builds.first.trigger_request.variables).to eq(variables) expect(commit.builds.first.trigger_request.variables).to eq(variables)
......
...@@ -5,16 +5,14 @@ describe Ci::API::API do ...@@ -5,16 +5,14 @@ describe Ci::API::API do
describe 'POST /projects/:project_id/refs/:ref/trigger' do describe 'POST /projects/:project_id/refs/:ref/trigger' do
let!(:trigger_token) { 'secure token' } let!(:trigger_token) { 'secure token' }
let!(:project) { FactoryGirl.create(:project) } let!(:project) { FactoryGirl.create(:project, ci_id: 10) }
let!(:project2) { FactoryGirl.create(:empty_project) } let!(:project2) { FactoryGirl.create(:empty_project, ci_id: 11) }
let!(:trigger) { FactoryGirl.create(:ci_trigger, project: project, token: trigger_token) } let!(:trigger) { FactoryGirl.create(:ci_trigger, project: project, token: trigger_token) }
let(:options) do let(:options) do
{ {
token: trigger_token token: trigger_token
} }
end end
let(:project_ci_id) { create_ci_id(project) }
let(:project2_ci_id) { create_ci_id(project2) }
before do before do
stub_ci_commit_to_return_yaml_file stub_ci_commit_to_return_yaml_file
...@@ -22,7 +20,7 @@ describe Ci::API::API do ...@@ -22,7 +20,7 @@ describe Ci::API::API do
context 'Handles errors' do context 'Handles errors' do
it 'should return bad request if token is missing' do it 'should return bad request if token is missing' do
post ci_api("/projects/#{project_ci_id}/refs/master/trigger") post ci_api("/projects/#{project.ci_id}/refs/master/trigger")
expect(response.status).to eq(400) expect(response.status).to eq(400)
end end
...@@ -32,7 +30,7 @@ describe Ci::API::API do ...@@ -32,7 +30,7 @@ describe Ci::API::API do
end end
it 'should return unauthorized if token is for different project' do it 'should return unauthorized if token is for different project' do
post ci_api("/projects/#{project2_ci_id}/refs/master/trigger"), options post ci_api("/projects/#{project2.ci_id}/refs/master/trigger"), options
expect(response.status).to eq(401) expect(response.status).to eq(401)
end end
end end
...@@ -41,14 +39,14 @@ describe Ci::API::API do ...@@ -41,14 +39,14 @@ describe Ci::API::API do
let(:commit) { project.ci_commits.last } let(:commit) { project.ci_commits.last }
it 'should create builds' do it 'should create builds' do
post ci_api("/projects/#{project_ci_id}/refs/master/trigger"), options post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options
expect(response.status).to eq(201) expect(response.status).to eq(201)
commit.builds.reload commit.builds.reload
expect(commit.builds.size).to eq(2) expect(commit.builds.size).to eq(2)
end end
it 'should return bad request with no builds created if there\'s no commit for that ref' do it 'should return bad request with no builds created if there\'s no commit for that ref' do
post ci_api("/projects/#{project_ci_id}/refs/other-branch/trigger"), options post ci_api("/projects/#{project.ci_id}/refs/other-branch/trigger"), options
expect(response.status).to eq(400) expect(response.status).to eq(400)
expect(json_response['message']).to eq('No builds created') expect(json_response['message']).to eq('No builds created')
end end
...@@ -59,19 +57,19 @@ describe Ci::API::API do ...@@ -59,19 +57,19 @@ describe Ci::API::API do
end end
it 'should validate variables to be a hash' do it 'should validate variables to be a hash' do
post ci_api("/projects/#{project_ci_id}/refs/master/trigger"), options.merge(variables: 'value') post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: 'value')
expect(response.status).to eq(400) expect(response.status).to eq(400)
expect(json_response['message']).to eq('variables needs to be a hash') expect(json_response['message']).to eq('variables needs to be a hash')
end end
it 'should validate variables needs to be a map of key-valued strings' do it 'should validate variables needs to be a map of key-valued strings' do
post ci_api("/projects/#{project_ci_id}/refs/master/trigger"), options.merge(variables: { key: %w(1 2) }) post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: { key: %w(1 2) })
expect(response.status).to eq(400) expect(response.status).to eq(400)
expect(json_response['message']).to eq('variables needs to be a map of key-valued strings') expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
end end
it 'create trigger request with variables' do it 'create trigger request with variables' do
post ci_api("/projects/#{project_ci_id}/refs/master/trigger"), options.merge(variables: variables) post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: variables)
expect(response.status).to eq(201) expect(response.status).to eq(201)
commit.builds.reload commit.builds.reload
expect(commit.builds.first.trigger_request.variables).to eq(variables) expect(commit.builds.first.trigger_request.variables).to eq(variables)
...@@ -79,8 +77,4 @@ describe Ci::API::API do ...@@ -79,8 +77,4 @@ describe Ci::API::API do
end end
end end
end end
def create_ci_id(project)
ActiveRecord::Base.connection.insert("INSERT INTO ci_projects (gitlab_id) VALUES(#{project.id})")
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment