1# -*- coding: utf-8 -*- 2 3# Copyright: (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr) 4# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 5 6from __future__ import absolute_import 7import pytest 8 9from ansible.modules.source_control.gitlab_hook import GitLabHook 10 11 12def _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 18pytestmark = [] 19try: 20 from .gitlab import (GitlabModuleTestCase, 21 python_version_match_requirement, 22 resp_get_project, resp_find_project_hook, 23 resp_create_project_hook, resp_delete_project_hook) 24 25 # GitLab module requirements 26 if python_version_match_requirement(): 27 from gitlab.v4.objects import ProjectHook 28except ImportError: 29 pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing")) 30 # Need to set these to something so that we don't fail when parsing 31 GitlabModuleTestCase = object 32 resp_get_project = _dummy 33 resp_find_project_hook = _dummy 34 resp_create_project_hook = _dummy 35 resp_delete_project_hook = _dummy 36 37# Unit tests requirements 38try: 39 from httmock import with_httmock # noqa 40except ImportError: 41 pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing")) 42 with_httmock = _dummy 43 44 45class TestGitlabHook(GitlabModuleTestCase): 46 def setUp(self): 47 super(TestGitlabHook, self).setUp() 48 49 self.moduleUtil = GitLabHook(module=self.mock_module, gitlab_instance=self.gitlab_instance) 50 51 @with_httmock(resp_get_project) 52 @with_httmock(resp_find_project_hook) 53 def test_hook_exist(self): 54 project = self.gitlab_instance.projects.get(1) 55 56 rvalue = self.moduleUtil.existsHook(project, "http://example.com/hook") 57 58 self.assertEqual(rvalue, True) 59 60 rvalue = self.moduleUtil.existsHook(project, "http://gitlab.com/hook") 61 62 self.assertEqual(rvalue, False) 63 64 @with_httmock(resp_get_project) 65 @with_httmock(resp_create_project_hook) 66 def test_create_hook(self): 67 project = self.gitlab_instance.projects.get(1) 68 69 hook = self.moduleUtil.createHook(project, {"url": "http://example.com/hook"}) 70 71 self.assertEqual(type(hook), ProjectHook) 72 self.assertEqual(hook.url, "http://example.com/hook") 73 74 @with_httmock(resp_get_project) 75 @with_httmock(resp_find_project_hook) 76 def test_update_hook(self): 77 project = self.gitlab_instance.projects.get(1) 78 hook = self.moduleUtil.findHook(project, "http://example.com/hook") 79 80 changed, newHook = self.moduleUtil.updateHook(hook, {"url": "http://gitlab.com/hook"}) 81 82 self.assertEqual(changed, True) 83 self.assertEqual(type(newHook), ProjectHook) 84 self.assertEqual(newHook.url, "http://gitlab.com/hook") 85 86 changed, newHook = self.moduleUtil.updateHook(hook, {"url": "http://gitlab.com/hook"}) 87 88 self.assertEqual(changed, False) 89 self.assertEqual(newHook.url, "http://gitlab.com/hook") 90 91 @with_httmock(resp_get_project) 92 @with_httmock(resp_find_project_hook) 93 @with_httmock(resp_delete_project_hook) 94 def test_delete_hook(self): 95 project = self.gitlab_instance.projects.get(1) 96 97 self.moduleUtil.existsHook(project, "http://example.com/hook") 98 99 rvalue = self.moduleUtil.deleteHook() 100 101 self.assertEqual(rvalue, None) 102