| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/test_copy.py |
| 4 |
# |
| 5 |
# Copyright 2021 Backblaze Inc. All Rights Reserved. |
| 6 |
# |
| 7 |
# License https://www.backblaze.com/using_b2_code.html |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
|
| 11 |
from unittest import mock |
| 12 |
|
| 13 |
from b2sdk.v2 import ( |
| 14 |
SSE_B2_AES, |
| 15 |
UNKNOWN_KEY_ID, |
| 16 |
EncryptionAlgorithm, |
| 17 |
EncryptionKey, |
| 18 |
EncryptionMode, |
| 19 |
EncryptionSetting, |
| 20 |
) |
| 21 |
|
| 22 |
from b2._internal.console_tool import CopyFileById |
| 23 |
|
| 24 |
from .test_base import TestBase |
| 25 |
|
| 26 |
|
| 27 |
class TestCopy(TestBase): |
| 28 |
def test_determine_source_metadata(self): |
| 29 |
mock_api = mock.MagicMock() |
| 30 |
mock_console_tool = mock.MagicMock() |
| 31 |
mock_console_tool.api = mock_api |
| 32 |
copy_file_command = CopyFileById(mock_console_tool) |
| 33 |
|
| 34 |
result = copy_file_command._determine_source_metadata( |
| 35 |
'id', |
| 36 |
destination_encryption=None, |
| 37 |
source_encryption=None, |
| 38 |
target_file_info=None, |
| 39 |
target_content_type=None, |
| 40 |
fetch_if_necessary=True, |
| 41 |
) |
| 42 |
assert result == (None, None) |
| 43 |
assert len(mock_api.method_calls) == 0 |
| 44 |
|
| 45 |
result = copy_file_command._determine_source_metadata( |
| 46 |
'id', |
| 47 |
destination_encryption=SSE_B2_AES, |
| 48 |
source_encryption=SSE_B2_AES, |
| 49 |
target_file_info={}, |
| 50 |
target_content_type='', |
| 51 |
fetch_if_necessary=True, |
| 52 |
) |
| 53 |
assert result == (None, None) |
| 54 |
assert len(mock_api.method_calls) == 0 |
| 55 |
|
| 56 |
result = copy_file_command._determine_source_metadata( |
| 57 |
'id', |
| 58 |
destination_encryption=SSE_B2_AES, |
| 59 |
source_encryption=SSE_B2_AES, |
| 60 |
target_file_info={}, |
| 61 |
target_content_type='', |
| 62 |
fetch_if_necessary=True, |
| 63 |
) |
| 64 |
assert result == (None, None) |
| 65 |
assert len(mock_api.method_calls) == 0 |
| 66 |
|
| 67 |
source_sse_c = EncryptionSetting( |
| 68 |
EncryptionMode.SSE_C, EncryptionAlgorithm.AES256, |
| 69 |
EncryptionKey(b'some_key', UNKNOWN_KEY_ID) |
| 70 |
) |
| 71 |
destination_sse_c = EncryptionSetting( |
| 72 |
EncryptionMode.SSE_C, EncryptionAlgorithm.AES256, |
| 73 |
EncryptionKey(b'some_other_key', 'key_id') |
| 74 |
) |
| 75 |
|
| 76 |
result = copy_file_command._determine_source_metadata( |
| 77 |
'id', |
| 78 |
destination_encryption=destination_sse_c, |
| 79 |
source_encryption=source_sse_c, |
| 80 |
target_file_info={}, |
| 81 |
target_content_type='', |
| 82 |
fetch_if_necessary=True, |
| 83 |
) |
| 84 |
assert result == (None, None) |
| 85 |
assert len(mock_api.method_calls) == 0 |
| 86 |
|
| 87 |
with self.assertRaises( |
| 88 |
ValueError, 'Attempting to copy file with metadata while either source or ' |
| 89 |
'destination uses SSE-C. Use --fetch-metadata to fetch source ' |
| 90 |
'file metadata before copying.' |
| 91 |
): |
| 92 |
copy_file_command._determine_source_metadata( |
| 93 |
'id', |
| 94 |
destination_encryption=destination_sse_c, |
| 95 |
source_encryption=source_sse_c, |
| 96 |
target_file_info=None, |
| 97 |
target_content_type=None, |
| 98 |
fetch_if_necessary=False, |
| 99 |
) |
| 100 |
assert len(mock_api.method_calls) == 0 |
| 101 |
|
| 102 |
result = copy_file_command._determine_source_metadata( |
| 103 |
'id', |
| 104 |
destination_encryption=destination_sse_c, |
| 105 |
source_encryption=source_sse_c, |
| 106 |
target_file_info=None, |
| 107 |
target_content_type=None, |
| 108 |
fetch_if_necessary=True, |
| 109 |
) |
| 110 |
assert result != (None, None) |
| 111 |
assert len(mock_api.method_calls) |