aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/tests/test_github_utils.py
blob: 7b41d2f6a2c1415eb16c966ac6ca8e67fb010dfb (plain) (blame)
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
import dataclasses
import datetime
import typing
import unittest
from unittest import mock

import django.test
import httpx
import jwt
import rest_framework.response
import rest_framework.test
from django.urls import reverse

from pydis_site import settings
from pydis_site.apps.api import github_utils


class GeneralUtilityTests(unittest.TestCase):
    """Test the utility methods which do not fit in another class."""

    def test_token_generation(self):
        """Test that the a valid JWT token is generated."""
        def encode(payload: dict, _: str, algorithm: str, *args, **kwargs) -> str:
            """
            Intercept the encode method.

            The result is encoded with an algorithm which does not require a PEM key, as it may
            not be available in testing environments.
            """
            self.assertEqual("RS256", algorithm, "The GitHub App JWT must be signed using RS256.")
            return original_encode(
                payload, "secret-encoding-key", *args, algorithm="HS256", **kwargs
            )

        original_encode = jwt.encode
        with mock.patch("jwt.encode", new=encode):
            token = github_utils.generate_token()
        decoded = jwt.decode(token, "secret-encoding-key", algorithms=["HS256"])

        delta = datetime.timedelta(minutes=10)
        self.assertAlmostEqual(decoded["exp"] - decoded["iat"], delta.total_seconds())
        then = datetime.datetime.now(tz=datetime.UTC) + delta
        self.assertLess(decoded["exp"], then.timestamp())


class CheckRunTests(unittest.TestCase):
    """Tests the check_run_status utility."""

    run_kwargs: typing.Mapping = {
        "name": "run_name",
        "head_sha": "sha",
        "status": "completed",
        "conclusion": "success",
        "created_at": datetime.datetime.now(tz=datetime.UTC).strftime(settings.GITHUB_TIMESTAMP_FORMAT),
        "artifacts_url": "url",
    }

    def test_completed_run(self):
        """Test that an already completed run returns the correct URL."""
        final_url = "some_url_string_1234"

        kwargs = dict(self.run_kwargs, artifacts_url=final_url)
        result = github_utils.check_run_status(github_utils.WorkflowRun(**kwargs))
        self.assertEqual(final_url, result)

    def test_pending_run(self):
        """Test that a pending run raises the proper exception."""
        kwargs = dict(self.run_kwargs, status="pending")
        with self.assertRaises(github_utils.RunPendingError):
            github_utils.check_run_status(github_utils.WorkflowRun(**kwargs))

    def test_timeout_error(self):
        """Test that a timeout is declared after a certain duration."""
        kwargs = dict(self.run_kwargs, status="pending")
        # Set the creation time to well before the MAX_RUN_TIME
        # to guarantee the right conclusion
        kwargs["created_at"] = (
            datetime.datetime.now(tz=datetime.UTC)
            - github_utils.MAX_RUN_TIME - datetime.timedelta(minutes=10)
        ).strftime(settings.GITHUB_TIMESTAMP_FORMAT)

        with self.assertRaises(github_utils.RunTimeoutError):
            github_utils.check_run_status(github_utils.WorkflowRun(**kwargs))

    def test_failed_run(self):
        """Test that a failed run raises the proper exception."""
        kwargs = dict(self.run_kwargs, conclusion="failed")
        with self.assertRaises(github_utils.ActionFailedError):
            github_utils.check_run_status(github_utils.WorkflowRun(**kwargs))


def get_response_authorize(_: httpx.Client, request: httpx.Request, **__) -> httpx.Response:
    """
    Helper method for the authorize tests.

    Requests are intercepted before being sent out, and the appropriate responses are returned.
    """
    path = request.url.path
    auth = request.headers.get("Authorization")

    if request.method == "GET":
        if path == "/app/installations":
            if auth == "bearer JWT initial token":
                return httpx.Response(200, request=request, json=[{
                    "account": {"login": "VALID_OWNER"},
                    "access_tokens_url": "https://example.com/ACCESS_TOKEN_URL"
                }])
            return httpx.Response(
                401, json={"error": "auth app/installations"}, request=request
            )

        elif path == "/installation/repositories":  # noqa: RET505
            if auth == "bearer app access token":
                return httpx.Response(200, request=request, json={
                    "repositories": [{
                        "name": "VALID_REPO"
                    }]
                })
        return httpx.Response(  # pragma: no cover
            401, json={"error": "auth installation/repositories"}, request=request
        )

    elif request.method == "POST":  # noqa: RET505
        if path == "/ACCESS_TOKEN_URL":
            if auth == "bearer JWT initial token":
                return httpx.Response(200, request=request, json={"token": "app access token"})
            return httpx.Response(401, json={"error": "auth access_token"}, request=request)  # pragma: no cover

    # Reaching this point means something has gone wrong
    return httpx.Response(500, request=request)  # pragma: no cover


@mock.patch("httpx.Client.send", new=get_response_authorize)
@mock.patch.object(github_utils, "generate_token", new=mock.Mock(return_value="JWT initial token"))
class AuthorizeTests(unittest.TestCase):
    """Test the authorize utility."""

    def test_invalid_apps_auth(self):
        """Test that an exception is raised if authorization was attempted with an invalid token."""
        with mock.patch.object(github_utils, "generate_token", return_value="Invalid token"):  # noqa: SIM117
            with self.assertRaises(httpx.HTTPStatusError) as error:
                github_utils.authorize("VALID_OWNER", "VALID_REPO")

        exception: httpx.HTTPStatusError = error.exception
        self.assertEqual(401, exception.response.status_code)
        self.assertEqual("auth app/installations", exception.response.json()["error"])

    def test_missing_repo(self):
        """Test that an exception is raised when the selected owner or repo are not available."""
        with self.assertRaises(github_utils.NotFoundError):
            github_utils.authorize("INVALID_OWNER", "VALID_REPO")
        with self.assertRaises(github_utils.NotFoundError):
            github_utils.authorize("VALID_OWNER", "INVALID_REPO")

    def test_valid_authorization(self):
        """Test that an accessible repository can be accessed."""
        client = github_utils.authorize("VALID_OWNER", "VALID_REPO")
        self.assertEqual("bearer app access token", client.headers.get("Authorization"))


class ArtifactFetcherTests(unittest.TestCase):
    """Test the get_artifact utility."""

    @staticmethod
    def get_response_get_artifact(request: httpx.Request, **_) -> httpx.Response:
        """
        Helper method for the get_artifact tests.

        Requests are intercepted before being sent out, and the appropriate responses are returned.
        """
        path = request.url.path

        if "force_error" in path:
            return httpx.Response(404, request=request)

        if request.method == "GET":
            if path == "/repos/owner/repo/actions/runs":
                run = github_utils.WorkflowRun(
                    name="action_name",
                    head_sha="action_sha",
                    created_at=(
                        datetime.datetime
                        .now(tz=datetime.UTC)
                        .strftime(settings.GITHUB_TIMESTAMP_FORMAT)
                    ),
                    status="completed",
                    conclusion="success",
                    artifacts_url="artifacts_url"
                )
                return httpx.Response(
                    200, request=request, json={"workflow_runs": [dataclasses.asdict(run)]}
                )
            elif path == "/artifact_url":  # noqa: RET505
                return httpx.Response(
                    200, request=request, json={"artifacts": [{
                        "name": "artifact_name",
                        "archive_download_url": "artifact_download_url"
                    }]}
                )
            elif path == "/artifact_download_url":
                response = httpx.Response(302, request=request)
                response.next_request = httpx.Request(
                    "GET",
                    httpx.URL("https://final_download.url")
                )
                return response

        # Reaching this point means something has gone wrong
        return httpx.Response(500, request=request)  # pragma: no cover

    def setUp(self) -> None:
        self.call_args = ["owner", "repo", "action_sha", "action_name", "artifact_name"]
        self.client = httpx.Client(base_url="https://example.com", timeout=5)

        self.patchers = [
            mock.patch.object(self.client, "send", new=self.get_response_get_artifact),
            mock.patch.object(github_utils, "authorize", return_value=self.client),
            mock.patch.object(github_utils, "check_run_status", return_value="artifact_url"),
        ]

        for patcher in self.patchers:
            patcher.start()

    def tearDown(self) -> None:
        for patcher in self.patchers:
            patcher.stop()

    def test_client_closed_on_errors(self):
        """Test that the client is terminated even if an error occurs at some point."""
        self.call_args[0] = "force_error"
        with self.assertRaises(httpx.HTTPStatusError):
            github_utils.get_artifact(*self.call_args)
        self.assertTrue(self.client.is_closed)

    def test_missing(self):
        """Test that an exception is raised if the requested artifact was not found."""
        cases = (
            "invalid sha",
            "invalid action name",
            "invalid artifact name",
        )
        for i, name in enumerate(cases, 2):
            with self.subTest(f"Test {name} raises an error"):
                new_args = self.call_args.copy()
                new_args[i] = name

                with self.assertRaises(github_utils.NotFoundError):
                    github_utils.get_artifact(*new_args)

    def test_valid(self):
        """Test that the correct download URL is returned for valid requests."""
        url = github_utils.get_artifact(*self.call_args)
        self.assertEqual("https://final_download.url", url)
        self.assertTrue(self.client.is_closed)


@mock.patch.object(github_utils, "get_artifact")
class GitHubArtifactViewTests(django.test.TestCase):
    """Test the GitHub artifact fetch API view."""

    def setUp(self):
        self.kwargs = {
            "owner": "test_owner",
            "repo": "test_repo",
            "sha": "test_sha",
            "action_name": "test_action",
            "artifact_name": "test_artifact",
        }
        self.url = reverse("api:github-artifacts", kwargs=self.kwargs)

    def test_correct_artifact(self, artifact_mock: mock.Mock):
        """Test a proper response is returned with proper input."""
        artifact_mock.return_value = "final download url"
        result = self.client.get(self.url)

        self.assertIsInstance(result, rest_framework.response.Response)
        self.assertEqual({"url": artifact_mock.return_value}, result.data)

    def test_failed_fetch(self, artifact_mock: mock.Mock):
        """Test that a proper error is returned when the request fails."""
        artifact_mock.side_effect = github_utils.NotFoundError("Test error message")
        result = self.client.get(self.url)

        self.assertIsInstance(result, rest_framework.response.Response)
        self.assertEqual({
            "error_type": github_utils.NotFoundError.__name__,
            "error": "Test error message",
            "requested_resource": "/".join(self.kwargs.values())
        }, result.data)