1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/tests/unit/plugins/modules/test_gitlab_hook.py
Felix Fontein b531ecdc9b
Unflatmap community.general (#5461)
* Move files.

* Update imports and references.

* Move wrongly placed files.

* Reverse redirects, deprecate long → short name redirects.

* Simplify contribution guidelines for new modules.

* Rewrite BOTMETA.

* Add changelog fragment.

* Fix ignore.txt files.
2022-11-02 20:42:29 +00:00

104 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
from ansible_collections.community.general.plugins.modules.gitlab_hook import GitLabHook
def _dummy(x):
"""Dummy function. Only used as a placeholder for toplevel definitions when the test is going
to be skipped anyway"""
return x
pytestmark = []
try:
from .gitlab import (GitlabModuleTestCase,
python_version_match_requirement,
resp_get_project, resp_find_project_hook,
resp_create_project_hook, resp_delete_project_hook)
# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import ProjectHook
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
resp_get_project = _dummy
resp_find_project_hook = _dummy
resp_create_project_hook = _dummy
resp_delete_project_hook = _dummy
# Unit tests requirements
try:
from httmock import with_httmock # noqa
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing"))
with_httmock = _dummy
class TestGitlabHook(GitlabModuleTestCase):
def setUp(self):
super(TestGitlabHook, self).setUp()
self.moduleUtil = GitLabHook(module=self.mock_module, gitlab_instance=self.gitlab_instance)
@with_httmock(resp_get_project)
@with_httmock(resp_find_project_hook)
def test_hook_exist(self):
project = self.gitlab_instance.projects.get(1)
rvalue = self.moduleUtil.exists_hook(project, "http://example.com/hook")
self.assertEqual(rvalue, True)
rvalue = self.moduleUtil.exists_hook(project, "http://gitlab.com/hook")
self.assertEqual(rvalue, False)
@with_httmock(resp_get_project)
@with_httmock(resp_create_project_hook)
def test_create_hook(self):
project = self.gitlab_instance.projects.get(1)
hook = self.moduleUtil.create_hook(project, {"url": "http://example.com/hook"})
self.assertEqual(type(hook), ProjectHook)
self.assertEqual(hook.url, "http://example.com/hook")
@with_httmock(resp_get_project)
@with_httmock(resp_find_project_hook)
def test_update_hook(self):
project = self.gitlab_instance.projects.get(1)
hook = self.moduleUtil.find_hook(project, "http://example.com/hook")
changed, newHook = self.moduleUtil.update_hook(hook, {"url": "http://gitlab.com/hook"})
self.assertEqual(changed, True)
self.assertEqual(type(newHook), ProjectHook)
self.assertEqual(newHook.url, "http://gitlab.com/hook")
changed, newHook = self.moduleUtil.update_hook(hook, {"url": "http://gitlab.com/hook"})
self.assertEqual(changed, False)
self.assertEqual(newHook.url, "http://gitlab.com/hook")
@with_httmock(resp_get_project)
@with_httmock(resp_find_project_hook)
@with_httmock(resp_delete_project_hook)
def test_delete_hook(self):
project = self.gitlab_instance.projects.get(1)
self.moduleUtil.exists_hook(project, "http://example.com/hook")
rvalue = self.moduleUtil.delete_hook()
self.assertEqual(rvalue, None)