FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-gitlab/gitlab/tests/test_gitlab.py at master · par-tec/python-gitlab · GitHub
par-tec
python-gitlab
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-gitlab
/
gitlab
/
tests
/
test_gitlab.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
729 lines (598 loc) · 29.5 KB
Breadcrumbs
python-gitlab
/
gitlab
/
tests
/
test_gitlab.py
Copy path
File metadata and controls
729 lines (598 loc) · 29.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Mika Mäenpää <mika.j.maenpaa@tut.fi>,
# Tampere University of Technology
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from
__future__
import
print_function
try
:
import
unittest
except
ImportError
:
import
unittest2
as
unittest
from
httmock
import
HTTMock
# noqa
from
httmock
import
response
# noqa
from
httmock
import
urlmatch
# noqa
import
gitlab
from
gitlab
import
*
# noqa
class
TestSanitize
(
unittest
.
TestCase
):
def
test_do_nothing
(
self
):
self
.
assertEqual
(
1
,
gitlab
.
_sanitize
(
1
))
self
.
assertEqual
(
1.5
,
gitlab
.
_sanitize
(
1.5
))
self
.
assertEqual
(
"foo"
,
gitlab
.
_sanitize
(
"foo"
))
def
test_slash
(
self
):
self
.
assertEqual
(
"foo%2Fbar"
,
gitlab
.
_sanitize
(
"foo/bar"
))
def
test_dict
(
self
):
source
=
{
"url"
:
"foo/bar"
,
"id"
:
1
}
expected
=
{
"url"
:
"foo%2Fbar"
,
"id"
:
1
}
self
.
assertEqual
(
expected
,
gitlab
.
_sanitize
(
source
))
class
TestGitlabRawMethods
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
gl
=
Gitlab
(
"http://localhost"
,
private_token
=
"private_token"
,
email
=
"testuser@test.com"
,
password
=
"testpassword"
,
ssl_verify
=
True
)
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/known_path"
,
method
=
"get"
)
def
resp_get
(
self
,
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'response'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
def
test_raw_get_unknown_path
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/unknown_path"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
resp
=
self
.
gl
.
_raw_get
(
"/unknown_path"
)
self
.
assertEqual
(
resp
.
status_code
,
404
)
def
test_raw_get_without_kwargs
(
self
):
with
HTTMock
(
self
.
resp_get
):
resp
=
self
.
gl
.
_raw_get
(
"/known_path"
)
self
.
assertEqual
(
resp
.
content
,
b'response'
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
def
test_raw_get_with_kwargs
(
self
):
with
HTTMock
(
self
.
resp_get
):
resp
=
self
.
gl
.
_raw_get
(
"/known_path"
,
sudo
=
"testing"
)
self
.
assertEqual
(
resp
.
content
,
b'response'
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
def
test_raw_post
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/known_path"
,
method
=
"post"
)
def
resp_post
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'response'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_post
):
resp
=
self
.
gl
.
_raw_post
(
"/known_path"
)
self
.
assertEqual
(
resp
.
content
,
b'response'
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
def
test_raw_post_unknown_path
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/unknown_path"
,
method
=
"post"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
resp
=
self
.
gl
.
_raw_post
(
"/unknown_path"
)
self
.
assertEqual
(
resp
.
status_code
,
404
)
def
test_raw_put
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/known_path"
,
method
=
"put"
)
def
resp_put
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'response'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_put
):
resp
=
self
.
gl
.
_raw_put
(
"/known_path"
)
self
.
assertEqual
(
resp
.
content
,
b'response'
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
def
test_raw_put_unknown_path
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/unknown_path"
,
method
=
"put"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
resp
=
self
.
gl
.
_raw_put
(
"/unknown_path"
)
self
.
assertEqual
(
resp
.
status_code
,
404
)
def
test_raw_delete
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/known_path"
,
method
=
"delete"
)
def
resp_delete
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'response'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_delete
):
resp
=
self
.
gl
.
_raw_delete
(
"/known_path"
)
self
.
assertEqual
(
resp
.
content
,
b'response'
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
def
test_raw_delete_unknown_path
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/unknown_path"
,
method
=
"delete"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
resp
=
self
.
gl
.
_raw_delete
(
"/unknown_path"
)
self
.
assertEqual
(
resp
.
status_code
,
404
)
class
TestGitlabMethods
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
gl
=
Gitlab
(
"http://localhost"
,
private_token
=
"private_token"
,
email
=
"testuser@test.com"
,
password
=
"testpassword"
,
ssl_verify
=
True
)
def
test_list
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1/repository/branches"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
(
'[{"branch_name": "testbranch", '
'"project_id": 1, "ref": "a"}]'
).
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
data
=
self
.
gl
.
list
(
ProjectBranch
,
project_id
=
1
,
page
=
1
,
per_page
=
20
)
self
.
assertEqual
(
len
(
data
),
1
)
data
=
data
[
0
]
self
.
assertEqual
(
data
.
branch_name
,
"testbranch"
)
self
.
assertEqual
(
data
.
project_id
,
1
)
self
.
assertEqual
(
data
.
ref
,
"a"
)
def
test_list_next_link
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
'/api/v3/projects/1/repository/branches'
,
method
=
"get"
)
def
resp_one
(
url
,
request
):
"""First request:
http://localhost/api/v3/projects/1/repository/branches?per_page=1
"""
headers
=
{
'content-type'
:
'application/json'
,
'link'
:
'<http://localhost/api/v3/projects/1/repository/branc'
'hes?page=2&per_page=0>; rel="next", <http://localhost/api/v3'
'/projects/1/repository/branches?page=2&per_page=0>; rel="las'
't", <http://localhost/api/v3/projects/1/repository/branches?'
'page=1&per_page=0>; rel="first"'
}
content
=
(
'[{"branch_name": "otherbranch", '
'"project_id": 1, "ref": "b"}]'
).
encode
(
"utf-8"
)
resp
=
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
return
resp
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
'/api/v3/projects/1/repository/branches'
,
method
=
"get"
,
query
=
r'.*page=2.*'
)
def
resp_two
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
,
'link'
:
'<http://localhost/api/v3/projects/1/repository/branc'
'hes?page=1&per_page=0>; rel="prev", <http://localhost/api/v3'
'/projects/1/repository/branches?page=2&per_page=0>; rel="las'
't", <http://localhost/api/v3/projects/1/repository/branches?'
'page=1&per_page=0>; rel="first"'
}
content
=
(
'[{"branch_name": "testbranch", '
'"project_id": 1, "ref": "a"}]'
).
encode
(
"utf-8"
)
resp
=
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
return
resp
with
HTTMock
(
resp_two
,
resp_one
):
data
=
self
.
gl
.
list
(
ProjectBranch
,
project_id
=
1
,
per_page
=
1
,
all
=
True
)
self
.
assertEqual
(
data
[
1
].
branch_name
,
"testbranch"
)
self
.
assertEqual
(
data
[
1
].
project_id
,
1
)
self
.
assertEqual
(
data
[
1
].
ref
,
"a"
)
self
.
assertEqual
(
data
[
0
].
branch_name
,
"otherbranch"
)
self
.
assertEqual
(
data
[
0
].
project_id
,
1
)
self
.
assertEqual
(
data
[
0
].
ref
,
"b"
)
self
.
assertEqual
(
len
(
data
),
2
)
def
test_list_401
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1/repository/branches"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message":"message"}'
.
encode
(
"utf-8"
)
return
response
(
401
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabAuthenticationError
,
self
.
gl
.
list
,
ProjectBranch
,
project_id
=
1
)
def
test_list_unknown_error
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1/repository/branches"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message":"message"}'
.
encode
(
"utf-8"
)
return
response
(
405
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabListError
,
self
.
gl
.
list
,
ProjectBranch
,
project_id
=
1
)
def
test_list_kw_missing
(
self
):
self
.
assertRaises
(
GitlabListError
,
self
.
gl
.
list
,
ProjectBranch
)
def
test_list_no_connection
(
self
):
self
.
gl
.
set_url
(
'http://localhost:66000'
)
self
.
assertRaises
(
GitlabConnectionError
,
self
.
gl
.
list
,
ProjectBranch
,
project_id
=
1
)
def
test_get
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"name": "testproject"}'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
data
=
self
.
gl
.
get
(
Project
,
id
=
1
)
expected
=
{
"name"
:
"testproject"
}
self
.
assertEqual
(
expected
,
data
)
def
test_get_unknown_path
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups/1"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabGetError
,
self
.
gl
.
get
,
Group
,
1
)
def
test_get_missing_kw
(
self
):
self
.
assertRaises
(
GitlabGetError
,
self
.
gl
.
get
,
ProjectBranch
)
def
test_get_401
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
401
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabAuthenticationError
,
self
.
gl
.
get
,
Project
,
1
)
def
test_get_404
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabGetError
,
self
.
gl
.
get
,
Project
,
1
)
def
test_get_unknown_error
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
405
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabGetError
,
self
.
gl
.
get
,
Project
,
1
)
def
test_delete_from_object
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups/1"
,
method
=
"delete"
)
def
resp_delete_group
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
''
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
obj
=
Group
(
self
.
gl
,
data
=
{
"name"
:
"testname"
,
"id"
:
1
})
with
HTTMock
(
resp_delete_group
):
data
=
self
.
gl
.
delete
(
obj
)
self
.
assertIs
(
data
,
True
)
def
test_delete_from_invalid_class
(
self
):
class
InvalidClass
(
object
):
pass
self
.
assertRaises
(
GitlabError
,
self
.
gl
.
delete
,
InvalidClass
,
1
)
def
test_delete_from_class
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups/1"
,
method
=
"delete"
)
def
resp_delete_group
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
''
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_delete_group
):
data
=
self
.
gl
.
delete
(
Group
,
1
)
self
.
assertIs
(
data
,
True
)
def
test_delete_unknown_path
(
self
):
obj
=
Project
(
self
.
gl
,
data
=
{
"name"
:
"testname"
,
"id"
:
1
})
obj
.
_from_api
=
True
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"delete"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabDeleteError
,
self
.
gl
.
delete
,
obj
)
def
test_delete_401
(
self
):
obj
=
Project
(
self
.
gl
,
data
=
{
"name"
:
"testname"
,
"id"
:
1
})
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"delete"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
401
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabAuthenticationError
,
self
.
gl
.
delete
,
obj
)
def
test_delete_unknown_error
(
self
):
obj
=
Project
(
self
.
gl
,
data
=
{
"name"
:
"testname"
,
"id"
:
1
})
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"delete"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
405
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabDeleteError
,
self
.
gl
.
delete
,
obj
)
def
test_create
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects"
,
method
=
"post"
)
def
resp_create_project
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"name": "testname", "id": 1}'
.
encode
(
"utf-8"
)
return
response
(
201
,
content
,
headers
,
None
,
5
,
request
)
obj
=
Project
(
self
.
gl
,
data
=
{
"name"
:
"testname"
})
with
HTTMock
(
resp_create_project
):
data
=
self
.
gl
.
create
(
obj
)
expected
=
{
u"name"
:
u"testname"
,
u"id"
:
1
}
self
.
assertEqual
(
expected
,
data
)
def
test_create_kw_missing
(
self
):
obj
=
Group
(
self
.
gl
,
data
=
{
"name"
:
"testgroup"
})
self
.
assertRaises
(
GitlabCreateError
,
self
.
gl
.
create
,
obj
)
def
test_create_unknown_path
(
self
):
obj
=
Project
(
self
.
gl
,
data
=
{
"name"
:
"name"
})
obj
.
id
=
1
obj
.
_from_api
=
True
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"delete"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabDeleteError
,
self
.
gl
.
delete
,
obj
)
def
test_create_401
(
self
):
obj
=
Group
(
self
.
gl
,
data
=
{
"name"
:
"testgroup"
,
"path"
:
"testpath"
})
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups"
,
method
=
"post"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
401
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabAuthenticationError
,
self
.
gl
.
create
,
obj
)
def
test_create_unknown_error
(
self
):
obj
=
Group
(
self
.
gl
,
data
=
{
"name"
:
"testgroup"
,
"path"
:
"testpath"
})
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups"
,
method
=
"post"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
405
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabCreateError
,
self
.
gl
.
create
,
obj
)
def
test_update
(
self
):
obj
=
User
(
self
.
gl
,
data
=
{
"email"
:
"testuser@testmail.com"
,
"password"
:
"testpassword"
,
"name"
:
u"testuser"
,
"username"
:
"testusername"
,
"can_create_group"
:
True
,
"id"
:
1
})
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/users/1"
,
method
=
"put"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"first": "return1"}'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
data
=
self
.
gl
.
update
(
obj
)
expected
=
{
"first"
:
"return1"
}
self
.
assertEqual
(
expected
,
data
)
def
test_update_kw_missing
(
self
):
obj
=
Hook
(
self
.
gl
,
data
=
{
"name"
:
"testgroup"
})
self
.
assertRaises
(
GitlabUpdateError
,
self
.
gl
.
update
,
obj
)
def
test_update_401
(
self
):
obj
=
Group
(
self
.
gl
,
data
=
{
"name"
:
"testgroup"
,
"path"
:
"testpath"
,
"id"
:
1
})
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups/1"
,
method
=
"put"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
401
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabAuthenticationError
,
self
.
gl
.
update
,
obj
)
def
test_update_unknown_error
(
self
):
obj
=
Group
(
self
.
gl
,
data
=
{
"name"
:
"testgroup"
,
"path"
:
"testpath"
,
"id"
:
1
})
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups/1"
,
method
=
"put"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
405
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabUpdateError
,
self
.
gl
.
update
,
obj
)
def
test_update_unknown_path
(
self
):
obj
=
Group
(
self
.
gl
,
data
=
{
"name"
:
"testgroup"
,
"path"
:
"testpath"
,
"id"
:
1
})
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups/1"
,
method
=
"put"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabUpdateError
,
self
.
gl
.
update
,
obj
)
class
TestGitlab
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
gl
=
Gitlab
(
"http://localhost"
,
private_token
=
"private_token"
,
email
=
"testuser@test.com"
,
password
=
"testpassword"
,
ssl_verify
=
True
)
def
test_set_url
(
self
):
self
.
gl
.
set_url
(
"http://new_url"
)
self
.
assertEqual
(
self
.
gl
.
_url
,
"http://new_url/api/v3"
)
def
test_set_token
(
self
):
token
=
"newtoken"
expected
=
{
"PRIVATE-TOKEN"
:
token
}
self
.
gl
.
set_token
(
token
)
self
.
assertEqual
(
self
.
gl
.
private_token
,
token
)
self
.
assertDictContainsSubset
(
expected
,
self
.
gl
.
headers
)
def
test_set_credentials
(
self
):
email
=
"credentialuser@test.com"
password
=
"credentialpassword"
self
.
gl
.
set_credentials
(
email
=
email
,
password
=
password
)
self
.
assertEqual
(
self
.
gl
.
email
,
email
)
self
.
assertEqual
(
self
.
gl
.
password
,
password
)
def
test_credentials_auth_nopassword
(
self
):
self
.
gl
.
set_credentials
(
email
=
None
,
password
=
None
)
self
.
assertRaises
(
GitlabAuthenticationError
,
self
.
gl
.
credentials_auth
)
def
test_credentials_auth_notok
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/session"
,
method
=
"post"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"message": "message"}'
.
encode
(
"utf-8"
)
return
response
(
404
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
self
.
assertRaises
(
GitlabAuthenticationError
,
self
.
gl
.
credentials_auth
)
def
test_auth_with_credentials
(
self
):
self
.
gl
.
set_token
(
None
)
self
.
test_credentials_auth
(
callback
=
self
.
gl
.
auth
)
def
test_auth_with_token
(
self
):
self
.
test_token_auth
(
callback
=
self
.
gl
.
auth
)
def
test_credentials_auth
(
self
,
callback
=
None
):
if
callback
is
None
:
callback
=
self
.
gl
.
credentials_auth
token
=
"credauthtoken"
id_
=
1
expected
=
{
"PRIVATE-TOKEN"
:
token
}
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/session"
,
method
=
"post"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{{"id": {0:d}, "private_token": "{1:s}"}}'
.
format
(
id_
,
token
).
encode
(
"utf-8"
)
return
response
(
201
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
callback
()
self
.
assertEqual
(
self
.
gl
.
private_token
,
token
)
self
.
assertDictContainsSubset
(
expected
,
self
.
gl
.
headers
)
self
.
assertEqual
(
self
.
gl
.
user
.
id
,
id_
)
def
test_token_auth
(
self
,
callback
=
None
):
if
callback
is
None
:
callback
=
self
.
gl
.
token_auth
name
=
"username"
id_
=
1
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/user"
,
method
=
"get"
)
def
resp_cont
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{{"id": {0:d}, "username": "{1:s}"}}'
.
format
(
id_
,
name
).
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_cont
):
callback
()
self
.
assertEqual
(
self
.
gl
.
user
.
username
,
name
)
self
.
assertEqual
(
self
.
gl
.
user
.
id
,
id_
)
self
.
assertEqual
(
type
(
self
.
gl
.
user
),
CurrentUser
)
def
test_hooks
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/hooks/1"
,
method
=
"get"
)
def
resp_get_hook
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"url": "testurl", "id": 1}'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_get_hook
):
data
=
self
.
gl
.
hooks
.
get
(
1
)
self
.
assertEqual
(
type
(
data
),
Hook
)
self
.
assertEqual
(
data
.
url
,
"testurl"
)
self
.
assertEqual
(
data
.
id
,
1
)
def
test_projects
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/1"
,
method
=
"get"
)
def
resp_get_project
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"name": "name", "id": 1}'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_get_project
):
data
=
self
.
gl
.
projects
.
get
(
1
)
self
.
assertEqual
(
type
(
data
),
Project
)
self
.
assertEqual
(
data
.
name
,
"name"
)
self
.
assertEqual
(
data
.
id
,
1
)
def
test_userprojects
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/projects/user/2"
,
method
=
"get"
)
def
resp_get_userproject
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"name": "name", "id": 1, "user_id": 2}'
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_get_userproject
):
self
.
assertRaises
(
NotImplementedError
,
self
.
gl
.
user_projects
.
get
,
1
,
user_id
=
2
)
def
test_groups
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/groups/1"
,
method
=
"get"
)
def
resp_get_group
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"name": "name", "id": 1, "path": "path"}'
content
=
content
.
encode
(
'utf-8'
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_get_group
):
data
=
self
.
gl
.
groups
.
get
(
1
)
self
.
assertEqual
(
type
(
data
),
Group
)
self
.
assertEqual
(
data
.
name
,
"name"
)
self
.
assertEqual
(
data
.
path
,
"path"
)
self
.
assertEqual
(
data
.
id
,
1
)
def
test_issues
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/issues"
,
method
=
"get"
)
def
resp_get_issue
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
(
'[{"name": "name", "id": 1}, '
'{"name": "other_name", "id": 2}]'
)
content
=
content
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_get_issue
):
data
=
self
.
gl
.
issues
.
get
(
2
)
self
.
assertEqual
(
data
.
id
,
2
)
self
.
assertEqual
(
data
.
name
,
'other_name'
)
def
test_users
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/users/1"
,
method
=
"get"
)
def
resp_get_user
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
(
'{"name": "name", "id": 1, "password": "password", '
'"username": "username", "email": "email"}'
)
content
=
content
.
encode
(
"utf-8"
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_get_user
):
user
=
self
.
gl
.
users
.
get
(
1
)
self
.
assertEqual
(
type
(
user
),
User
)
self
.
assertEqual
(
user
.
name
,
"name"
)
self
.
assertEqual
(
user
.
id
,
1
)
def
test_teams
(
self
):
@
urlmatch
(
scheme
=
"http"
,
netloc
=
"localhost"
,
path
=
"/api/v3/user_teams/1"
,
method
=
"get"
)
def
resp_get_group
(
url
,
request
):
headers
=
{
'content-type'
:
'application/json'
}
content
=
'{"name": "name", "id": 1, "path": "path"}'
content
=
content
.
encode
(
'utf-8'
)
return
response
(
200
,
content
,
headers
,
None
,
5
,
request
)
with
HTTMock
(
resp_get_group
):
data
=
self
.
gl
.
teams
.
get
(
1
)
self
.
assertEqual
(
type
(
data
),
Team
)
self
.
assertEqual
(
data
.
name
,
"name"
)
self
.
assertEqual
(
data
.
path
,
"path"
)
self
.
assertEqual
(
data
.
id
,
1
)
Back
|
FazBrowse Home
|
New Git URL