Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_gitlab_hook.py

raw
1 # Copyright (c) 2019, Guillaume Martinez ([email protected])
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import pytest
8
9 from ansible_collections.community.general.plugins.modules.gitlab_hook import GitLabHook
10
11
12 def _dummy(x):
13 """Dummy function. Only used as a placeholder for toplevel definitions when the test is going
14 to be skipped anyway"""
15 return x
16
17
18 pytestmark = []
19 try:
20 # GitLab module requirements
21 from gitlab.v4.objects import ProjectHook
22
23 from .gitlab import (
24 GitlabModuleTestCase,
25 resp_create_project_hook,
26 resp_delete_project_hook,
27 resp_find_project_hook,
28 resp_get_project,
29 )
30 except ImportError:
31 pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
32 # Need to set these to something so that we don't fail when parsing
33 GitlabModuleTestCase = object # type: ignore
34 resp_get_project = _dummy
35 resp_find_project_hook = _dummy
36 resp_create_project_hook = _dummy
37 resp_delete_project_hook = _dummy
38
39 # Unit tests requirements
40 try:
41 from httmock import with_httmock # noqa
42 except ImportError:
43 pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing"))
44 with_httmock = _dummy
45
46
47 class TestGitlabHook(GitlabModuleTestCase):
48 def setUp(self):
49 super().setUp()
50
51 self.moduleUtil = GitLabHook(module=self.mock_module, gitlab_instance=self.gitlab_instance)
52
53 @with_httmock(resp_get_project)
54 @with_httmock(resp_find_project_hook)
55 def test_hook_exist(self):
56 project = self.gitlab_instance.projects.get(1)
57
58 rvalue = self.moduleUtil.exists_hook(project, "http://example.com/hook")
59
60 self.assertEqual(rvalue, True)
61
62 rvalue = self.moduleUtil.exists_hook(project, "http://gitlab.com/hook")
63
64 self.assertEqual(rvalue, False)
65
66 @with_httmock(resp_get_project)
67 @with_httmock(resp_create_project_hook)
68 def test_create_hook(self):
69 project = self.gitlab_instance.projects.get(1)
70
71 hook = self.moduleUtil.create_hook(project, {"url": "http://example.com/hook"})
72
73 self.assertEqual(type(hook), ProjectHook)
74 self.assertEqual(hook.url, "http://example.com/hook")
75
76 @with_httmock(resp_get_project)
77 @with_httmock(resp_find_project_hook)
78 def test_update_hook(self):
79 project = self.gitlab_instance.projects.get(1)
80 hook = self.moduleUtil.find_hook(project, "http://example.com/hook")
81
82 changed, newHook = self.moduleUtil.update_hook(hook, {"url": "http://gitlab.com/hook"})
83
84 self.assertEqual(changed, True)
85 self.assertEqual(type(newHook), ProjectHook)
86 self.assertEqual(newHook.url, "http://gitlab.com/hook")
87
88 changed, newHook = self.moduleUtil.update_hook(hook, {"url": "http://gitlab.com/hook"})
89
90 self.assertEqual(changed, False)
91 self.assertEqual(newHook.url, "http://gitlab.com/hook")
92
93 @with_httmock(resp_get_project)
94 @with_httmock(resp_find_project_hook)
95 @with_httmock(resp_delete_project_hook)
96 def test_delete_hook(self):
97 project = self.gitlab_instance.projects.get(1)
98
99 self.moduleUtil.exists_hook(project, "http://example.com/hook")
100
101 rvalue = self.moduleUtil.delete_hook()
102
103 self.assertEqual(rvalue, None)