Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/utils.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020-2021 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.contenttypes.models import ContentType | |||||
from django.http import HttpResponse, StreamingHttpResponse | from django.http import HttpResponse, 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 | ||||
▲ Show 20 Lines • Show All 186 Lines • ▼ Show 20 Lines | 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" | ||||
) | ) | ||||
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 | ||||
anlambert: `create_django_permission` is a better name imho | |||||
def create_django_permission(perm_name: str) -> Permission: | |||||
"""Create permission out of a permission name string | |||||
Args: | |||||
perm_name: Permission name (e.g. swh.web.api.throttling_exempted, | |||||
swh.ambassador, ...) | |||||
Returns: | |||||
The persisted permission | |||||
""" | |||||
perm_splitted = perm_name.split(".") | |||||
app_label = ".".join(perm_splitted[:-1]) | |||||
perm_name = perm_splitted[-1] | |||||
content_type = ContentType.objects.create(app_label=app_label, model="dummy") | |||||
return Permission.objects.create( | |||||
codename=perm_name, name=perm_name, content_type=content_type, | |||||
) |
create_django_permission is a better name imho