Skip to content

Commit

Permalink
En dash (#31)
Browse files Browse the repository at this point in the history
* Explicitly encode POST data

* Explicitly encode POST data

* Explicitly encode POST data

* Explicitly encode POST data
  • Loading branch information
Iain-S authored Jul 3, 2024
1 parent 84b9bb5 commit eddad41
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion controller_function/tests/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_get_desired_states(self) -> None:

# requests.get() will return an object with a .json() method...
mock_get.return_value.json.return_value = [
json.loads(x.model_dump_json()) for x in expected
json.loads(x.model_dump_json().encode("utf-8")) for x in expected
]

# ...and a status_code.
Expand Down
12 changes: 10 additions & 2 deletions status_function/status/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ def send_status(hostname_or_ip: HttpUrl, status_data: list) -> None:
"""
logger.warning("Sending status data.")

# Note that omitting the encoding appears to work but will
# fail server-side with some characters, such as en-dash.
data = (
models.AllSubscriptionStatus(status_list=status_data)
.model_dump_json()
.encode("utf-8")
)

for _ in range(2):
started_sending_at = datetime.now()
# Note that we need to
resp = requests.post(
# todo: test for this change
str(hostname_or_ip) + "accounting/all-status",
models.AllSubscriptionStatus(status_list=status_data).model_dump_json(),
data=data,
auth=BearerAuth(),
timeout=60,
)
Expand Down
12 changes: 7 additions & 5 deletions status_function/tests/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ def test_send_status(self) -> None:
],
)

expected_json = status.models.AllSubscriptionStatus(
status_list=[example_status]
).model_dump_json()
expected_data = (
status.models.AllSubscriptionStatus(status_list=[example_status])
.model_dump_json()
.encode("utf-8")
)

with patch("status.BearerAuth") as mock_auth:
with patch("requests.post") as mock_post:
Expand All @@ -113,7 +115,7 @@ def test_send_status(self) -> None:

expected_call = call(
"https://my.org/accounting/all-status",
expected_json,
data=expected_data,
auth=mock_auth.return_value,
timeout=60,
)
Expand All @@ -137,7 +139,7 @@ def test_send_status(self) -> None:

mock_post.assert_called_once_with(
"https://my.org/accounting/all-status",
expected_json,
data=expected_data,
auth=mock_auth.return_value,
timeout=60,
)
Expand Down
2 changes: 1 addition & 1 deletion usage_function/costmanagement/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def send_usage(hostname_or_ip, all_usage):
logger.warning("Uploading cost-management usage.")
resp = requests.post(
hostname_or_ip + "/accounting/all-cm-usage",
all_usage.model_dump_json(),
data=all_usage.model_dump_json().encode("utf-8"),
auth=BearerAuth(),
timeout=60,
)
Expand Down
6 changes: 3 additions & 3 deletions usage_function/tests/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def test_send_usage(self) -> None:
),
]
)
expected_json = local_usage.model_dump_json()
expected_data = local_usage.model_dump_json().encode("utf-8")

with patch("costmanagement.BearerAuth") as mock_auth:
with patch("requests.post") as mock_post:
Expand All @@ -326,7 +326,7 @@ def send():

expected_call = call(
"https://123.234.345.456/accounting/all-cm-usage",
expected_json,
data=expected_data,
auth=mock_auth.return_value,
timeout=60,
)
Expand Down Expand Up @@ -356,7 +356,7 @@ def send():

mock_post.assert_called_once_with(
"https://123.234.345.456/accounting/all-cm-usage",
expected_json,
data=expected_data,
auth=mock_auth.return_value,
timeout=60,
)
Expand Down
20 changes: 12 additions & 8 deletions usage_function/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ def test_retrieve_and_send_usage(self) -> None:

usage = utils.models.Usage(**usage_dict)

expected_json = utils.models.AllUsage(
usage_list=[usage]
).model_dump_json()
expected_data = (
utils.models.AllUsage(usage_list=[usage])
.model_dump_json()
.encode("utf-8")
)

expected_call = call(
"https://123.123.123.123/accounting/all-usage",
expected_json,
data=expected_data,
auth=mock_auth.return_value,
timeout=60,
)
Expand All @@ -160,13 +162,15 @@ def test_retrieve_and_send_usage(self) -> None:

usage = utils.usage.models.Usage(**usage_dict)

expected_json = utils.usage.models.AllUsage(
usage_list=[usage]
).model_dump_json()
expected_data = (
utils.usage.models.AllUsage(usage_list=[usage])
.model_dump_json()
.encode("utf-8")
)

mock_post.assert_called_once_with(
"https://123.123.123.123/accounting/all-usage",
expected_json,
data=expected_data,
auth=mock_auth.return_value,
timeout=60,
)
Expand Down
6 changes: 5 additions & 1 deletion usage_function/utils/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,14 @@ def get_first_run_time():
else:
path = "accounting/all-usage"

# Note that omitting the encoding appears to work but will
# fail server-side with some characters, such as en-dash.
data = models.AllUsage(usage_list=all_item_list).model_dump_json().encode("utf-8")

for _ in range(2):
resp = requests.post(
str(hostname_or_ip) + path,
models.AllUsage(usage_list=all_item_list).model_dump_json(),
data=data,
auth=BearerAuth(),
timeout=60,
)
Expand Down

0 comments on commit eddad41

Please sign in to comment.