mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-04 07:24:00 -07:00
Some checks failed
EOL CI / EOL Sanity (Ⓐ2.16) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py2.7) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py3.11) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py3.6) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/3/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/3/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/3/) (push) Has been cancelled
nox / Run extra sanity tests (push) Has been cancelled
* gitlab_protected_branch: fix typo * gitlab_protected_branch: lump parameters into options dictionary Hardcoding parameter lists gets repetitive. Refactor this module to use an options dictionary like many other gitlab_* modules. This makes it cleaner to add new options. * gitlab_protected_branch: update when possible Until now, the module deletes and re-creates the protected branch if any change is detected. This makes sense for the access level parameters, as these are not easily mutated after creation. However, in order to add further options which _can_ easily be updated, we should support updating by default, unless known-immutable parameters are changing. * gitlab_protected_branch: add `allow_force_push` option * gitlab_protected_branch: add `code_owner_approval_required` option * gitlab_protected_branch: add issues to changelog * Update changelog. --------- Co-authored-by: Felix Fontein <felix@fontein.de>
115 lines
4.5 KiB
Python
115 lines
4.5 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.module_utils.version import LooseVersion
|
|
|
|
from ansible_collections.community.general.plugins.modules.gitlab_protected_branch import GitlabProtectedBranch
|
|
|
|
|
|
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, python_gitlab_module_version,
|
|
python_gitlab_version_match_requirement,
|
|
resp_get_protected_branch, resp_get_project_by_name,
|
|
resp_get_protected_branch_not_exist,
|
|
resp_delete_protected_branch, resp_get_user)
|
|
|
|
# GitLab module requirements
|
|
if python_version_match_requirement():
|
|
from gitlab.v4.objects import Project # noqa: F401, pylint: disable=unused-import
|
|
gitlab_req_version = python_gitlab_version_match_requirement()
|
|
gitlab_module_version = python_gitlab_module_version()
|
|
if LooseVersion(gitlab_module_version) < LooseVersion(gitlab_req_version):
|
|
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing (Wrong version)"))
|
|
except ImportError:
|
|
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
|
|
|
|
# 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 MockProtectedBranch():
|
|
def __init__(self, merge_access_levels, push_access_levels):
|
|
self.merge_access_levels = merge_access_levels
|
|
self.push_access_levels = push_access_levels
|
|
|
|
|
|
class TestGitlabProtectedBranch(GitlabModuleTestCase):
|
|
@with_httmock(resp_get_project_by_name)
|
|
@with_httmock(resp_get_user)
|
|
def setUp(self):
|
|
super(TestGitlabProtectedBranch, self).setUp()
|
|
|
|
self.gitlab_instance.user = self.gitlab_instance.users.get(1)
|
|
self.moduleUtil = GitlabProtectedBranch(module=self.mock_module, project="foo-bar/diaspora-client", gitlab_instance=self.gitlab_instance)
|
|
|
|
@with_httmock(resp_get_protected_branch)
|
|
def test_protected_branch_exist(self):
|
|
rvalue = self.moduleUtil.protected_branch_exist(name="master")
|
|
self.assertEqual(rvalue.name, "master")
|
|
|
|
@with_httmock(resp_get_protected_branch_not_exist)
|
|
def test_protected_branch_exist_not_exist(self):
|
|
rvalue = self.moduleUtil.protected_branch_exist(name="master")
|
|
self.assertEqual(rvalue, False)
|
|
|
|
def test_can_update_zero_delta(self):
|
|
protected_branch = MockProtectedBranch(
|
|
merge_access_levels=[{"access_level": 40}],
|
|
push_access_levels=[{"access_level": 40}],
|
|
)
|
|
options = {
|
|
"merge_access_levels": 40,
|
|
"push_access_level": 40
|
|
}
|
|
rvalue = self.moduleUtil.can_update(protected_branch, options)
|
|
self.assertEqual(rvalue, True)
|
|
|
|
def test_can_update_no_configured(self):
|
|
protected_branch = MockProtectedBranch(
|
|
merge_access_levels=[{"access_level": 40}],
|
|
push_access_levels=[{"access_level": 40}],
|
|
)
|
|
options = {
|
|
"merge_access_levels": None,
|
|
"push_access_level": None
|
|
}
|
|
rvalue = self.moduleUtil.can_update(protected_branch, options)
|
|
self.assertEqual(rvalue, True)
|
|
|
|
def test_can_update_different_settings(self):
|
|
protected_branch = MockProtectedBranch(
|
|
merge_access_levels=[{"access_level": 40}],
|
|
push_access_levels=[{"access_level": 40}],
|
|
)
|
|
options = {
|
|
"merge_access_levels": 40,
|
|
"push_access_level": 30
|
|
}
|
|
rvalue = self.moduleUtil.can_update(protected_branch, options)
|
|
self.assertEqual(rvalue, False)
|
|
|
|
@with_httmock(resp_get_protected_branch)
|
|
@with_httmock(resp_delete_protected_branch)
|
|
def test_delete_protected_branch(self):
|
|
rvalue = self.moduleUtil.delete_protected_branch(name="master")
|
|
self.assertEqual(rvalue, None)
|