File: test_protected_branches.py

package info (click to toggle)
gitlabracadabra 2.8.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,520 kB
  • sloc: python: 12,305; javascript: 663; makefile: 4
file content (163 lines) | stat: -rw-r--r-- 6,646 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#
# Copyright (C) 2019-2025 Mathieu Parent <math.parent@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from unittest.mock import call

from gitlabracadabra.objects.project import GitLabracadabraProject
from gitlabracadabra.tests import my_vcr, patch
from gitlabracadabra.tests.case import TestCaseWithManager


class TestProtectedBranches(TestCaseWithManager):
    """Test ProtectedBranchesMixin."""

    @my_vcr.use_cassette
    def test_protected_branches_ce(self, cass):
        """Test new protected branch.

        Args:
            cass: VCR cassette.
        """
        project = GitLabracadabraProject(
            "memory",
            "test/protected-branches",
            {
                "create_object": True,
                "initialize_with_readme": True,
                "protected_branches": {
                    "main": {"push_access_level": "developer", "merge_access_level": "noone"},
                    "develop": {"push_access_level": "noone", "merge_access_level": "developer"},
                },
            },
        )
        with patch("gitlabracadabra.mixins.protected_branches.logger", autospec=True) as logger:
            assert project.errors() == []
            project.process()
            assert cass.all_played
            logger.assert_has_calls([])

    @my_vcr.use_cassette
    def test_protected_branches_delete(self, cass):
        """Test deleting unknown protected branch.

        Args:
            cass: VCR cassette.
        """
        project = GitLabracadabraProject(
            "memory",
            "test/protected-branches2",
            {
                "protected_branches": {},
                "unknown_protected_branches": "delete",
            },
        )
        with patch("gitlabracadabra.mixins.protected_branches.logger", autospec=True) as logger:
            assert project.errors() == []
            project.process()
            assert cass.all_played
            logger.info.assert_called_once_with(
                "[%s] Deleting unknown protected branch: %s",
                "test/protected-branches2",
                "main",
            )

    @my_vcr.use_cassette
    def test_protected_branches_ee(self, cass):
        """Test protected branch EE.

        Args:
            cass: VCR cassette.
        """
        project = GitLabracadabraProject(
            "memory",
            "gitlabracadabra/test-group/protected-branches",
            {
                "protected_branches": {
                    "main": {
                        "allowed_to_merge": [
                            {"role": "maintainer"},
                            {"user": "kubitus-bot"},
                            # {'group': 'some-group'},
                        ],
                        "allowed_to_push": [
                            {"role": "noone"},
                            {"user": "kubitus-bot"},
                            # {'group': 'some-group'},
                            # {'deploy_key': 'My Key'},
                        ],
                        "allow_force_push": True,
                        "code_owner_approval_required": True,
                    },
                    "develop": {
                        "allowed_to_merge": [
                            {"role": "developer"},
                            {"user": "kubitus-bot"},
                            # {'group': 'some-group'},
                        ],
                        "allowed_to_push": [
                            {"role": "developer"},
                            {"user": "kubitus-bot"},
                            # {'group': 'some-group'},
                            {"deploy_key": "My Key"},
                        ],
                        "allow_force_push": True,
                        "code_owner_approval_required": True,
                    },
                },
            },
        )
        with patch("gitlabracadabra.mixins.protected_branches.logger", autospec=True) as logger:
            assert project.errors() == []
            project.process()
            assert cass.all_played
            logger.assert_has_calls(
                [
                    call.info(
                        "[%s] Creating protected branch %s: %s",
                        "gitlabracadabra/test-group/protected-branches",
                        "develop",
                        (
                            '{"allow_force_push": true, '
                            '"allowed_to_merge": [{"access_level": 30}, {"user_id": 9280824}], '
                            '"allowed_to_push": [{"access_level": 30}, {"deploy_key_id": 15407951},'
                            ' {"user_id": 9280824}], '
                            '"code_owner_approval_required": true, '
                            '"name": "develop"}'
                        ),
                    ),
                    call.info(
                        "[%s] Changing protected branch %s: %s -> %s",
                        "gitlabracadabra/test-group/protected-branches",
                        "main",
                        (
                            '{"allow_force_push": false, '
                            '"allowed_to_merge": [{"access_level": 40}], '
                            '"allowed_to_push": [{"access_level": 40}], '
                            '"allowed_to_unprotect": [], '
                            '"code_owner_approval_required": false, '
                            '"name": "main"}'
                        ),
                        (
                            '{"allow_force_push": true, '
                            '"allowed_to_merge": [{"access_level": 40}, {"user_id": 9280824}], '
                            '"allowed_to_push": [{"access_level": 0}, {"user_id": 9280824}], '
                            '"allowed_to_unprotect": [], '
                            '"code_owner_approval_required": true, '
                            '"name": "main"}'
                        ),
                    ),
                ]
            )