Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_gitlab_protected_branch.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.module_utils._version import LooseVersion
10 from ansible_collections.community.general.plugins.modules.gitlab_protected_branch import GitlabProtectedBranch
11
12
13 def _dummy(x):
14 """Dummy function. Only used as a placeholder for toplevel definitions when the test is going
15 to be skipped anyway"""
16 return x
17
18
19 pytestmark = []
20 try:
21 # GitLab module requirements
22 from gitlab.v4.objects import Project # noqa: F401, pylint: disable=unused-import
23
24 from .gitlab import (
25 GitlabModuleTestCase,
26 python_gitlab_module_version,
27 python_gitlab_version_match_requirement,
28 resp_delete_protected_branch,
29 resp_get_project_by_name,
30 resp_get_protected_branch,
31 resp_get_protected_branch_not_exist,
32 resp_get_user,
33 )
34
35 gitlab_req_version = python_gitlab_version_match_requirement()
36 gitlab_module_version = python_gitlab_module_version()
37 if LooseVersion(gitlab_module_version) < LooseVersion(gitlab_req_version):
38 pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing (Wrong version)"))
39 except ImportError:
40 pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
41
42 # Unit tests requirements
43 try:
44 from httmock import with_httmock # noqa
45 except ImportError:
46 pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing"))
47 with_httmock = _dummy
48
49
50 class MockProtectedBranch:
51 def __init__(self, merge_access_levels, push_access_levels):
52 self.merge_access_levels = merge_access_levels
53 self.push_access_levels = push_access_levels
54
55
56 class TestGitlabProtectedBranch(GitlabModuleTestCase):
57 @with_httmock(resp_get_project_by_name)
58 @with_httmock(resp_get_user)
59 def setUp(self):
60 super().setUp()
61
62 self.gitlab_instance.user = self.gitlab_instance.users.get(1)
63 self.moduleUtil = GitlabProtectedBranch(
64 module=self.mock_module, project="foo-bar/diaspora-client", gitlab_instance=self.gitlab_instance
65 )
66
67 @with_httmock(resp_get_protected_branch)
68 def test_protected_branch_exist(self):
69 rvalue = self.moduleUtil.protected_branch_exist(name="master")
70 self.assertEqual(rvalue.name, "master")
71
72 @with_httmock(resp_get_protected_branch_not_exist)
73 def test_protected_branch_exist_not_exist(self):
74 rvalue = self.moduleUtil.protected_branch_exist(name="master")
75 self.assertEqual(rvalue, False)
76
77 def test_can_update_zero_delta(self):
78 protected_branch = MockProtectedBranch(
79 merge_access_levels=[{"access_level": 40}],
80 push_access_levels=[{"access_level": 40}],
81 )
82 options = {"merge_access_levels": 40, "push_access_level": 40}
83 rvalue = self.moduleUtil.can_update(protected_branch, options)
84 self.assertEqual(rvalue, True)
85
86 def test_can_update_no_configured(self):
87 protected_branch = MockProtectedBranch(
88 merge_access_levels=[{"access_level": 40}],
89 push_access_levels=[{"access_level": 40}],
90 )
91 options = {"merge_access_levels": None, "push_access_level": None}
92 rvalue = self.moduleUtil.can_update(protected_branch, options)
93 self.assertEqual(rvalue, True)
94
95 def test_can_update_different_settings(self):
96 protected_branch = MockProtectedBranch(
97 merge_access_levels=[{"access_level": 40}],
98 push_access_levels=[{"access_level": 40}],
99 )
100 options = {"merge_access_levels": 40, "push_access_level": 30}
101 rvalue = self.moduleUtil.can_update(protected_branch, options)
102 self.assertEqual(rvalue, False)
103
104 @with_httmock(resp_get_protected_branch)
105 @with_httmock(resp_delete_protected_branch)
106 def test_delete_protected_branch(self):
107 rvalue = self.moduleUtil.delete_protected_branch(name="master")
108 self.assertEqual(rvalue, None)