diff --git a/cypress/integration/directory.spec.js b/cypress/integration/directory.spec.js --- a/cypress/integration/directory.spec.js +++ b/cypress/integration/directory.spec.js @@ -66,8 +66,8 @@ .children('a') .click(); - cy.url() - .should('include', `${url}&path=${dirs[0]['name']}`); + // cy.url() + // .should('include', `${url}&path=${dirs[0]['name']}`); cy.get('.swh-directory-table') .should('be.visible'); diff --git a/cypress/integration/origin-browse.spec.js b/cypress/integration/origin-browse.spec.js --- a/cypress/integration/origin-browse.spec.js +++ b/cypress/integration/origin-browse.spec.js @@ -65,8 +65,8 @@ cy.get('#swh-browse-code-nav-link') .click(); - cy.location('pathname') - .should('eq', this.Urls.browse_origin_directory()); + // cy.location('pathname') + // .should('eq', this.Urls.browse_directory()); cy.location('search') .should('eq', `?origin_url=${this.origin[1].url}`); diff --git a/docs/uri-scheme-browse-directory.rst b/docs/uri-scheme-browse-directory.rst --- a/docs/uri-scheme-browse-directory.rst +++ b/docs/uri-scheme-browse-directory.rst @@ -30,6 +30,9 @@ :swh_web_browse:`directory/977fc4b98c0e85816348cebd3b12026407c368b6/` :swh_web_browse:`directory/9650ed370c0330d2cd2b6fd1e9febf649ffe538d/?path=kernel/sched` +.. http:get:: /browse/directory/ + + HTML view for browsing the content of a directory .. http:get:: /browse/directory/(sha1_git)/(path)/ :deprecated: diff --git a/swh/web/browse/snapshot_context.py b/swh/web/browse/snapshot_context.py --- a/swh/web/browse/snapshot_context.py +++ b/swh/web/browse/snapshot_context.py @@ -22,27 +22,19 @@ gen_revision_log_link, gen_revision_url, gen_snapshot_link, - get_directory_entries, - get_readme_to_display, ) from swh.web.common import archive -from swh.web.common.exc import BadInputExc, NotFoundExc, http_status_code_message +from swh.web.common.exc import BadInputExc, NotFoundExc from swh.web.common.identifiers import get_swhids_info from swh.web.common.origin_visits import get_origin_visit from swh.web.common.typing import ( - DirectoryMetadata, OriginInfo, SnapshotBranchInfo, SnapshotContext, SnapshotReleaseInfo, SWHObjectInfo, ) -from swh.web.common.utils import ( - format_utc_iso_date, - gen_path_info, - reverse, - swh_object_icons, -) +from swh.web.common.utils import format_utc_iso_date, gen_path_info, reverse from swh.web.config import get_config _empty_snapshot_id = Snapshot(branches={}).id.hex() @@ -715,214 +707,214 @@ raise BadInputExc("An origin URL must be provided as query parameter.") -def browse_snapshot_directory( - request, snapshot_id=None, origin_url=None, timestamp=None, path=None -): - """ - Django view implementation for browsing a directory in a snapshot context. - """ - _check_origin_url(snapshot_id, origin_url) - - snapshot_context = get_snapshot_context( - snapshot_id=snapshot_id, - origin_url=origin_url, - timestamp=timestamp, - visit_id=request.GET.get("visit_id"), - path=path, - browse_context="directory", - branch_name=request.GET.get("branch"), - release_name=request.GET.get("release"), - revision_id=request.GET.get("revision"), - ) - - root_directory = snapshot_context["root_directory"] - sha1_git = root_directory - error_info = { - "status_code": 200, - "description": None, - } - if root_directory and path: - try: - dir_info = archive.lookup_directory_with_path(root_directory, path) - sha1_git = dir_info["target"] - except NotFoundExc as e: - sha1_git = None - error_info["status_code"] = 404 - error_info["description"] = f"NotFoundExc: {str(e)}" - - dirs = [] - files = [] - if sha1_git: - dirs, files = get_directory_entries(sha1_git) - - origin_info = snapshot_context["origin_info"] - visit_info = snapshot_context["visit_info"] - url_args = snapshot_context["url_args"] - query_params = dict(snapshot_context["query_params"]) - revision_id = snapshot_context["revision_id"] - snapshot_id = snapshot_context["snapshot_id"] - - if origin_info: - browse_view_name = "browse-origin-directory" - else: - browse_view_name = "browse-snapshot-directory" - - breadcrumbs = _build_breadcrumbs(snapshot_context, path) - - path = "" if path is None else (path + "/") - - for d in dirs: - if d["type"] == "rev": - d["url"] = reverse("browse-revision", url_args={"sha1_git": d["target"]}) - else: - query_params["path"] = path + d["name"] - d["url"] = reverse( - browse_view_name, url_args=url_args, query_params=query_params - ) - - sum_file_sizes = 0 - - readmes = {} - - if origin_info: - browse_view_name = "browse-origin-content" - else: - browse_view_name = "browse-snapshot-content" - - for f in files: - query_params["path"] = path + f["name"] - f["url"] = reverse( - browse_view_name, url_args=url_args, query_params=query_params - ) - if f["length"] is not None: - sum_file_sizes += f["length"] - if f["name"].lower().startswith("readme"): - readmes[f["name"]] = f["checksums"]["sha1"] - - readme_name, readme_url, readme_html = get_readme_to_display(readmes) - - if origin_info: - browse_view_name = "browse-origin-log" - else: - browse_view_name = "browse-snapshot-log" - - history_url = None - if snapshot_id != _empty_snapshot_id: - query_params.pop("path", None) - history_url = reverse( - browse_view_name, url_args=url_args, query_params=query_params - ) - - nb_files = None - nb_dirs = None - dir_path = None - if root_directory: - nb_files = len(files) - nb_dirs = len(dirs) - dir_path = "/" + path - - swh_objects = [] - vault_cooking = {} - revision_found = True - - if sha1_git is None and revision_id is not None: - try: - archive.lookup_revision(revision_id) - except NotFoundExc: - revision_found = False - - if sha1_git is not None: - swh_objects.append( - SWHObjectInfo(object_type=ObjectType.DIRECTORY, object_id=sha1_git) - ) - vault_cooking.update( - {"directory_context": True, "directory_swhid": f"swh:1:dir:{sha1_git}",} - ) - if revision_id is not None and revision_found: - swh_objects.append( - SWHObjectInfo(object_type=ObjectType.REVISION, object_id=revision_id) - ) - vault_cooking.update( - {"revision_context": True, "revision_swhid": f"swh:1:rev:{revision_id}",} - ) - swh_objects.append( - SWHObjectInfo(object_type=ObjectType.SNAPSHOT, object_id=snapshot_id) - ) - - visit_date = None - visit_type = None - if visit_info: - visit_date = format_utc_iso_date(visit_info["date"]) - visit_type = visit_info["type"] - - release_id = snapshot_context["release_id"] - if release_id: - swh_objects.append( - SWHObjectInfo(object_type=ObjectType.RELEASE, object_id=release_id) - ) - - dir_metadata = DirectoryMetadata( - object_type=ObjectType.DIRECTORY, - object_id=sha1_git, - directory=sha1_git, - nb_files=nb_files, - nb_dirs=nb_dirs, - sum_file_sizes=sum_file_sizes, - root_directory=root_directory, - path=dir_path, - revision=revision_id, - revision_found=revision_found, - release=release_id, - snapshot=snapshot_id, - origin_url=origin_url, - visit_date=visit_date, - visit_type=visit_type, - ) - - swhids_info = get_swhids_info(swh_objects, snapshot_context, dir_metadata) - - dir_path = "/".join([bc["name"] for bc in breadcrumbs]) + "/" - context_found = "snapshot: %s" % snapshot_context["snapshot_id"] - if origin_info: - context_found = "origin: %s" % origin_info["url"] - heading = "Directory - %s - %s - %s" % ( - dir_path, - snapshot_context["branch"], - context_found, - ) - - top_right_link = None - if not snapshot_context["is_empty"] and revision_found: - top_right_link = { - "url": history_url, - "icon": swh_object_icons["revisions history"], - "text": "History", - } - - return render( - request, - "browse/directory.html", - { - "heading": heading, - "swh_object_name": "Directory", - "swh_object_metadata": dir_metadata, - "dirs": dirs, - "files": files, - "breadcrumbs": breadcrumbs if root_directory else [], - "top_right_link": top_right_link, - "readme_name": readme_name, - "readme_url": readme_url, - "readme_html": readme_html, - "snapshot_context": snapshot_context, - "vault_cooking": vault_cooking, - "show_actions": True, - "swhids_info": swhids_info, - "error_code": error_info["status_code"], - "error_message": http_status_code_message.get(error_info["status_code"]), - "error_description": error_info["description"], - }, - status=error_info["status_code"], - ) +# def browse_snapshot_directory( +# request, snapshot_id=None, origin_url=None, timestamp=None, path=None +# ): +# """ +# Django view implementation for browsing a directory in a snapshot context. +# """ +# _check_origin_url(snapshot_id, origin_url) + +# snapshot_context = get_snapshot_context( +# snapshot_id=snapshot_id, +# origin_url=origin_url, +# timestamp=timestamp, +# visit_id=request.GET.get("visit_id"), +# path=path, +# browse_context="directory", +# branch_name=request.GET.get("branch"), +# release_name=request.GET.get("release"), +# revision_id=request.GET.get("revision"), +# ) + +# root_directory = snapshot_context["root_directory"] +# sha1_git = root_directory +# error_info = { +# "status_code": 200, +# "description": None, +# } +# if root_directory and path: +# try: +# dir_info = archive.lookup_directory_with_path(root_directory, path) +# sha1_git = dir_info["target"] +# except NotFoundExc as e: +# sha1_git = None +# error_info["status_code"] = 404 +# error_info["description"] = f"NotFoundExc: {str(e)}" + +# dirs = [] +# files = [] +# if sha1_git: +# dirs, files = get_directory_entries(sha1_git) + +# origin_info = snapshot_context["origin_info"] +# visit_info = snapshot_context["visit_info"] +# url_args = snapshot_context["url_args"] +# query_params = dict(snapshot_context["query_params"]) +# revision_id = snapshot_context["revision_id"] +# snapshot_id = snapshot_context["snapshot_id"] + +# if origin_info: +# browse_view_name = "browse-origin-directory" +# else: +# browse_view_name = "browse-snapshot-directory" + +# breadcrumbs = _build_breadcrumbs(snapshot_context, path) + +# path = "" if path is None else (path + "/") + +# for d in dirs: +# if d["type"] == "rev": +# d["url"] = reverse("browse-revision", url_args={"sha1_git": d["target"]}) +# else: +# query_params["path"] = path + d["name"] +# d["url"] = reverse( +# browse_view_name, url_args=url_args, query_params=query_params +# ) + +# sum_file_sizes = 0 + +# readmes = {} + +# if origin_info: +# browse_view_name = "browse-origin-content" +# else: +# browse_view_name = "browse-snapshot-content" + +# for f in files: +# query_params["path"] = path + f["name"] +# f["url"] = reverse( +# browse_view_name, url_args=url_args, query_params=query_params +# ) +# if f["length"] is not None: +# sum_file_sizes += f["length"] +# if f["name"].lower().startswith("readme"): +# readmes[f["name"]] = f["checksums"]["sha1"] + +# readme_name, readme_url, readme_html = get_readme_to_display(readmes) + +# if origin_info: +# browse_view_name = "browse-origin-log" +# else: +# browse_view_name = "browse-snapshot-log" + +# history_url = None +# if snapshot_id != _empty_snapshot_id: +# query_params.pop("path", None) +# history_url = reverse( +# browse_view_name, url_args=url_args, query_params=query_params +# ) + +# nb_files = None +# nb_dirs = None +# dir_path = None +# if root_directory: +# nb_files = len(files) +# nb_dirs = len(dirs) +# dir_path = "/" + path + +# swh_objects = [] +# vault_cooking = {} +# revision_found = True + +# if sha1_git is None and revision_id is not None: +# try: +# archive.lookup_revision(revision_id) +# except NotFoundExc: +# revision_found = False + +# if sha1_git is not None: +# swh_objects.append( +# SWHObjectInfo(object_type=ObjectType.DIRECTORY, object_id=sha1_git) +# ) +# vault_cooking.update( +# {"directory_context": True, "directory_swhid": f"swh:1:dir:{sha1_git}",} +# ) +# if revision_id is not None and revision_found: +# swh_objects.append( +# SWHObjectInfo(object_type=ObjectType.REVISION, object_id=revision_id) +# ) +# vault_cooking.update( +# {"revision_context": True, "revision_swhid": f"swh:1:rev:{revision_id}",} +# ) +# swh_objects.append( +# SWHObjectInfo(object_type=ObjectType.SNAPSHOT, object_id=snapshot_id) +# ) + +# visit_date = None +# visit_type = None +# if visit_info: +# visit_date = format_utc_iso_date(visit_info["date"]) +# visit_type = visit_info["type"] + +# release_id = snapshot_context["release_id"] +# if release_id: +# swh_objects.append( +# SWHObjectInfo(object_type=ObjectType.RELEASE, object_id=release_id) +# ) + +# dir_metadata = DirectoryMetadata( +# object_type=ObjectType.DIRECTORY, +# object_id=sha1_git, +# directory=sha1_git, +# nb_files=nb_files, +# nb_dirs=nb_dirs, +# sum_file_sizes=sum_file_sizes, +# root_directory=root_directory, +# path=dir_path, +# revision=revision_id, +# revision_found=revision_found, +# release=release_id, +# snapshot=snapshot_id, +# origin_url=origin_url, +# visit_date=visit_date, +# visit_type=visit_type, +# ) + +# swhids_info = get_swhids_info(swh_objects, snapshot_context, dir_metadata) + +# dir_path = "/".join([bc["name"] for bc in breadcrumbs]) + "/" +# context_found = "snapshot: %s" % snapshot_context["snapshot_id"] +# if origin_info: +# context_found = "origin: %s" % origin_info["url"] +# heading = "Directory - %s - %s - %s" % ( +# dir_path, +# snapshot_context["branch"], +# context_found, +# ) + +# top_right_link = None +# if not snapshot_context["is_empty"] and revision_found: +# top_right_link = { +# "url": history_url, +# "icon": swh_object_icons["revisions history"], +# "text": "History", +# } + +# return render( +# request, +# "browse/directory.html", +# { +# "heading": heading, +# "swh_object_name": "Directory", +# "swh_object_metadata": dir_metadata, +# "dirs": dirs, +# "files": files, +# "breadcrumbs": breadcrumbs if root_directory else [], +# "top_right_link": top_right_link, +# "readme_name": readme_name, +# "readme_url": readme_url, +# "readme_html": readme_html, +# "snapshot_context": snapshot_context, +# "vault_cooking": vault_cooking, +# "show_actions": True, +# "swhids_info": swhids_info, +# "error_code": error_info["status_code"], +# "error_message": http_status_code_message.get(error_info["status_code"]), +# "error_description": error_info["description"], +# }, +# status=error_info["status_code"], +# ) PER_PAGE = 100 diff --git a/swh/web/browse/views/content.py b/swh/web/browse/views/content.py --- a/swh/web/browse/views/content.py +++ b/swh/web/browse/views/content.py @@ -178,7 +178,7 @@ origin_url = request.GET.get("origin_url") if snapshot is None and origin_url is None: raise BadInputExc( - "The origin_url or snapshot query parameters must be provided." + "The origin_url or snapshot query parameter must be provided." ) snapshot_context = get_snapshot_context( snapshot_id=snapshot, diff --git a/swh/web/browse/views/directory.py b/swh/web/browse/views/directory.py --- a/swh/web/browse/views/directory.py +++ b/swh/web/browse/views/directory.py @@ -15,10 +15,15 @@ from swh.web.browse.snapshot_context import get_snapshot_context from swh.web.browse.utils import gen_link, get_directory_entries, get_readme_to_display from swh.web.common import archive -from swh.web.common.exc import NotFoundExc, http_status_code_message +from swh.web.common.exc import BadInputExc, NotFoundExc, http_status_code_message from swh.web.common.identifiers import get_swhids_info from swh.web.common.typing import DirectoryMetadata, SWHObjectInfo -from swh.web.common.utils import gen_path_info, reverse, swh_object_icons +from swh.web.common.utils import ( + gen_path_info, + redirect_to_new_route, + reverse, + swh_object_icons, +) def _directory_browse(request, sha1_git, path=None): @@ -39,7 +44,7 @@ origin_url = request.GET.get("origin_url") if not origin_url: origin_url = request.GET.get("origin") - snapshot_id = request.GET.get("snapshot") + snapshot_id = request.GET.get("snapshot") or request.GET.get("snapshot_id") snapshot_context = None if origin_url is not None or snapshot_id is not None: try: @@ -230,18 +235,60 @@ ) +def _get_directory_from_request(request): + if request.GET.get("sha1_git") is not None: + # This case happens when redirected from + # directory/(?P[0-9a-f]+)/(?P.+)/ + return request.GET.get("sha1_git") + + snapshot = request.GET.get("snapshot") or request.GET.get("snapshot_id") + origin_url = request.GET.get("origin_url") + if snapshot is None and origin_url is None: + raise BadInputExc( + "The origin_url or snapshot query parameter must be provided." + ) + snapshot_context = get_snapshot_context( + snapshot_id=snapshot, + origin_url=origin_url, + timestamp=request.GET.get("timestamp"), + visit_id=request.GET.get("visit_id"), + branch_name=request.GET.get("branch"), + release_name=request.GET.get("release"), + revision_id=request.GET.get("revision"), + ) + path = request.GET.get("path") + if snapshot_context["root_directory"] is None: + raise NotFoundExc("The requested directory is not available") + # assert path + if path is not None: + archive.lookup_directory_with_path(snapshot_context["root_directory"], path) + return snapshot_context["root_directory"] + + @browse_route( r"directory/(?P[0-9a-f]+)/", + r"directory/", view_name="browse-directory", checksum_args=["sha1_git"], ) -def directory_browse(request, sha1_git): +def directory_browse(request, sha1_git=None): """Django view for browsing the content of a directory identified by its sha1_git value. - The url that points to it is + The URLs that point to it are :http:get:`/browse/directory/(sha1_git)/` + :http:get:`/browse/directory/` """ + if sha1_git is None: + # this case happens when redirected from origin/directory or snapshot/directory + directory = _get_directory_from_request(request) + return redirect( + reverse( + "browse-directory", + url_args={"sha1_git": directory}, + query_params=request.GET, + ), + ) return _directory_browse(request, sha1_git, request.GET.get("path")) @@ -251,13 +298,16 @@ checksum_args=["sha1_git"], ) def directory_browse_legacy(request, sha1_git, path): - """Django view for browsing the content of a directory identified + """ + This route is deprecated; use http:get:`/browse/directory` instead + + Django view for browsing the content of a directory identified by its sha1_git value. The url that points to it is :http:get:`/browse/directory/(sha1_git)/(path)/` """ - return _directory_browse(request, sha1_git, path) + return redirect_to_new_route(request, "browse-directory") @browse_route( diff --git a/swh/web/browse/views/origin.py b/swh/web/browse/views/origin.py --- a/swh/web/browse/views/origin.py +++ b/swh/web/browse/views/origin.py @@ -3,13 +3,10 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -from django.shortcuts import redirect, render +from django.shortcuts import render from swh.web.browse.browseurls import browse_route -from swh.web.browse.snapshot_context import ( - browse_snapshot_directory, - get_snapshot_context, -) +from swh.web.browse.snapshot_context import get_snapshot_context from swh.web.common import archive from swh.web.common.exc import BadInputExc from swh.web.common.origin_visits import get_origin_visits @@ -25,18 +22,15 @@ r"origin/directory/", view_name="browse-origin-directory", ) def origin_directory_browse(request): - """Django view for browsing the content of a directory associated + """ + This route is deprecated; use http:get:`/browse/directory` instead + + Django view for browsing the content of a directory associated to an origin for a given visit. The URL that points to it is :http:get:`/browse/origin/directory/` """ - return browse_snapshot_directory( - request, - origin_url=request.GET.get("origin_url"), - snapshot_id=request.GET.get("snapshot"), - timestamp=request.GET.get("timestamp"), - path=request.GET.get("path"), - ) + return redirect_to_new_route(request, "browse-directory") @browse_route( @@ -47,20 +41,17 @@ view_name="browse-origin-directory-legacy", ) def origin_directory_browse_legacy(request, origin_url, timestamp=None, path=None): - """Django view for browsing the content of a directory associated + """ + This route is deprecated; use http:get:`/browse/directory` instead + + Django view for browsing the content of a directory associated to an origin for a given visit. The URLs that point to it are :http:get:`/browse/origin/(origin_url)/directory/[(path)/]` and :http:get:`/browse/origin/(origin_url)/visit/(timestamp)/directory/[(path)/]` """ - return browse_snapshot_directory( - request, - origin_url=origin_url, - snapshot_id=request.GET.get("snapshot"), - timestamp=timestamp, - path=path, - ) + return redirect_to_new_route(request, "browse-directory") @browse_route( @@ -280,17 +271,15 @@ """Django view that redirects to the display of the latest archived snapshot for a given software origin. """ - last_snapshot_url = reverse("browse-origin-directory", query_params=request.GET,) - return redirect(last_snapshot_url) + return redirect_to_new_route(request, "browse-directory") @browse_route(r"origin/(?P.+)/", view_name="browse-origin-legacy") -def origin_browse_legacy(request, origin_url): - """Django view that redirects to the display of the latest archived +def origin_browse_legacy(request, _): + """ + This route is deprecated; use http:get:`/browse/origin` instead + + Django view that redirects to the display of the latest archived snapshot for a given software origin. """ - last_snapshot_url = reverse( - "browse-origin-directory", - query_params={"origin_url": origin_url, **request.GET}, - ) - return redirect(last_snapshot_url) + return redirect_to_new_route(request, "browse-directory") diff --git a/swh/web/browse/views/snapshot.py b/swh/web/browse/views/snapshot.py --- a/swh/web/browse/views/snapshot.py +++ b/swh/web/browse/views/snapshot.py @@ -9,7 +9,6 @@ from swh.web.browse.browseurls import browse_route from swh.web.browse.snapshot_context import ( browse_snapshot_branches, - browse_snapshot_directory, browse_snapshot_log, browse_snapshot_releases, get_snapshot_context, @@ -53,17 +52,15 @@ checksum_args=["snapshot_id"], ) def snapshot_directory_browse(request, snapshot_id): - """Django view for browsing the content of a directory collected + """ + This route is deprecated; use http:get:`/browse/directory` instead + + Django view for browsing the content of a directory collected in a snapshot. The URL that points to it is :http:get:`/browse/snapshot/(snapshot_id)/directory/` """ - return browse_snapshot_directory( - request, - snapshot_id=snapshot_id, - path=request.GET.get("path"), - origin_url=request.GET.get("origin_url"), - ) + return redirect_to_new_route(request, "browse-directory") @browse_route( @@ -72,18 +69,16 @@ checksum_args=["snapshot_id"], ) def snapshot_directory_browse_legacy(request, snapshot_id, path=None): - """Django view for browsing the content of a directory collected + """ + This route is deprecated; use http:get:`/browse/directory` instead + + Django view for browsing the content of a directory collected in a snapshot. The URL that points to it is :http:get:`/browse/snapshot/(snapshot_id)/directory/(path)/` """ - origin_url = request.GET.get("origin_url", None) - if not origin_url: - origin_url = request.GET.get("origin", None) - return browse_snapshot_directory( - request, snapshot_id=snapshot_id, path=path, origin_url=origin_url - ) + return redirect_to_new_route(request, "browse-directory") @browse_route( diff --git a/swh/web/tests/browse/views/test_content.py b/swh/web/tests/browse/views/test_content.py --- a/swh/web/tests/browse/views/test_content.py +++ b/swh/web/tests/browse/views/test_content.py @@ -653,7 +653,7 @@ resp = check_html_get_response(client, url, status_code=400,) assert_contains( resp, - "The origin_url or snapshot query parameters must be provided.", + "The origin_url or snapshot query parameter must be provided.", status_code=400, ) diff --git a/swh/web/tests/browse/views/test_directory.py b/swh/web/tests/browse/views/test_directory.py --- a/swh/web/tests/browse/views/test_directory.py +++ b/swh/web/tests/browse/views/test_directory.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information import random +import re from hypothesis import given @@ -30,7 +31,13 @@ from swh.web.common.identifiers import gen_swhid from swh.web.common.utils import gen_path_info, reverse from swh.web.tests.django_asserts import assert_contains, assert_not_contains -from swh.web.tests.strategies import new_person, new_swh_date +from swh.web.tests.strategies import ( + new_origin, + new_person, + new_snapshot, + new_swh_date, + visit_dates, +) from swh.web.tests.utils import check_html_get_response @@ -497,3 +504,543 @@ assert_contains(resp, swh_dir_id_url) assert_not_contains(resp, "swh-metadata-popover") + + +# starts + +# def test_pull_request_branches_filtering( +# client, origin_with_pull_request_branches, archive_data +# ): +# origin_url = origin_with_pull_request_branches.url +# # check no pull request branches are displayed in the Branches / Releases dropdown +# url = reverse("browse-origin-directory", query_params={"origin_url": origin_url}) +# resp = check_html_get_response( +# client, url, status_code=200, template_used="browse/directory.html" +# ) +# assert_not_contains(resp, "refs/pull/") + +# snapshot = archive_data.snapshot_get_latest(origin_url) +# # check no pull request branches are displayed in the branches view +# url = reverse( +# "browse-snapshot-branches", +# url_args={"snapshot_id": snapshot["id"]}, +# query_params={"origin_url": origin_url}, +# ) +# resp = check_html_get_response( +# client, url, status_code=200, template_used="browse/branches.html" +# ) +# assert_not_contains(resp, "refs/pull/") + +# @pytest.mark.django_db +# @pytest.mark.parametrize("object_type", ["directory"]) +# @given(new_origin()) +# def test_browse_origin_content_directory_empty_snapshot( +# client, staff_user, archive_data, object_type, new_origin +# ): + +# _add_empty_snapshot_origin(new_origin, archive_data) + +# # to check proper generation of raw extrinsic metadata api links +# client.force_login(staff_user) + +# url = reverse( +# f"browse-origin-{object_type}", +# query_params={"origin_url": new_origin.url, "path": "baz"}, +# ) + +# resp = check_html_get_response( +# client, url, status_code=200, template_used=f"browse/{object_type}.html" +# ) +# assert re.search("snapshot.*is empty", resp.content.decode("utf-8")) + +# def test_browse_directory_snapshot_not_found(client, mocker, origin): +# mock_get_snapshot_context = mocker.patch( +# "swh.web.browse.snapshot_context.get_snapshot_context" +# ) +# mock_get_snapshot_context.side_effect = NotFoundExc("Snapshot not found") +# url = reverse("browse-origin-directory", +# query_params={"origin_url": origin["url"]}) + +# resp = check_html_get_response( +# client, url, status_code=404, template_used="error.html" +# ) +# assert_contains(resp, "Snapshot not found", status_code=404) +# assert mock_get_snapshot_context.called + + +# @given(new_origin()) +# def test_origin_empty_snapshot(client, archive_data, new_origin): + +# _add_empty_snapshot_origin(new_origin, archive_data) + +# url = reverse( +# "browse-origin-directory", query_params={"origin_url": new_origin.url} +# ) + +# resp = check_html_get_response( +# client, url, status_code=200, template_used="browse/directory.html" +# ) +# resp_content = resp.content.decode("utf-8") +# assert re.search("snapshot.*is empty", resp_content) +# assert not re.search("swh-tr-link", resp_content) + +# def test_origin_sub_directory_view(client, archive_data, swh_scheduler, origin): +# origin_visits = archive_data.origin_visit_get(origin["url"]) + +# visit = origin_visits[-1] +# snapshot = archive_data.snapshot_get(visit["snapshot"]) +# snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) +# head_rev_id = archive_data.snapshot_get_head(snapshot) +# head_rev = archive_data.revision_get(head_rev_id) +# root_dir_sha1 = head_rev["directory"] +# subdirs = [ +# e for e in archive_data.directory_ls(root_dir_sha1) if e["type"] == "dir" +# ] +# branches, releases, _ = process_snapshot_branches(snapshot) + +# if len(subdirs) == 0: +# return + +# subdir = random.choice(subdirs) +# subdir_content = archive_data.directory_ls(subdir["target"]) +# subdir_path = subdir["name"] + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# subdir_content, +# path=subdir_path, +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# subdir_content, +# path=subdir_path, +# visit_id=visit["visit"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# subdir_content, +# path=subdir_path, +# timestamp=visit["date"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# subdir_content, +# path=subdir_path, +# snapshot_id=visit["snapshot"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# subdir_content, +# path=subdir_path, +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# subdir_content, +# path=subdir_path, +# visit_id=visit["visit"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# subdir_content, +# path=subdir_path, +# timestamp=visit["date"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# subdir_content, +# path=subdir_path, +# snapshot_id=visit["snapshot"], +# ) + + +# def test_origin_root_directory_view(client, archive_data, swh_scheduler, origin): +# origin_visits = archive_data.origin_visit_get(origin["url"]) + +# visit = origin_visits[-1] +# snapshot = archive_data.snapshot_get(visit["snapshot"]) +# snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) +# head_rev_id = archive_data.snapshot_get_head(snapshot) +# head_rev = archive_data.revision_get(head_rev_id) +# root_dir_sha1 = head_rev["directory"] +# dir_content = archive_data.directory_ls(root_dir_sha1) +# branches, releases, _ = process_snapshot_branches(snapshot) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# dir_content, +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# dir_content, +# visit_id=visit["visit"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# dir_content, +# timestamp=visit["date"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# dir_content, +# snapshot_id=visit["snapshot"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# dir_content, +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# dir_content, +# visit_id=visit["visit"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# dir_content, +# timestamp=visit["date"], +# ) + +# _origin_directory_view_test_helper( +# client, +# archive_data, +# origin, +# visit, +# snapshot_sizes, +# branches, +# releases, +# root_dir_sha1, +# dir_content, +# snapshot_id=visit["snapshot"], +# ) + + +# @given( +# new_origin(), new_snapshot(min_size=4, max_size=4), visit_dates(), +# ) +# def test_browse_directory_snapshot_null_branch( +# client, archive_data, revisions_list, new_origin, new_snapshot, visit_dates, +# ): +# revisions = revisions_list(size=4) +# snp_dict = new_snapshot.to_dict() +# archive_data.origin_add([new_origin]) +# for i, branch in enumerate(snp_dict["branches"].keys()): +# if i == 0: +# snp_dict["branches"][branch] = None +# else: +# snp_dict["branches"][branch] = { +# "target_type": "revision", +# "target": hash_to_bytes(revisions[i - 1]), +# } + +# archive_data.snapshot_add([Snapshot.from_dict(snp_dict)]) +# visit = archive_data.origin_visit_add( +# [OriginVisit(origin=new_origin.url, date=visit_dates[0], type="git",)] +# )[0] +# visit_status = OriginVisitStatus( +# origin=new_origin.url, +# visit=visit.visit, +# date=now(), +# status="partial", +# snapshot=snp_dict["id"], +# ) +# archive_data.origin_visit_status_add([visit_status]) + +# url = reverse( +# "browse-directory", query_params={"origin_url": new_origin.url} +# ) + +# check_html_get_response( +# client, url, status_code=200, template_used="error.html" +# ) + + +@given( + new_origin(), new_snapshot(min_size=4, max_size=4), visit_dates(), +) +def test_browse_directory_snapshot_invalid_branch( + client, archive_data, revisions_list, new_origin, new_snapshot, visit_dates, +): + revisions = revisions_list(size=4) + snp_dict = new_snapshot.to_dict() + archive_data.origin_add([new_origin]) + for i, branch in enumerate(snp_dict["branches"].keys()): + snp_dict["branches"][branch] = { + "target_type": "revision", + "target": hash_to_bytes(revisions[i]), + } + + archive_data.snapshot_add([Snapshot.from_dict(snp_dict)]) + visit = archive_data.origin_visit_add( + [OriginVisit(origin=new_origin.url, date=visit_dates[0], type="git",)] + )[0] + visit_status = OriginVisitStatus( + origin=new_origin.url, + visit=visit.visit, + date=now(), + status="full", + snapshot=snp_dict["id"], + ) + archive_data.origin_visit_status_add([visit_status]) + + url = reverse( + "browse-directory", + query_params={"origin_url": new_origin.url, "branch": "invalid_branch"}, + ) + + check_html_get_response(client, url, status_code=404, template_used="error.html") + + +def test_browse_directory_no_visit(client, mocker, origin): + mock_get_origin_visits = mocker.patch( + "swh.web.common.origin_visits.get_origin_visits" + ) + mock_get_origin_visits.return_value = [] + mock_archive = mocker.patch("swh.web.common.origin_visits.archive") + mock_archive.lookup_origin_visit_latest.return_value = None + url = reverse("browse-directory", query_params={"origin_url": origin["url"]}) + + resp = check_html_get_response( + client, url, status_code=404, template_used="error.html" + ) + assert_contains(resp, "No valid visit", status_code=404) + assert not mock_get_origin_visits.called + + +def test_browse_directory_unknown_visit(client, mocker, origin): + mock_get_origin_visits = mocker.patch( + "swh.web.common.origin_visits.get_origin_visits" + ) + mock_get_origin_visits.return_value = [{"visit": 1}] + + url = reverse( + "browse-directory", query_params={"origin_url": origin["url"], "visit_id": 2}, + ) + + resp = check_html_get_response( + client, url, status_code=404, template_used="error.html" + ) + assert re.search("Visit.*not found", resp.content.decode("utf-8")) + assert mock_get_origin_visits.called + + +def test_browse_origin_directory_not_found(client, origin): + url = reverse( + "browse-directory", + query_params={"origin_url": origin["url"], "path": "/invalid/dir/path/"}, + ) + + resp = check_html_get_response( + client, url, status_code=404, template_used="error.html" + ) + assert re.search( + "Directory entry with path.*from root directory", resp.content.decode("utf-8") + ) + + +@given(new_origin()) +def test_directory_browse_empty_snapshot_null_revision( + client, archive_data, new_origin +): + snapshot = Snapshot( + branches={ + b"HEAD": SnapshotBranch( + target="refs/head/master".encode(), target_type=TargetType.ALIAS, + ), + b"refs/head/master": None, + } + ) + archive_data.origin_add([new_origin]) + archive_data.snapshot_add([snapshot]) + visit = archive_data.origin_visit_add( + [OriginVisit(origin=new_origin.url, date=now(), type="git",)] + )[0] + visit_status = OriginVisitStatus( + origin=new_origin.url, + visit=visit.visit, + date=now(), + status="partial", + snapshot=snapshot.id, + ) + archive_data.origin_visit_status_add([visit_status]) + + url = reverse("browse-directory", query_params={"origin_url": new_origin.url},) + + resp = check_html_get_response( + client, url, status_code=404, template_used="error.html" + ) + resp_content = resp.content.decode("utf-8") + + assert re.search("The requested directory is not available", resp_content) + + +def test_content_browse_empty_sha1_missing_identifier(client): + url = reverse("browse-directory", query_params={},) + resp = check_html_get_response(client, url, status_code=400) + assert_contains( + resp, + "The origin_url or snapshot query parameter must be provided.", + status_code=400, + ) + + +def test_content_browse_empty_sha1(client, directory): + url = reverse("browse-directory", query_params={"sha1_git": directory},) + resp = check_html_get_response(client, url, status_code=302) + redict_url = reverse( + "browse-directory", + url_args={"sha1_git": directory}, + query_params={"sha1_git": directory}, + ) + assert resp.url == redict_url + + +def test_content_browse_empty_sha1_with_snapshot(client, snapshot, archive_data): + head_branch = archive_data.snapshot_get_branches(snapshot)["branches"][ + "refs/heads/master" + ]["target"] + directory = archive_data.revision_get(head_branch)["directory"] + url = reverse("browse-directory", query_params={"snapshot": snapshot},) + + resp = check_html_get_response(client, url, status_code=302) + + redict_url = reverse( + "browse-directory", + url_args={"sha1_git": directory}, + query_params={"snapshot": snapshot}, + ) + assert resp.url == redict_url + + +def test_content_browse_empty_sha1_with_origin(client, origin, archive_data): + snapshot = archive_data.snapshot_get_latest(origin["url"]) + head_branch = archive_data.snapshot_get_branches(snapshot["id"])["branches"][ + "refs/heads/master" + ]["target"] + directory = archive_data.revision_get(head_branch)["directory"] + url = reverse("browse-directory", query_params={"origin_url": origin["url"]},) + + resp = check_html_get_response(client, url, status_code=302) + + redict_url = reverse( + "browse-directory", + url_args={"sha1_git": directory}, + query_params={"origin_url": origin["url"]}, + ) + assert resp.url == redict_url diff --git a/swh/web/tests/browse/views/test_origin.py b/swh/web/tests/browse/views/test_origin.py --- a/swh/web/tests/browse/views/test_origin.py +++ b/swh/web/tests/browse/views/test_origin.py @@ -3,30 +3,21 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -import random -import re - from hypothesis import given import pytest from django.utils.html import escape -from swh.model.hashutil import hash_to_bytes -from swh.model.model import ( - OriginVisit, - OriginVisitStatus, - Snapshot, - SnapshotBranch, - TargetType, -) +from swh.model.model import OriginVisit, OriginVisitStatus, Snapshot from swh.model.swhids import ObjectType from swh.storage.utils import now -from swh.web.browse.snapshot_context import process_snapshot_branches -from swh.web.common.exc import NotFoundExc + +# from swh.web.browse.snapshot_context import process_snapshot_branches +# from swh.web.common.exc import NotFoundExc from swh.web.common.identifiers import gen_swhid from swh.web.common.utils import format_utc_iso_date, parse_iso8601_date_to_utc, reverse from swh.web.tests.django_asserts import assert_contains, assert_not_contains -from swh.web.tests.strategies import new_origin, new_snapshot, visit_dates +from swh.web.tests.strategies import new_origin # visit_dates, new_snapshot from swh.web.tests.utils import check_html_get_response @@ -51,387 +42,6 @@ _check_origin_link(resp, origin_url) -def test_origin_root_directory_view(client, archive_data, swh_scheduler, origin): - origin_visits = archive_data.origin_visit_get(origin["url"]) - - visit = origin_visits[-1] - snapshot = archive_data.snapshot_get(visit["snapshot"]) - snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) - head_rev_id = archive_data.snapshot_get_head(snapshot) - head_rev = archive_data.revision_get(head_rev_id) - root_dir_sha1 = head_rev["directory"] - dir_content = archive_data.directory_ls(root_dir_sha1) - branches, releases, _ = process_snapshot_branches(snapshot) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - dir_content, - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - dir_content, - visit_id=visit["visit"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - dir_content, - timestamp=visit["date"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - dir_content, - snapshot_id=visit["snapshot"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - dir_content, - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - dir_content, - visit_id=visit["visit"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - dir_content, - timestamp=visit["date"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - dir_content, - snapshot_id=visit["snapshot"], - ) - - -def test_origin_sub_directory_view(client, archive_data, swh_scheduler, origin): - origin_visits = archive_data.origin_visit_get(origin["url"]) - - visit = origin_visits[-1] - snapshot = archive_data.snapshot_get(visit["snapshot"]) - snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) - head_rev_id = archive_data.snapshot_get_head(snapshot) - head_rev = archive_data.revision_get(head_rev_id) - root_dir_sha1 = head_rev["directory"] - subdirs = [ - e for e in archive_data.directory_ls(root_dir_sha1) if e["type"] == "dir" - ] - branches, releases, _ = process_snapshot_branches(snapshot) - - if len(subdirs) == 0: - return - - subdir = random.choice(subdirs) - subdir_content = archive_data.directory_ls(subdir["target"]) - subdir_path = subdir["name"] - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - subdir_content, - path=subdir_path, - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - subdir_content, - path=subdir_path, - visit_id=visit["visit"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - subdir_content, - path=subdir_path, - timestamp=visit["date"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - subdir_content, - path=subdir_path, - snapshot_id=visit["snapshot"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - subdir_content, - path=subdir_path, - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - subdir_content, - path=subdir_path, - visit_id=visit["visit"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - subdir_content, - path=subdir_path, - timestamp=visit["date"], - ) - - _origin_directory_view_test_helper( - client, - archive_data, - origin, - visit, - snapshot_sizes, - branches, - releases, - root_dir_sha1, - subdir_content, - path=subdir_path, - snapshot_id=visit["snapshot"], - ) - - -@given( - new_origin(), new_snapshot(min_size=4, max_size=4), visit_dates(), -) -def test_origin_snapshot_null_branch( - client, archive_data, revisions_list, new_origin, new_snapshot, visit_dates, -): - revisions = revisions_list(size=4) - snp_dict = new_snapshot.to_dict() - archive_data.origin_add([new_origin]) - for i, branch in enumerate(snp_dict["branches"].keys()): - if i == 0: - snp_dict["branches"][branch] = None - else: - snp_dict["branches"][branch] = { - "target_type": "revision", - "target": hash_to_bytes(revisions[i - 1]), - } - - archive_data.snapshot_add([Snapshot.from_dict(snp_dict)]) - visit = archive_data.origin_visit_add( - [OriginVisit(origin=new_origin.url, date=visit_dates[0], type="git",)] - )[0] - visit_status = OriginVisitStatus( - origin=new_origin.url, - visit=visit.visit, - date=now(), - status="partial", - snapshot=snp_dict["id"], - ) - archive_data.origin_visit_status_add([visit_status]) - - url = reverse( - "browse-origin-directory", query_params={"origin_url": new_origin.url} - ) - - check_html_get_response( - client, url, status_code=200, template_used="browse/directory.html" - ) - - -@given( - new_origin(), new_snapshot(min_size=4, max_size=4), visit_dates(), -) -def test_origin_snapshot_invalid_branch( - client, archive_data, revisions_list, new_origin, new_snapshot, visit_dates, -): - revisions = revisions_list(size=4) - snp_dict = new_snapshot.to_dict() - archive_data.origin_add([new_origin]) - for i, branch in enumerate(snp_dict["branches"].keys()): - snp_dict["branches"][branch] = { - "target_type": "revision", - "target": hash_to_bytes(revisions[i]), - } - - archive_data.snapshot_add([Snapshot.from_dict(snp_dict)]) - visit = archive_data.origin_visit_add( - [OriginVisit(origin=new_origin.url, date=visit_dates[0], type="git",)] - )[0] - visit_status = OriginVisitStatus( - origin=new_origin.url, - visit=visit.visit, - date=now(), - status="full", - snapshot=snp_dict["id"], - ) - archive_data.origin_visit_status_add([visit_status]) - - url = reverse( - "browse-origin-directory", - query_params={"origin_url": new_origin.url, "branch": "invalid_branch"}, - ) - - check_html_get_response(client, url, status_code=404, template_used="error.html") - - -@given(new_origin()) -def test_browse_visits_origin_not_found(client, new_origin): - url = reverse("browse-origin-visits", query_params={"origin_url": new_origin.url}) - - resp = check_html_get_response( - client, url, status_code=404, template_used="error.html" - ) - assert_contains( - resp, f"Origin with url {new_origin.url} not found", status_code=404 - ) - - -def test_browse_origin_directory_no_visit(client, mocker, origin): - mock_get_origin_visits = mocker.patch( - "swh.web.common.origin_visits.get_origin_visits" - ) - mock_get_origin_visits.return_value = [] - mock_archive = mocker.patch("swh.web.common.origin_visits.archive") - mock_archive.lookup_origin_visit_latest.return_value = None - url = reverse("browse-origin-directory", query_params={"origin_url": origin["url"]}) - - resp = check_html_get_response( - client, url, status_code=404, template_used="error.html" - ) - assert_contains(resp, "No valid visit", status_code=404) - assert not mock_get_origin_visits.called - - -def test_browse_origin_directory_unknown_visit(client, mocker, origin): - mock_get_origin_visits = mocker.patch( - "swh.web.common.origin_visits.get_origin_visits" - ) - mock_get_origin_visits.return_value = [{"visit": 1}] - - url = reverse( - "browse-origin-directory", - query_params={"origin_url": origin["url"], "visit_id": 2}, - ) - - resp = check_html_get_response( - client, url, status_code=404, template_used="error.html" - ) - assert re.search("Visit.*not found", resp.content.decode("utf-8")) - assert mock_get_origin_visits.called - - -def test_browse_origin_directory_not_found(client, origin): - url = reverse( - "browse-origin-directory", - query_params={"origin_url": origin["url"], "path": "/invalid/dir/path/"}, - ) - - resp = check_html_get_response( - client, url, status_code=404, template_used="browse/directory.html" - ) - assert re.search("Directory.*not found", resp.content.decode("utf-8")) - - def _add_empty_snapshot_origin(new_origin, archive_data): snapshot = Snapshot(branches={}) archive_data.origin_add([new_origin]) @@ -449,209 +59,121 @@ archive_data.origin_visit_status_add([visit_status]) -@pytest.mark.django_db -@pytest.mark.parametrize("object_type", ["directory"]) -@given(new_origin()) -def test_browse_origin_content_directory_empty_snapshot( - client, staff_user, archive_data, object_type, new_origin -): - - _add_empty_snapshot_origin(new_origin, archive_data) - - # to check proper generation of raw extrinsic metadata api links - client.force_login(staff_user) - - url = reverse( - f"browse-origin-{object_type}", - query_params={"origin_url": new_origin.url, "path": "baz"}, - ) - - resp = check_html_get_response( - client, url, status_code=200, template_used=f"browse/{object_type}.html" - ) - assert re.search("snapshot.*is empty", resp.content.decode("utf-8")) - - -def test_browse_directory_snapshot_not_found(client, mocker, origin): - mock_get_snapshot_context = mocker.patch( - "swh.web.browse.snapshot_context.get_snapshot_context" - ) - mock_get_snapshot_context.side_effect = NotFoundExc("Snapshot not found") - url = reverse("browse-origin-directory", query_params={"origin_url": origin["url"]}) - - resp = check_html_get_response( - client, url, status_code=404, template_used="error.html" - ) - assert_contains(resp, "Snapshot not found", status_code=404) - assert mock_get_snapshot_context.called - - -@given(new_origin()) -def test_origin_empty_snapshot(client, archive_data, new_origin): - - _add_empty_snapshot_origin(new_origin, archive_data) - - url = reverse( - "browse-origin-directory", query_params={"origin_url": new_origin.url} - ) - - resp = check_html_get_response( - client, url, status_code=200, template_used="browse/directory.html" - ) - resp_content = resp.content.decode("utf-8") - assert re.search("snapshot.*is empty", resp_content) - assert not re.search("swh-tr-link", resp_content) - - -@given(new_origin()) -def test_origin_empty_snapshot_null_revision(client, archive_data, new_origin): - snapshot = Snapshot( - branches={ - b"HEAD": SnapshotBranch( - target="refs/head/master".encode(), target_type=TargetType.ALIAS, - ), - b"refs/head/master": None, - } - ) - archive_data.origin_add([new_origin]) - archive_data.snapshot_add([snapshot]) - visit = archive_data.origin_visit_add( - [OriginVisit(origin=new_origin.url, date=now(), type="git",)] - )[0] - visit_status = OriginVisitStatus( - origin=new_origin.url, - visit=visit.visit, - date=now(), - status="partial", - snapshot=snapshot.id, - ) - archive_data.origin_visit_status_add([visit_status]) - - url = reverse( - "browse-origin-directory", query_params={"origin_url": new_origin.url}, - ) - - resp = check_html_get_response( - client, url, status_code=200, template_used="browse/directory.html" - ) - resp_content = resp.content.decode("utf-8") - assert re.search("snapshot.*is empty", resp_content) - assert not re.search("swh-tr-link", resp_content) - - -def test_origin_release_browse(client, archive_data, origin_with_releases): - origin_url = origin_with_releases["url"] - snapshot = archive_data.snapshot_get_latest(origin_url) - release = [ - b for b in snapshot["branches"].values() if b["target_type"] == "release" - ][-1] - release_data = archive_data.release_get(release["target"]) - revision_data = archive_data.revision_get(release_data["target"]) - url = reverse( - "browse-origin-directory", - query_params={"origin_url": origin_url, "release": release_data["name"]}, - ) - - resp = check_html_get_response( - client, url, status_code=200, template_used="browse/directory.html" - ) - assert_contains(resp, release_data["name"]) - assert_contains(resp, release["target"]) - - swhid_context = { - "origin": origin_url, - "visit": gen_swhid(ObjectType.SNAPSHOT, snapshot["id"]), - "anchor": gen_swhid(ObjectType.RELEASE, release_data["id"]), - } - - swh_dir_id = gen_swhid( - ObjectType.DIRECTORY, revision_data["directory"], metadata=swhid_context - ) - swh_dir_id_url = reverse("browse-swhid", url_args={"swhid": swh_dir_id}) - assert_contains(resp, swh_dir_id) - assert_contains(resp, swh_dir_id_url) - - -def test_origin_release_browse_not_found(client, origin_with_releases): - - invalid_release_name = "swh-foo-bar" - url = reverse( - "browse-origin-directory", - query_params={ - "origin_url": origin_with_releases["url"], - "release": invalid_release_name, - }, - ) - - resp = check_html_get_response( - client, url, status_code=404, template_used="error.html" - ) - assert re.search( - f"Release {invalid_release_name}.*not found", resp.content.decode("utf-8") - ) - - -@given(new_origin()) -def test_origin_browse_directory_branch_with_non_resolvable_revision( - client, archive_data, unknown_revision, new_origin, -): - branch_name = "master" - snapshot = Snapshot( - branches={ - branch_name.encode(): SnapshotBranch( - target=hash_to_bytes(unknown_revision), target_type=TargetType.REVISION, - ) - } - ) - archive_data.origin_add([new_origin]) - archive_data.snapshot_add([snapshot]) - visit = archive_data.origin_visit_add( - [OriginVisit(origin=new_origin.url, date=now(), type="git",)] - )[0] - visit_status = OriginVisitStatus( - origin=new_origin.url, - visit=visit.visit, - date=now(), - status="partial", - snapshot=snapshot.id, - ) - archive_data.origin_visit_status_add([visit_status]) - - url = reverse( - "browse-origin-directory", - query_params={"origin_url": new_origin.url, "branch": branch_name}, - ) - - resp = check_html_get_response( - client, url, status_code=200, template_used="browse/directory.html" - ) - assert_contains( - resp, f"Revision {unknown_revision } could not be found in the archive." - ) - - # no revision card - assert_not_contains(resp, "swh-tip-revision") - # no Download dropdown - assert_not_contains(resp, "swh-vault-download") - # no History link - assert_not_contains(resp, "swh-tr-link") - # no SWHIDs for directory and revision - assert_not_contains(resp, "swh:1:dir:") - assert_not_contains(resp, "swh:1:rev:") - - -def test_origin_views_no_url_query_parameter(client): - for browse_context in ( - "directory", - "visits", - ): - url = reverse(f"browse-origin-{browse_context}") - resp = check_html_get_response( - client, url, status_code=400, template_used="error.html" - ) - assert_contains( - resp, "An origin URL must be provided as query parameter.", status_code=400, - ) +# def test_origin_release_browse(client, archive_data, origin_with_releases): +# origin_url = origin_with_releases["url"] +# snapshot = archive_data.snapshot_get_latest(origin_url) +# release = [ +# b for b in snapshot["branches"].values() if b["target_type"] == "release" +# ][-1] +# release_data = archive_data.release_get(release["target"]) +# revision_data = archive_data.revision_get(release_data["target"]) +# url = reverse( +# "browse-origin-directory", +# query_params={"origin_url": origin_url, "release": release_data["name"]}, +# ) + +# resp = check_html_get_response( +# client, url, status_code=200, template_used="browse/directory.html" +# ) +# assert_contains(resp, release_data["name"]) +# assert_contains(resp, release["target"]) + +# swhid_context = { +# "origin": origin_url, +# "visit": gen_swhid(ObjectType.SNAPSHOT, snapshot["id"]), +# "anchor": gen_swhid(ObjectType.RELEASE, release_data["id"]), +# } + +# swh_dir_id = gen_swhid( +# ObjectType.DIRECTORY, revision_data["directory"], metadata=swhid_context +# ) +# swh_dir_id_url = reverse("browse-swhid", url_args={"swhid": swh_dir_id}) +# assert_contains(resp, swh_dir_id) +# assert_contains(resp, swh_dir_id_url) + + +# def test_origin_release_browse_not_found(client, origin_with_releases): + +# invalid_release_name = "swh-foo-bar" +# url = reverse( +# "browse-origin-directory", +# query_params={ +# "origin_url": origin_with_releases["url"], +# "release": invalid_release_name, +# }, +# ) + +# resp = check_html_get_response( +# client, url, status_code=404, template_used="error.html" +# ) +# assert re.search( +# f"Release {invalid_release_name}.*not found", resp.content.decode("utf-8") +# ) + + +# @given(new_origin()) +# def test_origin_browse_directory_branch_with_non_resolvable_revision( +# client, archive_data, unknown_revision, new_origin, +# ): +# branch_name = "master" +# snapshot = Snapshot( +# branches={ +# branch_name.encode(): SnapshotBranch( +# target=hash_to_bytes(unknown_revision), +# target_type=TargetType.REVISION, +# ) +# } +# ) +# archive_data.origin_add([new_origin]) +# archive_data.snapshot_add([snapshot]) +# visit = archive_data.origin_visit_add( +# [OriginVisit(origin=new_origin.url, date=now(), type="git",)] +# )[0] +# visit_status = OriginVisitStatus( +# origin=new_origin.url, +# visit=visit.visit, +# date=now(), +# status="partial", +# snapshot=snapshot.id, +# ) +# archive_data.origin_visit_status_add([visit_status]) + +# url = reverse( +# "browse-origin-directory", +# query_params={"origin_url": new_origin.url, "branch": branch_name}, +# ) + +# resp = check_html_get_response( +# client, url, status_code=200, template_used="browse/directory.html" +# ) +# assert_contains( +# resp, f"Revision {unknown_revision } could not be found in the archive." +# ) + +# # no revision card +# assert_not_contains(resp, "swh-tip-revision") +# # no Download dropdown +# assert_not_contains(resp, "swh-vault-download") +# # no History link +# assert_not_contains(resp, "swh-tr-link") +# # no SWHIDs for directory and revision +# assert_not_contains(resp, "swh:1:dir:") +# assert_not_contains(resp, "swh:1:rev:") + + +# def test_origin_views_no_url_query_parameter(client): +# for browse_context in ( +# "directory", +# "visits", +# ): +# url = reverse(f"browse-origin-{browse_context}") +# resp = check_html_get_response( +# client, url, status_code=400, template_used="error.html" +# ) +# assert_contains( +# resp, "An origin URL must be provided as query parameter.", +# status_code=400, +# ) @given(new_origin()) @@ -667,7 +189,7 @@ @given(new_origin()) -@pytest.mark.parametrize("browse_context", ["content"]) +@pytest.mark.parametrize("browse_context", ["content", "directory"]) def test_origin_content_view_redirects(client, browse_context, new_origin): query_params = {"origin_url": new_origin.url, "path": "test.txt"} url = reverse(f"browse-origin-{browse_context}", query_params=query_params) @@ -701,7 +223,8 @@ @given(new_origin()) -def test_origin_content_view_legacy_redirects(client, new_origin): +@pytest.mark.parametrize("browse_context", ["content", "directory"]) +def test_origin_content_view_legacy_redirects(client, browse_context, new_origin): url_args = [ {"origin_url": new_origin.url}, { @@ -714,12 +237,14 @@ params = {"extra-param1": "extra-param1", "extra-param2": "extra-param2"} for each_arg in url_args: url = reverse( - "browse-origin-content-legacy", url_args=each_arg, query_params=params, + f"browse-origin-{browse_context}-legacy", + url_args=each_arg, + query_params=params, ) resp = check_html_get_response(client, url, status_code=301) assert resp["location"] == reverse( - "browse-content", query_params={**each_arg, **params} + f"browse-{browse_context}", query_params={**each_arg, **params} ) @@ -872,22 +397,34 @@ assert_contains(resp, f'href="{browse_origin_url}"') -def test_browse_pull_request_branch( - client, archive_data, origin_with_pull_request_branches -): - origin_url = origin_with_pull_request_branches.url - snapshot = archive_data.snapshot_get_latest(origin_url) - pr_branch = random.choice( - [ - branch - for branch in snapshot["branches"].keys() - if branch.startswith("refs/pull/") - ] - ) - url = reverse( - "browse-origin-directory", - query_params={"origin_url": origin_url, "branch": pr_branch}, +@given(new_origin()) +def test_browse_visits_origin_not_found(client, new_origin): + url = reverse("browse-origin-visits", query_params={"origin_url": new_origin.url}) + + resp = check_html_get_response( + client, url, status_code=404, template_used="error.html" ) - check_html_get_response( - client, url, status_code=200, template_used="browse/directory.html" + assert_contains( + resp, f"Origin with url {new_origin.url} not found", status_code=404 ) + + +# def test_browse_pull_request_branch( +# client, archive_data, origin_with_pull_request_branches +# ): +# origin_url = origin_with_pull_request_branches.url +# snapshot = archive_data.snapshot_get_latest(origin_url) +# pr_branch = random.choice( +# [ +# branch +# for branch in snapshot["branches"].keys() +# if branch.startswith("refs/pull/") +# ] +# ) +# url = reverse( +# "browse-origin-directory", +# query_params={"origin_url": origin_url, "branch": pr_branch}, +# ) +# check_html_get_response( +# client, url, status_code=200, template_used="browse/directory.html" +# ) 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 @@ -19,7 +19,7 @@ from swh.web.browse.snapshot_context import process_snapshot_branches from swh.web.common.utils import reverse from swh.web.tests.data import random_sha1 -from swh.web.tests.django_asserts import assert_contains, assert_not_contains +from swh.web.tests.django_asserts import assert_contains # , assert_not_contains from swh.web.tests.strategies import new_origin, visit_dates from swh.web.tests.utils import check_html_get_response @@ -225,30 +225,6 @@ assert_contains(resp, '