Skip to content

Commit

Permalink
updated tests to maintain coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zenmonkeykstop committed Dec 5, 2020
1 parent 8fcff8e commit d533c90
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
67 changes: 67 additions & 0 deletions securedrop/tests/test_journalist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions securedrop/tests/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d533c90

Please sign in to comment.