Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/utils.py
# Copyright (C) 2020-2022 The Software Heritage developers | # Copyright (C) 2020-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 | ||||
from typing import Any, Dict, Optional, cast | from typing import Any, Dict, Optional, cast | ||||
from django.contrib.auth.models import Permission | from django.contrib.auth.models import Permission | ||||
from django.contrib.contenttypes.models import ContentType | from django.contrib.contenttypes.models import ContentType | ||||
from django.http import HttpResponse, StreamingHttpResponse | from django.http.response import HttpResponse, HttpResponseBase, StreamingHttpResponse | ||||
from django.test.client import Client | from django.test.client import Client | ||||
from rest_framework.response import Response | from rest_framework.response import Response | ||||
from rest_framework.test import APIClient | from rest_framework.test import APIClient | ||||
from swh.web.tests.django_asserts import assert_template_used | from swh.web.tests.django_asserts import assert_template_used | ||||
def _assert_http_response( | def _assert_http_response( | ||||
response: HttpResponse, status_code: int, content_type: str | response: HttpResponseBase, status_code: int, content_type: str | ||||
) -> HttpResponse: | ) -> HttpResponseBase: | ||||
if isinstance(response, Response): | if isinstance(response, Response): | ||||
drf_response = cast(Response, response) | drf_response = cast(Response, response) | ||||
error_context = ( | error_context = ( | ||||
drf_response.data.pop("traceback") | drf_response.data.pop("traceback") | ||||
if isinstance(drf_response.data, dict) and "traceback" in drf_response.data | if isinstance(drf_response.data, dict) and "traceback" in drf_response.data | ||||
else drf_response.data | else drf_response.data | ||||
) | ) | ||||
elif isinstance(response, StreamingHttpResponse): | elif isinstance(response, StreamingHttpResponse): | ||||
error_context = getattr(response, "traceback", response.streaming_content) | error_context = getattr(response, "traceback", response.streaming_content) | ||||
else: | elif isinstance(response, HttpResponse): | ||||
error_context = getattr(response, "traceback", response.content) | error_context = getattr(response, "traceback", response.content) | ||||
assert response.status_code == status_code, error_context | assert response.status_code == status_code, error_context | ||||
if content_type != "*/*": | if content_type != "*/*": | ||||
assert response["Content-Type"].startswith(content_type) | assert response["Content-Type"].startswith(content_type) | ||||
return response | return response | ||||
def check_http_get_response( | def check_http_get_response( | ||||
client: Client, | client: Client, | ||||
url: str, | url: str, | ||||
status_code: int, | status_code: int, | ||||
content_type: str = "*/*", | content_type: str = "*/*", | ||||
http_origin: Optional[str] = None, | http_origin: Optional[str] = None, | ||||
server_name: Optional[str] = None, | server_name: Optional[str] = None, | ||||
) -> HttpResponse: | ) -> HttpResponseBase: | ||||
"""Helper function to check HTTP response for a GET request. | """Helper function to check HTTP response for a GET request. | ||||
Args: | Args: | ||||
client: Django test client | client: Django test client | ||||
url: URL to check response | url: URL to check response | ||||
status_code: expected HTTP status code | status_code: expected HTTP status code | ||||
content_type: expected response content type | content_type: expected response content type | ||||
http_origin: optional HTTP_ORIGIN header value | http_origin: optional HTTP_ORIGIN header value | ||||
Show All 16 Lines | |||||
def check_http_post_response( | def check_http_post_response( | ||||
client: Client, | client: Client, | ||||
url: str, | url: str, | ||||
status_code: int, | status_code: int, | ||||
content_type: str = "*/*", | content_type: str = "*/*", | ||||
request_content_type="application/json", | request_content_type="application/json", | ||||
data: Optional[Dict[str, Any]] = None, | data: Optional[Dict[str, Any]] = None, | ||||
http_origin: Optional[str] = None, | http_origin: Optional[str] = None, | ||||
) -> HttpResponse: | ) -> HttpResponseBase: | ||||
"""Helper function to check HTTP response for a POST request. | """Helper function to check HTTP response for a POST request. | ||||
Args: | Args: | ||||
client: Django test client | client: Django test client | ||||
url: URL to check response | url: URL to check response | ||||
status_code: expected HTTP status code | status_code: expected HTTP status code | ||||
content_type: expected response content type | content_type: expected response content type | ||||
request_content_type: content type of request body | request_content_type: content type of request body | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | |||||
def check_api_post_response( | def check_api_post_response( | ||||
api_client: APIClient, | api_client: APIClient, | ||||
url: str, | url: str, | ||||
status_code: int, | status_code: int, | ||||
content_type: str = "*/*", | content_type: str = "*/*", | ||||
data: Optional[Dict[str, Any]] = None, | data: Optional[Dict[str, Any]] = None, | ||||
) -> HttpResponse: | ) -> HttpResponseBase: | ||||
"""Helper function to check Web API response for a POST request | """Helper function to check Web API response for a POST request | ||||
for all accepted content types. | for all accepted content types. | ||||
Args: | Args: | ||||
api_client: DRF test client | api_client: DRF test client | ||||
url: Web API URL to check response | url: Web API URL to check response | ||||
status_code: expected HTTP status code | status_code: expected HTTP status code | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
def check_html_get_response( | def check_html_get_response( | ||||
client: Client, | client: Client, | ||||
url: str, | url: str, | ||||
status_code: int, | status_code: int, | ||||
template_used: Optional[str] = None, | template_used: Optional[str] = None, | ||||
http_origin: Optional[str] = None, | http_origin: Optional[str] = None, | ||||
server_name: Optional[str] = None, | server_name: Optional[str] = None, | ||||
) -> HttpResponse: | ) -> HttpResponseBase: | ||||
"""Helper function to check HTML responses for a GET request. | """Helper function to check HTML responses for a GET request. | ||||
Args: | Args: | ||||
client: Django test client | client: Django test client | ||||
url: URL to check responses | url: URL to check responses | ||||
status_code: expected HTTP status code | status_code: expected HTTP status code | ||||
template_used: optional used Django template to check | template_used: optional used Django template to check | ||||
▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines |