Skip to content

Commit

Permalink
Use signed image urls for saxon HTML transform
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Nov 1, 2024
1 parent 7a3605c commit bdb16c9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
67 changes: 67 additions & 0 deletions judgments/tests/fixtures/sample_judgment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<akomaNtoso xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:uk="https://caselaw.nationalarchives.gov.uk/akn">
<judgment name="decision">
<meta>
<identification source="#tna">
<FRBRWork>
<FRBRthis value="https://caselaw.nationalarchives.gov.uk/id/ewhc/ch/2023/9999"/>
<FRBRuri value="https://caselaw.nationalarchives.gov.uk/id/ewhc/ch/2023/9999"/>
<FRBRdate date="2023-06-05" name="decision"/>
<FRBRauthor href="#"/>
<FRBRcountry value="GB-UKM"/>
<FRBRnumber value=""/>
<FRBRname value="Sample v Judgment"/>
</FRBRWork>
<FRBRExpression>
<FRBRthis value="https://caselaw.nationalarchives.gov.uk/ewhc/ch/2023/9999"/>
<FRBRuri value="https://caselaw.nationalarchives.gov.uk/ewhc/ch/2023/9999"/>
<FRBRdate date="2023-06-05" name="decision"/>
<FRBRauthor href="#"/>
<FRBRlanguage language="eng"/>
</FRBRExpression>
<FRBRManifestation>
<FRBRthis value="https://caselaw.nationalarchives.gov.uk/ewhc/ch/2023/9999/data.xml"/>
<FRBRuri value="https://caselaw.nationalarchives.gov.uk/ewhc/ch/2023/9999/data.xml"/>
<FRBRdate date="2023-06-05T13:07:36" name="transform"/>
<FRBRauthor href="#tna"/>
<FRBRformat value="application/xml"/>
</FRBRManifestation>
</identification>
<lifecycle source="#">
<eventRef date="2023-06-05" refersTo="#decision" source="#"/>
</lifecycle>
<references source="#tna">
<TLCOrganization eId="tna" href="https://www.nationalarchives.gov.uk/" showAs="The National Archives"/>
<TLCEvent eId="decision" href="#" showAs="decision"/>
</references>
<proprietary source="#">
<uk:parser>0.14.8</uk:parser>
<uk:hash>376b2d62bc03ad74f3793f7b6175580eb44f9e2771e939732cf48e9000000000</uk:hash>
<uk:cite>[2023] EWHC 9999 (Ch)</uk:cite>
<uk:court>EWHC-Chancery</uk:court>
</proprietary>
<presentation source="#">
<html:style>
#judgment { font-size: 11pt; font-family: Calibri, sans-serif; }
#judgment .ListParagraph { margin-left: 0.5in; }
#judgment .FootnoteText { font-size: 10pt; }
#judgment .NormalWeb { font-family: 'Times New Roman'; font-size: 12pt; }
#judgment .FootnoteTextChar { font-size: 10pt; }
#judgment .FootnoteReference { vertical-align: super; }
#judgment .Strong { font-weight: bold; }
#judgment .TableGrid { }
#judgment .TableGrid td { border: 0.5pt solid; }
</html:style>
</presentation>
</meta>
<header>
<p>sample judgment</p>
</header>
<judgmentBody>
<decision>
<p>sample judgment with a unique word: capybara</p>
<img src="cat.jpg" />
</decision>
</judgmentBody>
</judgment>
</akomaNtoso>
30 changes: 28 additions & 2 deletions judgments/tests/test_view_helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from unittest.mock import patch

import pytest
from caselawclient.errors import DocumentNotFoundError
from caselawclient.factories import JudgmentFactory
from caselawclient.factories import DocumentBodyFactory, JudgmentFactory
from django.contrib.auth.models import Group, User
from django.http import Http404
from django.test import TestCase
from django.test import Client, TestCase

from judgments.utils.view_helpers import (
get_document_by_uri_or_404,
Expand Down Expand Up @@ -69,3 +70,28 @@ def test_is_developer(self):
assert user_is_developer(self.super_user) is False
assert user_is_developer(self.editor_user) is False
assert user_is_developer(self.developer_user) is True


@pytest.mark.django_db
class TestDocumentView(TestCase):
client = Client(raise_request_exception=False)

@patch.dict(os.environ, {"XSLT_IMAGE_LOCATION": "/VALUE_OF_ENVIRONMENT_VARIABLE/"})
@patch("judgments.utils.view_helpers.get_linked_document_uri")
@patch("judgments.utils.view_helpers.get_document_by_uri_or_404")
def test_document_view_has_image(
self,
mock_get_document_by_uri,
mock_get_linked_document_uri,
):
self.client.force_login(User.objects.get_or_create(username="testuser")[0])
with open("judgments/tests/fixtures/sample_judgment.xml") as f:
sample_judgment = f.read()
mock_get_document_by_uri.return_value = JudgmentFactory.build(
uri="/eat/2023/1",
body=DocumentBodyFactory.build(xml_string=sample_judgment),
)
response = self.client.get("/eat/2023/1")

assert b"capybara" in response.content
assert b"/VALUE_OF_ENVIRONMENT_VARIABLE/ewhc/ch/2023/9999/cat.jpg" in response.content
10 changes: 8 additions & 2 deletions judgments/utils/view_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any

import ds_caselaw_utils as caselawutils
import environ
from caselawclient.client_helpers.search_helpers import search_and_parse_response
from caselawclient.errors import DocumentNotFoundError
from caselawclient.models.documents import Document, DocumentURIString
Expand All @@ -12,6 +13,7 @@
from judgments.utils.link_generators import build_jira_create_link
from judgments.utils.paginator import paginator

env = environ.Env()
RESULTS_ORDER = "-date"


Expand Down Expand Up @@ -88,11 +90,15 @@ def get_context_data(self, **kwargs):

version_uri = self.request.GET.get("version_uri", None)

xslt_image_location = env("XSLT_IMAGE_LOCATION", default=None)

if version_uri:
context["current_version_number"] = extract_version_number_from_filename(version_uri)
context["document_html"] = get_document_by_uri_or_404(version_uri).body.content_as_html
context["document_html"] = get_document_by_uri_or_404(version_uri).body.content_as_html(
image_base_url=xslt_image_location,
)
else:
context["document_html"] = document.body.content_as_html
context["document_html"] = document.body.content_as_html(image_base_url=xslt_image_location)

# TODO: Remove this once we fully deprecate 'judgment' contexts
context["judgment"] = document
Expand Down

0 comments on commit bdb16c9

Please sign in to comment.