mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Fix the fixtures for templates lacking certain permissions
These fixtures were both calling other fixtures as functions and being called as functions in the tests. Rewriting the tests to make them Pytest 4 compatible means we are no longer using `sample_template_without_letter_permission`, so this has been deleted.
This commit is contained in:
@@ -262,13 +262,9 @@ def sample_template(
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_template_without_sms_permission(notify_db, notify_db_session):
|
||||
return sample_template(notify_db, notify_db_session, permissions=[EMAIL_TYPE])
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_template_without_letter_permission(notify_db, notify_db_session):
|
||||
return sample_template(notify_db, notify_db_session, template_type="letter", permissions=[EMAIL_TYPE])
|
||||
def sample_template_without_sms_permission(notify_db_session):
|
||||
service = create_service(service_permissions=[EMAIL_TYPE], check_if_service_exists=True)
|
||||
return create_template(service, template_type=SMS_TYPE)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
@@ -312,8 +308,9 @@ def sample_email_template(
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_template_without_email_permission(notify_db, notify_db_session):
|
||||
return sample_email_template(notify_db, notify_db_session, permissions=[SMS_TYPE])
|
||||
def sample_template_without_email_permission(notify_db_session):
|
||||
service = create_service(service_permissions=[SMS_TYPE], check_if_service_exists=True)
|
||||
return create_template(service, template_type=EMAIL_TYPE)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -30,8 +30,7 @@ from tests.app.conftest import (
|
||||
sample_service_whitelist as create_sample_service_whitelist,
|
||||
sample_api_key as create_sample_api_key,
|
||||
sample_service,
|
||||
sample_template_without_sms_permission,
|
||||
sample_template_without_email_permission)
|
||||
)
|
||||
from tests.app.db import create_service, create_reply_to_email
|
||||
|
||||
|
||||
@@ -1187,26 +1186,22 @@ def test_should_allow_international_number_on_sms_notification(client, notify_db
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'template_factory, to, expected_error', [
|
||||
(sample_template_without_sms_permission, '+447700900986', 'Cannot send text messages'),
|
||||
(sample_template_without_email_permission, 'notify@digital.cabinet-office.gov.uk', 'Cannot send emails')
|
||||
])
|
||||
def test_should_not_allow_notification_if_service_permission_not_set(
|
||||
client, notify_db, notify_db_session, mocker, template_factory, to, expected_error):
|
||||
template_without_permission = template_factory(notify_db, notify_db_session)
|
||||
mocked = mocker.patch(
|
||||
'app.celery.provider_tasks.deliver_{}.apply_async'.format(template_without_permission.template_type))
|
||||
def test_should_not_allow_sms_notifications_if_service_permission_not_set(
|
||||
client,
|
||||
mocker,
|
||||
sample_template_without_sms_permission,
|
||||
):
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
|
||||
data = {
|
||||
'to': to,
|
||||
'template': str(template_without_permission.id)
|
||||
'to': '+447700900986',
|
||||
'template': str(sample_template_without_sms_permission.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(service_id=template_without_permission.service_id)
|
||||
auth_header = create_authorization_header(service_id=sample_template_without_sms_permission.service_id)
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/{}'.format(template_without_permission.template_type),
|
||||
path='/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
@@ -1215,7 +1210,34 @@ def test_should_not_allow_notification_if_service_permission_not_set(
|
||||
error_json = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert error_json['result'] == 'error'
|
||||
assert error_json['message']['service'][0] == expected_error
|
||||
assert error_json['message']['service'][0] == 'Cannot send text messages'
|
||||
|
||||
|
||||
def test_should_not_allow_email_notifications_if_service_permission_not_set(
|
||||
client,
|
||||
mocker,
|
||||
sample_template_without_email_permission,
|
||||
):
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
|
||||
data = {
|
||||
'to': 'notify@digital.cabinet-office.gov.uk',
|
||||
'template': str(sample_template_without_email_permission.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_template_without_email_permission.service_id)
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert not mocked.called
|
||||
assert response.status_code == 400
|
||||
error_json = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert error_json['result'] == 'error'
|
||||
assert error_json['message']['service'][0] == 'Cannot send emails'
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -23,11 +23,7 @@ from app.models import (
|
||||
from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template
|
||||
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import (
|
||||
sample_template as create_sample_template,
|
||||
sample_template_without_email_permission,
|
||||
sample_template_without_letter_permission,
|
||||
sample_template_without_sms_permission)
|
||||
from tests.app.conftest import sample_template as create_sample_template
|
||||
from tests.app.db import (
|
||||
create_service, create_letter_contact, create_template, create_notification,
|
||||
create_template_folder,
|
||||
@@ -246,14 +242,21 @@ def test_should_raise_error_on_create_if_no_permission(
|
||||
assert json_resp['message'] == expected_error
|
||||
|
||||
|
||||
@pytest.mark.parametrize('template_factory, expected_error', [
|
||||
(sample_template_without_sms_permission, {'template_type': ['Updating text message templates is not allowed']}),
|
||||
(sample_template_without_email_permission, {'template_type': ['Updating email templates is not allowed']}),
|
||||
(sample_template_without_letter_permission, {'template_type': ['Updating letter templates is not allowed']})
|
||||
@pytest.mark.parametrize('template_type, permissions, expected_error', [
|
||||
(SMS_TYPE, [EMAIL_TYPE], {'template_type': ['Updating text message templates is not allowed']}),
|
||||
(EMAIL_TYPE, [LETTER_TYPE], {'template_type': ['Updating email templates is not allowed']}),
|
||||
(LETTER_TYPE, [SMS_TYPE], {'template_type': ['Updating letter templates is not allowed']})
|
||||
])
|
||||
def test_should_be_error_on_update_if_no_permission(
|
||||
client, sample_user, template_factory, expected_error, notify_db, notify_db_session):
|
||||
template_without_permission = template_factory(notify_db, notify_db_session)
|
||||
client,
|
||||
sample_user,
|
||||
notify_db_session,
|
||||
template_type,
|
||||
permissions,
|
||||
expected_error,
|
||||
):
|
||||
service = create_service(service_permissions=permissions)
|
||||
template_without_permission = create_template(service, template_type=template_type)
|
||||
data = {
|
||||
'content': 'new template content',
|
||||
'created_by': str(sample_user.id)
|
||||
|
||||
Reference in New Issue
Block a user