Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion application/tests/web_main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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"},
Expand Down
12 changes: 12 additions & 0 deletions application/web/web_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down