Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_raw.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import hashlib | import hashlib | ||||
import pytest | |||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.web.api.throttling import SwhWebUserRateThrottle | |||||
from swh.web.auth.utils import API_RAW_OBJECT_PERMISSION | |||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.settings.tests import api_raw_object_rate | |||||
from swh.web.tests.utils import ( | from swh.web.tests.utils import ( | ||||
check_api_get_responses, | check_api_get_responses, | ||||
check_http_get_response, | check_http_get_response, | ||||
create_django_permission, | |||||
) | ) | ||||
@pytest.fixture | |||||
def privileged_user(regular_user): | |||||
regular_user.user_permissions.add( | |||||
create_django_permission(API_RAW_OBJECT_PERMISSION) | |||||
) | |||||
return regular_user | |||||
def test_api_raw_not_found(api_client, unknown_core_swhid): | def test_api_raw_not_found(api_client, unknown_core_swhid): | ||||
url = reverse("api-1-raw-object", url_args={"swhid": str(unknown_core_swhid)}) | url = reverse("api-1-raw-object", url_args={"swhid": str(unknown_core_swhid)}) | ||||
rv = check_api_get_responses(api_client, url, status_code=404) | rv = check_api_get_responses(api_client, url, status_code=404) | ||||
assert rv.data == { | assert rv.data == { | ||||
"exception": "NotFoundExc", | "exception": "NotFoundExc", | ||||
"reason": f"Object with id {unknown_core_swhid} not found.", | "reason": f"Object with id {unknown_core_swhid} not found.", | ||||
} | } | ||||
Show All 27 Lines | |||||
def test_api_raw_release(api_client, archive_data, release): | def test_api_raw_release(api_client, archive_data, release): | ||||
_test_api_raw_hash(api_client, archive_data, release, "rel") | _test_api_raw_hash(api_client, archive_data, release, "rel") | ||||
def test_api_raw_snapshot(api_client, archive_data, snapshot): | def test_api_raw_snapshot(api_client, archive_data, snapshot): | ||||
_test_api_raw_hash(api_client, archive_data, snapshot, "snp") | _test_api_raw_hash(api_client, archive_data, snapshot, "snp") | ||||
def test_api_raw_rate_limit(api_client, revision): | |||||
url = reverse( | |||||
"api-1-raw-object", | |||||
url_args={"swhid": f"swh:1:rev:{revision}"}, | |||||
) | |||||
for _ in range(api_raw_object_rate): | |||||
check_http_get_response(api_client, url, status_code=200) | |||||
check_http_get_response(api_client, url, status_code=429) | |||||
@pytest.mark.django_db | |||||
def test_api_raw_no_rate_limit_for_privileged_user( | |||||
api_client, revision, privileged_user | |||||
): | |||||
api_client.force_login(privileged_user) | |||||
url = reverse( | |||||
"api-1-raw-object", | |||||
url_args={"swhid": f"swh:1:rev:{revision}"}, | |||||
) | |||||
for _ in range(api_raw_object_rate * SwhWebUserRateThrottle.NUM_REQUESTS_FACTOR): | |||||
check_http_get_response(api_client, url, status_code=200) | |||||
check_http_get_response(api_client, url, status_code=200) |