BigW Consortium Gitlab

triggers_spec.rb 6.9 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe API::API do
  include ApiHelpers

6 7
  let(:user) { create(:user) }
  let(:user2) { create(:user) }
8 9
  let!(:trigger_token) { 'secure_token' }
  let!(:trigger_token_2) { 'secure_token_2' }
10
  let!(:project) { create(:project, creator_id: user.id) }
11 12
  let!(:master) { create(:project_member, :master, user: user, project: project) }
  let!(:developer) { create(:project_member, :developer, user: user2, project: project) }
13 14 15 16
  let!(:trigger) { create(:ci_trigger, project: project, token: trigger_token) }
  let!(:trigger2) { create(:ci_trigger, project: project, token: trigger_token_2) }
  let!(:trigger_request) { create(:ci_trigger_request, trigger: trigger, created_at: '2015-01-01 12:13:14') }

17
  describe 'POST /projects/:project_id/trigger' do
18
    let!(:project2) { create(:empty_project) }
19 20 21 22 23 24 25
    let(:options) do
      {
        token: trigger_token
      }
    end

    before do
26
      stub_ci_pipeline_to_return_yaml_file
27 28 29 30 31 32 33 34 35
    end

    context 'Handles errors' do
      it 'should return bad request if token is missing' do
        post api("/projects/#{project.id}/trigger/builds"), ref: 'master'
        expect(response.status).to eq(400)
      end

      it 'should return not found if project is not found' do
Kamil Trzcinski committed
36
        post api('/projects/0/trigger/builds'), options.merge(ref: 'master')
37 38 39 40 41 42 43 44 45 46
        expect(response.status).to eq(404)
      end

      it 'should return unauthorized if token is for different project' do
        post api("/projects/#{project2.id}/trigger/builds"), options.merge(ref: 'master')
        expect(response.status).to eq(401)
      end
    end

    context 'Have a commit' do
47
      let(:pipeline) { project.pipelines.last }
48 49 50 51

      it 'should create builds' do
        post api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'master')
        expect(response.status).to eq(201)
52 53
        pipeline.builds.reload
        expect(pipeline.builds.size).to eq(2)
54 55 56 57 58 59 60 61 62 63 64 65 66 67
      end

      it 'should return bad request with no builds created if there\'s no commit for that ref' do
        post api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'other-branch')
        expect(response.status).to eq(400)
        expect(json_response['message']).to eq('No builds created')
      end

      context 'Validates variables' do
        let(:variables) do
          { 'TRIGGER_KEY' => 'TRIGGER_VALUE' }
        end

        it 'should validate variables to be a hash' do
Kamil Trzcinski committed
68
          post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: 'value', ref: 'master')
69 70 71 72 73
          expect(response.status).to eq(400)
          expect(json_response['message']).to eq('variables needs to be a hash')
        end

        it 'should validate variables needs to be a map of key-valued strings' do
Kamil Trzcinski committed
74
          post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: { key: %w(1 2) }, ref: 'master')
75 76 77 78 79
          expect(response.status).to eq(400)
          expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
        end

        it 'create trigger request with variables' do
Kamil Trzcinski committed
80
          post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: variables, ref: 'master')
81
          expect(response.status).to eq(201)
82 83
          pipeline.builds.reload
          expect(pipeline.builds.first.trigger_request.variables).to eq(variables)
84 85 86 87
        end
      end
    end
  end
88

89
  describe 'GET /projects/:id/triggers' do
90 91 92 93 94 95
    context 'authenticated user with valid permissions' do
      it 'should return list of triggers' do
        get api("/projects/#{project.id}/triggers", user)

        expect(response.status).to eq(200)
        expect(json_response).to be_a(Array)
96
        expect(json_response[0]).to have_key('token')
97 98 99 100 101 102 103 104 105 106 107
      end
    end

    context 'authenticated user with invalid permissions' do
      it 'should not return triggers list' do
        get api("/projects/#{project.id}/triggers", user2)

        expect(response.status).to eq(403)
      end
    end

108
    context 'unauthenticated user' do
109 110 111 112 113 114 115
      it 'should not return triggers list' do
        get api("/projects/#{project.id}/triggers")

        expect(response.status).to eq(401)
      end
    end
  end
116

117
  describe 'GET /projects/:id/triggers/:token' do
118
    context 'authenticated user with valid permissions' do
119 120
      it 'should return trigger details' do
        get api("/projects/#{project.id}/triggers/#{trigger.token}", user)
121

122 123
        expect(response.status).to eq(200)
        expect(json_response).to be_a(Hash)
124 125
      end

126
      it 'should respond with 404 Not Found if requesting non-existing trigger' do
127
        get api("/projects/#{project.id}/triggers/abcdef012345", user)
128 129 130 131 132 133 134

        expect(response.status).to eq(404)
      end
    end

    context 'authenticated user with invalid permissions' do
      it 'should not return triggers list' do
135
        get api("/projects/#{project.id}/triggers/#{trigger.token}", user2)
136 137 138 139 140

        expect(response.status).to eq(403)
      end
    end

141
    context 'unauthenticated user' do
142
      it 'should not return triggers list' do
143
        get api("/projects/#{project.id}/triggers/#{trigger.token}")
144 145 146 147 148 149 150

        expect(response.status).to eq(401)
      end
    end
  end

  describe 'POST /projects/:id/triggers' do
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    context 'authenticated user with valid permissions' do
      it 'should create trigger' do
        expect do
          post api("/projects/#{project.id}/triggers", user)
        end.to change{project.triggers.count}.by(1)

        expect(response.status).to eq(201)
        expect(json_response).to be_a(Hash)
      end
    end

    context 'authenticated user with invalid permissions' do
      it 'should not create trigger' do
        post api("/projects/#{project.id}/triggers", user2)

        expect(response.status).to eq(403)
      end
    end

170
    context 'unauthenticated user' do
171 172 173 174 175 176 177 178
      it 'should not create trigger' do
        post api("/projects/#{project.id}/triggers")

        expect(response.status).to eq(401)
      end
    end
  end

179
  describe 'DELETE /projects/:id/triggers/:token' do
180 181 182
    context 'authenticated user with valid permissions' do
      it 'should delete trigger' do
        expect do
183
          delete api("/projects/#{project.id}/triggers/#{trigger.token}", user)
184 185 186 187
        end.to change{project.triggers.count}.by(-1)
        expect(response.status).to eq(200)
      end

188
      it 'should respond with 404 Not Found if requesting non-existing trigger' do
189
        delete api("/projects/#{project.id}/triggers/abcdef012345", user)
190 191 192 193 194 195 196

        expect(response.status).to eq(404)
      end
    end

    context 'authenticated user with invalid permissions' do
      it 'should not delete trigger' do
197
        delete api("/projects/#{project.id}/triggers/#{trigger.token}", user2)
198 199 200 201 202

        expect(response.status).to eq(403)
      end
    end

203
    context 'unauthenticated user' do
204
      it 'should not delete trigger' do
205
        delete api("/projects/#{project.id}/triggers/#{trigger.token}")
206 207 208 209 210

        expect(response.status).to eq(401)
      end
    end
  end
211
end