@@ -49,8 +49,26 @@ def setUp(self):
49
49
}"""
50
50
self .username_password = gogs_client .UsernamePassword (
51
51
"auth_username" , "password" )
52
+ self .hook_json_str = """{
53
+ "id": 4,
54
+ "type": "gogs",
55
+ "config": {
56
+ "content_type": "json",
57
+ "url": "http://test.io/hook"
58
+ },
59
+ "events": [
60
+ "create",
61
+ "push",
62
+ "issues"
63
+ ],
64
+ "active": false,
65
+ "updated_at": "2017-03-31T12:42:58Z",
66
+ "created_at": "2017-03-31T12:42:58Z"
67
+ }"""
68
+
52
69
self .expected_repo = gogs_client .GogsRepo .from_json (json .loads (self .repo_json_str ))
53
70
self .expected_user = gogs_client .GogsUser .from_json (json .loads (self .user_json_str ))
71
+ self .expected_hook = gogs_client .GogsRepo .Hook .from_json (json .loads (self .hook_json_str ))
54
72
self .token = gogs_client .Token .from_json (json .loads (self .token_json_str ))
55
73
56
74
@responses .activate
@@ -184,7 +202,7 @@ def test_update_user1(self):
184
202
.build ()
185
203
186
204
def callback (request ):
187
- data = self . data_of_query (request .body )
205
+ data = json . loads (request .body )
188
206
self .assertEqual (data ["login_name" ], "loginname" )
189
207
self .assertEqual (data ["full_name" ], "Example User" )
190
208
self .assertEqual (data ["email" ], "user@example.com" )
@@ -267,6 +285,45 @@ def test_ensure_auth_token(self):
267
285
token = self .client .ensure_token (self .username_password , self .token .name )
268
286
self .assert_tokens_equals (token , self .token )
269
287
288
+ @responses .activate
289
+ def test_create_hook1 (self ):
290
+ uri = self .path ("/repos/username/repo1/hooks" )
291
+ responses .add (responses .POST , uri , body = self .hook_json_str )
292
+ hook = self .client .create_hook (self .token ,
293
+ repo_name = "repo1" ,
294
+ hook_type = "gogs" ,
295
+ config = {
296
+ "content_type" : "json2" ,
297
+ "url" : "http://test.io/hook"
298
+ },
299
+ events = ["create" , "push" , "issues" ],
300
+ active = False ,
301
+ organization = "username" )
302
+ self .assert_hooks_equals (hook , self .expected_hook )
303
+ self .assertEqual (len (responses .calls ), 1 )
304
+ call = responses .calls [0 ]
305
+ self .assertEqual (call .request .url , self .path_with_token (uri ))
306
+
307
+ @responses .activate
308
+ def test_update_hook1 (self ):
309
+ update = gogs_client .GogsHookUpdate .Builder ()\
310
+ .set_events (["issues_comments" ])\
311
+ .set_config ({"url" : "http://newurl.com/hook" })\
312
+ .set_active (True )\
313
+ .build ()
314
+
315
+ def callback (request ):
316
+ data = json .loads (request .body )
317
+ self .assertEqual (data ["config" ]["url" ], "http://newurl.com/hook" )
318
+ self .assertEqual (data ["events" ], ['issues_comments' ])
319
+ self .assertEqual (data ["active" ], True )
320
+ return 200 , {}, self .hook_json_str
321
+ uri = self .path ("/repos/username/repo1/hooks/4" )
322
+ responses .add_callback (responses .PATCH , uri , callback = callback )
323
+ hook = self .client .update_hook (self .token , "repo1" , 4 , update , organization = "username" )
324
+ self .assert_hooks_equals (hook , self .expected_hook )
325
+
326
+
270
327
# helper methods
271
328
272
329
@staticmethod
@@ -310,6 +367,12 @@ def assert_tokens_equals(self, token, expected):
310
367
self .assertEqual (token .name , expected .name )
311
368
self .assertEqual (token .token , expected .token )
312
369
370
+ def assert_hooks_equals (self , hook , expected ):
371
+ self .assertEqual (hook .hook_id , expected .hook_id )
372
+ self .assertEqual (hook .hook_type , expected .hook_type )
373
+ self .assertEqual (hook .events , expected .events )
374
+ self .assertEqual (hook .config , expected .config )
375
+ self .assertEqual (hook .active , expected .active )
313
376
314
377
if __name__ == "__main__" :
315
378
unittest .main ()
0 commit comments