diff --git a/application/tests/web_main_test.py b/application/tests/web_main_test.py index e5d285acd..2ff274ed2 100644 --- a/application/tests/web_main_test.py +++ b/application/tests/web_main_test.py @@ -571,6 +571,7 @@ def test_smartlink(self) -> None: collection.add_link(dcb, dasvs, ltype=defs.LinkTypes.LinkedTo) collection.add_link(dcd, dcwe, ltype=defs.LinkTypes.LinkedTo) + # single CRE linked via sectionID: should redirect directly to the CRE page (issue #486) response = client.get( "/smartlink/standard/CWE/456", headers={"Content-Type": "application/json"}, @@ -579,9 +580,27 @@ def test_smartlink(self) -> None: for head in response.headers: if head[0] == "Location": location = head[1] - self.assertEqual(location, "/node/standard/CWE/sectionid/456") + self.assertEqual(location, "/cre/222-222") self.assertEqual(302, response.status_code) + # single CRE linked via section: should redirect directly to the CRE page (issue #486) + response = client.get( + "/smartlink/standard/ASVS/v0.1.2", + headers={"Content-Type": "application/json"}, + ) + location = "" + for head in response.headers: + if head[0] == "Location": + location = head[1] + self.assertEqual(location, "/cre/333-333") + self.assertEqual(302, response.status_code) + + # multi-CRE case: add a second CRE linked to the same ASVS node, + # should fall back to the node page instead of jumping to a CRE + cres["ce"] = defs.CRE(id="444-444", description="CE", name="CE", tags=["te"]) + dce = collection.add_cre(cres["ce"]) + collection.add_link(dce, dasvs, ltype=defs.LinkTypes.LinkedTo) + response = client.get( "/smartlink/standard/ASVS/v0.1.2", headers={"Content-Type": "application/json"}, diff --git a/application/web/web_main.py b/application/web/web_main.py index 4049f8981..6bff58e53 100644 --- a/application/web/web_main.py +++ b/application/web/web_main.py @@ -756,6 +756,18 @@ def smartlink( logger.info( f"found node of type {ntype}, name {name} and section {section}, redirecting to opencre" ) + # If there is exactly one CRE linked to this node, jump directly to the CRE page (issue #486) + cre_links = [ + link + for link in nodes[0].links + if link.document.doctype == defs.Credoctypes.CRE and link.document.id + ] + if len(cre_links) == 1: + cre_id = cre_links[0].document.id + logger.info( + f"single CRE {cre_id} linked to node {name}/{section}, redirecting directly to CRE page" + ) + return redirect(f"/cre/{cre_id}") if found_section_id: return redirect(f"/node/{ntype}/{name}/sectionid/{section}") return redirect(f"/node/{ntype}/{name}/section/{section}")