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 User(**attrs): global USER_COUNT USER_COUNT += 1 defaults = dict( uuid='user-uuid-{}'.format(USER_COUNT), username='******'.format(USER_COUNT), ) defaults.update(attrs) return db.User(**defaults)
def User(**attrs): global USER_COUNT USER_COUNT += 1 defaults = dict( uuid="user-uuid-{}".format(USER_COUNT), username="******".format(USER_COUNT), firstname="slim", lastname="shady", ) defaults.update(attrs) return db.User(**defaults)
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