Skip to content

Commit

Permalink
Call MarklogicApiClient explicitly to set user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Sep 4, 2023
1 parent 9192e25 commit 835c18f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
15 changes: 14 additions & 1 deletion ds-caselaw-ingester/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@
from boto3.session import Session
from botocore.exceptions import NoCredentialsError
from caselawclient.Client import (
DEFAULT_USER_AGENT,
MarklogicApiClient,
MarklogicCommunicationError,
MarklogicResourceNotFoundError,
api_client,
)
from dotenv import load_dotenv
from notifications_python_client.notifications import NotificationsAPIClient

load_dotenv()


rollbar.init(os.getenv("ROLLBAR_TOKEN"), environment=os.getenv("ROLLBAR_ENV"))

api_client = MarklogicApiClient(
host=os.getenv("MARKLOGIC_HOST", default=None),
username=os.getenv("MARKLOGIC_USER", default=None),
password=os.getenv("MARKLOGIC_PASSWORD", default=None),
use_https=os.getenv("MARKLOGIC_USE_HTTPS", default=False),
user_agent=f"ds-caselaw-ingester/unknown {DEFAULT_USER_AGENT}",
)


class Message(object):
@classmethod
Expand Down
25 changes: 17 additions & 8 deletions ds-caselaw-ingester/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from caselawclient.Client import (
MarklogicCommunicationError,
MarklogicResourceNotFoundError,
api_client,
)
from notifications_python_client.notifications import NotificationsAPIClient

Expand Down Expand Up @@ -239,7 +238,8 @@ def test_extract_docx_filename_failure(self):
with pytest.raises(lambda_function.DocxFilenameNotFoundException):
lambda_function.extract_docx_filename(metadata, "anything")

def test_store_metadata(self):
@patch("lambda_function.api_client")
def test_store_metadata(self, api_client):
metadata = {
"parameters": {
"TDR": {
Expand Down Expand Up @@ -580,14 +580,16 @@ def test_malformed_message(self):
with pytest.raises(lambda_function.InvalidMessageException):
lambda_function.get_consignment_reference(message)

def test_update_judgment_xml_success(self):
@patch("lambda_function.api_client")
def test_update_judgment_xml_success(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.get_judgment_xml = MagicMock(return_value=True)
api_client.save_judgment_xml = MagicMock(return_value=True)
result = lambda_function.update_judgment_xml("a/fake/uri", xml)
assert result is True

def test_update_judgment_xml_judgment_does_not_exist(self):
@patch("lambda_function.api_client")
def test_update_judgment_xml_judgment_does_not_exist(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.get_judgment_xml = MagicMock(
side_effect=MarklogicResourceNotFoundError("error")
Expand All @@ -596,7 +598,8 @@ def test_update_judgment_xml_judgment_does_not_exist(self):
result = lambda_function.update_judgment_xml("a/fake/uri", xml)
assert result is False

def test_update_judgment_xml_judgment_does_not_save(self):
@patch("lambda_function.api_client")
def test_update_judgment_xml_judgment_does_not_save(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.get_judgment_xml = MagicMock(return_value=True)
api_client.save_judgment_xml = MagicMock(
Expand All @@ -605,13 +608,15 @@ def test_update_judgment_xml_judgment_does_not_save(self):
result = lambda_function.update_judgment_xml("a/fake/uri", xml)
assert result is False

def test_insert_document_xml_success(self):
@patch("lambda_function.api_client")
def test_insert_document_xml_success(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.insert_document_xml = MagicMock(return_value=True)
result = lambda_function.insert_document_xml("a/fake/uri", xml)
assert result is True

def test_insert_document_xml_failure(self):
@patch("lambda_function.api_client")
def test_insert_document_xml_failure(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.insert_document_xml = MagicMock(
side_effect=MarklogicCommunicationError("error")
Expand Down Expand Up @@ -695,8 +700,12 @@ def test_get_best_xml_with_no_xml_file(self):
assert result.__class__ == ET.Element
assert result.tag == "error"

def test_unpublish_updated_judgment(self):
@patch("lambda_function.api_client")
def test_unpublish_updated_judgment(self, api_client):
uri = "a/fake/uri"
api_client.set_published = MagicMock()
lambda_function.unpublish_updated_judgment(uri)
api_client.set_published.assert_called_with(uri, False)

def test_user_agent(self):
assert "ingester" in lambda_function.api_client.session.headers["User-Agent"]
3 changes: 2 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
django-environ~=0.10
ds-caselaw-marklogic-api-client==14.0.2
ds-caselaw-marklogic-api-client==14.1.0
requests-toolbelt~=1.0
urllib3~=1.26
boto3
Expand All @@ -8,3 +8,4 @@ notifications-python-client~=8.0

mypy-boto3-s3
mypy-boto3-sns
python-dotenv
2 changes: 1 addition & 1 deletion scripts/test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MARKLOGIC_HOST= MARKLOGIC_USER= MARKLOGIC_PASSWORD= python -m pytest ds-caselaw-ingester/tests.py $*
python -m pytest ds-caselaw-ingester/tests.py $*

0 comments on commit 835c18f

Please sign in to comment.