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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
require 'spec_helper'
describe API::API do
include ApiHelpers
let(:gitlab_url) { GitlabCi.config.gitlab_server.url }
let(:private_token) { Network.new.authenticate(access_token: "some_token")["private_token"] }
let(:options) {
{
private_token: private_token,
url: gitlab_url
}
}
before {
stub_gitlab_calls
}
context "requests for scoped projects" do
# NOTE: These ids are tied to the actual projects on demo.gitlab.com
describe "GET /projects" do
let!(:project1) { FactoryGirl.create(:project, name: "gitlabhq", gitlab_id: 3) }
let!(:project2) { FactoryGirl.create(:project, name: "gitlab-ci", gitlab_id: 4) }
it "should return all projects on the CI instance" do
get api("/projects"), options
response.status.should == 200
json_response.count.should == 2
json_response.first["id"].should == project1.id
json_response.last["id"].should == project2.id
end
end
describe "GET /projects/owned" do
# NOTE: This user doesn't own any of these projects on demo.gitlab.com
let!(:project1) { FactoryGirl.create(:project, name: "gitlabhq", gitlab_id: 3) }
let!(:project2) { FactoryGirl.create(:project, name: "random-project", gitlab_id: 9898) }
it "should return all projects on the CI instance" do
get api("/projects/owned"), options
response.status.should == 200
json_response.count.should == 0
end
end
end
describe "POST /projects/:project_id/jobs" do
let!(:project) { FactoryGirl.create(:project) }
let(:job_info) {
{
name: "A Job Name",
commands: "ls -lad",
active: false,
build_branches: false,
build_tags: true,
tags: "release, deployment",
}
}
let(:invalid_job_info) { {} }
context "Invalid Job Info" do
before do
options.merge!(invalid_job_info)
end
it "should error with invalid data" do
post api("/projects/#{project.id}/jobs"), options
response.status.should == 400
end
end
context "Valid Job Info" do
before do
options.merge!(job_info)
end
it "should create a job for specified project" do
post api("/projects/#{project.id}/jobs"), options
response.status.should == 201
json_response["name"].should == job_info[:name]
json_response["commands"].should == job_info[:commands]
json_response["active"].should == job_info[:active]
json_response["build_branches"].should == job_info[:build_branches]
json_response["build_tags"].should == job_info[:build_tags]
json_response["tags"].should have(2).items
end
it "fails to create job for non existsing project" do
post api("/projects/non-existant-id/jobs"), options
response.status.should == 404
end
it "non-manager is not authorized" do
User.any_instance.stub(:can_manage_project?).and_return(false)
post api("/projects/#{project.id}/jobs"), options
response.status.should == 401
end
end
end
describe "POST /projects/:project_id/deploy_jobs" do
let!(:project) { FactoryGirl.create(:project) }
let(:job_info) {
{
name: "A Job Name",
commands: "ls -lad",
active: false,
refs: "master",
tags: "release, deployment",
}
}
let(:invalid_job_info) { {} }
context "Invalid Job Info" do
before do
options.merge!(invalid_job_info)
end
it "should error with invalid data" do
post api("/projects/#{project.id}/deploy_jobs"), options
response.status.should == 400
end
end
context "Valid Job Info" do
before do
options.merge!(job_info)
end
it "should create a job for specified project" do
post api("/projects/#{project.id}/deploy_jobs"), options
response.status.should == 201
json_response["name"].should == job_info[:name]
json_response["commands"].should == job_info[:commands]
json_response["active"].should == job_info[:active]
json_response["refs"].should == job_info[:refs]
json_response["tags"].should have(2).items
end
it "fails to create job for non existsing project" do
post api("/projects/non-existant-id/deploy_jobs"), options
response.status.should == 404
end
it "non-manager is not authorized" do
User.any_instance.stub(:can_manage_project?).and_return(false)
post api("/projects/#{project.id}/deploy_jobs"), options
response.status.should == 401
end
end
end
describe "GET /projects/:project_id/jobs" do
let!(:project) { FactoryGirl.create(:project) }
let(:job_info) {
{
name: "A Job Name",
commands: "ls -lad",
}
}
before do
options.merge!(job_info)
end
it "should list the project's jobs" do
get api("/projects/#{project.id}/jobs"), options
response.status.should == 200
json_response.count.should == 1
json_response.first["project_id"].should == project.id
end
it "should delete default job, add & list the project's new jobs" do
# delete default job
get api("/projects/#{project.id}/jobs"), options
response.status.should == 200
json_response.count.should == 1
job_id = json_response.first["id"]
delete api("/projects/#{project.id}/jobs/#{job_id}"), options
# add a new job
post api("/projects/#{project.id}/jobs"), options
response.status.should == 201
# get the new job
get api("/projects/#{project.id}/jobs"), options
# assert job
response.status.should == 200
json_response.count.should == 1
json_response.first["project_id"].should == project.id
json_response.first["name"].should == job_info[:name]
json_response.first["commands"].should == job_info[:commands]
end
it "fails to list jobs for non existsing project" do
get api("/projects/non-existant-id/jobs"), options
response.status.should == 404
end
end
describe "DELETE /projects/:id/jobs/:job_id" do
let!(:project) { FactoryGirl.create(:project) }
let(:job_info) {
{
commands: "ls -lad",
name: "A Job Name",
}
}
before do
options.merge!(job_info)
end
it "should delete a project job" do
job = FactoryGirl.create(:job, project: project)
delete api("/projects/#{project.id}/jobs/#{job.id}"), options
response.status.should == 200
end
it "fails to delete a job for a non existsing project" do
delete api("/projects/non-existant-id/jobs/non-existant-job-id"), options
response.status.should == 404
end
it "fails to delete a job for a non existsing job id" do
delete api("/projects/#{project.id}/jobs/non-existant-job-id"), options
response.status.should == 404
end
it "non-manager is not authorized" do
User.any_instance.stub(:can_manage_project?).and_return(false)
job = FactoryGirl.create(:job, project: project)
delete api("/projects/#{project.id}/jobs/#{job.id}"), options
response.status.should == 401
end
end
describe "POST /projects/:project_id/webhooks" do
let!(:project) { FactoryGirl.create(:project) }
context "Valid Webhook URL" do
let!(:webhook) { {web_hook: "http://example.com/sth/1/ala_ma_kota" } }
before do
options.merge!(webhook)
end
it "should create webhook for specified project" do
post api("/projects/#{project.id}/webhooks"), options
response.status.should == 201
json_response["url"].should == webhook[:web_hook]
end
it "fails to create webhook for non existsing project" do
post api("/projects/non-existant-id/webhooks"), options
response.status.should == 404
end
it "non-manager is not authorized" do
User.any_instance.stub(:can_manage_project?).and_return(false)
post api("/projects/#{project.id}/webhooks"), options
response.status.should == 401
end
end
context "Invalid Webhook URL" do
let!(:webhook) { {web_hook: "ala_ma_kota" } }
before do
options.merge!(webhook)
end
it "fails to create webhook for not valid url" do
post api("/projects/#{project.id}/webhooks"), options
response.status.should == 400
end
end
context "Missed web_hook parameter" do
it "fails to create webhook for not provided url" do
post api("/projects/#{project.id}/webhooks"), options
response.status.should == 400
end
end
end
describe "GET /projects/:id" do
let!(:project) { FactoryGirl.create(:project) }
context "with an existing project" do
it "should retrieve the project info" do
get api("/projects/#{project.id}"), options
response.status.should == 200
json_response['id'].should == project.id
end
end
context "with a non-existing project" do
it "should return 404 error if project not found" do
get api("/projects/non_existent_id"), options
response.status.should == 404
end
end
end
describe "PUT /projects/:id" do
let!(:project) { FactoryGirl.create(:project) }
let!(:project_info) { {name: "An updated name!" } }
before do
options.merge!(project_info)
end
it "should update a specific project's information" do
put api("/projects/#{project.id}"), options
response.status.should == 200
json_response["name"].should == project_info[:name]
end
it "fails to update a non-existing project" do
put api("/projects/non-existant-id"), options
response.status.should == 404
end
it "non-manager is not authorized" do
User.any_instance.stub(:can_manage_project?).and_return(false)
put api("/projects/#{project.id}"), options
response.status.should == 401
end
end
describe "DELETE /projects/:id" do
let!(:project) { FactoryGirl.create(:project) }
it "should delete a specific project" do
delete api("/projects/#{project.id}"), options
response.status.should == 200
expect { project.reload }.to raise_error
end
it "non-manager is not authorized" do
User.any_instance.stub(:can_manage_project?).and_return(false)
delete api("/projects/#{project.id}"), options
response.status.should == 401
end
it "is getting not found error" do
delete api("/projects/not-existing_id"), options
response.status.should == 404
end
end
describe "POST /projects" do
let(:project_info) {
{
name: "My project",
gitlab_id: 1,
gitlab_url: "http://example.com/testing/testing",
ssh_url_to_repo: "ssh://example.com/testing/testing.git"
}
}
let(:invalid_project_info) { {} }
context "with valid project info" do
before do
options.merge!(project_info)
end
it "should create a project with valid data" do
post api("/projects"), options
response.status.should == 201
json_response['name'].should == project_info[:name]
end
end
context "with invalid project info" do
before do
options.merge!(invalid_project_info)
end
it "should error with invalid data" do
post api("/projects"), options
response.status.should == 400
end
end
describe "POST /projects/:id/runners/:id" do
let(:project) { FactoryGirl.create(:project) }
let(:runner) { FactoryGirl.create(:runner) }
it "should add the project to the runner" do
post api("/projects/#{project.id}/runners/#{runner.id}"), options
response.status.should == 201
project.reload
project.runners.first.id.should == runner.id
end
it "should fail if it tries to link a non-existing project or runner" do
post api("/projects/#{project.id}/runners/non-existing"), options
response.status.should == 404
post api("/projects/non-existing/runners/#{runner.id}"), options
response.status.should == 404
end
it "non-manager is not authorized" do
User.any_instance.stub(:can_manage_project?).and_return(false)
post api("/projects/#{project.id}/runners/#{runner.id}"), options
response.status.should == 401
end
end
describe "DELETE /projects/:id/runners/:id" do
let(:project) { FactoryGirl.create(:project) }
let(:runner) { FactoryGirl.create(:runner) }
before do
post api("/projects/#{project.id}/runners/#{runner.id}"), options
end
it "should remove the project from the runner" do
project.runners.should be_present
delete api("/projects/#{project.id}/runners/#{runner.id}"), options
response.status.should == 200
project.reload
project.runners.should be_empty
end
it "non-manager is not authorized" do
User.any_instance.stub(:can_manage_project?).and_return(false)
post api("/projects/#{project.id}/runners/#{runner.id}"), options
response.status.should == 401
end
end
end
end
|