Raise error instead of trying to fix bad names.

Thanks for the advice, @radl97
This commit is contained in:
Andras Schmelczer 2022-09-17 20:19:23 +02:00
parent 04c3486b83
commit ec64a0b5f1
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
4 changed files with 23 additions and 3 deletions

View file

@ -64,7 +64,13 @@ class LargeFileBase(ABC):
keep_last_n: Optional[int] = None,
cache_only_mode: bool = False,
):
self._name = re.sub(r"[^a-zA-Z0-9._-]+", "", name)
clean_name = re.sub(r"[^!a-zA-Z0-9._-]+", "", name)
if clean_name != name:
raise ValueError(
f"Given name contains illegal characters, consider changing it to: `{clean_name}`"
)
self._name = name
self._version = version
self._mode = mode
self._keep_last_n = keep_last_n

View file

@ -44,7 +44,7 @@ class LargeFileMongo(LargeFileBase):
def _client(self) -> GridFSBucket:
if self.mongo_connection_string is None or self.mongo_database is None:
raise ValueError(
"Please configure the MongoDB access options by calling LargeFileMongo.configure_credentials or set offline_mode=True in the constructor."
"Please configure the MongoDB access options by calling LargeFileMongo.configure_credentials or set cache_only_mode=True in the constructor."
)
db: Database = MongoClient(self.mongo_connection_string)[self.mongo_database]

View file

@ -56,7 +56,7 @@ class LargeFileS3(LargeFileBase):
or self.bucket_name is None
):
raise ValueError(
"Please configure the S3 access options by calling LargeFileS3.configure_credentials or set offline_mode=True in the constructor."
"Please configure the S3 access options by calling LargeFileS3.configure_credentials or set cache_only_mode=True in the constructor."
)
return boto3.client(

View file

@ -24,6 +24,20 @@ def test_uninitialized() -> None:
LargeFileS3("test-file")
def test_bad_file_name() -> None:
with pytest.raises(ValueError):
LargeFileS3("../no-no", cache_only_mode=True)
with pytest.raises(ValueError):
LargeFileS3("is this wrong?", cache_only_mode=True)
with pytest.raises(FileNotFoundError):
LargeFileS3("!378378_fer-ef", cache_only_mode=True) # this should work
with pytest.raises(FileNotFoundError):
LargeFileS3("3", cache_only_mode=True) # this should work
def test_bad_file_modes() -> None:
with pytest.raises(ValueError):
LargeFileS3("test-file", "w", version=3)