Skip to content

Commit abd3c01

Browse files
author
Costin Bleotu
committed
Created tests for hook creation and update
1 parent 4c2c477 commit abd3c01

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

tests/interface_test.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,26 @@ def setUp(self):
4949
}"""
5050
self.username_password = gogs_client.UsernamePassword(
5151
"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+
5269
self.expected_repo = gogs_client.GogsRepo.from_json(json.loads(self.repo_json_str))
5370
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))
5472
self.token = gogs_client.Token.from_json(json.loads(self.token_json_str))
5573

5674
@responses.activate
@@ -184,7 +202,7 @@ def test_update_user1(self):
184202
.build()
185203

186204
def callback(request):
187-
data = self.data_of_query(request.body)
205+
data = json.loads(request.body)
188206
self.assertEqual(data["login_name"], "loginname")
189207
self.assertEqual(data["full_name"], "Example User")
190208
self.assertEqual(data["email"], "user@example.com")
@@ -267,6 +285,45 @@ def test_ensure_auth_token(self):
267285
token = self.client.ensure_token(self.username_password, self.token.name)
268286
self.assert_tokens_equals(token, self.token)
269287

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+
270327
# helper methods
271328

272329
@staticmethod
@@ -310,6 +367,12 @@ def assert_tokens_equals(self, token, expected):
310367
self.assertEqual(token.name, expected.name)
311368
self.assertEqual(token.token, expected.token)
312369

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)
313376

314377
if __name__ == "__main__":
315378
unittest.main()

0 commit comments

Comments
 (0)