def test_Controller_on_file_download_Reply(homedir, config, mocker): """ If the handler is passed a reply, check the download_reply function is the one called against the API. Using the `config` fixture to ensure the config is written to disk. """ mock_gui = mocker.MagicMock() mock_session = mocker.MagicMock() co = Controller('http://localhost', mock_gui, mock_session, homedir) source = factory.Source() journalist = db.User('Testy mcTestface') reply = db.Reply(uuid='reply-uuid', journalist=journalist, source=source, filename='1-my-reply.gpg', size=123) # Not a sdclientapi.Submission co.call_api = mocker.MagicMock() co.api = mocker.MagicMock() reply_sdk_object = mocker.MagicMock() mock_reply = mocker.patch('sdclientapi.Reply') mock_reply.return_value = reply_sdk_object co.on_file_download(source, reply) co.call_api.assert_called_once_with(co.api.download_reply, co.on_file_download_success, co.on_file_download_failure, reply_sdk_object, co.data_dir, current_object=reply)
def Reply(**attrs): global REPLY_COUNT REPLY_COUNT += 1 defaults = dict( uuid='source-uuid-{}'.format(REPLY_COUNT), filename='{}-reply.gpg'.format(REPLY_COUNT), size=123, is_decrypted=True, is_downloaded=True, content='content', ) defaults.update(attrs) return db.Reply(**defaults)
def Reply(**attrs): global REPLY_COUNT REPLY_COUNT += 1 defaults = dict( uuid="reply-uuid-{}".format(REPLY_COUNT), filename="{}-reply.gpg".format(REPLY_COUNT), size=123, is_decrypted=True, is_downloaded=True, content="content", ) defaults.update(attrs) return db.Reply(**defaults)
def on_reply_success(self, result, current_object: Tuple[str, str]) -> None: source_uuid, reply_uuid = current_object source = self.session.query( db.Source).filter_by(uuid=source_uuid).one() reply_db_object = db.Reply( uuid=result.uuid, source_id=source.id, journalist_id=self.api.token_journalist_uuid, filename=result.filename, ) self.session.add(reply_db_object) self.session.commit() self.reply_succeeded.emit(reply_uuid)
def _on_reply_complete(self, result, current_object: Tuple[str, str]) -> None: source_uuid, reply_uuid = current_object source = self.session.query( db.Source).filter_by(uuid=source_uuid).one() if isinstance(result, sdclientapi.Reply): reply_db_object = db.Reply( uuid=result.uuid, source_id=source.id, journalist_id=self.api.token_journalist_uuid, filename=result.filename, ) self.session.add(reply_db_object) self.session.commit() self.reply_succeeded.emit(reply_uuid) else: self.reply_failed.emit(reply_uuid)
def test_update_sources_deletes_files_associated_with_the_source( homedir, mocker): """ Check that: * Sources are deleted on disk after sync. """ mock_session = mocker.MagicMock() # Test scenario: one source locally, no sources on server. remote_sources = [] # A local source object. To ensure that all submissions/replies from # various stages of processing are cleaned up, we'll add several filenames # associated with each message, document, and reply for each stage of processing. # This simulates if a step failed. msg_server_filename = '1-pericardial-surfacing-msg.gpg' msg_local_filename_decrypted = '1-pericardial-surfacing-msg' file_server_filename = '1-pericardial-surfacing-doc.gz.gpg' file_local_filename_decompressed = '1-pericardial-surfacing-doc' file_local_filename_decrypted = '1-pericardial-surfacing-doc.gz' reply_server_filename = '1-pericardial-surfacing-reply.gpg' reply_local_filename_decrypted = '1-pericardial-surfacing-reply' # Here we're not mocking out the models use so that we can use the collection attribute. local_source = factory.Source() file_submission = db.Submission(source=local_source, uuid="test", size=123, filename=file_server_filename, download_url='http://test/test') msg_submission = db.Submission(source=local_source, uuid="test", size=123, filename=msg_server_filename, download_url='http://test/test') user = db.User('hehe') reply = db.Reply(source=local_source, journalist=user, filename=reply_server_filename, size=1234, uuid='test') local_source.submissions = [file_submission, msg_submission] local_source.replies = [reply] # Make the test files on disk in tmpdir so we can check they get deleted. test_filename_absolute_paths = [] for test_filename in [ msg_server_filename, msg_local_filename_decrypted, file_server_filename, file_local_filename_decompressed, file_local_filename_decrypted, reply_server_filename, reply_local_filename_decrypted ]: abs_server_filename = add_test_file_to_temp_dir(homedir, test_filename) test_filename_absolute_paths.append(abs_server_filename) local_sources = [local_source] update_sources(remote_sources, local_sources, mock_session, homedir) # Ensure the files associated with the reply are deleted on disk. for test_filename in test_filename_absolute_paths: assert not os.path.exists(test_filename) # Ensure the record for the local source is gone, along with its # related files. mock_session.delete.assert_called_with(local_source) # Session is committed to database. assert mock_session.commit.call_count == 1