Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/utils.py
# Copyright (C) 2020-2021 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 | ||||
▲ Show 20 Lines • Show All 180 Lines • ▼ Show 20 Lines | ) -> Response: | ||||
check_api_post_response( | check_api_post_response( | ||||
api_client, url, status_code, content_type="application/yaml", data=data | api_client, url, status_code, content_type="application/yaml", data=data | ||||
) | ) | ||||
return cast(Response, response_json) | return cast(Response, response_json) | ||||
def check_html_get_response( | def check_html_get_response( | ||||
client: Client, url: str, status_code: int, template_used: Optional[str] = None | client: Client, | ||||
url: str, | |||||
status_code: int, | |||||
template_used: Optional[str] = None, | |||||
http_origin: Optional[str] = None, | |||||
server_name: Optional[str] = None, | |||||
) -> HttpResponse: | ) -> HttpResponse: | ||||
"""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 | ||||
Returns: | Returns: | ||||
The HTML response | The HTML response | ||||
""" | """ | ||||
response = check_http_get_response( | response = check_http_get_response( | ||||
client, url, status_code, content_type="text/html" | client, | ||||
url, | |||||
status_code, | |||||
content_type="text/html", | |||||
http_origin=http_origin, | |||||
server_name=server_name, | |||||
) | ) | ||||
if template_used is not None: | if template_used is not None: | ||||
assert_template_used(response, template_used) | assert_template_used(response, template_used) | ||||
return response | return response | ||||
def create_django_permission(perm_name: str) -> Permission: | def create_django_permission(perm_name: str) -> Permission: | ||||
"""Create permission out of a permission name string | """Create permission out of a permission name string | ||||
Show All 24 Lines |