def test_FileWidget_mousePressEvent_open(mocker): """ Should fire the expected open event handler in the logic layer. """ mock_controller = mocker.MagicMock() source = factory.Source() submission = db.Submission(source, 'submission-uuid', 123, 'mah-reply.gpg', 'http://mah-server/mah-reply-url') submission.is_downloaded = True fw = FileWidget(source, submission, mock_controller) fw.mouseReleaseEvent(None) fw.controller.on_file_open.assert_called_once_with(submission)
def test_FileWidget_init_right(mocker): """ Check the FileWidget is configured correctly for align-right. """ mock_controller = mocker.MagicMock() source = factory.Source() submission = db.Submission(source, 'submission-uuid', 123, 'mah-reply.gpg', 'http://mah-server/mah-reply-url') submission.is_downloaded = True fw = FileWidget(source, submission, mock_controller, align='right') layout = fw.layout() assert isinstance(layout.takeAt(0), QSpacerItem) assert isinstance(layout.takeAt(0), QWidgetItem) assert isinstance(layout.takeAt(0), QWidgetItem) assert fw.controller == mock_controller
def test_Client_on_file_download_user_not_signed_in(homedir, config, mocker): """ If a user clicks the download button but is not logged in, an error should appear. Using the `config` fixture to ensure the config is written to disk. """ mock_gui = mocker.MagicMock() mock_session = mocker.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, homedir) source = factory.Source() submission = db.Submission(source, 'submission-uuid', 1234, 'myfile.doc.gpg', 'http://myserver/myfile') cl.on_action_requiring_login = mocker.MagicMock() cl.api = None submission_sdk_object = mocker.MagicMock() mock_submission = mocker.patch('sdclientapi.Submission') mock_submission.return_value = submission_sdk_object cl.on_file_download(source, submission) cl.on_action_requiring_login.assert_called_once_with()
def test_Client_on_file_download_Submission(homedir, config, mocker): """ If the handler is passed a submission, check the download_submission 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() cl = Client('http://localhost', mock_gui, mock_session, homedir) source = factory.Source() submission = db.Submission(source, 'submission-uuid', 1234, 'myfile.doc.gpg', 'http://myserver/myfile') cl.call_api = mocker.MagicMock() cl.api = mocker.MagicMock() submission_sdk_object = mocker.MagicMock() mock_submission = mocker.patch('sdclientapi.Submission') mock_submission.return_value = submission_sdk_object cl.on_file_download(source, submission) cl.call_api.assert_called_once_with(cl.api.download_submission, cl.on_file_downloaded, cl.on_download_timeout, submission_sdk_object, cl.data_dir, current_object=submission)
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