Skip to content

Commit

Permalink
Merge pull request #43 from greglucas/rm-upload-restrictions
Browse files Browse the repository at this point in the history
FIX: Remove restriction on upload data directory structure
  • Loading branch information
greglucas authored Jun 11, 2024
2 parents a44fc82 + 5846db5 commit 2a1abc9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The folder structure for data files within the IMAP SDC is rigidly
defined, so the data access will mimic that structure to make sure
all data is stored in the same heirarchical structure as the SDC.
This will enable seamless transition between a user's local system
and the SDC.
and the SDC. This is only used for downloads.

A user's root data location can be specified as an environment
variable ``IMAP_DATA_DIR`` or through a configuration dictionary
Expand Down
17 changes: 2 additions & 15 deletions imap_data_access/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ def upload(file_path: Union[Path, str], *, api_key: Optional[str] = None) -> Non
Parameters
----------
file_path : pathlib.Path or str
Path to the file to upload. It must be located within
the ``imap_data_access.config["DATA_DIR"]`` directory.
Path to the file to upload.
api_key : str, optional
API key to authenticate with the data access API. If not provided,
the value from the IMAP_API_KEY environment variable will be used.
Expand All @@ -174,21 +173,9 @@ def upload(file_path: Union[Path, str], *, api_key: Optional[str] = None) -> Non
if not file_path.exists():
raise FileNotFoundError(file_path)

if not file_path.is_relative_to(imap_data_access.config["DATA_DIR"]):
raise ValueError(
f"File {file_path} is not within the data directory: "
f"{imap_data_access.config['DATA_DIR']}"
)

# Strip off the data directory to get the upload path + name
# Must be posix style for the URL
upload_name = str(
file_path.relative_to(imap_data_access.config["DATA_DIR"]).as_posix()
)

url = f"{imap_data_access.config['DATA_ACCESS_URL']}"
# The upload name needs to be given as a path parameter
url += f"/upload/{upload_name}"
url += f"/upload/{file_path.name}"
logger.info("Uploading file %s to %s", file_path, url)

# Create a request header with the API key
Expand Down
21 changes: 1 addition & 20 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,6 @@ def test_upload_no_file(mock_urlopen: unittest.mock.MagicMock):
assert mock_urlopen.call_count == 0


def test_upload_not_relative_to_base(
monkeypatch: pytest.fixture, mock_urlopen: unittest.mock.MagicMock
):
"""Test a call to the upload API for a file stored in a bad location.
Parameters
----------
monkeypatch : pytest.fixture
Used to set the ``DATA_DIR`` during tests
mock_urlopen : unittest.mock.MagicMock
Mock object for ``urlopen``
"""
# Change the base directory to something else temporarily
monkeypatch.setitem(imap_data_access.config, "DATA_DIR", Path.cwd() / "/a/b/c")
with pytest.raises(ValueError, match="File"):
imap_data_access.upload(Path(__file__))
assert mock_urlopen.call_count == 0


@pytest.mark.parametrize(
"upload_file_path", ["a/b/test-file.txt", Path("a/b/test-file.txt")]
)
Expand Down Expand Up @@ -307,7 +288,7 @@ def test_upload(
urlopen_call = mock_calls[0]
request_sent = urlopen_call.args[0]
called_url = request_sent.full_url
expected_url_encoded = "https://api.test.com/upload/a/b/test-file.txt"
expected_url_encoded = "https://api.test.com/upload/test-file.txt"
assert called_url == expected_url_encoded
assert request_sent.method == "GET"
# An API key needs to be added to the header for uploads
Expand Down

0 comments on commit 2a1abc9

Please sign in to comment.