Skip to content

Commit

Permalink
task/DES-2767: fix file metadata route path issue (#1235)
Browse files Browse the repository at this point in the history
* Normalize path to be consistent

* Remove some unneeded fixgtures

---------

Co-authored-by: Jake Rosenberg <[email protected]>
  • Loading branch information
nathanfranklin and jarosenb authored May 13, 2024
1 parent a68714a commit 1275b7b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
14 changes: 13 additions & 1 deletion designsafe/apps/api/filemeta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
from django.utils import timezone


def _get_normalized_path(path) -> str:
""" Return a file path that begins with /"
For example, "file.jpg" becomes "/file.jpg"
"""
if not path.startswith('/'):
path = '/' + path
return path


class FileMetaModel(models.Model):
"""Model for File Meta"""

Expand Down Expand Up @@ -41,7 +51,8 @@ def create_or_update_file_meta(cls, value):
- tuple (instance, created): The FileMetaModel instance and a boolean indicating if it was created (True) or updated (False).
"""
system = value.get("system")
path = value.get("path")
path = _get_normalized_path(value.get("path"))
value["path"] = path

# Use a transaction to ensure atomicity
with transaction.atomic():
Expand All @@ -64,4 +75,5 @@ def get_by_path_and_system(cls, system, path):
Raises:
- DoesNotExist: if file metadata entry not found
"""
path = _get_normalized_path(path)
return cls.objects.get(value__system=system, value__path=path)
27 changes: 25 additions & 2 deletions designsafe/apps/api/filemeta/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ def test_create_file_meta_update_existing_entry(
def test_create_file_metadata_missing_system_or_path(
client,
authenticated_user,
filemeta_db_mock,
filemeta_value_mock,
mock_access_success,
):
value_missing_system_path = {"foo": "bar"}
Expand All @@ -191,3 +189,28 @@ def test_create_file_metadata_missing_system_or_path(
content_type="application/json",
)
assert response.status_code == 400


@pytest.mark.django_db
def test_create_using_path_without_starting_slashes_issue_DES_2767 (
filemeta_value_mock,
):
# testing that "file.txt" and "/file.txt" are referring to the same
# file and that "file.txt" is normalized to "/file.txt"
filemeta_value_mock["path"] = "file.txt"

file_meta, created = FileMetaModel.create_or_update_file_meta(filemeta_value_mock)
assert created
assert file_meta.value["path"] == "/file.txt"


@pytest.mark.django_db
def test_get_using_path_with_or_without_starting_slashes_issue_DES_2767(
filemeta_value_mock,
):
filemeta_value_mock["path"] = "file.txt"
FileMetaModel.create_or_update_file_meta(filemeta_value_mock)

system = filemeta_value_mock["system"]
FileMetaModel.get_by_path_and_system(system, "file.txt")
FileMetaModel.get_by_path_and_system(system, "/file.txt")

0 comments on commit 1275b7b

Please sign in to comment.