|
22 | 22 | from code42cli.worker import WorkerStats |
23 | 23 |
|
24 | 24 | _NAMESPACE = "code42cli.cmds.devices" |
| 25 | +TEST_NEW_DEVICE_NAME = "test-new-device-name-123" |
25 | 26 | TEST_DATE_OLDER = "2020-01-01T12:00:00.774Z" |
26 | 27 | TEST_DATE_NEWER = "2021-01-01T12:00:00.774Z" |
27 | 28 | TEST_DATE_MIDDLE = "2020-06-01T12:00:00" |
@@ -468,6 +469,61 @@ def worker_stats(mocker, worker_stats_factory): |
468 | 469 | return stats |
469 | 470 |
|
470 | 471 |
|
| 472 | +def test_rename_calls_get_and_update_settings_with_expected_params(runner, cli_state): |
| 473 | + cli_state.sdk.devices.get_settings.return_value = mock_device_settings |
| 474 | + runner.invoke( |
| 475 | + cli, |
| 476 | + [ |
| 477 | + "devices", |
| 478 | + "rename", |
| 479 | + TEST_DEVICE_GUID, |
| 480 | + "--new-device-name", |
| 481 | + TEST_NEW_DEVICE_NAME, |
| 482 | + ], |
| 483 | + obj=cli_state, |
| 484 | + ) |
| 485 | + cli_state.sdk.devices.get_settings.assert_called_once_with(TEST_DEVICE_GUID) |
| 486 | + cli_state.sdk.devices.update_settings.assert_called_once_with(mock_device_settings) |
| 487 | + |
| 488 | + |
| 489 | +def test_rename_when_missing_guid_prints_error(runner, cli_state): |
| 490 | + result = runner.invoke( |
| 491 | + cli, ["devices", "rename", "-n", TEST_NEW_DEVICE_NAME], obj=cli_state |
| 492 | + ) |
| 493 | + assert result.exit_code == 2 |
| 494 | + assert "Missing argument 'DEVICE_GUID'" in result.output |
| 495 | + |
| 496 | + |
| 497 | +def test_rename_when_missing_name_prints_error(runner, cli_state): |
| 498 | + result = runner.invoke(cli, ["devices", "rename", TEST_DEVICE_GUID], obj=cli_state) |
| 499 | + assert result.exit_code == 2 |
| 500 | + assert "Missing option '-n' / '--new-device-name'" in result.output |
| 501 | + |
| 502 | + |
| 503 | +def test_rename_when_guid_not_found_py42_raises_exception_prints_error( |
| 504 | + runner, cli_state, custom_error |
| 505 | +): |
| 506 | + cli_state.sdk.devices.get_settings.side_effect = Py42NotFoundError(custom_error) |
| 507 | + |
| 508 | + result = runner.invoke( |
| 509 | + cli, |
| 510 | + [ |
| 511 | + "devices", |
| 512 | + "rename", |
| 513 | + TEST_DEVICE_GUID, |
| 514 | + "--new-device-name", |
| 515 | + TEST_NEW_DEVICE_NAME, |
| 516 | + ], |
| 517 | + obj=cli_state, |
| 518 | + ) |
| 519 | + cli_state.sdk.devices.get_settings.assert_called_once_with(TEST_DEVICE_GUID) |
| 520 | + assert result.exit_code == 1 |
| 521 | + assert ( |
| 522 | + f"Error: The device with GUID '{TEST_DEVICE_GUID}' was not found." |
| 523 | + in result.output |
| 524 | + ) |
| 525 | + |
| 526 | + |
471 | 527 | def test_deactivate_deactivates_device( |
472 | 528 | runner, cli_state, deactivate_device_success, get_device_by_guid_success |
473 | 529 | ): |
@@ -983,3 +1039,52 @@ def _get(guid): |
983 | 1039 | handler(guid="test") |
984 | 1040 | handler(guid="not test") |
985 | 1041 | assert worker_stats.increment_total_errors.call_count == 1 |
| 1042 | + |
| 1043 | + |
| 1044 | +def test_bulk_rename_uses_expected_arguments(runner, mocker, cli_state): |
| 1045 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 1046 | + with runner.isolated_filesystem(): |
| 1047 | + with open("test_bulk_rename.csv", "w") as csv: |
| 1048 | + csv.writelines(["guid,name\n", "test-guid,test-name\n"]) |
| 1049 | + runner.invoke( |
| 1050 | + cli, ["devices", "bulk", "rename", "test_bulk_rename.csv"], obj=cli_state, |
| 1051 | + ) |
| 1052 | + assert bulk_processor.call_args[0][1] == [ |
| 1053 | + {"guid": "test-guid", "name": "test-name", "renamed": "False"} |
| 1054 | + ] |
| 1055 | + |
| 1056 | + |
| 1057 | +def test_bulk_rename_ignores_blank_lines(runner, mocker, cli_state): |
| 1058 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 1059 | + with runner.isolated_filesystem(): |
| 1060 | + with open("test_bulk_rename.csv", "w") as csv: |
| 1061 | + csv.writelines(["guid,name\n", "\n", "test-guid,test-name\n\n"]) |
| 1062 | + runner.invoke( |
| 1063 | + cli, ["devices", "bulk", "rename", "test_bulk_rename.csv"], obj=cli_state, |
| 1064 | + ) |
| 1065 | + assert bulk_processor.call_args[0][1] == [ |
| 1066 | + {"guid": "test-guid", "name": "test-name", "renamed": "False"} |
| 1067 | + ] |
| 1068 | + bulk_processor.assert_called_once() |
| 1069 | + |
| 1070 | + |
| 1071 | +def test_bulk_rename_uses_handler_that_when_encounters_error_increments_total_errors( |
| 1072 | + runner, mocker, cli_state, worker_stats |
| 1073 | +): |
| 1074 | + def _get(guid): |
| 1075 | + if guid == "test": |
| 1076 | + raise Exception("TEST") |
| 1077 | + return create_mock_response(mocker, data=TEST_DEVICE_RESPONSE) |
| 1078 | + |
| 1079 | + cli_state.sdk.devices.get_settings = _get |
| 1080 | + bulk_processor = mocker.patch(f"{_NAMESPACE}.run_bulk_process") |
| 1081 | + with runner.isolated_filesystem(): |
| 1082 | + with open("test_bulk_rename.csv", "w") as csv: |
| 1083 | + csv.writelines(["guid,name\n", "1,2\n"]) |
| 1084 | + runner.invoke( |
| 1085 | + cli, ["devices", "bulk", "rename", "test_bulk_rename.csv"], obj=cli_state, |
| 1086 | + ) |
| 1087 | + handler = bulk_processor.call_args[0][0] |
| 1088 | + handler(guid="test", name="test-name-1") |
| 1089 | + handler(guid="not test", name="test-name-2") |
| 1090 | + assert worker_stats.increment_total_errors.call_count == 1 |
0 commit comments