|
1 | | -from filesender.api import iter_files |
| 1 | +from filesender.api import iter_files, _file_key |
| 2 | +from filesender.response_types import _FileWithUid, _FileWithPuid |
| 3 | +from filesender.download import files_from_page |
2 | 4 | from pathlib import Path |
3 | 5 | import tempfile |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +# Minimal file dicts matching the structure of the real API response |
| 10 | + |
| 11 | +FILE_WITH_UID: _FileWithUid = { |
| 12 | + "id": 28000001, |
| 13 | + "transfer_id": 3500001, |
| 14 | + "uid": "abc123def456", |
| 15 | + "name": "example.txt", |
| 16 | + "size": 1024, |
| 17 | + "sha1": "da39a3ee5e6b4b0d3255bfef95601890afd80709", |
| 18 | +} |
| 19 | + |
| 20 | +FILE_WITH_PUID: _FileWithPuid = { |
| 21 | + "id": 28622026, |
| 22 | + "transfer_id": 3573766, |
| 23 | + "puid": "91dc452a-7d5c-4e34-9bee-6bc44fa5e012", |
| 24 | + "name": "final_train.h5", |
| 25 | + "size": "5243079740", # AARNet server returns size as a string |
| 26 | + "sha1": "da39a3ee5e6b4b0d3255bfef95601890afd80709", |
| 27 | +} |
| 28 | + |
| 29 | +# Minimal HTML matching the structure of the FileSender download page |
| 30 | + |
| 31 | +DOWNLOAD_PAGE_ENCRYPTED = b""" |
| 32 | +<html><body> |
| 33 | +<div class="file" |
| 34 | + data-id="1" |
| 35 | + data-transfer-id="1" |
| 36 | + data-name="secret.txt" |
| 37 | + data-size="1024" |
| 38 | + data-mime="text/plain" |
| 39 | + data-encrypted="1" |
| 40 | + data-encrypted-size="1040" |
| 41 | + data-fileiv="aabbccdd" |
| 42 | + data-fileaead="eeff0011" |
| 43 | + data-key-version="1" |
| 44 | + data-key-salt="saltsalt" |
| 45 | + data-client-entropy="entropydata" |
| 46 | + data-password-version="1" |
| 47 | + data-password-encoding="base64" |
| 48 | + data-password-hash-iterations="10000" |
| 49 | + data-transferid="1"> |
| 50 | +</div> |
| 51 | +</body></html> |
| 52 | +""" |
| 53 | + |
| 54 | +DOWNLOAD_PAGE_UNENCRYPTED = b""" |
| 55 | +<html><body> |
| 56 | +<div class="file" |
| 57 | + data-id="2" |
| 58 | + data-transfer-id="2" |
| 59 | + data-name="plain.txt" |
| 60 | + data-size="2048" |
| 61 | + data-mime="text/plain" |
| 62 | + data-encrypted="0" |
| 63 | + data-encrypted-size="" |
| 64 | + data-fileiv="" |
| 65 | + data-fileaead="" |
| 66 | + data-key-version="" |
| 67 | + data-key-salt="" |
| 68 | + data-client-entropy="" |
| 69 | + data-password-version="" |
| 70 | + data-password-encoding="" |
| 71 | + data-password-hash-iterations="" |
| 72 | + data-transferid="2"> |
| 73 | +</div> |
| 74 | +</body></html> |
| 75 | +""" |
| 76 | + |
| 77 | + |
| 78 | +def test_file_key_uid(): |
| 79 | + assert _file_key(FILE_WITH_UID) == "abc123def456" |
| 80 | + |
| 81 | + |
| 82 | +def test_file_key_puid(): |
| 83 | + assert _file_key(FILE_WITH_PUID) == "91dc452a-7d5c-4e34-9bee-6bc44fa5e012" |
| 84 | + |
| 85 | + |
| 86 | +def test_file_key_neither(): |
| 87 | + file: _FileWithUid = { |
| 88 | + "id": 1, |
| 89 | + "transfer_id": 1, |
| 90 | + "uid": "x", |
| 91 | + "name": "test.txt", |
| 92 | + "size": 0, |
| 93 | + "sha1": "", |
| 94 | + } |
| 95 | + # Strip the uid at runtime to simulate a response with neither field |
| 96 | + d = dict(file) |
| 97 | + del d["uid"] |
| 98 | + with pytest.raises(Exception, match="neither 'uid' nor 'puid'"): |
| 99 | + _file_key(d) # type: ignore[arg-type] |
| 100 | + |
| 101 | + |
| 102 | +def test_files_from_page_encrypted(): |
| 103 | + (file,) = files_from_page(DOWNLOAD_PAGE_ENCRYPTED) |
| 104 | + assert file["name"] == "secret.txt" |
| 105 | + assert file["size"] == 1024 |
| 106 | + assert file["encrypted_size"] == 1040 |
| 107 | + assert file["key_version"] == 1 |
| 108 | + assert file["password_hash_iterations"] == 10000 |
| 109 | + |
| 110 | + |
| 111 | +def test_files_from_page_unencrypted(): |
| 112 | + (file,) = files_from_page(DOWNLOAD_PAGE_UNENCRYPTED) |
| 113 | + assert file["name"] == "plain.txt" |
| 114 | + assert file["size"] == 2048 |
| 115 | + assert file["encrypted_size"] is None |
| 116 | + assert file["key_version"] is None |
| 117 | + assert file["password_hash_iterations"] is None |
4 | 118 |
|
5 | 119 | def test_iter_files(): |
6 | 120 | with tempfile.TemporaryDirectory() as _tempdir: |
|
0 commit comments