2020import warnings
2121
2222import pytest
23- from httmock import HTTMock , response , urlmatch , with_httmock # noqa
23+ import responses
2424
2525import gitlab
2626
2727localhost = "http://localhost"
28- username = "username"
29- user_id = 1
3028token = "abc123"
3129
3230
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+ }
3840
3941
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 ():
4244 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" ,
4950 "Link" : ("<http://localhost/api/v4/tests?per_page=1&page=2>;" ' rel="next"' ),
5051 }
51- content = '[{"a": "b"}]'
52- return response (200 , content , headers , None , 5 , request )
5352
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+ }
5462
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 ():
6366 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 ,
7084 }
71- content = '[{"c": "d"}]'
72- return response (200 , content , headers , None , 5 , request )
7385
7486
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 )
7891 assert len (obj ) == 2
7992 assert obj ._next_url == "http://localhost/api/v4/tests?per_page=1&page=2"
8093 assert obj .current_page == 1
@@ -84,15 +97,17 @@ def test_gitlab_build_list(gl):
8497 assert obj .total_pages == 2
8598 assert obj .total == 2
8699
87- with HTTMock ( resp_page_2 ):
88- test_list = list (obj )
100+ responses . add ( ** resp_page_2 )
101+ test_list = list (obj )
89102 assert len (test_list ) == 2
90103 assert test_list [0 ]["a" ] == "b"
91104 assert test_list [1 ]["c" ] == "d"
92105
93106
94- @with_httmock (resp_page_1 , resp_page_2 )
95- def test_gitlab_all_omitted_when_as_list (gl ):
107+ @responses .activate
108+ def test_gitlab_all_omitted_when_as_list (gl , resp_page_1 , resp_page_2 ):
109+ responses .add (** resp_page_1 )
110+ responses .add (** resp_page_2 )
96111 result = gl .http_list ("/tests" , as_list = False , all = True )
97112 assert isinstance (result , gitlab .GitlabList )
98113
@@ -119,11 +134,12 @@ def test_gitlab_pickability(gl):
119134 assert unpickled ._objects == original_gl_objects
120135
121136
122- @with_httmock (resp_get_user )
123- def test_gitlab_token_auth (gl , callback = None ):
137+ @responses .activate
138+ def test_gitlab_token_auth (gl , resp_get_user ):
139+ responses .add (** resp_get_user )
124140 gl .auth ()
125- assert gl .user .username == username
126- assert gl .user .id == user_id
141+ assert gl .user .username == " username"
142+ assert gl .user .id == 1
127143 assert isinstance (gl .user , gitlab .v4 .objects .CurrentUser )
128144
129145
0 commit comments