20
20
import warnings
21
21
22
22
import pytest
23
- from httmock import HTTMock , response , urlmatch , with_httmock # noqa
23
+ import responses
24
24
25
25
import gitlab
26
26
27
27
localhost = "http://localhost"
28
- username = "username"
29
- user_id = 1
30
28
token = "abc123"
31
29
32
30
33
- @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/user" , method = "get" )
34
- def resp_get_user (url , request ):
35
- headers = {"content-type" : "application/json" }
36
- content = f'{{"id": { user_id :d} , "username": "{ username :s} "}}' .encode ("utf-8" )
37
- return response (200 , content , headers , None , 5 , request )
31
+ @pytest .fixture
32
+ def resp_get_user ():
33
+ return {
34
+ "method" : responses .GET ,
35
+ "url" : "http://localhost/api/v4/user" ,
36
+ "json" : {"id" : 1 , "username" : "username" },
37
+ "content_type" : "application/json" ,
38
+ "status" : 200 ,
39
+ }
38
40
39
41
40
- @urlmatch ( scheme = "http" , netloc = "localhost" , path = "/api/v4/tests" , method = "get" )
41
- def resp_page_1 (url , request ):
42
+ @pytest . fixture
43
+ def resp_page_1 ():
42
44
headers = {
43
- "content-type" : "application/json" ,
44
- "X-Page" : 1 ,
45
- "X-Next-Page" : 2 ,
46
- "X-Per-Page" : 1 ,
47
- "X-Total-Pages" : 2 ,
48
- "X-Total" : 2 ,
45
+ "X-Page" : "1" ,
46
+ "X-Next-Page" : "2" ,
47
+ "X-Per-Page" : "1" ,
48
+ "X-Total-Pages" : "2" ,
49
+ "X-Total" : "2" ,
49
50
"Link" : ("<http://localhost/api/v4/tests?per_page=1&page=2>;" ' rel="next"' ),
50
51
}
51
- content = '[{"a": "b"}]'
52
- return response (200 , content , headers , None , 5 , request )
53
52
53
+ return {
54
+ "method" : responses .GET ,
55
+ "url" : "http://localhost/api/v4/tests" ,
56
+ "json" : [{"a" : "b" }],
57
+ "headers" : headers ,
58
+ "content_type" : "application/json" ,
59
+ "status" : 200 ,
60
+ "match_querystring" : True ,
61
+ }
54
62
55
- @urlmatch (
56
- scheme = "http" ,
57
- netloc = "localhost" ,
58
- path = "/api/v4/tests" ,
59
- method = "get" ,
60
- query = r".*page=2" ,
61
- )
62
- def resp_page_2 (url , request ):
63
+
64
+ @pytest .fixture
65
+ def resp_page_2 ():
63
66
headers = {
64
- "content-type" : "application/json" ,
65
- "X-Page" : 2 ,
66
- "X-Next-Page" : 2 ,
67
- "X-Per-Page" : 1 ,
68
- "X-Total-Pages" : 2 ,
69
- "X-Total" : 2 ,
67
+ "X-Page" : "2" ,
68
+ "X-Next-Page" : "2" ,
69
+ "X-Per-Page" : "1" ,
70
+ "X-Total-Pages" : "2" ,
71
+ "X-Total" : "2" ,
72
+ }
73
+ params = {"per_page" : "1" , "page" : "2" }
74
+
75
+ return {
76
+ "method" : responses .GET ,
77
+ "url" : "http://localhost/api/v4/tests" ,
78
+ "json" : [{"c" : "d" }],
79
+ "headers" : headers ,
80
+ "content_type" : "application/json" ,
81
+ "status" : 200 ,
82
+ "match" : [responses .matchers .query_param_matcher (params )],
83
+ "match_querystring" : False ,
70
84
}
71
- content = '[{"c": "d"}]'
72
- return response (200 , content , headers , None , 5 , request )
73
85
74
86
75
- def test_gitlab_build_list (gl ):
76
- with HTTMock (resp_page_1 ):
77
- obj = gl .http_list ("/tests" , as_list = False )
87
+ @responses .activate
88
+ def test_gitlab_build_list (gl , resp_page_1 , resp_page_2 ):
89
+ responses .add (** resp_page_1 )
90
+ obj = gl .http_list ("/tests" , as_list = False )
78
91
assert len (obj ) == 2
79
92
assert obj ._next_url == "http://localhost/api/v4/tests?per_page=1&page=2"
80
93
assert obj .current_page == 1
@@ -84,15 +97,18 @@ def test_gitlab_build_list(gl):
84
97
assert obj .total_pages == 2
85
98
assert obj .total == 2
86
99
87
- with HTTMock (resp_page_2 ):
88
- test_list = list (obj )
100
+ responses .add (** resp_page_2 )
101
+ gl .enable_debug ()
102
+ test_list = list (obj )
89
103
assert len (test_list ) == 2
90
104
assert test_list [0 ]["a" ] == "b"
91
105
assert test_list [1 ]["c" ] == "d"
92
106
93
107
94
- @with_httmock (resp_page_1 , resp_page_2 )
95
- def test_gitlab_all_omitted_when_as_list (gl ):
108
+ @responses .activate
109
+ def test_gitlab_all_omitted_when_as_list (gl , resp_page_1 , resp_page_2 ):
110
+ responses .add (** resp_page_1 )
111
+ responses .add (** resp_page_2 )
96
112
result = gl .http_list ("/tests" , as_list = False , all = True )
97
113
assert isinstance (result , gitlab .GitlabList )
98
114
@@ -119,11 +135,12 @@ def test_gitlab_pickability(gl):
119
135
assert unpickled ._objects == original_gl_objects
120
136
121
137
122
- @with_httmock (resp_get_user )
123
- def test_gitlab_token_auth (gl , callback = None ):
138
+ @responses .activate
139
+ def test_gitlab_token_auth (gl , resp_get_user ):
140
+ responses .add (** resp_get_user )
124
141
gl .auth ()
125
- assert gl .user .username == username
126
- assert gl .user .id == user_id
142
+ assert gl .user .username == " username"
143
+ assert gl .user .id == 1
127
144
assert isinstance (gl .user , gitlab .v4 .objects .CurrentUser )
128
145
129
146
0 commit comments