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
23 changes: 21 additions & 2 deletions application/tests/web_main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,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 to CWE/456 — should redirect directly to the CRE
response = client.get(
"/smartlink/standard/CWE/456",
headers={"Content-Type": "application/json"},
Expand All @@ -577,9 +578,10 @@ 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 to ASVS/v0.1.2 — should redirect directly to the CRE
response = client.get(
"/smartlink/standard/ASVS/v0.1.2",
headers={"Content-Type": "application/json"},
Expand All @@ -588,7 +590,24 @@ def test_smartlink(self) -> None:
for head in response.headers:
if head[0] == "Location":
location = head[1]
self.assertEqual(location, "/node/standard/ASVS/section/v0.1.2")
self.assertEqual(location, "/cre/333-333")
self.assertEqual(302, response.status_code)

# Multiple CREs linked — should redirect to the node page, not a single CRE
multi_std = defs.Standard(name="MultiCRE", section="s1")
dmulti = collection.add_node(multi_std)
collection.add_link(dcd, dmulti, ltype=defs.LinkTypes.LinkedTo)
collection.add_link(dcb, dmulti, ltype=defs.LinkTypes.LinkedTo)

response = client.get(
"/smartlink/standard/MultiCRE/s1",
headers={"Content-Type": "application/json"},
)
location = ""
for head in response.headers:
if head[0] == "Location":
location = head[1]
self.assertEqual(location, "/node/standard/MultiCRE/section/s1")
self.assertEqual(302, response.status_code)

# negative test, this cwe does not exist, therefore we redirect to Mitre!
Expand Down
7 changes: 7 additions & 0 deletions application/web/web_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,13 @@ def smartlink(
)
found_section_id = True
if nodes and len(nodes[0].links):
if len(nodes[0].links) == 1:
cre_doc = nodes[0].links[0].document
logger.info(
f"found node of type {ntype}, name {name} and section {section}, "
f"single CRE linked, redirecting directly to CRE {cre_doc.id}"
)
return redirect(f"/cre/{cre_doc.id}")
logger.info(
f"found node of type {ntype}, name {name} and section {section}, redirecting to opencre"
)
Expand Down
Loading