def test_MessageSync_run_failure(): submission = mock.MagicMock() submission.download_url = "http://foo" submission.filename = "foo.gpg" fh = mock.MagicMock() fh.name = "foo" with mock.patch('securedrop_client.storage.find_new_submissions', return_value=[ submission ]),\ mock.patch('subprocess.call', return_value=1), \ mock.patch('shutil.move'), \ mock.patch('shutil.copy'), \ mock.patch('os.unlink'), \ mock.patch('securedrop_client.message_sync.storage' '.mark_file_as_downloaded'), \ mock.patch( 'tempfile.NamedTemporaryFile', return_value=fh), \ mock.patch('builtins.open', mock.mock_open(read_data="blah")): api = mock.MagicMock() home = "/home/user/.sd" is_qubes = False ms = MessageSync(api, home, is_qubes) ms.api.download_submission = mock.MagicMock( return_value=(1234, "/home/user/downloads/foo") ) ms.run(False)
def test_MessageSync_run_success(mocker): submission = mocker.MagicMock() submission.uuid = 'mock id' submission.download_url = "http://foo" submission.filename = "foo.gpg" # mock the fetching of submissions mocker.patch('securedrop_client.storage.find_new_submissions', return_value=[submission]) mocker.patch('securedrop_client.message_sync.storage.mark_file_as_downloaded') # don't create the signal mocker.patch('securedrop_client.message_sync.pyqtSignal') # mock the GpgHelper creation since we don't have directories/keys setup mocker.patch('securedrop_client.message_sync.GpgHelper') api = mocker.MagicMock() home = "/home/user/.sd" is_qubes = True ms = MessageSync(api, home, is_qubes) ms.api.download_submission = mocker.MagicMock(return_value=(1234, "/home/user/downloads/foo")) mock_message_downloaded = mocker.Mock() mock_emit = mocker.Mock() mock_message_downloaded.emit = mock_emit mocker.patch.object(ms, 'message_downloaded', new=mock_message_downloaded) # check that it runs without raising exceptions ms.run(False) assert mock_emit.called
def test_MessageSync_run_failure(mocker, source): message = factory.Message(source=source['source']) # mock the fetching of submissions mocker.patch('securedrop_client.storage.find_new_messages', return_value=[message]) mocker.patch('securedrop_client.storage.find_new_files', return_value=[]) # mock the handling of the downloaded files mocker.patch( 'securedrop_client.message_sync.storage.mark_file_as_downloaded') mocker.patch( 'securedrop_client.message_sync.storage.mark_message_as_downloaded') # mock the GpgHelper creation since we don't have directories/keys setup mocker.patch('securedrop_client.message_sync.GpgHelper') api = mocker.MagicMock() home = "/home/user/.sd" is_qubes = False ms = MessageSync(api, home, is_qubes) ms.api.download_submission = mocker.MagicMock( return_value=(1234, "/home/user/downloads/foo")) # check that it runs without raising exceptions ms.run(False)
def test_MessageSync_exception(homedir, config, mocker, source): """ Mostly here for code coverage- makes sure that if an exception is raised in the download thread, the code which catches it is actually run. Using the `config` fixture to ensure the config is written to disk. """ message = factory.Message(source=source['source']) api = mocker.MagicMock() is_qubes = False # mock to return the submission we want mocker.patch('securedrop_client.storage.find_new_messages', return_value=[message]) mocker.patch('securedrop_client.storage.find_new_files', return_value=[]) # mock to prevent GpgHelper from raising errors on init mocker.patch('securedrop_client.crypto.safe_mkdir') ms = MessageSync(api, str(homedir), is_qubes) mocker.patch.object(ms.gpg, 'decrypt_submission_or_reply', side_effect=CryptoError) # check that it runs without raising exceptions ms.run(False)
def test_MessageSync_run_success(mocker, session, source): """Test when a message successfully downloads and decrypts.""" message = factory.Message(source=source['source'], is_downloaded=False, is_decrypted=None, content=None) session.add(message) session.commit() expected_content = 'foo' def set_object_decryption_status_with_content_side_effect( *nargs, **kwargs): message.content = expected_content # mock the fetching of submissions mocker.patch('securedrop_client.storage.find_new_messages', return_value=[message]) mock_download_status = mocker.patch( 'securedrop_client.message_sync.storage.mark_message_as_downloaded') mock_decryption_status = mocker.patch( 'securedrop_client.message_sync.storage.set_object_decryption_status_with_content', side_effect=set_object_decryption_status_with_content_side_effect) # don't create the signal mocker.patch('securedrop_client.message_sync.pyqtSignal') def mock_decrypt_submission_or_reply(filepath, plaintext_filename, is_doc): with open(plaintext_filename, 'w') as f: f.write(expected_content) # mock the GpgHelper creation since we don't have directories/keys setup mock_gpg_helper = mocker.MagicMock( decrypt_submission_or_reply=mock_decrypt_submission_or_reply) mocker.patch('securedrop_client.message_sync.GpgHelper', return_value=mock_gpg_helper) api = mocker.MagicMock(session=session) ms = MessageSync(api, 'mock', True) ms.session = session # "patch" it with a real session ms.api.download_submission = mocker.MagicMock( return_value=(1234, "/home/user/downloads/foo")) mock_message_ready = mocker.patch.object(ms, 'message_ready') # check that it runs without raising exceptions ms.run(False) mock_decryption_status.assert_called_once_with(message, api.session, True, expected_content) mock_download_status.called_once_with(message, api.mock_session) mock_message_ready.emit.assert_called_once_with(message.uuid, expected_content)
def test_MessageSync_exception(): """ Mostly here for code coverage- makes sure that if an exception is raised in the download thread, the code which catches it is actually run """ submission = mock.MagicMock() api = mock.MagicMock() home = "/home/user/.sd" is_qubes = False ms = MessageSync(api, home, is_qubes) with mock.patch('securedrop_client.storage.find_new_submissions', return_value=[ submission ]),\ mock.patch("sdclientapi.sdlocalobjects.Submission", mock.MagicMock(side_effect=Exception())): ms.run(False)
def test_MessageSync_run_decryption_error(mocker, session, source): """Test when a message successfully downloads, but does not successfully decrypt.""" message = factory.Message(source=source['source'], is_downloaded=False, is_decrypted=None, content=None) session.add(message) session.commit() # mock the fetching of submissions mocker.patch('securedrop_client.storage.find_new_messages', return_value=[message]) mock_download_status = mocker.patch( 'securedrop_client.message_sync.storage.mark_message_as_downloaded') mock_decryption_status = mocker.patch( 'securedrop_client.message_sync.storage.set_object_decryption_status_with_content' ) # don't create the signal mocker.patch('securedrop_client.message_sync.pyqtSignal') # mock the GpgHelper creation since we don't have directories/keys setup mocker.patch('securedrop_client.message_sync.GpgHelper') api = mocker.MagicMock(session=session) ms = MessageSync(api, 'mock', True) ms.session = session # "patch" it with a real session mocker.patch.object(ms.gpg, 'decrypt_submission_or_reply', side_effect=CryptoError) ms.api.download_submission = mocker.MagicMock( return_value=(1234, "/home/user/downloads/foo")) mock_message_ready = mocker.patch.object(ms, 'message_ready') # check that it runs without raising exceptions ms.run(False) mock_download_status.assert_called_once_with(message.uuid, session) mock_decryption_status.assert_called_once_with(message, api.session, False) mock_message_ready.emit.assert_called_once_with( message.uuid, '<Message not yet available>')
def test_MessageSync_run_failure(mocker): submission = mocker.MagicMock() submission.download_url = "http://foo" submission.filename = "foo.gpg" # mock the fetching of submissions mocker.patch('securedrop_client.storage.find_new_submissions', return_value=[submission]) # mock the handling of the downloaded files mocker.patch('securedrop_client.message_sync.storage.mark_file_as_downloaded') # mock the GpgHelper creation since we don't have directories/keys setup mocker.patch('securedrop_client.message_sync.GpgHelper') api = mocker.MagicMock() home = "/home/user/.sd" is_qubes = False ms = MessageSync(api, home, is_qubes) ms.api.download_submission = mocker.MagicMock(return_value=(1234, "/home/user/downloads/foo")) # check that it runs without raising exceptions ms.run(False)