From 7dc23009ad30a18a560e296d6cb8c9d48f1174ae Mon Sep 17 00:00:00 2001 From: Nick Jackson Date: Wed, 27 Nov 2024 11:27:46 +0000 Subject: [PATCH] URI mismatch detector is now correctly comparing strings --- judgments/tests/test_document_edit.py | 25 ++++++++++++++++++++++++- judgments/utils/__init__.py | 4 ++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/judgments/tests/test_document_edit.py b/judgments/tests/test_document_edit.py index 934aefe6a..5f08703ca 100644 --- a/judgments/tests/test_document_edit.py +++ b/judgments/tests/test_document_edit.py @@ -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): @@ -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 /uksc/1234/123" in message assert b"but based on its NCN should be at /uksc/1234/999" in message assert b'' in message diff --git a/judgments/utils/__init__.py b/judgments/utils/__init__.py index ecc8a9cc2..f2bb69649 100644 --- a/judgments/utils/__init__.py +++ b/judgments/utils/__init__.py @@ -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