BigW Consortium Gitlab

triggers_spec.rb 2.87 KB
Newer Older
1 2
require 'spec_helper'

3
describe Ci::API::API do
4 5 6 7
  include ApiHelpers

  describe 'POST /projects/:project_id/refs/:ref/trigger' do
    let!(:trigger_token) { 'secure token' }
Kamil Trzcinski committed
8 9
    let!(:project) { FactoryGirl.create(:project, ci_id: 10) }
    let!(:project2) { FactoryGirl.create(:empty_project, ci_id: 11) }
10
    let!(:trigger) { FactoryGirl.create(:ci_trigger, project: project, token: trigger_token) }
Valery Sizov committed
11
    let(:options) do
12 13 14
      {
        token: trigger_token
      }
Valery Sizov committed
15
    end
16

17
    before do
18
      stub_ci_pipeline_to_return_yaml_file
19 20
    end

21 22
    context 'Handles errors' do
      it 'should return bad request if token is missing' do
Kamil Trzcinski committed
23
        post ci_api("/projects/#{project.ci_id}/refs/master/trigger")
24
        expect(response.status).to eq(400)
25 26 27
      end

      it 'should return not found if project is not found' do
Valery Sizov committed
28
        post ci_api('/projects/0/refs/master/trigger'), options
29
        expect(response.status).to eq(404)
30 31 32
      end

      it 'should return unauthorized if token is for different project' do
Kamil Trzcinski committed
33
        post ci_api("/projects/#{project2.ci_id}/refs/master/trigger"), options
34
        expect(response.status).to eq(401)
35 36 37 38
      end
    end

    context 'Have a commit' do
39
      let(:pipeline) { project.pipelines.last }
40 41

      it 'should create builds' do
Kamil Trzcinski committed
42
        post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options
43
        expect(response.status).to eq(201)
44 45
        pipeline.builds.reload
        expect(pipeline.builds.size).to eq(2)
46 47 48
      end

      it 'should return bad request with no builds created if there\'s no commit for that ref' do
Kamil Trzcinski committed
49
        post ci_api("/projects/#{project.ci_id}/refs/other-branch/trigger"), options
50 51
        expect(response.status).to eq(400)
        expect(json_response['message']).to eq('No builds created')
52 53 54
      end

      context 'Validates variables' do
Valery Sizov committed
55 56 57
        let(:variables) do
          { 'TRIGGER_KEY' => 'TRIGGER_VALUE' }
        end
58 59

        it 'should validate variables to be a hash' do
Kamil Trzcinski committed
60
          post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: 'value')
61 62
          expect(response.status).to eq(400)
          expect(json_response['message']).to eq('variables needs to be a hash')
63 64 65
        end

        it 'should validate variables needs to be a map of key-valued strings' do
Kamil Trzcinski committed
66
          post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: { key: %w(1 2) })
67 68
          expect(response.status).to eq(400)
          expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
69 70 71
        end

        it 'create trigger request with variables' do
Kamil Trzcinski committed
72
          post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: variables)
73
          expect(response.status).to eq(201)
74 75
          pipeline.builds.reload
          expect(pipeline.builds.first.trigger_request.variables).to eq(variables)
76 77 78 79 80
        end
      end
    end
  end
end