BigW Consortium Gitlab

triggers_spec.rb 2.89 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' }
8
    let!(:project) { FactoryGirl.create(:ci_project) }
9
    let!(:gl_project) { FactoryGirl.create(:project, gitlab_ci_project: project) }
10 11
    let!(:project2) { FactoryGirl.create(:ci_project) }
    let!(:trigger) { FactoryGirl.create(:ci_trigger, project: project, token: trigger_token) }
Valery Sizov committed
12
    let(:options) do
13 14 15
      {
        token: trigger_token
      }
Valery Sizov committed
16
    end
17

18 19 20 21
    before do
      stub_ci_commit_to_return_yaml_file
    end

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

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

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

    context 'Have a commit' do
40
      let(:commit) { project.commits.last }
41 42

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

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

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

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

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

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