2016-11-07 17:41:49 +00:00
|
|
|
import pytest
|
2016-11-10 13:09:25 +00:00
|
|
|
from marshmallow import ValidationError
|
2017-03-08 23:15:56 +00:00
|
|
|
from sqlalchemy import desc
|
|
|
|
|
|
2019-11-11 16:35:18 +00:00
|
|
|
from app.dao.provider_details_dao import dao_update_provider_details, get_provider_details_by_identifier
|
2017-03-08 23:15:56 +00:00
|
|
|
from app.models import ProviderDetailsHistory
|
2019-09-13 13:26:46 +01:00
|
|
|
from tests.app.db import create_api_key
|
2016-11-10 13:09:25 +00:00
|
|
|
|
2016-11-07 17:41:49 +00:00
|
|
|
|
2016-09-15 15:59:34 +01:00
|
|
|
def test_job_schema_doesnt_return_notifications(sample_notification_with_job):
|
2016-06-10 15:54:21 +01:00
|
|
|
from app.schemas import job_schema
|
|
|
|
|
|
2016-09-15 15:59:34 +01:00
|
|
|
job = sample_notification_with_job.job
|
2016-06-10 15:54:21 +01:00
|
|
|
assert job.notifications.count() == 1
|
|
|
|
|
|
|
|
|
|
data, errors = job_schema.dump(job)
|
|
|
|
|
|
|
|
|
|
assert not errors
|
|
|
|
|
assert 'notifications' not in data
|
2016-09-23 14:44:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_notification_schema_ignores_absent_api_key(sample_notification_with_job):
|
|
|
|
|
from app.schemas import notification_with_template_schema
|
|
|
|
|
|
|
|
|
|
data = notification_with_template_schema.dump(sample_notification_with_job).data
|
|
|
|
|
assert data['key_name'] is None
|
|
|
|
|
|
|
|
|
|
|
2019-09-13 13:26:46 +01:00
|
|
|
def test_notification_schema_adds_api_key_name(sample_notification):
|
2016-09-23 14:44:15 +01:00
|
|
|
from app.schemas import notification_with_template_schema
|
|
|
|
|
|
2019-09-13 13:26:46 +01:00
|
|
|
api_key = create_api_key(sample_notification.service, key_name='Test key')
|
|
|
|
|
sample_notification.api_key = api_key
|
|
|
|
|
|
|
|
|
|
data = notification_with_template_schema.dump(sample_notification).data
|
2016-09-23 14:44:15 +01:00
|
|
|
assert data['key_name'] == 'Test key'
|
2016-11-07 17:41:49 +00:00
|
|
|
|
|
|
|
|
|
2017-05-10 17:30:09 +01:00
|
|
|
@pytest.mark.parametrize('schema_name', [
|
|
|
|
|
'notification_with_template_schema',
|
|
|
|
|
'notification_schema',
|
|
|
|
|
'notification_with_template_schema',
|
|
|
|
|
'notification_with_personalisation_schema',
|
|
|
|
|
])
|
|
|
|
|
def test_notification_schema_has_correct_status(sample_notification, schema_name):
|
|
|
|
|
from app import schemas
|
|
|
|
|
|
|
|
|
|
data = getattr(schemas, schema_name).dump(sample_notification).data
|
|
|
|
|
|
|
|
|
|
assert data['status'] == sample_notification.status
|
|
|
|
|
|
|
|
|
|
|
2016-11-07 17:41:49 +00:00
|
|
|
@pytest.mark.parametrize('user_attribute, user_value', [
|
|
|
|
|
('name', 'New User'),
|
|
|
|
|
('email_address', 'newuser@mail.com'),
|
|
|
|
|
('mobile_number', '+4407700900460')
|
|
|
|
|
])
|
2016-11-10 13:09:25 +00:00
|
|
|
def test_user_update_schema_accepts_valid_attribute_pairs(user_attribute, user_value):
|
2016-11-07 17:41:49 +00:00
|
|
|
update_dict = {
|
|
|
|
|
user_attribute: user_value
|
|
|
|
|
}
|
|
|
|
|
from app.schemas import user_update_schema_load_json
|
|
|
|
|
|
|
|
|
|
data, errors = user_update_schema_load_json.load(update_dict)
|
|
|
|
|
assert not errors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('user_attribute, user_value', [
|
|
|
|
|
('name', None),
|
|
|
|
|
('name', ''),
|
|
|
|
|
('email_address', 'bademail@...com'),
|
|
|
|
|
('mobile_number', '+44077009')
|
|
|
|
|
])
|
2016-11-10 13:09:25 +00:00
|
|
|
def test_user_update_schema_rejects_invalid_attribute_pairs(user_attribute, user_value):
|
2016-11-07 17:41:49 +00:00
|
|
|
from app.schemas import user_update_schema_load_json
|
|
|
|
|
update_dict = {
|
|
|
|
|
user_attribute: user_value
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 13:09:25 +00:00
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
data, errors = user_update_schema_load_json.load(update_dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('user_attribute', [
|
|
|
|
|
'id', 'updated_at', 'created_at', 'user_to_service',
|
|
|
|
|
'_password', 'verify_codes', 'logged_in_at', 'password_changed_at',
|
|
|
|
|
'failed_login_count', 'state', 'platform_admin'
|
|
|
|
|
])
|
|
|
|
|
def test_user_update_schema_rejects_disallowed_attribute_keys(user_attribute):
|
|
|
|
|
update_dict = {
|
|
|
|
|
user_attribute: 'not important'
|
|
|
|
|
}
|
|
|
|
|
from app.schemas import user_update_schema_load_json
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValidationError) as excinfo:
|
2016-11-07 17:41:49 +00:00
|
|
|
data, errors = user_update_schema_load_json.load(update_dict)
|
2016-11-10 13:09:25 +00:00
|
|
|
|
|
|
|
|
assert excinfo.value.messages['_schema'][0] == 'Unknown field name {}'.format(user_attribute)
|
2017-03-08 23:15:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_provider_details_schema_returns_user_details(
|
|
|
|
|
mocker,
|
|
|
|
|
sample_user,
|
2019-11-11 16:35:18 +00:00
|
|
|
restore_provider_details
|
2017-03-08 23:15:56 +00:00
|
|
|
):
|
|
|
|
|
from app.schemas import provider_details_schema
|
2019-11-11 16:35:18 +00:00
|
|
|
current_sms_provider = get_provider_details_by_identifier('mmg')
|
2017-03-08 23:15:56 +00:00
|
|
|
current_sms_provider.created_by = sample_user
|
|
|
|
|
data = provider_details_schema.dump(current_sms_provider).data
|
|
|
|
|
|
|
|
|
|
assert sorted(data['created_by'].keys()) == sorted(['id', 'email_address', 'name'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_provider_details_history_schema_returns_user_details(
|
|
|
|
|
mocker,
|
|
|
|
|
sample_user,
|
|
|
|
|
restore_provider_details,
|
|
|
|
|
):
|
|
|
|
|
from app.schemas import provider_details_schema
|
2019-11-11 16:35:18 +00:00
|
|
|
current_sms_provider = get_provider_details_by_identifier('mmg')
|
2017-03-08 23:15:56 +00:00
|
|
|
current_sms_provider.created_by_id = sample_user.id
|
|
|
|
|
data = provider_details_schema.dump(current_sms_provider).data
|
|
|
|
|
|
|
|
|
|
dao_update_provider_details(current_sms_provider)
|
|
|
|
|
|
|
|
|
|
current_sms_provider_in_history = ProviderDetailsHistory.query.filter(
|
|
|
|
|
ProviderDetailsHistory.id == current_sms_provider.id
|
|
|
|
|
).order_by(
|
|
|
|
|
desc(ProviderDetailsHistory.version)
|
|
|
|
|
).first()
|
|
|
|
|
data = provider_details_schema.dump(current_sms_provider_in_history).data
|
|
|
|
|
|
|
|
|
|
assert sorted(data['created_by'].keys()) == sorted(['id', 'email_address', 'name'])
|