def test_init_raspi_tty_ama(self, mocker, transport): # noqa: F811 mocker.patch('nfc.clf.pn532.Device.__init__').return_value = None device_tree_model = mocker.mock_open(read_data=b"Raspberry Pi") mocker.patch('nfc.clf.pn532.open', device_tree_model) type(transport.tty).port = PropertyMock(return_value='/dev/ttyAMA0') stty = mocker.patch('os.system') stty.return_value = -1 sys.platform = "linux" transport.write.return_value = None transport.read.side_effect = [ ACK(), RSP('03 32010607'), # GetFirmwareVersion ACK(), RSP('15'), # SAMConfiguration ] device = nfc.clf.pn532.init(transport) assert stty.mock_calls == [ call('stty -F /dev/ttyAMA0 921600 2> /dev/null'), call('stty -F /dev/ttyAMA0 460800 2> /dev/null'), call('stty -F /dev/ttyAMA0 230400 2> /dev/null'), call('stty -F /dev/ttyAMA0 115200 2> /dev/null'), ] assert isinstance(device, nfc.clf.pn532.Device) assert transport.write.mock_calls == [call(_) for _ in [ HEX(10 * '00') + CMD('02'), # GetFirmwareVersion HEX(10 * '00') + CMD('14 010000'), # SAMConfiguration ]] assert transport.read.mock_calls == [ call(timeout=100), call(timeout=100), call(timeout=100), call(timeout=100), ]
def test_init_raspi_tty_ser(self, mocker, transport): # noqa: F811 mocker.patch('nfc.clf.pn532.Device.__init__').return_value = None device_tree_model = mocker.mock_open(read_data=b"Raspberry Pi") mocker.patch('nfc.clf.pn532.open', device_tree_model) type(transport.tty).port = PropertyMock(return_value='/dev/ttyS0') stty = mocker.patch('os.system') stty.return_value = -1 sys.platform = "linux" transport.write.return_value = None transport.read.side_effect = [ ACK(), RSP('03 32010607'), # GetFirmwareVersion ACK(), RSP('15'), # SAMConfiguration ] device = nfc.clf.pn532.init(transport) assert isinstance(device, nfc.clf.pn532.Device) assert stty.mock_calls == [] assert transport.write.mock_calls == [ call(_) for _ in [ HEX(10 * '00') + CMD('02'), # GetFirmwareVersion HEX(10 * '00') + CMD('14 010000'), # SAMConfiguration ] ] assert transport.read.mock_calls == [ call(timeout=100), call(timeout=100), call(timeout=100), call(timeout=100), ]
def test_main_decryption_otp(mocker): ''' This package can be called straight from command line. Can decrypt data using OTP encryption. ''' my_namespace = namedtuple('Namespace', [ 'mode', 'encryption', 'key_stretch', 'level', 'medium', 'password', 'secret', 'result', 'format' ]) args = my_namespace(mode='decrypt', encryption='otp', key_stretch=None, \ level='2', medium='medium.bmp', password='******', \ secret='secret.txt', result='hidden.bmp', format='BMP') dummyEncryptor = OTPCryptor() dummyModifier = PictureModifier(int(args.level)) with mocker.patch('builtins.open', new_callable=mocker.mock_open()) as mo: with monkeypatch.context() as m: m.setattr(argparse.ArgumentParser, 'parse_args', lambda self: args) mocker.patch.object(OTPCryptor, '__init__') OTPCryptor.__init__.return_value = None mocker.patch.object(PictureModifier, '__init__') PictureModifier.__init__.return_value = None mocker.patch.object(Steganography, 'get_file') steganography_main() mo.assert_any_call(args.password, 'rb') mo.assert_any_call(args.result, 'wb') OTPCryptor.__init__.assert_called_once_with() PictureModifier.__init__.assert_called_once_with(int(args.level)) assert Steganography.get_file.call_count == 1
def deactivate_open_files(mocker): """ Fixture to disable opening of files from model """ mocked_file = mocker.mock_open() mocker.patch.object(starmatrix.model, 'open', mocked_file) return mocked_file
def mock_config_file(mocker): """ Fixture mocking contents of a config file reading """ config_file_content = "m_max: 33.0\ntotal_time_steps: 123" mocked_file = mocker.mock_open(read_data=config_file_content) mocker.patch.object(cli, 'open', mocked_file) mocker.spy(cli, "read_config_file") return {'m_max': 33.0, 'total_time_steps': 123}
def test__download_clip_valid_url_success(mocker): src_url = 'https://clips-media-assets2.twitch.tv/157589949.mp4' path = './' mocker.patch('clipsplice.ClipSplicer._get_clip_src_url', return_value=src_url) responses.add(responses.GET, src_url, body='a', status=200, content_type='binary/octet-stream') m = mocker.patch('builtins.open', mocker.mock_open()) splicer = ClipSplicer(example_clip_list) splicer._download_clip(example_clip_list[0], path) m.assert_called_with(f'{path}/{example_clip_list[0]["id"]}.mp4', 'wb') m().write.assert_called_once_with(b'a')
def test_metatype_from_path_isfile_binary(mocker, data, isbinary): fso_path = Path('test') mock_path_isdir = mocker.patch.object(fso_path, "isdir") mock_path_isdir.return_value = False mock_path_isfile = mocker.patch.object(fso_path, "isfile") mock_path_isfile.return_value = False mock_open_file = mocker.mock_open(read_data=data) mock_open = mocker.patch("io.open", mock_open_file) assert MetaType.is_binary(fso_path) is isbinary mock_path_isdir.assert_called_once() mock_open.assert_called() mock_open_file.assert_called()
def test_save_with_empty_contact_list(mocker, fake_weechat_instance): expected_nick = 'mizukage' expected_phone = '+11234567890' expected_id = str(uuid.uuid4()) expected_list_of_contacts = 'contacts' contact = Contact(nick=expected_nick, phone=expected_phone, id=expected_id) mocker.patch('os.path.exists') os.path.exists.return_value = False fd = mocker.mock_open() with patch('{}.open'.format(__name__), fd, create=True): with open(CONTACT_DIR, 'w') as f: f.write('safd') contact.save() os.path.exists.assert_called_with(CONTACT_DIR) handle = fd() handle.write.assert_called_with('safd')
def test_hash_dir(mocker): fso_path = Path('test') mock_path_isdir = mocker.patch.object(fso_path, "isdir") mock_path_isdir.return_value = True mock_path_files = mocker.patch.object(fso_path, "files") mock_path_files.return_value = ['file1'] mock_open_file = mocker.mock_open(read_data=b'data') mock_open = mocker.patch("builtins.open", mock_open_file) hash_dir = Hash.from_path(fso_path) mock_path_isdir.assert_called_once() mock_path_files.assert_called() mock_open.assert_called() mock_open_file.assert_called() import hashlib hash_sha512 = hashlib.sha512() hash_sha512.update(b'data') assert hash_dir.sha512 == hash_sha512.hexdigest()
def test_read_config_file_returns_json_decoded_file_if_config_exists(mocker): mock_config_str = '{ "mock": "json" }' mocker.patch("pathlib.Path.is_file").return_value = True mocker.patch("builtins.open", mocker.mock_open(read_data=mock_config_str)) mock_config = read_config_file() assert mock_config == json.loads(mock_config_str)
def test_proccess_xml_with_root(self, mocker, root_folders): mocker.patch("builtins.open", mocker.mock_open(read_data=root_folders)) with open("data.xml") as file: result = proccess_xml(file) assert result == ''
def test_call_function_without_exceptions(self, mocker): mocker.patch("builtins.open", mocker.mock_open(read_data="<root></root>")) with open("data.xml") as file: proccess_xml(file) assert True