File: test_cmds.py

package info (click to toggle)
pontos 25.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,744 kB
  • sloc: python: 44,602; makefile: 21; sh: 10; xml: 3
file content (290 lines) | stat: -rw-r--r-- 8,958 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# SPDX-FileCopyrightText: 2022-2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

# pylint: disable=no-member

from argparse import Namespace
from pathlib import Path
from unittest.mock import MagicMock, call, patch

from pontos.github.cmds import (
    create_pull_request,
    create_release,
    create_tag,
    file_status,
    labels,
    repos,
    update_pull_request,
)
from pontos.github.models.base import FileStatus
from pontos.github.models.organization import RepositoryType
from pontos.testing import temp_file
from tests import AsyncIteratorMock, IsolatedAsyncioTestCase

here = Path(__file__).parent


class TestCmds(IsolatedAsyncioTestCase):
    @patch("pontos.github.api.api.GitHubAsyncRESTPullRequests", spec=True)
    async def test_file_status(self, api_mock):
        terminal = MagicMock()
        api_mock.return_value.exists.return_value = True
        api_mock.return_value.files.return_value = {
            FileStatus.ADDED: [Path("tests/github/foo/bar")],
            FileStatus.MODIFIED: [
                Path("tests/github/bar/baz"),
                Path("tests/github/baz/boo"),
            ],
        }

        with (
            temp_file(name="some.file") as test_file,
            test_file.open("w", encoding="utf8") as output,
        ):
            args = Namespace(
                repo="foo/bar",
                pull_request=8,
                output=output,
                status=[FileStatus.ADDED, FileStatus.MODIFIED],
                token="GITHUB_TOKEN",
            )

            await file_status(terminal, args)

            api_mock.return_value.exists.assert_awaited_once_with(
                repo="foo/bar", pull_request=8
            )
            api_mock.return_value.files.assert_awaited_once_with(
                repo="foo/bar",
                pull_request=8,
                status_list=[FileStatus.ADDED, FileStatus.MODIFIED],
            )

            output.flush()

            content = test_file.read_text(encoding="utf-8")
            self.assertEqual(
                content, f"{here}/foo/bar\n{here}/bar/baz\n{here}/baz/boo\n"
            )

    @patch("pontos.github.api.api.GitHubAsyncRESTReleases", spec=True)
    async def test_create_release_no_tag(self, api_mock):
        terminal = MagicMock()
        api_mock.return_value.exists.return_value = True

        args = Namespace(
            repo="foo/bar",
            tag="test_tag",
            name="test_release",
            body=None,
            target_commitish=None,
            draft=False,
            prerelease=False,
            token="GITHUB_TOKEN",
        )

        with self.assertRaises(SystemExit) as syse:
            await create_release(terminal, args)

        self.assertEqual(syse.exception.code, 1)

        api_mock.return_value.exists.assert_awaited_once_with(
            repo="foo/bar", tag="test_tag"
        )

    @patch("pontos.github.api.api.GitHubAsyncRESTReleases", spec=True)
    async def test_create_release(self, api_mock):
        terminal = MagicMock()
        api_mock.return_value.exists.return_value = False
        api_mock.return_value.create.return_value = True

        args = Namespace(
            repo="foo/bar",
            tag="test_tag",
            name="test_release",
            body=None,
            target_commitish=None,
            draft=False,
            prerelease=False,
            token="GITHUB_TOKEN",
        )

        await create_release(terminal, args)

        api_mock.return_value.exists.assert_awaited_once_with(
            repo="foo/bar", tag="test_tag"
        )
        api_mock.return_value.create.assert_awaited_once_with(
            repo="foo/bar",
            tag="test_tag",
            body=None,
            name="test_release",
            target_commitish=None,
            draft=False,
            prerelease=False,
        )

    @patch("pontos.github.api.api.GitHubAsyncRESTTags", spec=True)
    async def test_create_tag(self, api_mock):
        terminal = MagicMock()
        api_mock.return_value.create.return_value = MagicMock(sha="123")
        api_mock.return_value.create_tag_reference.return_value = True

        args = Namespace(
            repo="foo/bar",
            tag="test_tag",
            name="test_release",
            message="test msg",
            git_object="commit",
            git_object_type=None,
            email="test@test.test",
            date=None,
            token="GITHUB_TOKEN",
        )

        await create_tag(terminal, args)

        api_mock.return_value.create.assert_awaited_once_with(
            repo="foo/bar",
            tag="test_tag",
            message="test msg",
            git_object="commit",
            name="test_release",
            email="test@test.test",
            git_object_type=None,
            date=None,
        )
        api_mock.return_value.create_tag_reference.assert_awaited_once_with(
            repo="foo/bar", tag="test_tag", sha="123"
        )

    @patch("pontos.github.api.api.GitHubAsyncRESTBranches", spec=True)
    @patch("pontos.github.api.api.GitHubAsyncRESTPullRequests", spec=True)
    async def test_create_pull_request(
        self,
        pulls_api_mock,
        branches_api_mock,
    ):
        terminal = MagicMock()
        branches_api_mock.return_value.exists.return_value = [False, False]
        pulls_api_mock.return_value.create.return_value = True

        args = Namespace(
            repo="foo/bar",
            head="some-branch",
            target="main",
            title="foo",
            body="foo bar",
            token="GITHUB_TOKEN",
        )

        await create_pull_request(terminal, args)

        branches_api_mock.return_value.exists.assert_has_awaits(
            [
                call(repo="foo/bar", branch="some-branch"),
                call(repo="foo/bar", branch="main"),
            ]
        )

        pulls_api_mock.return_value.create.assert_awaited_once_with(
            repo="foo/bar",
            head_branch="some-branch",
            base_branch="main",
            title="foo",
            body="foo bar",
        )

    @patch("pontos.github.api.api.GitHubAsyncRESTBranches", spec=True)
    @patch("pontos.github.api.api.GitHubAsyncRESTPullRequests", spec=True)
    async def test_update_pull_request(
        self,
        pulls_api_mock,
        branches_api_mock,
    ):
        terminal = MagicMock()
        branches_api_mock.return_value.exists.return_value = True
        pulls_api_mock.return_value.create.return_value = True

        args = Namespace(
            repo="foo/bar",
            target="main",
            pull_request=9,
            title="foo",
            body="foo bar",
            token="GITHUB_TOKEN",
        )

        await update_pull_request(terminal, args)

        branches_api_mock.return_value.exists.assert_awaited_once_with(
            repo="foo/bar", branch="main"
        )

        pulls_api_mock.return_value.update.assert_awaited_once_with(
            repo="foo/bar",
            pull_request=9,
            base_branch="main",
            title="foo",
            body="foo bar",
        )

    @patch("pontos.github.api.api.GitHubAsyncRESTPullRequests", spec=True)
    @patch("pontos.github.api.api.GitHubAsyncRESTLabels", spec=True)
    async def test_labels(
        self,
        labels_api_mock,
        pulls_api_mock,
    ):
        terminal = MagicMock()
        pulls_api_mock.return_value.exists.return_value = True
        labels_api_mock.return_value.get_all.return_value = AsyncIteratorMock(
            ["foo", "bar"]
        )

        args = Namespace(
            repo="foo/bar",
            issue=9,
            labels=["baz"],
            token="GITHUB_TOKEN",
        )

        await labels(terminal, args)

        pulls_api_mock.return_value.exists.assert_awaited_once_with(
            repo="foo/bar", pull_request=9
        )

        labels_api_mock.return_value.get_all.assert_called_once_with(
            repo="foo/bar",
            issue=9,
        )

        labels_api_mock.return_value.set_all.assert_awaited_once_with(
            repo="foo/bar", issue=9, labels=["foo", "bar", "baz"]
        )

    @patch("pontos.github.api.api.GitHubAsyncRESTOrganizations", spec=True)
    async def test_repos(self, api_mock):
        terminal = MagicMock()
        api_mock.return_value.exists.return_value = True
        api_mock.return_value.get_repositories.return_value = AsyncIteratorMock(
            ["repo1", "repo2"]
        )

        args = Namespace(
            orga="foo",
            repo="bar",
            path=None,
            type=RepositoryType.PUBLIC,
            token="GITHUB_TOKEN",
        )

        await repos(terminal, args)

        api_mock.return_value.exists.assert_awaited_once_with("foo")
        api_mock.return_value.get_repositories.assert_called_once_with(
            organization="foo", repository_type=RepositoryType.PUBLIC
        )