summaryrefslogtreecommitdiff
path: root/spec/requests/api/triggers_spec.rb
blob: fc7394c55d7d5a418129bdb6945195c4a186f1f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 eq 400
      end

      it 'should return not found if project is not found' do
        post api('/projects/0/refs/master/trigger'), options
        response.status.should eq 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 eq 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 eq 201
        @commit.builds.reload
        @commit.builds.size.should eq 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 eq 400
        json_response['message'].should eq '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 eq 400
          json_response['message'].should eq '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 eq 400
          json_response['message'].should eq '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 eq 201
          @commit.builds.reload
          @commit.builds.first.trigger_request.variables.should eq variables
        end
      end
    end
  end
end