Skip to content

Commit 8a18342

Browse files
authored
tests + correct utf-8 header set for upload_stream (#159)
The same bug that was found in: #157 Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent 233e925 commit 8a18342

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ answer newbie questions, and generally made NC-Py-API that much better:
55
Andrey Borysenko <andrey18106x@gmail.com>
66
Alexander Piskun <bigcat88@icloud.com>
77
CooperGerman <https://github.com/CooperGerman>
8+
Tobias Tschech <Tobias@tschech-online.de>
89
<Please alphabetize new entries>
910

1011
A big THANK YOU goes to:

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [0.5.1 - 2023-11-xx]
5+
## [0.5.1 - 2023-11-12]
66

77
### Fixed
88

9+
- `move`, `copy`, `trashbin_restore` correctly set `utf-8` headers. #157 Thanks to @tschechniker
10+
- `upload_stream` correctly set `utf-8` headers. #159
911
- `headers` can now be `httpx.Headers` and not only `dict`. #158
1012

1113
## [0.5.0 - 2023-10-23]

nc_py_api/files/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ def __upload_stream(self, path: str, fp, chunk_size: int) -> FsNode:
696696
_dav_path = self._dav_get_obj_path(self._session.user, "nc-py-api-" + random_string(56), root_path="/uploads")
697697
_v2 = bool(self._session.cfg.options.upload_chunk_v2 and chunk_size >= 5 * 1024 * 1024)
698698
full_path = self._dav_get_obj_path(self._session.user, path)
699-
headers = {"Destination": self._session.cfg.dav_endpoint + full_path}
699+
headers = Headers({"Destination": self._session.cfg.dav_endpoint + full_path}, encoding="utf-8")
700700
if _v2:
701701
response = self._session.dav("MKCOL", _dav_path, headers=headers)
702702
else:

tests/actual_tests/files_test.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,14 @@ def test_file_upload_file(nc_any):
201201
assert nc_any.files.download("test_dir_tmp/test_file_upload_file") == content
202202

203203

204-
def test_file_upload_chunked_v2(nc_any):
204+
@pytest.mark.parametrize("dest_path", ("test_dir_tmp/test_file_upl_chunk_v2", "test_dir_tmp/test_file_upl_chunk_v2_ü"))
205+
def test_file_upload_chunked_v2(nc_any, dest_path):
205206
with NamedTemporaryFile() as tmp_file:
206207
tmp_file.seek(7 * 1024 * 1024)
207208
tmp_file.write(b"\0")
208209
tmp_file.flush()
209-
nc_any.files.upload_stream("test_dir_tmp/test_file_upload_chunked_v2", tmp_file.name)
210-
assert len(nc_any.files.download("test_dir_tmp/test_file_upload_chunked_v2")) == 7 * 1024 * 1024 + 1
210+
nc_any.files.upload_stream(dest_path, tmp_file.name)
211+
assert len(nc_any.files.download(dest_path)) == 7 * 1024 * 1024 + 1
211212

212213

213214
@pytest.mark.parametrize("file_name", ("chunked_zero", "chunked_zero/", "chunked_zero//"))
@@ -288,43 +289,44 @@ def test_favorites(nc_any):
288289
assert not favorites
289290

290291

291-
def test_copy_file(nc_any, rand_bytes):
292-
copied_file = nc_any.files.copy("test_64_bytes.bin", "test_dir_tmp/test_64_bytes.bin")
292+
@pytest.mark.parametrize("dest_path", ("test_dir_tmp/test_64_bytes.bin", "test_dir_tmp/test_64_bytes_ü.bin"))
293+
def test_copy_file(nc_any, rand_bytes, dest_path):
294+
copied_file = nc_any.files.copy("test_64_bytes.bin", dest_path)
293295
assert copied_file.file_id
294296
assert copied_file.is_dir is False
295-
assert nc_any.files.download("test_dir_tmp/test_64_bytes.bin") == rand_bytes
297+
assert nc_any.files.download(dest_path) == rand_bytes
296298
with pytest.raises(NextcloudException):
297-
nc_any.files.copy("test_64_bytes.bin", "test_dir_tmp/test_64_bytes.bin")
298-
copied_file = nc_any.files.copy("test_12345_text.txt", "test_dir_tmp/test_64_bytes.bin", overwrite=True)
299+
nc_any.files.copy("test_64_bytes.bin", dest_path)
300+
copied_file = nc_any.files.copy("test_12345_text.txt", dest_path, overwrite=True)
299301
assert copied_file.file_id
300302
assert copied_file.is_dir is False
301-
assert nc_any.files.download("test_dir_tmp/test_64_bytes.bin") == b"12345"
303+
assert nc_any.files.download(dest_path) == b"12345"
302304

303305

304-
def test_move_file(nc_any):
306+
@pytest.mark.parametrize("dest_path", ("test_dir_tmp/dest move test file", "test_dir_tmp/dest move test file-ä"))
307+
def test_move_file(nc_any, dest_path):
305308
src = "test_dir_tmp/src move test file"
306-
dest = "test_dir_tmp/dest move test file"
307309
content = b"content of the file"
308310
content2 = b"content of the file-second part"
309311
nc_any.files.upload(src, content=content)
310-
nc_any.files.delete(dest, not_fail=True)
311-
result = nc_any.files.move(src, dest)
312+
nc_any.files.delete(dest_path, not_fail=True)
313+
result = nc_any.files.move(src, dest_path)
312314
assert result.etag
313315
assert result.file_id
314316
assert result.is_dir is False
315-
assert nc_any.files.download(dest) == content
317+
assert nc_any.files.download(dest_path) == content
316318
with pytest.raises(NextcloudException):
317319
nc_any.files.download(src)
318320
nc_any.files.upload(src, content=content2)
319321
with pytest.raises(NextcloudException):
320-
nc_any.files.move(src, dest)
321-
result = nc_any.files.move(src, dest, overwrite=True)
322+
nc_any.files.move(src, dest_path)
323+
result = nc_any.files.move(src, dest_path, overwrite=True)
322324
assert result.etag
323325
assert result.file_id
324326
assert result.is_dir is False
325327
with pytest.raises(NextcloudException):
326328
nc_any.files.download(src)
327-
assert nc_any.files.download(dest) == content2
329+
assert nc_any.files.download(dest_path) == content2
328330

329331

330332
def test_move_copy_dir(nc_any):
@@ -515,10 +517,11 @@ def test_fs_node_last_modified_time():
515517
assert fs_node.info.last_modified == datetime(2022, 4, 5, 1, 2, 3)
516518

517519

518-
def test_trashbin(nc_any):
520+
@pytest.mark.parametrize("file_path", ("test_dir_tmp/trashbin_test", "test_dir_tmp/trashbin_test-ä"))
521+
def test_trashbin(nc_any, file_path):
519522
r = nc_any.files.trashbin_list()
520523
assert isinstance(r, list)
521-
new_file = nc_any.files.upload("test_dir_tmp/trashbin_test", content=b"")
524+
new_file = nc_any.files.upload(file_path, content=b"")
522525
nc_any.files.delete(new_file)
523526
# minimum one object now in a trashbin
524527
r = nc_any.files.trashbin_list()
@@ -528,7 +531,7 @@ def test_trashbin(nc_any):
528531
# no objects should be in trashbin
529532
r = nc_any.files.trashbin_list()
530533
assert not r
531-
new_file = nc_any.files.upload("test_dir_tmp/trashbin_test", content=b"")
534+
new_file = nc_any.files.upload(file_path, content=b"")
532535
nc_any.files.delete(new_file)
533536
# one object now in a trashbin
534537
r = nc_any.files.trashbin_list()
@@ -537,7 +540,7 @@ def test_trashbin(nc_any):
537540
i: FsNode = r[0]
538541
assert i.info.in_trash is True
539542
assert i.info.trashbin_filename.find("trashbin_test") != -1
540-
assert i.info.trashbin_original_location == "test_dir_tmp/trashbin_test"
543+
assert i.info.trashbin_original_location == file_path
541544
assert isinstance(i.info.trashbin_deletion_time, int)
542545
# restore that object
543546
nc_any.files.trashbin_restore(r[0])
@@ -561,13 +564,14 @@ def test_trashbin(nc_any):
561564
assert not r
562565

563566

564-
def test_file_versions(nc_any):
567+
@pytest.mark.parametrize("dest_path", ("/test_dir_tmp/file_versions.txt", "/test_dir_tmp/file_versions-ä.txt"))
568+
def test_file_versions(nc_any, dest_path):
565569
if nc_any.check_capabilities("files.versioning"):
566570
pytest.skip("Need 'Versions' App to be enabled.")
567571
for i in (0, 1):
568-
nc_any.files.delete("/test_dir_tmp/file_versions_test.txt", not_fail=True)
569-
nc_any.files.upload("/test_dir_tmp/file_versions_test.txt", content=b"22")
570-
new_file = nc_any.files.upload("/test_dir_tmp/file_versions_test.txt", content=b"333")
572+
nc_any.files.delete(dest_path, not_fail=True)
573+
nc_any.files.upload(dest_path, content=b"22")
574+
new_file = nc_any.files.upload(dest_path, content=b"333")
571575
if i:
572576
new_file = nc_any.files.by_id(new_file)
573577
versions = nc_any.files.get_versions(new_file)

0 commit comments

Comments
 (0)