triggers_spec.rb 7.34 KB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1 2
require 'spec_helper'

3
describe API::Triggers do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
4 5
  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, :repository, creator: user) }
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') }

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

    before do
26
      stub_ci_pipeline_to_return_yaml_file
Kamil Trzcinski's avatar
Kamil Trzcinski committed
27 28 29
    end

    context 'Handles errors' do
30
      it 'returns bad request if token is missing' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
31
        post api("/projects/#{project.id}/trigger/builds"), ref: 'master'
32
        expect(response).to have_http_status(400)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
33 34
      end

35
      it 'returns not found if project is not found' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
36
        post api('/projects/0/trigger/builds'), options.merge(ref: 'master')
37
        expect(response).to have_http_status(404)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
38 39
      end

40
      it 'returns unauthorized if token is for different project' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
41
        post api("/projects/#{project2.id}/trigger/builds"), options.merge(ref: 'master')
42
        expect(response).to have_http_status(401)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
43 44 45 46
      end
    end

    context 'Have a commit' do
47
      let(:pipeline) { project.pipelines.last }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
48

49
      it 'creates builds' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
50
        post api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'master')
51
        expect(response).to have_http_status(201)
52
        pipeline.builds.reload
53 54
        expect(pipeline.builds.pending.size).to eq(2)
        expect(pipeline.builds.size).to eq(5)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
55 56
      end

57 58 59 60 61 62 63
      it 'creates builds on webhook from other gitlab repository and branch' do
        expect do
          post api("/projects/#{project.id}/ref/master/trigger/builds?token=#{trigger_token}"), { ref: 'refs/heads/other-branch' }
        end.to change(project.builds, :count).by(5)
        expect(response).to have_http_status(201)
      end

64
      it 'returns bad request with no builds created if there\'s no commit for that ref' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
65
        post api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'other-branch')
66
        expect(response).to have_http_status(400)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
67 68 69 70 71 72 73 74
        expect(json_response['message']).to eq('No builds created')
      end

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

75
        it 'validates variables to be a hash' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
76
          post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: 'value', ref: 'master')
77
          expect(response).to have_http_status(400)
Robert Schilling's avatar
Robert Schilling committed
78
          expect(json_response['error']).to eq('variables is invalid')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
79 80
        end

81
        it 'validates variables needs to be a map of key-valued strings' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
82
          post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: { key: %w(1 2) }, ref: 'master')
83
          expect(response).to have_http_status(400)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
84 85 86
          expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
        end

87
        it 'creates trigger request with variables' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
88
          post api("/projects/#{project.id}/trigger/builds"), options.merge(variables: variables, ref: 'master')
89
          expect(response).to have_http_status(201)
90 91
          pipeline.builds.reload
          expect(pipeline.builds.first.trigger_request.variables).to eq(variables)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
92 93 94 95
        end
      end
    end
  end
96

97
  describe 'GET /projects/:id/triggers' do
98
    context 'authenticated user with valid permissions' do
99
      it 'returns list of triggers' do
100 101
        get api("/projects/#{project.id}/triggers", user)

102
        expect(response).to have_http_status(200)
103
        expect(json_response).to be_a(Array)
104
        expect(json_response[0]).to have_key('token')
105 106 107 108
      end
    end

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

112
        expect(response).to have_http_status(403)
113 114 115
      end
    end

116
    context 'unauthenticated user' do
117
      it 'does not return triggers list' do
118 119
        get api("/projects/#{project.id}/triggers")

120
        expect(response).to have_http_status(401)
121 122 123
      end
    end
  end
124

125
  describe 'GET /projects/:id/triggers/:token' do
126
    context 'authenticated user with valid permissions' do
127
      it 'returns trigger details' do
128
        get api("/projects/#{project.id}/triggers/#{trigger.token}", user)
129

130
        expect(response).to have_http_status(200)
131
        expect(json_response).to be_a(Hash)
132 133
      end

134
      it 'responds with 404 Not Found if requesting non-existing trigger' do
135
        get api("/projects/#{project.id}/triggers/abcdef012345", user)
136

137
        expect(response).to have_http_status(404)
138 139 140 141
      end
    end

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

145
        expect(response).to have_http_status(403)
146 147 148
      end
    end

149
    context 'unauthenticated user' do
150
      it 'does not return triggers list' do
151
        get api("/projects/#{project.id}/triggers/#{trigger.token}")
152

153
        expect(response).to have_http_status(401)
154 155 156 157 158
      end
    end
  end

  describe 'POST /projects/:id/triggers' do
159
    context 'authenticated user with valid permissions' do
160
      it 'creates trigger' do
161 162 163 164
        expect do
          post api("/projects/#{project.id}/triggers", user)
        end.to change{project.triggers.count}.by(1)

165
        expect(response).to have_http_status(201)
166 167 168 169 170
        expect(json_response).to be_a(Hash)
      end
    end

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

174
        expect(response).to have_http_status(403)
175 176 177
      end
    end

178
    context 'unauthenticated user' do
179
      it 'does not create trigger' do
180 181
        post api("/projects/#{project.id}/triggers")

182
        expect(response).to have_http_status(401)
183 184 185 186
      end
    end
  end

187
  describe 'DELETE /projects/:id/triggers/:token' do
188
    context 'authenticated user with valid permissions' do
189
      it 'deletes trigger' do
190
        expect do
191
          delete api("/projects/#{project.id}/triggers/#{trigger.token}", user)
192
        end.to change{project.triggers.count}.by(-1)
193
        expect(response).to have_http_status(200)
194 195
      end

196
      it 'responds with 404 Not Found if requesting non-existing trigger' do
197
        delete api("/projects/#{project.id}/triggers/abcdef012345", user)
198

199
        expect(response).to have_http_status(404)
200 201 202 203
      end
    end

    context 'authenticated user with invalid permissions' do
204
      it 'does not delete trigger' do
205
        delete api("/projects/#{project.id}/triggers/#{trigger.token}", user2)
206

207
        expect(response).to have_http_status(403)
208 209 210
      end
    end

211
    context 'unauthenticated user' do
212
      it 'does not delete trigger' do
213
        delete api("/projects/#{project.id}/triggers/#{trigger.token}")
214

215
        expect(response).to have_http_status(401)
216 217 218
      end
    end
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
219
end