Skip to content

Commit

Permalink
start porting tests to use new code
Browse files Browse the repository at this point in the history
  • Loading branch information
heartsucker committed May 22, 2019
1 parent 7bbe7bb commit bd8f402
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 329 deletions.
57 changes: 38 additions & 19 deletions tests/gui/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ def test_init(mocker):
mock_mv = mocker.patch('securedrop_client.gui.main.MainView')
mocker.patch('securedrop_client.gui.main.QHBoxLayout', mock_lo)
mocker.patch('securedrop_client.gui.main.QMainWindow')
mock_session_maker = mocker.MagicMock()

w = Window()
w = Window(mock_session_maker)

mock_li.assert_called_once_with(w.icon)
mock_lp.assert_called_once_with()
mock_mv.assert_called_once_with(w.main_pane)
mock_mv.assert_called_once_with(mock_session_maker, w.main_pane)
assert mock_lo().addWidget.call_count == 2


Expand All @@ -36,8 +37,10 @@ def test_setup(mocker):
Ensure the passed in controller is referenced and the various views are
instantiated as expected.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
mock_controller = mocker.MagicMock()

w = Window(mock_session_maker)
w.show_login = mocker.MagicMock()
w.top_pane = mocker.MagicMock()
w.left_pane = mocker.MagicMock()
Expand All @@ -53,7 +56,8 @@ def test_setup(mocker):


def test_show_main_window(mocker):
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.autosize_window = mocker.MagicMock()
w.show = mocker.MagicMock()
w.set_logged_in_as = mocker.MagicMock()
Expand All @@ -66,7 +70,8 @@ def test_show_main_window(mocker):


def test_show_main_window_without_username(mocker):
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.autosize_window = mocker.MagicMock()
w.show = mocker.MagicMock()
w.set_logged_in_as = mocker.MagicMock()
Expand All @@ -82,7 +87,8 @@ def test_autosize_window(mocker):
"""
Check the autosizing fits to the full screen size.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.resize = mocker.MagicMock()
mock_screen = mocker.MagicMock()
mock_screen.width.return_value = 1024
Expand All @@ -99,7 +105,8 @@ def test_show_login(mocker):
"""
The login dialog is displayed with a clean state.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.controller = mocker.MagicMock()
mock_ld = mocker.patch('securedrop_client.gui.main.LoginDialog')

Expand All @@ -114,7 +121,8 @@ def test_show_login_error(mocker):
"""
Ensures that an error message is displayed in the login dialog.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.show_login = mocker.MagicMock()
w.setup(mocker.MagicMock())
w.login_dialog = mocker.MagicMock()
Expand All @@ -128,7 +136,8 @@ def test_hide_login(mocker):
"""
Ensure the login dialog is closed and hidden.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.show_login = mocker.MagicMock()
ld = mocker.MagicMock()
w.login_dialog = ld
Expand All @@ -143,7 +152,8 @@ def test_show_sources(mocker):
"""
Ensure the sources list is passed to the main view to be updated.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.main_view = mocker.MagicMock()
w.show_sources([1, 2, 3])
w.main_view.show_sources.assert_called_once_with([1, 2, 3])
Expand All @@ -154,7 +164,8 @@ def test_update_error_status_default(mocker):
Ensure that the error to be shown in the error status bar will be passed to the top pane with a
default duration of 10 seconds.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.top_pane = mocker.MagicMock()
w.update_error_status(message='test error message')
w.top_pane.update_error_status.assert_called_once_with('test error message', 10000)
Expand All @@ -165,7 +176,8 @@ def test_update_error_status(mocker):
Ensure that the error to be shown in the error status bar will be passed to the top pane with
the duration of seconds provided.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.top_pane = mocker.MagicMock()
w.update_error_status(message='test error message', duration=123)
w.top_pane.update_error_status.assert_called_once_with('test error message', 123)
Expand All @@ -176,7 +188,8 @@ def test_update_activity_status_default(mocker):
Ensure that the activity to be shown in the activity status bar will be passed to the top pane
with a default duration of 0 seconds, i.e. forever.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.top_pane = mocker.MagicMock()
w.update_activity_status(message='test message')
w.top_pane.update_activity_status.assert_called_once_with('test message', 0)
Expand All @@ -187,7 +200,8 @@ def test_update_activity_status(mocker):
Ensure that the activity to be shown in the activity status bar will be passed to the top pane
with the duration of seconds provided.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.top_pane = mocker.MagicMock()
w.update_activity_status(message='test message', duration=123)
w.top_pane.update_activity_status.assert_called_once_with('test message', 123)
Expand All @@ -197,7 +211,8 @@ def test_clear_error_status(mocker):
"""
Ensure clear_error_status is called.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.top_pane = mocker.MagicMock()

w.clear_error_status()
Expand All @@ -209,7 +224,8 @@ def test_show_sync(mocker):
"""
If there's a value display the result of its "humanize" method.humanize
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.update_activity_status = mocker.MagicMock()
updated_on = mocker.MagicMock()
w.show_sync(updated_on)
Expand All @@ -221,7 +237,8 @@ def test_show_sync_no_sync(mocker):
"""
If there's no value to display, default to a "waiting" message.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.update_activity_status = mocker.MagicMock()
w.show_sync(None)
w.update_activity_status.assert_called_once_with('Waiting to refresh...', 5000)
Expand All @@ -231,7 +248,8 @@ def test_set_logged_in_as(mocker):
"""
Given a username, the left pane is appropriately called to update.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.left_pane = mocker.MagicMock()

w.set_logged_in_as('test')
Expand All @@ -243,7 +261,8 @@ def test_logout(mocker):
"""
Ensure the left pane updates to the logged out state.
"""
w = Window()
mock_session_maker = mocker.MagicMock()
w = Window(mock_session_maker)
w.left_pane = mocker.MagicMock()
w.top_pane = mocker.MagicMock()

Expand Down
1 change: 1 addition & 0 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ def test_MainView_init():
"""
Ensure the MainView instance is correctly set up.
"""
mock_session_maker = mocker.MagicMock()
mv = MainView(None)
assert isinstance(mv.source_list, SourceList)
assert isinstance(mv.view_holder, QWidget)
Expand Down
14 changes: 7 additions & 7 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_start_app(homedir, mocker):
"""
Ensure the expected things are configured and the application is started.
"""
mock_session_class = mocker.MagicMock()
mock_session_maker = mocker.MagicMock()
mock_args = mocker.MagicMock()
mock_qt_args = mocker.MagicMock()
mock_args.sdc_home = str(homedir)
Expand All @@ -123,13 +123,13 @@ def test_start_app(homedir, mocker):
mock_controller = mocker.patch('securedrop_client.app.Controller')
mocker.patch('securedrop_client.app.prevent_second_instance')
mocker.patch('securedrop_client.app.sys')
mocker.patch('securedrop_client.app.sessionmaker', return_value=mock_session_class)
mocker.patch('securedrop_client.app.make_session_maker', return_value=mock_session_maker)

start_app(mock_args, mock_qt_args)
mock_app.assert_called_once_with(mock_qt_args)
mock_win.assert_called_once_with()
mock_win.assert_called_once_with(mock_session_maker)
mock_controller.assert_called_once_with('http://localhost:8081/',
mock_win(), mock_session_class(),
mock_win(), mock_session_maker,
homedir, False)


Expand Down Expand Up @@ -170,7 +170,7 @@ def test_start_app(homedir, mocker):
def test_create_app_dir_permissions(tmpdir, mocker):

for idx, case in enumerate(PERMISSIONS_CASES):
mock_session_class = mocker.MagicMock()
mock_session_maker = mocker.MagicMock()
mock_args = mocker.MagicMock()
mock_qt_args = mocker.MagicMock()

Expand All @@ -192,7 +192,7 @@ def test_create_app_dir_permissions(tmpdir, mocker):
mocker.patch('securedrop_client.app.Controller')
mocker.patch('securedrop_client.app.sys')
mocker.patch('securedrop_client.app.prevent_second_instance')
mocker.patch('securedrop_client.app.sessionmaker', return_value=mock_session_class)
mocker.patch('securedrop_client.app.make_session_maker', return_value=mock_session_maker)

def func():
start_app(mock_args, mock_qt_args)
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_signal_interception(mocker, homedir):
mocker.patch('securedrop_client.app.QApplication')
mocker.patch('securedrop_client.app.prevent_second_instance')
mocker.patch('sys.exit')
mocker.patch('securedrop_client.db.make_engine')
mocker.patch('securedrop_client.db.make_session_maker')
mocker.patch('securedrop_client.app.init')
mocker.patch('securedrop_client.logic.Controller.setup')
mocker.patch('securedrop_client.logic.GpgHelper')
Expand Down
28 changes: 14 additions & 14 deletions tests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
JOURNO_KEY = f.read()


def test_message_logic(homedir, config, mocker):
def test_message_logic(homedir, config, mocker, session_maker):
"""
Ensure that messages are handled.
Using the `config` fixture to ensure the config is written to disk.
"""
gpg = GpgHelper(homedir, is_qubes=False)
gpg = GpgHelper(homedir, session_maker, is_qubes=False)

test_msg = 'tests/files/test-msg.gpg'
expected_output_filename = 'test-msg'
Expand All @@ -32,12 +32,12 @@ def test_message_logic(homedir, config, mocker):
assert dest == os.path.join(homedir, 'data', expected_output_filename)


def test_gunzip_logic(homedir, config, mocker):
def test_gunzip_logic(homedir, config, mocker, session_maker):
"""
Ensure that gzipped documents/files are handled
Using the `config` fixture to ensure the config is written to disk.
"""
gpg = GpgHelper(homedir, is_qubes=False)
gpg = GpgHelper(homedir, session_maker, is_qubes=False)

test_gzip = 'tests/files/test-doc.gz.gpg'
expected_output_filename = 'test-doc'
Expand All @@ -52,12 +52,12 @@ def test_gunzip_logic(homedir, config, mocker):
assert mock_unlink.call_count == 2


def test_subprocess_raises_exception(homedir, config, mocker):
def test_subprocess_raises_exception(homedir, config, mocker, session_maker):
"""
Ensure that failed GPG commands raise an exception.
Using the `config` fixture to ensure the config is written to disk.
"""
gpg = GpgHelper(homedir, is_qubes=False)
gpg = GpgHelper(homedir, session_maker, is_qubes=False)

test_gzip = 'tests/files/test-doc.gz.gpg'
output_filename = 'test-doc'
Expand All @@ -73,21 +73,21 @@ def test_subprocess_raises_exception(homedir, config, mocker):
assert mock_unlink.call_count == 1


def test_import_key(homedir, config, source):
def test_import_key(homedir, config, source, session_maker):
'''
Check the happy path that we can import a single PGP key.
Using the `config` fixture to ensure the config is written to disk.
'''
helper = GpgHelper(homedir, is_qubes=False)
helper = GpgHelper(homedir, session_maker, is_qubes=False)
helper.import_key(source['uuid'], source['public_key'], source['fingerprint'])


def test_import_key_gpg_call_fail(homedir, config, mocker):
def test_import_key_gpg_call_fail(homedir, config, mocker, session_maker):
'''
Check that a `CryptoError` is raised if calling `gpg` fails.
Using the `config` fixture to ensure the config is written to disk.
'''
helper = GpgHelper(homedir, is_qubes=False)
helper = GpgHelper(homedir, session_maker, is_qubes=False)
err = CalledProcessError(cmd=['foo'], returncode=1)
mock_call = mocker.patch('securedrop_client.crypto.subprocess.check_call',
side_effect=err)
Expand All @@ -99,12 +99,12 @@ def test_import_key_gpg_call_fail(homedir, config, mocker):
assert mock_call.called


def test_encrypt(homedir, source, config, mocker):
def test_encrypt(homedir, source, config, mocker, session_maker):
'''
Check that calling `encrypt` encrypts the message.
Using the `config` fixture to ensure the config is written to disk.
'''
helper = GpgHelper(homedir, is_qubes=False)
helper = GpgHelper(homedir, session_maker, is_qubes=False)

# first we have to ensure the pubkeys are available
helper._import(PUB_KEY)
Expand Down Expand Up @@ -134,12 +134,12 @@ def test_encrypt(homedir, source, config, mocker):
assert decrypted == plaintext


def test_encrypt_fail(homedir, source, config, mocker):
def test_encrypt_fail(homedir, source, config, mocker, session_maker):
'''
Check that a `CryptoError` is raised if the call to `gpg` fails.
Using the `config` fixture to ensure the config is written to disk.
'''
helper = GpgHelper(homedir, is_qubes=False)
helper = GpgHelper(homedir, session_maker, is_qubes=False)

# first we have to ensure the pubkeys are available
helper._import(PUB_KEY)
Expand Down
Loading

0 comments on commit bd8f402

Please sign in to comment.