From d533c9058842d442f8b913e080d6435cd27b52de Mon Sep 17 00:00:00 2001 From: Kevin O'Gorman Date: Fri, 4 Dec 2020 16:39:22 -0500 Subject: [PATCH] updated tests to maintain coverage --- securedrop/tests/test_journalist.py | 67 +++++++++++++++++++++++++++++ securedrop/tests/test_source.py | 13 ++++++ 2 files changed, 80 insertions(+) diff --git a/securedrop/tests/test_journalist.py b/securedrop/tests/test_journalist.py index f61b9b3d9c..6af1652abd 100644 --- a/securedrop/tests/test_journalist.py +++ b/securedrop/tests/test_journalist.py @@ -1506,6 +1506,41 @@ def test_no_prevent_document_uploads(journalist_app, test_admin): app.post(url_for('admin.update_submission_preferences'), follow_redirects=True) ins.assert_message_flashed('Preferences saved.', 'submission-preferences-success') + assert InstanceConfig.get_current().allow_document_uploads is True + + +def test_prevent_document_uploads_invalid(journalist_app, test_admin): + with journalist_app.test_client() as app: + _login_user(app, test_admin['username'], test_admin['password'], + test_admin['otp_secret']) + form_true = journalist_app_module.forms.SubmissionPreferencesForm( + prevent_document_uploads=True) + app.post(url_for('admin.update_submission_preferences'), + data=form_true.data, + follow_redirects=True) + assert InstanceConfig.get_current().allow_document_uploads is False + + with patch('flask_wtf.FlaskForm.validate_on_submit') as fMock: + fMock.return_value = False + form_false = journalist_app_module.forms.SubmissionPreferencesForm( + prevent_document_uploads=False) + app.post(url_for('admin.update_submission_preferences'), + data=form_false.data, + follow_redirects=True) + assert InstanceConfig.get_current().allow_document_uploads is False + + +def test_orgname_default_set(journalist_app, test_admin): + + class dummy_current(): + organization_name = None + + with patch.object(InstanceConfig, 'get_current') as iMock: + with journalist_app.test_client() as app: + iMock.return_value = dummy_current() + _login_user(app, test_admin['username'], test_admin['password'], + test_admin['otp_secret']) + assert g.organization_name == "SecureDrop" def test_orgname_valid_succeeds(journalist_app, test_admin): @@ -1618,6 +1653,38 @@ def test_logo_upload_with_invalid_filetype_fails(journalist_app, test_admin): assert "You can only upload PNG image files." in text +def test_logo_upload_save_fails(journalist_app, test_admin): + # Save original logo to restore after test run + logo_image_location = os.path.join(config.SECUREDROP_ROOT, + "static/i/logo.png") + with io.open(logo_image_location, 'rb') as logo_file: + original_image = logo_file.read() + + try: + with journalist_app.test_client() as app: + _login_user(app, test_admin['username'], test_admin['password'], + test_admin['otp_secret']) + # Create 1px * 1px 'white' PNG file from its base64 string + form = journalist_app_module.forms.LogoForm( + logo=(BytesIO(base64.decodebytes + (b"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQ" + b"VR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=")), 'test.png') + ) + with InstrumentedApp(journalist_app) as ins: + with patch('werkzeug.datastructures.FileStorage.save') as sMock: + sMock.side_effect = Exception + app.post(url_for('admin.manage_config'), + data=form.data, + follow_redirects=True) + + ins.assert_message_flashed("Unable to process the image file." + " Try another one.", "logo-error") + finally: + # Restore original image to logo location for subsequent tests + with io.open(logo_image_location, 'wb') as logo_file: + logo_file.write(original_image) + + def test_creation_of_ossec_test_log_event(journalist_app, test_admin, mocker): mocked_error_logger = mocker.patch('journalist.app.logger.error') with journalist_app.test_client() as app: diff --git a/securedrop/tests/test_source.py b/securedrop/tests/test_source.py index 48bc1ceb48..4b96de3426 100644 --- a/securedrop/tests/test_source.py +++ b/securedrop/tests/test_source.py @@ -33,6 +33,19 @@ def test_page_not_found(source_app): ins.assert_template_used('notfound.html') +def test_orgname_default_set(source_app): + + class dummy_current(): + organization_name = None + + with patch.object(InstanceConfig, 'get_current') as iMock: + with source_app.test_client() as app: + iMock.return_value = dummy_current() + resp = app.get(url_for('main.index')) + assert resp.status_code == 200 + assert g.organization_name == "SecureDrop" + + def test_index(source_app): """Test that the landing page loads and looks how we expect""" with source_app.test_client() as app: