File: test_client.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 (279 lines) | stat: -rw-r--r-- 9,000 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
# SPDX-FileCopyrightText: 2022-2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

# pylint: disable=arguments-differ,redefined-builtin

from unittest.mock import MagicMock, call, patch

from pontos.github.api.client import (
    DEFAULT_ACCEPT_HEADER,
    GITHUB_API_VERSION,
    GitHubAsyncRESTClient,
)
from pontos.github.api.helper import DEFAULT_GITHUB_API_URL
from tests import AsyncMock, IsolatedAsyncioTestCase, aiter, anext


class GitHubAsyncRESTClientTestCase(IsolatedAsyncioTestCase):
    @patch("pontos.github.api.client.httpx.AsyncClient")
    def setUp(self, async_client: MagicMock) -> None:
        self.http_client = AsyncMock()
        async_client.return_value = self.http_client
        self.client = GitHubAsyncRESTClient("token")

    async def test_get(self):
        await self.client.get("/foo/bar")

        self.http_client.get.assert_awaited_once_with(
            f"{DEFAULT_GITHUB_API_URL}/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            params=None,
            follow_redirects=True,
        )

    async def test_get_url(self):
        await self.client.get("https://github.com/foo/bar")

        self.http_client.get.assert_awaited_once_with(
            "https://github.com/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            params=None,
            follow_redirects=True,
        )

    async def test_get_all(self):
        url = "https://foo.bar"
        response1 = MagicMock(links={"next": {"url": url}})
        response2 = MagicMock(links=None)

        self.http_client.get.side_effect = [
            response1,
            response2,
        ]
        it = aiter(self.client.get_all("/foo/bar"))

        await anext(it)
        await anext(it)

        with self.assertRaises(StopAsyncIteration):
            await anext(it)

        self.http_client.get.assert_has_awaits(
            [
                call(
                    f"{DEFAULT_GITHUB_API_URL}/foo/bar",
                    headers={
                        "Accept": DEFAULT_ACCEPT_HEADER,
                        "Authorization": "token token",
                        "X-GitHub-Api-Version": GITHUB_API_VERSION,
                    },
                    params=None,
                    follow_redirects=True,
                ),
                call(
                    f"{url}",
                    headers={
                        "Accept": DEFAULT_ACCEPT_HEADER,
                        "Authorization": "token token",
                        "X-GitHub-Api-Version": GITHUB_API_VERSION,
                    },
                    params=None,
                    follow_redirects=True,
                ),
            ]
        )

    async def test_delete(self):
        await self.client.delete("/foo/bar")

        self.http_client.delete.assert_awaited_once_with(
            f"{DEFAULT_GITHUB_API_URL}/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            params=None,
        )

    async def test_delete_url(self):
        await self.client.delete("https://github.com/foo/bar")

        self.http_client.delete.assert_awaited_once_with(
            "https://github.com/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            params=None,
        )

    async def test_post(self):
        await self.client.post("/foo/bar", data={"foo": "bar"})

        self.http_client.post.assert_awaited_once_with(
            f"{DEFAULT_GITHUB_API_URL}/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            json={"foo": "bar"},
            params=None,
            content=None,
        )

    async def test_post_url(self):
        await self.client.post(
            "https://github.com/foo/bar", data={"foo": "bar"}
        )

        self.http_client.post.assert_awaited_once_with(
            "https://github.com/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            json={"foo": "bar"},
            params=None,
            content=None,
        )

    async def test_post_with_content_length(self):
        await self.client.post(
            "/foo/bar", data={"foo": "bar"}, content_length=123
        )

        self.http_client.post.assert_awaited_once_with(
            f"{DEFAULT_GITHUB_API_URL}/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
                "Content-Length": "123",
            },
            json={"foo": "bar"},
            params=None,
            content=None,
        )

    async def test_put(self):
        await self.client.put("/foo/bar", data={"foo": "bar"})

        self.http_client.put.assert_awaited_once_with(
            f"{DEFAULT_GITHUB_API_URL}/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            json={"foo": "bar"},
            params=None,
            content=None,
        )

    async def test_put_url(self):
        await self.client.put("https://github.com/foo/bar", data={"foo": "bar"})

        self.http_client.put.assert_awaited_once_with(
            "https://github.com/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            json={"foo": "bar"},
            params=None,
            content=None,
        )

    async def test_patch(self):
        await self.client.patch("/foo/bar", data={"foo": "bar"})

        self.http_client.patch.assert_awaited_once_with(
            f"{DEFAULT_GITHUB_API_URL}/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            json={"foo": "bar"},
            params=None,
            content=None,
        )

    async def test_patch_url(self):
        await self.client.patch(
            "https://github.com/foo/bar", data={"foo": "bar"}
        )

        self.http_client.patch.assert_awaited_once_with(
            "https://github.com/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            json={"foo": "bar"},
            params=None,
            content=None,
        )

    async def test_stream(self):
        response = MagicMock()
        response.__aenter__.return_value = MagicMock()
        self.http_client.stream = MagicMock()
        self.http_client.stream.return_value = response

        async with self.client.stream("/foo/bar"):
            pass

        self.http_client.stream.assert_called_once_with(
            "GET",
            f"{DEFAULT_GITHUB_API_URL}/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            follow_redirects=True,
        )

    async def test_stream_url(self):
        response = MagicMock()
        response.__aenter__.return_value = MagicMock()
        self.http_client.stream = MagicMock()
        self.http_client.stream.return_value = response

        async with self.client.stream("https://github.com/foo/bar"):
            pass

        self.http_client.stream.assert_called_once_with(
            "GET",
            "https://github.com/foo/bar",
            headers={
                "Accept": DEFAULT_ACCEPT_HEADER,
                "Authorization": "token token",
                "X-GitHub-Api-Version": GITHUB_API_VERSION,
            },
            follow_redirects=True,
        )

    async def test_context_manager(self):
        async with self.client:
            pass

        self.http_client.__aenter__.assert_awaited_once()
        self.http_client.__aexit__.assert_awaited_once()