|
18 | 18 | from app.commands.file import file_put
|
19 | 19 | from app.commands.file import file_resume
|
20 | 20 | from app.commands.file import file_trash
|
| 21 | +from app.configs.app_config import AppConfig |
21 | 22 | from app.models.item import ItemType
|
22 | 23 | from app.services.file_manager.file_metadata.file_metadata_client import FileMetaClient
|
23 | 24 | from app.services.file_manager.file_upload.models import FileObject
|
@@ -91,6 +92,98 @@ def test_file_upload_failed_with_invalid_attribute_file(cli_runner):
|
91 | 92 | assert result.output == customized_error_msg(ECustomizedError.INVALID_TEMPLATE) + '\n'
|
92 | 93 |
|
93 | 94 |
|
| 95 | +def test_file_upload_failed_with_invalid_source_file(cli_runner): |
| 96 | + # create invalid source file with wrong format |
| 97 | + runner = click.testing.CliRunner() |
| 98 | + with runner.isolated_filesystem(): |
| 99 | + with open('wrong_source.json', 'w') as f: |
| 100 | + f.write('wrong_source.json') |
| 101 | + |
| 102 | + result = cli_runner.invoke( |
| 103 | + file_put, |
| 104 | + ['test', '--thread', 1, '--source-file', 'wrong_source.json', 'wrong_source.json'], |
| 105 | + ) |
| 106 | + assert result.exit_code == 1 |
| 107 | + assert result.output == customized_error_msg(ECustomizedError.INVALID_SOURCE_FILE) + '\n' |
| 108 | + |
| 109 | + |
| 110 | +def test_file_upload_failed_with_duplicated_source_items(cli_runner, mocker): |
| 111 | + runner = click.testing.CliRunner() |
| 112 | + with runner.isolated_filesystem(): |
| 113 | + |
| 114 | + with open('source.txt', 'w') as f: |
| 115 | + f.write('[\'test_project/users/test\', \'test_project/users/test\']') |
| 116 | + |
| 117 | + mocker.patch( |
| 118 | + 'app.commands.file.identify_target_folder', return_value=('test_project', ItemType.FOLDER, 'users/admin') |
| 119 | + ) |
| 120 | + mocker.patch( |
| 121 | + 'app.services.file_manager.file_upload.upload_validator.search_item', |
| 122 | + return_value={'code': 200, 'result': {'id': 'id'}}, |
| 123 | + ) |
| 124 | + |
| 125 | + result = cli_runner.invoke( |
| 126 | + file_put, |
| 127 | + ['test', '--thread', 1, '--source-file', 'source.txt', 'source.txt'], |
| 128 | + ) |
| 129 | + assert result.exit_code == 1 |
| 130 | + assert ( |
| 131 | + customized_error_msg(ECustomizedError.INVALID_UPLOAD_REQUEST) % 'Source file list contains duplication' |
| 132 | + in result.output |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +def test_file_upload_failed_with_n2n_relationship_in_source_lineage(cli_runner, mocker): |
| 137 | + runner = click.testing.CliRunner() |
| 138 | + with runner.isolated_filesystem(): |
| 139 | + |
| 140 | + test_folder = 'test_folder' |
| 141 | + makedirs(test_folder, exist_ok=True) |
| 142 | + |
| 143 | + with open('source.txt', 'w') as f: |
| 144 | + f.write('[\'test1\', \'test2\']') |
| 145 | + |
| 146 | + mocker.patch( |
| 147 | + 'app.commands.file.identify_target_folder', return_value=('test_project', ItemType.FOLDER, 'users/admin') |
| 148 | + ) |
| 149 | + mocker.patch( |
| 150 | + 'app.commands.file.validate_upload_event', |
| 151 | + return_value={'source_file': ['test1', 'test2'], 'attribute': None}, |
| 152 | + ) |
| 153 | + mocker.patch('app.commands.file.assemble_path', return_value=('test', {'id': 'id'}, False, 'test')) |
| 154 | + |
| 155 | + result = cli_runner.invoke( |
| 156 | + file_put, |
| 157 | + ['test', '--thread', 1, '--source-file', 'source.txt', test_folder], |
| 158 | + ) |
| 159 | + assert result.exit_code == 1 |
| 160 | + assert customized_error_msg(ECustomizedError.UNSUPPORT_SOURCE_MANIFEST) in result.output |
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.parametrize('source_zone', [AppConfig.Env.green_zone, AppConfig.Env.core_zone]) |
| 164 | +def test_file_upload_failed_with_invalid_source_items(cli_runner, mocker, source_zone): |
| 165 | + runner = click.testing.CliRunner() |
| 166 | + with runner.isolated_filesystem(): |
| 167 | + file_path = 'test_project/users/admin/test.txt' |
| 168 | + with open('source.txt', 'w') as f: |
| 169 | + f.write(f'[\'{file_path}\']') |
| 170 | + |
| 171 | + mocker.patch( |
| 172 | + 'app.commands.file.identify_target_folder', return_value=('test_project', ItemType.FOLDER, 'users/admin') |
| 173 | + ) |
| 174 | + mocker.patch( |
| 175 | + 'app.services.file_manager.file_upload.upload_validator.search_item', |
| 176 | + return_value={'code': 404, 'result': {}}, |
| 177 | + ) |
| 178 | + |
| 179 | + result = cli_runner.invoke( |
| 180 | + file_put, |
| 181 | + ['test', '--thread', 1, '--source-file', 'source.txt', '--source-zone', source_zone, 'source.txt'], |
| 182 | + ) |
| 183 | + assert result.exit_code == 1 |
| 184 | + assert result.output == customized_error_msg(ECustomizedError.INVALID_SOURCE_ITEM) % (file_path, source_zone) + '\n' |
| 185 | + |
| 186 | + |
94 | 187 | def test_resumable_upload_command_success(mocker, cli_runner):
|
95 | 188 | runner = click.testing.CliRunner()
|
96 | 189 | with runner.isolated_filesystem():
|
|
0 commit comments