|
| 1 | +# SPDX-FileCopyrightText: 2026 Andrew Zhang <whisper67265@outlook.com> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: BSL-1.0 |
| 4 | + |
| 5 | +"""P2 integration auth tests. |
| 6 | +
|
| 7 | +Verifies authentication and authorization behavior across all |
| 8 | +Boost endpoint routes: |
| 9 | +- Valid token grants access to protected endpoints |
| 10 | +- Invalid/missing tokens are rejected |
| 11 | +- Unauthenticated endpoints remain accessible without a token |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from tests.integration.lib.http import http_get, http_json |
| 19 | + |
| 20 | +pytestmark = pytest.mark.integration |
| 21 | + |
| 22 | +_VALID_ADD_OR_UPDATE_BODY = { |
| 23 | + "organization": "test-org", |
| 24 | + "version": "test-1.0.0", |
| 25 | + "add_or_update": {"zh_Hans": ["test-submodule"]}, |
| 26 | +} |
| 27 | + |
| 28 | +_FAKE_TOKEN = "wlu_this_token_does_not_exist_in_weblate" |
| 29 | + |
| 30 | + |
| 31 | +class TestBoostEndpointAuth: |
| 32 | + """Authentication and authorization across all Boost endpoint routes.""" |
| 33 | + |
| 34 | + def test_valid_token_on_info(self, api_token: str) -> None: |
| 35 | + code, body = http_get("/boost-endpoint/info/", token=api_token) |
| 36 | + assert code == 200, f"expected 200: {code} {body}" |
| 37 | + assert isinstance(body, dict) |
| 38 | + assert "module" in body |
| 39 | + |
| 40 | + def test_valid_token_on_add_or_update(self, api_token: str) -> None: |
| 41 | + code, body = http_json( |
| 42 | + "POST", |
| 43 | + "/boost-endpoint/add-or-update/", |
| 44 | + token=api_token, |
| 45 | + body=_VALID_ADD_OR_UPDATE_BODY, |
| 46 | + ) |
| 47 | + assert code == 202, f"expected 202: {code} {body}" |
| 48 | + assert isinstance(body, dict) |
| 49 | + assert body.get("status") == "accepted" |
| 50 | + assert body.get("task_id") |
| 51 | + |
| 52 | + def test_invalid_token_rejected(self) -> None: |
| 53 | + code, _ = http_get("/boost-endpoint/info/", token=_FAKE_TOKEN) |
| 54 | + assert code in (401, 403), f"expected 401/403: {code}" |
| 55 | + |
| 56 | + def test_no_token_rejected(self) -> None: |
| 57 | + code, _ = http_get("/boost-endpoint/info/") |
| 58 | + assert code in (401, 403), f"expected 401/403: {code}" |
| 59 | + |
| 60 | + def test_invalid_token_on_add_or_update(self) -> None: |
| 61 | + code, _ = http_json( |
| 62 | + "POST", |
| 63 | + "/boost-endpoint/add-or-update/", |
| 64 | + token=_FAKE_TOKEN, |
| 65 | + body=_VALID_ADD_OR_UPDATE_BODY, |
| 66 | + ) |
| 67 | + assert code in (401, 403), f"expected 401/403: {code}" |
| 68 | + |
| 69 | + def test_no_token_on_add_or_update(self) -> None: |
| 70 | + code, _ = http_json( |
| 71 | + "POST", |
| 72 | + "/boost-endpoint/add-or-update/", |
| 73 | + body=_VALID_ADD_OR_UPDATE_BODY, |
| 74 | + ) |
| 75 | + assert code in (401, 403), f"expected 401/403: {code}" |
| 76 | + |
| 77 | + def test_ping_no_auth_required(self) -> None: |
| 78 | + code, body = http_get("/boost-endpoint/plugin-ping/") |
| 79 | + assert code == 200 |
| 80 | + assert body == "ok" or body == b"ok" |
0 commit comments