diff --git a/requirements-test.txt b/requirements-test.txt --- a/requirements-test.txt +++ b/requirements-test.txt @@ -3,6 +3,7 @@ django-stubs django-test-migrations hypothesis +iso8601 pytest pytest-django pytest-mock diff --git a/swh/web/common/identifiers.py b/swh/web/common/identifiers.py --- a/swh/web/common/identifiers.py +++ b/swh/web/common/identifiers.py @@ -109,13 +109,15 @@ object_id = swhid_parsed.object_id browse_url = None url_args = {} - query_dict = QueryDict("", mutable=True) + query_dict: QueryDict = QueryDict("", mutable=True) fragment = "" process_lines = object_type == ObjectType.CONTENT if query_params and len(query_params) > 0: for k in sorted(query_params.keys()): - query_dict[k] = query_params[k] + value = query_params[k] + assert isinstance(value, str) + query_dict[k] = value if swhid_parsed.origin: origin_url = unquote(swhid_parsed.origin) @@ -149,15 +151,19 @@ # their path prefixed by root directory id for breadcrumbs display query_dict["path"] = hash_to_hex(directory) + query_dict["path"] else: + path_ = query_dict["path"] + assert isinstance(path_, str) # remove leading slash from SWHID content path - query_dict["path"] = query_dict["path"][1:] + query_dict["path"] = path_[1:] elif object_type == ObjectType.DIRECTORY: object_id = directory # remove leading and trailing slashes from SWHID directory path - if query_dict["path"].endswith("/"): - query_dict["path"] = query_dict["path"][1:-1] + path_ = query_dict["path"] + assert isinstance(path_, str) + if path_.endswith("/"): + query_dict["path"] = path_[1:-1] else: - query_dict["path"] = query_dict["path"][1:] + query_dict["path"] = path_[1:] # snapshot context if swhid_parsed.visit: diff --git a/swh/web/common/utils.py b/swh/web/common/utils.py --- a/swh/web/common/utils.py +++ b/swh/web/common/utils.py @@ -96,13 +96,14 @@ ) if query_params: - query_params = {k: v for k, v in query_params.items() if v is not None} - - if query_params and len(query_params) > 0: - query_dict = QueryDict("", mutable=True) - for k in sorted(query_params.keys()): - query_dict[k] = query_params[k] - url += "?" + query_dict.urlencode(safe="/;:") + query_params_dict = {k: v for k, v in query_params.items() if v is not None} + if len(query_params_dict) > 0: + query_dict = QueryDict("", mutable=True) + for k in sorted(query_params_dict.keys()): + value = query_params_dict[k] + assert isinstance(value, str) + query_dict[k] = value + url += "?" + query_dict.urlencode(safe="/;:") if request is not None: url = request.build_absolute_uri(url) diff --git a/swh/web/tests/browse/views/test_snapshot.py b/swh/web/tests/browse/views/test_snapshot.py --- a/swh/web/tests/browse/views/test_snapshot.py +++ b/swh/web/tests/browse/views/test_snapshot.py @@ -1,4 +1,4 @@ -# Copyright (C) 2021 The Software Heritage developers +# Copyright (C) 2021-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -7,8 +7,8 @@ import re import string -from dateutil import parser from hypothesis import given +import iso8601 import pytest from django.utils.html import escape @@ -84,7 +84,8 @@ resp = check_html_get_response( client, url, status_code=200, template_used="includes/snapshot-context.html" ) - requested_time = parser.parse(visit["date"]).strftime("%d %B %Y, %H:%M") + iso8601.parametrize + requested_time = iso8601.parse_date(visit["date"]).strftime("%d %B %Y, %H:%M") assert_contains(resp, requested_time) assert_contains(resp, visit["origin"]) diff --git a/swh/web/tests/conftest.py b/swh/web/tests/conftest.py --- a/swh/web/tests/conftest.py +++ b/swh/web/tests/conftest.py @@ -21,7 +21,7 @@ from django.contrib.auth.models import User from django.core.cache import cache -from django.test.utils import setup_databases # type: ignore +from django.test.utils import setup_databases from rest_framework.test import APIClient, APIRequestFactory from swh.model.hashutil import ( diff --git a/swh/web/tests/utils.py b/swh/web/tests/utils.py --- a/swh/web/tests/utils.py +++ b/swh/web/tests/utils.py @@ -16,7 +16,7 @@ def _assert_http_response( - response: HttpResponse, status_code: int, content_type: str + response: Response, status_code: int, content_type: str ) -> HttpResponse: if isinstance(response, Response): @@ -57,13 +57,15 @@ Returns: The HTTP response """ + actual_response = client.get( + url, + HTTP_ACCEPT=content_type, + HTTP_ORIGIN=http_origin, + SERVER_NAME=server_name if server_name else "testserver", + ) + assert isinstance(actual_response, Response) return _assert_http_response( - response=client.get( - url, - HTTP_ACCEPT=content_type, - HTTP_ORIGIN=http_origin, - SERVER_NAME=server_name if server_name else "testserver", - ), + response=actual_response, status_code=status_code, content_type=content_type, ) @@ -91,14 +93,16 @@ Returns: The HTTP response """ + actual_response = client.post( + url, + data=data, + content_type=request_content_type, + HTTP_ACCEPT=content_type, + HTTP_ORIGIN=http_origin, + ) + assert isinstance(actual_response, Response) return _assert_http_response( - response=client.post( - url, - data=data, - content_type=request_content_type, - HTTP_ACCEPT=content_type, - HTTP_ORIGIN=http_origin, - ), + response=actual_response, status_code=status_code, content_type=content_type, )