Skip to content

Commit

Permalink
Merge pull request #1773 from nationalarchives/FCL-493-eui-unexpected…
Browse files Browse the repository at this point in the history
…ly-showing-uri-mismatch-banner-for-matching-ur-is

[FCL-493] Fix NCN/URI mismatch detector incorrectly raising false positives
  • Loading branch information
jacksonj04 authored Nov 27, 2024
2 parents 67a38b1 + 7dc2300 commit d60936a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 24 additions & 1 deletion judgments/tests/test_document_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ def test_date_error(self, mock_judgment, api_client):


class TestDocumentBadURIWarning(TestCase):
MISMATCH_HEADER_STRING = "Document URI/NCN mismatch"

@patch("judgments.utils.view_helpers.get_document_by_uri_or_404")
@patch("judgments.utils.view_helpers.get_linked_document_uri")
def test_good_ncn_has_no_banner(self, linked_document_uri, mock_judgment):
judgment = JudgmentFactory.build(
uri=DocumentURIString("uksc/1234/123"),
neutral_citation="[1234] UKSC 123",
body=DocumentBodyFactory.build(name="Test v Tested"),
)

mock_judgment.return_value = judgment

self.client.force_login(User.objects.get_or_create(username="testuser")[0])

response = self.client.get(
"/uksc/1234/123",
)

self.assertNotContains(response, self.MISMATCH_HEADER_STRING)

@patch("judgments.utils.view_helpers.get_document_by_uri_or_404")
@patch("judgments.utils.view_helpers.get_linked_document_uri")
def test_bad_ncn_has_banner(self, linked_document_uri, mock_judgment):
Expand All @@ -104,9 +125,11 @@ def test_bad_ncn_has_banner(self, linked_document_uri, mock_judgment):
"/uksc/1234/123",
)

self.assertContains(response, self.MISMATCH_HEADER_STRING)

root = lxml.html.fromstring(response.content)
message = lxml.html.tostring(root.xpath("//div[@class='page-notification--warning']")[0])
assert b"Document URI/NCN mismatch" in message

assert b"This document is currently located at <strong>/uksc/1234/123</strong>" in message
assert b"but based on its NCN should be at <strong>/uksc/1234/999</strong>" in message
assert b'<input type="hidden" name="judgment_uri" value="uksc/1234/123">' in message
Expand Down
4 changes: 2 additions & 2 deletions judgments/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ def _build_related_document_uri(document: Document) -> str:
return document.uri + press_summary_suffix


def get_corrected_ncn_url(document) -> str | None:
def get_corrected_ncn_url(document: Document) -> str | None:
ncn_uri = caselawutils.neutral_url(document.neutral_citation)

if "press-summary" in document.uri:
return None
if "/" + document.uri != ncn_uri:
if document.uri != ncn_uri:
return ncn_uri
return None

0 comments on commit d60936a

Please sign in to comment.