def test_Client_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()
    cl = Client('http://localhost', mock_gui, mock_session, homedir)
    source = factory.Source()
    journalist = db.User('Testy mcTestface')
    reply = db.Reply('reply-uuid', journalist, source, 'my-reply.gpg',
                     123)  # Not a sdclientapi.Submission
    cl.call_api = mocker.MagicMock()
    cl.api = mocker.MagicMock()
    reply_sdk_object = mocker.MagicMock()
    mock_reply = mocker.patch('sdclientapi.Reply')
    mock_reply.return_value = reply_sdk_object
    cl.on_file_download(source, reply)
    cl.call_api.assert_called_once_with(cl.api.download_reply,
                                        cl.on_file_downloaded,
                                        cl.on_download_timeout,
                                        reply_sdk_object,
                                        cl.data_dir,
                                        current_object=reply)
def test_Client_unstars_a_source_if_unstarred(homedir, config, mocker):
    """
    Ensure that the client stars a source if it is unstarred.
    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_db_object = mocker.MagicMock()
    source_db_object.uuid = mocker.MagicMock()
    source_db_object.is_starred = False
    cl.call_api = mocker.MagicMock()
    cl.api = mocker.MagicMock()
    cl.api.add_star = mocker.MagicMock()
    cl.on_update_star_complete = mocker.MagicMock()
    cl.on_sidebar_action_timeout = mocker.MagicMock()
    source_sdk_object = mocker.MagicMock()
    mock_source = mocker.patch('sdclientapi.Source')
    mock_source.return_value = source_sdk_object
    cl.update_star(source_db_object)
    cl.call_api.assert_called_once_with(cl.api.add_star,
                                        cl.on_update_star_complete,
                                        cl.on_sidebar_action_timeout,
                                        source_sdk_object)
    mock_gui.update_error_status.assert_called_once_with("")
Ejemplo n.º 3
0
def test_Client_on_file_download_Reply(safe_tmpdir):
    """
    If the handler is passed a reply, check the download_reply
    function is the one called against the API.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    source = models.Source('source-uuid', 'testy-mctestface', False,
                           'mah pub key', 1, False, datetime.now())
    journalist = models.User('Testy mcTestface')
    reply = models.Reply('reply-uuid', journalist, source, 'my-reply.gpg',
                         123)  # Not a sdclientapi.Submission
    cl.call_api = mock.MagicMock()
    cl.api = mock.MagicMock()
    reply_sdk_object = mock.MagicMock()
    with mock.patch('sdclientapi.Reply') as mock_reply:
        mock_reply.return_value = reply_sdk_object
        cl.on_file_download(source, reply)
    cl.call_api.assert_called_once_with(cl.api.download_reply,
                                        cl.on_file_downloaded,
                                        cl.on_download_timeout,
                                        reply_sdk_object,
                                        cl.data_dir,
                                        current_object=reply)
Ejemplo n.º 4
0
def test_Client_authenticated_no_api(safe_tmpdir):
    """
    If the API is authenticated return True.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    cl.api = None
    assert cl.authenticated() is False
Ejemplo n.º 5
0
def test_Client_authenticated_yes(safe_tmpdir):
    """
    If the API is authenticated return True.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    cl.api = mock.MagicMock()
    cl.api.token = {'token': 'foo'}
    assert cl.authenticated() is True
def test_Client_authenticated_no_api(homedir, config, mocker):
    """
    If the API is authenticated return True.
    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)
    cl.api = None
    assert cl.authenticated() is False
Ejemplo n.º 7
0
def test_Client_authenticated_no():
    """
    If the API is authenticated return True.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session)
    cl.api = mock.MagicMock()
    cl.api.token = {'token': ''}
    assert cl.authenticated() is False
Ejemplo n.º 8
0
def test_Client_logout(safe_tmpdir):
    """
    The API is reset to None and the UI is set to logged out state.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    cl.api = mock.MagicMock()
    cl.logout()
    assert cl.api is None
    cl.gui.logout.assert_called_once_with()
Ejemplo n.º 9
0
def test_Client_on_authenticate_ok():
    """
    Ensure the client syncs when the user successfully logs in.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session)
    cl.sync_api = mock.MagicMock()
    cl.api = mock.MagicMock()
    cl.api.username = '******'
    cl.on_authenticate(True)
    cl.sync_api.assert_called_once_with()
    cl.gui.set_logged_in_as.assert_called_once_with('test')
Ejemplo n.º 10
0
def test_Client_update_star_not_logged_in(safe_tmpdir):
    """
    Ensure that starring/unstarring a source when not logged in calls
    the method that displays an error status in the left sidebar.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    source_db_object = mock.MagicMock()
    cl.on_action_requiring_login = mock.MagicMock()
    cl.api = None
    cl.update_star(source_db_object)
    cl.on_action_requiring_login.assert_called_with()
Ejemplo n.º 11
0
def test_Client_on_sync_timeout(safe_tmpdir):
    """
    Display error message in status bar if sync times out.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    cl.api = "this is populated"
    cl.on_sync_timeout()
    assert cl.api is not None
    mock_gui.update_error_status.\
        assert_called_once_with(error='The connection to the SecureDrop '
                                'server timed out. Please try again.')
Ejemplo n.º 12
0
def test_Client_on_sync_timeout(homedir, config, mocker):
    """
    Display error message in status bar if sync times out.
    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)
    cl.api = "this is populated"
    cl.on_sync_timeout()
    assert cl.api is not None
    mock_gui.update_error_status.\
        assert_called_once_with(error='The connection to the SecureDrop '
                                'server timed out. Please try again.')
Ejemplo n.º 13
0
def test_Client_delete_source(homedir, config, mocker):
    '''
    Using the `config` fixture to ensure the config is written to disk.
    '''
    mock_gui = mocker.MagicMock()
    mock_session = mocker.MagicMock()
    mock_source = mocker.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, homedir)
    cl.call_api = mocker.MagicMock()
    cl.api = mocker.MagicMock()
    cl.delete_source(mock_source)
    cl.call_api.assert_called_with(cl.api.delete_source,
                                   cl._on_delete_source_complete,
                                   cl._on_delete_action_timeout, mock_source)
Ejemplo n.º 14
0
def test_Client_update_star_not_logged_in(homedir, config, mocker):
    """
    Ensure that starring/unstarring a source when not logged in calls
    the method that displays an error status in the left sidebar.
    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_db_object = mocker.MagicMock()
    cl.on_action_requiring_login = mocker.MagicMock()
    cl.api = None
    cl.update_star(source_db_object)
    cl.on_action_requiring_login.assert_called_with()
Ejemplo n.º 15
0
def test_Client_on_authenticate_ok(safe_tmpdir):
    """
    Ensure the client syncs when the user successfully logs in.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    cl.sync_api = mock.MagicMock()
    cl.api = mock.MagicMock()
    cl.api.username = '******'
    cl.on_authenticate(True)
    cl.sync_api.assert_called_once_with()
    cl.gui.set_logged_in_as.assert_called_once_with('test')
    # Error status bar should be cleared
    cl.gui.update_error_status.assert_called_once_with("")
Ejemplo n.º 16
0
def test_Client_start_reply_thread_if_already_running(homedir, config, mocker):
    """
    Ensure that when starting the reply thread, we don't start another thread
    if it's already running. Instead, we just authenticate the existing thread.
    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)
    cl.api = 'api object'
    cl.reply_sync = mocker.MagicMock()
    cl.reply_thread = mocker.MagicMock()
    cl.reply_thread.api = None
    cl.start_reply_thread()
    cl.reply_sync.api = cl.api
    cl.reply_thread.start.assert_not_called()
Ejemplo n.º 17
0
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()
Ejemplo n.º 18
0
def test_Client_on_authenticate_ok(homedir, config, mocker):
    """
    Ensure the client syncs when the user successfully logs in.
    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)
    cl.sync_api = mocker.MagicMock()
    cl.api = mocker.MagicMock()
    cl.start_message_thread = mocker.MagicMock()
    cl.start_reply_thread = mocker.MagicMock()
    cl.api.username = '******'
    cl.on_authenticate(True)
    cl.sync_api.assert_called_once_with()
    cl.start_message_thread.assert_called_once_with()
    cl.gui.set_logged_in_as.assert_called_once_with('test')
    # Error status bar should be cleared
    cl.gui.update_error_status.assert_called_once_with("")
Ejemplo n.º 19
0
def test_Client_logout(homedir, config, mocker):
    """
    The API is reset to None and the UI is set to logged out state.
    The message and reply threads should also have the
    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)
    cl.api = mocker.MagicMock()
    cl.message_sync = mocker.MagicMock()
    cl.reply_sync = mocker.MagicMock()
    cl.message_sync.api = mocker.MagicMock()
    cl.reply_sync.api = mocker.MagicMock()
    cl.logout()
    assert cl.api is None
    assert cl.message_sync.api is None
    assert cl.reply_sync.api is None
    cl.gui.logout.assert_called_once_with()
Ejemplo n.º 20
0
def test_Client_on_file_download_Submission(safe_tmpdir):
    """
    If the handler is passed a submission, check the download_submission
    function is the one called against the API.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    source = models.Source('source-uuid', 'testy-mctestface', False,
                           'mah pub key', 1, False, datetime.now())
    submission = models.Submission(source, 'submission-uuid', 1234,
                                   'myfile.doc.gpg', 'http://myserver/myfile')
    cl.call_api = mock.MagicMock()
    cl.api = mock.MagicMock()
    submission_sdk_object = mock.MagicMock()
    with mock.patch('sdclientapi.Submission') as mock_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)
Ejemplo n.º 21
0
def test_Client_unstars_a_source_if_unstarred(safe_tmpdir):
    """
    Ensure that the client stars a source if it is unstarred.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    source_db_object = mock.MagicMock()
    source_db_object.uuid = mock.MagicMock()
    source_db_object.is_starred = False
    cl.call_api = mock.MagicMock()
    cl.api = mock.MagicMock()
    cl.api.add_star = mock.MagicMock()
    cl.on_update_star_complete = mock.MagicMock()
    cl.on_sidebar_action_timeout = mock.MagicMock()
    source_sdk_object = mock.MagicMock()
    with mock.patch('sdclientapi.Source') as mock_source:
        mock_source.return_value = source_sdk_object
        cl.update_star(source_db_object)
        cl.call_api.assert_called_once_with(cl.api.add_star,
                                            cl.on_update_star_complete,
                                            cl.on_sidebar_action_timeout,
                                            source_sdk_object)
    mock_gui.update_error_status.assert_called_once_with("")
Ejemplo n.º 22
0
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)