def test__export_archive_does_not_raise_ExportError_when_CalledProcessError( mocker): ''' Ensure ExportError is raised if a CalledProcessError is encountered. ''' mock_error = subprocess.CalledProcessError('mock_cmd', 123) mocker.patch('subprocess.check_output', side_effect=mock_error) export = Export() with pytest.raises(ExportError, match='CALLED_PROCESS_ERROR'): export._export_archive('mock.sd-export')
def test__export_archive_does_not_raise_ExportError_when_CalledProcessError( mocker): """ Ensure ExportError is raised if a CalledProcessError is encountered. """ mock_error = subprocess.CalledProcessError("mock_cmd", 123) mocker.patch("subprocess.check_output", side_effect=mock_error) export = Export() with pytest.raises(ExportError, match="CALLED_PROCESS_ERROR"): export._export_archive("mock.sd-export")
def test__export_archive_with_evil_command(mocker): ''' Ensure shell command is shell-escaped. ''' export = Export() check_output = mocker.patch('subprocess.check_output', return_value=b'') export._export_archive('somefile; rm -rf ~') check_output.assert_called_once_with([ 'qvm-open-in-vm', 'sd-devices', "'somefile; rm -rf ~'", '--view-only' ], stderr=-2)
def test__export_archive_with_evil_command(mocker): """ Ensure shell command is shell-escaped. """ export = Export() check_output = mocker.patch("subprocess.check_output", return_value=b"") export._export_archive("somefile; rm -rf ~") check_output.assert_called_once_with([ "qvm-open-in-vm", "sd-devices", "'somefile; rm -rf ~'", "--view-only" ], stderr=-2)
def test__export_archive(mocker): ''' Ensure the subprocess call returns the expected output. ''' mocker.patch('subprocess.check_output', return_value=b'mock') export = Export() status = export._export_archive('mock.sd-export') assert status == 'mock'
def test__export_archive(mocker): """ Ensure the subprocess call returns the expected output. """ mocker.patch("subprocess.check_output", return_value=b"mock") export = Export() status = export._export_archive("mock.sd-export") assert status == "mock"
def test__run_disk_test_raises_ExportError_if_not_USB_ENCRYPTED(mocker): """ Ensure ExportError is raised if _run_disk_test returns anything other than 'USB_ENCRYPTED'. """ export = Export() export._create_archive = mocker.MagicMock(return_value="mock_archive_path") export._export_archive = mocker.MagicMock( return_value="SOMETHING_OTHER_THAN_USB_ENCRYPTED") with pytest.raises(ExportError): export._run_disk_test("mock_archive_dir")
def test__run_print_raises_ExportError_if_not_empty_string(mocker): ''' Ensure ExportError is raised if _run_print returns anything other than ''. ''' export = Export() export._create_archive = mocker.MagicMock(return_value='mock_archive_path') export._export_archive = mocker.MagicMock( return_value='SOMETHING_OTHER_THAN_EMPTY_STRING') with pytest.raises(ExportError): export._run_print('mock_archive_dir', ['mock_filepath'])
def test__run_usb_test_raises_ExportError_if_not_USB_CONNECTED(mocker): ''' Ensure ExportError is raised if _run_disk_test returns anything other than 'USB_CONNECTED'. ''' export = Export() export._create_archive = mocker.MagicMock(return_value='mock_archive_path') export._export_archive = mocker.MagicMock( return_value='SOMETHING_OTHER_THAN_USB_CONNECTED') with pytest.raises(ExportError): export._run_usb_test('mock_archive_dir')
def test__run_printer_preflight_raises_ExportError_if_not_empty_string(mocker): """ Ensure ExportError is raised if _run_disk_test returns anything other than 'USB_CONNECTED'. """ export = Export() export._create_archive = mocker.MagicMock(return_value="mock_archive_path") export._export_archive = mocker.MagicMock( return_value="SOMETHING_OTHER_THAN_EMPTY_STRING") with pytest.raises(ExportError): export._run_printer_preflight("mock_archive_dir")
def test__run_disk_export_raises_ExportError_if_not_empty_string(mocker): """ Ensure ExportError is raised if _run_disk_test returns anything other than ''. """ export = Export() export._create_archive = mocker.MagicMock(return_value="mock_archive_path") export._export_archive = mocker.MagicMock( return_value="SOMETHING_OTHER_THAN_EMPTY_STRING") with pytest.raises(ExportError): export._run_disk_export("mock_archive_dir", ["mock_filepath"], "mock_passphrase")
def test__run_disk_test(mocker): """ Ensure _export_archive and _create_archive are called with the expected parameters, _export_archive is called with the return value of _create_archive, and _run_disk_test returns without error if 'USB_ENCRYPTED' is the ouput status of _export_archive. """ export = Export() export._create_archive = mocker.MagicMock(return_value="mock_archive_path") export._export_archive = mocker.MagicMock(return_value="USB_ENCRYPTED") export._run_disk_test("mock_archive_dir") export._export_archive.assert_called_once_with("mock_archive_path") export._create_archive.assert_called_once_with("mock_archive_dir", "disk-test.sd-export", {"device": "disk-test"})
def test__run_printer_preflight(mocker): ''' Ensure _export_archive and _create_archive are called with the expected parameters, _export_archive is called with the return value of _create_archive, and _run_disk_test returns without error if 'USB_CONNECTED' is the return value of _export_archive. ''' export = Export() export._create_archive = mocker.MagicMock(return_value='mock_archive_path') export._export_archive = mocker.MagicMock(return_value='') export._run_printer_preflight('mock_archive_dir') export._export_archive.assert_called_once_with('mock_archive_path') export._create_archive.assert_called_once_with( 'mock_archive_dir', 'printer-preflight.sd-export', {'device': 'printer-preflight'})
def test__run_disk_test(mocker): ''' Ensure _export_archive and _create_archive are called with the expected parameters, _export_archive is called with the return value of _create_archive, and _run_disk_test returns without error if 'USB_ENCRYPTED' is the ouput status of _export_archive. ''' export = Export() export._create_archive = mocker.MagicMock(return_value='mock_archive_path') export._export_archive = mocker.MagicMock(return_value='USB_ENCRYPTED') export._run_disk_test('mock_archive_dir') export._export_archive.assert_called_once_with('mock_archive_path') export._create_archive.assert_called_once_with('mock_archive_dir', 'disk-test.sd-export', {'device': 'disk-test'})
def test__run_print(mocker): """ Ensure _export_archive and _create_archive are called with the expected parameters and _export_archive is called with the return value of _create_archive. """ export = Export() export._create_archive = mocker.MagicMock(return_value="mock_archive_path") export._export_archive = mocker.MagicMock(return_value="") export._run_print("mock_archive_dir", ["mock_filepath"]) export._export_archive.assert_called_once_with("mock_archive_path") export._create_archive.assert_called_once_with("mock_archive_dir", "print_archive.sd-export", {"device": "printer"}, ["mock_filepath"])
def test__run_print(mocker): ''' Ensure _export_archive and _create_archive are called with the expected parameters and _export_archive is called with the return value of _create_archive. ''' export = Export() export._create_archive = mocker.MagicMock(return_value='mock_archive_path') export._export_archive = mocker.MagicMock(return_value='') export._run_print('mock_archive_dir', ['mock_filepath']) export._export_archive.assert_called_once_with('mock_archive_path') export._create_archive.assert_called_once_with('mock_archive_dir', 'print_archive.sd-export', { 'device': 'printer', }, ['mock_filepath'])
def test__run_printer_preflight(mocker): """ Ensure _export_archive and _create_archive are called with the expected parameters, _export_archive is called with the return value of _create_archive, and _run_disk_test returns without error if 'USB_CONNECTED' is the return value of _export_archive. """ export = Export() export._create_archive = mocker.MagicMock(return_value="mock_archive_path") export._export_archive = mocker.MagicMock(return_value="") export._run_printer_preflight("mock_archive_dir") export._export_archive.assert_called_once_with("mock_archive_path") export._create_archive.assert_called_once_with( "mock_archive_dir", "printer-preflight.sd-export", {"device": "printer-preflight"})
def test__run_disk_export(mocker): ''' Ensure _export_archive and _create_archive are called with the expected parameters, _export_archive is called with the return value of _create_archive, and _run_disk_test returns without error if '' is the ouput status of _export_archive. ''' export = Export() export._create_archive = mocker.MagicMock(return_value='mock_archive_path') export._export_archive = mocker.MagicMock(return_value='') export._run_disk_export('mock_archive_dir', ['mock_filepath'], 'mock_passphrase') export._export_archive.assert_called_once_with('mock_archive_path') export._create_archive.assert_called_once_with( 'mock_archive_dir', 'archive.sd-export', { 'encryption_key': 'mock_passphrase', 'device': 'disk', 'encryption_method': 'luks' }, ['mock_filepath'])
def test__run_disk_export(mocker): """ Ensure _export_archive and _create_archive are called with the expected parameters, _export_archive is called with the return value of _create_archive, and _run_disk_test returns without error if '' is the ouput status of _export_archive. """ export = Export() export._create_archive = mocker.MagicMock(return_value="mock_archive_path") export._export_archive = mocker.MagicMock(return_value="") export._run_disk_export("mock_archive_dir", ["mock_filepath"], "mock_passphrase") export._export_archive.assert_called_once_with("mock_archive_path") export._create_archive.assert_called_once_with( "mock_archive_dir", "archive.sd-export", { "encryption_key": "mock_passphrase", "device": "disk", "encryption_method": "luks" }, ["mock_filepath"], )