Skip to content

Commit

Permalink
fix CLI file-upload test considering file content-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault committed Oct 20, 2023
1 parent dc98daf commit 68fb3cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
21 changes: 14 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import base64
import inspect
import json
import os
import tempfile
import uuid
from contextlib import ExitStack
Expand All @@ -25,7 +26,7 @@
WeaverClient,
main as weaver_cli
)
from weaver.formats import ContentType
from weaver.formats import ContentEncoding, ContentType


@pytest.mark.cli
Expand Down Expand Up @@ -238,18 +239,24 @@ def mock_upload(_href, *_, **__):


@pytest.mark.cli
def test_file_inputs_uploaded_to_vault():
fake_href = "https://some-host.com/some-file.zip"
@pytest.mark.parametrize(
["test_file_name", "expect_file_format"],
[
("some-file.zip", {"mediaType": ContentType.APP_ZIP, "encoding": ContentEncoding.BASE64}),
("some-text.txt", {"mediaType": ContentType.TEXT_PLAIN}),
("some-data.json", {"mediaType": ContentType.APP_JSON}),
]
)
def test_file_inputs_uploaded_to_vault(test_file_name, expect_file_format):
fake_href = f"https://some-host.com/{test_file_name}"
fake_id = "fake_id"
fake_token = "fake_token"

output_body = {"file_href": fake_href, "file_id": fake_id, "access_token": fake_token}
expected_output = (
{
"file": {
"format": {
"mediaType": ContentType.APP_ZIP
},
"format": expect_file_format,
"href": fake_href
}
},
Expand All @@ -263,7 +270,7 @@ def test_file_inputs_uploaded_to_vault():
def mock_upload(_href, *_, **__):
return mock_result

with tempfile.NamedTemporaryFile(mode="w", suffix=".zip") as input_file:
with tempfile.NamedTemporaryFile(mode="w", suffix=os.path.splitext(test_file_name)[-1]) as input_file:
inputs = {"file": {"href": input_file.name}}
with mock.patch("weaver.cli.WeaverClient.upload", side_effect=mock_upload):
result = WeaverClient()._upload_files(inputs=inputs)
Expand Down
7 changes: 5 additions & 2 deletions weaver/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,11 +1027,11 @@ def _upload_files(self, inputs, url=None):

fmt = data.get("format", {})
ctype = get_field(fmt, "mime_type", search_variations=True)
c_enc = get_field(fmt, "encoding", search_variations=True) or None
if not ctype:
ext = os.path.splitext(href)[-1]
ctype = get_content_type(ext)
fmt = get_format(ctype, default=ContentType.TEXT_PLAIN)
c_enc = get_field(fmt, "encoding", search_variations=True) or None
res = self.upload(href, content_type=fmt.mime_type, content_encoding=c_enc, url=url)
if res.code != 200:
return res
Expand All @@ -1040,7 +1040,10 @@ def _upload_files(self, inputs, url=None):
token = res.body["access_token"]
auth_tokens[vault_id] = token
LOGGER.info("Converted (input: %s) [%s] -> [%s]", input_id, file, vault_href)
input_vault_href = {"href": vault_href, "format": {"mediaType": ctype, "encoding": c_enc}}
input_vault_href = {
"href": vault_href,
"format": {"mediaType": ctype, "encoding": c_enc} if c_enc else {"mediaType": ctype}
}
if input_array:
update_inputs[input_id][input_index] = input_vault_href
else:
Expand Down
12 changes: 7 additions & 5 deletions weaver/processes/builtin/echo_process.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ inputs:
uom:
type: string
reference:
type: anyUri
type: string
format: uri
dateInput:
title: Date Literal Input Example
description: This is an example of a DATE literal input.
Expand Down Expand Up @@ -57,6 +58,9 @@ inputs:
description: This is an example of a complex object input.
schema:
type: object
required:
- property1
- property5
properties:
property1:
type: string
Expand All @@ -68,9 +72,6 @@ inputs:
type: dateTime
property5:
type: boolean
required:
- property1
- property5
geometryInput:
title: Geometry input
description: |
Expand Down Expand Up @@ -159,7 +160,8 @@ outputs:
uom:
type: string
reference:
type: anyURI
type: string
format: uri
dateOutput:
schema:
type: dateTime
Expand Down

0 comments on commit 68fb3cd

Please sign in to comment.