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
|
|
|
|
|
|
2021-03-10 13:55:06 +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
|
|
|
|
|
|
2022-05-06 15:52:44 +01:00
|
|
|
data = job_schema.dump(job)
|
2016-06-10 15:54:21 +01:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
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
|
|
|
|
|
|
2022-05-06 15:52:44 +01:00
|
|
|
data = notification_with_template_schema.dump(sample_notification_with_job)
|
2023-08-29 14:54:30 -07:00
|
|
|
assert data["key_name"] is None
|
2016-09-23 14:44:15 +01:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
api_key = create_api_key(sample_notification.service, key_name="Test key")
|
2019-09-13 13:26:46 +01:00
|
|
|
sample_notification.api_key = api_key
|
|
|
|
|
|
2022-05-06 15:52:44 +01:00
|
|
|
data = notification_with_template_schema.dump(sample_notification)
|
2023-08-29 14:54:30 -07:00
|
|
|
assert data["key_name"] == "Test key"
|
2016-11-07 17:41:49 +00:00
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"schema_name",
|
|
|
|
|
[
|
|
|
|
|
"notification_with_template_schema",
|
|
|
|
|
"notification_schema",
|
|
|
|
|
"notification_with_template_schema",
|
|
|
|
|
"notification_with_personalisation_schema",
|
|
|
|
|
],
|
|
|
|
|
)
|
2017-05-10 17:30:09 +01:00
|
|
|
def test_notification_schema_has_correct_status(sample_notification, schema_name):
|
|
|
|
|
from app import schemas
|
|
|
|
|
|
2022-05-06 15:52:44 +01:00
|
|
|
data = getattr(schemas, schema_name).dump(sample_notification)
|
2017-05-10 17:30:09 +01:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
assert data["status"] == sample_notification.status
|
2017-05-10 17:30:09 +01:00
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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):
|
2023-08-29 14:54:30 -07:00
|
|
|
update_dict = {user_attribute: user_value}
|
2016-11-07 17:41:49 +00:00
|
|
|
from app.schemas import user_update_schema_load_json
|
|
|
|
|
|
2022-05-06 15:25:14 +01:00
|
|
|
errors = user_update_schema_load_json.validate(update_dict)
|
2016-11-07 17:41:49 +00:00
|
|
|
assert not errors
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@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
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
|
|
|
update_dict = {user_attribute: user_value}
|
2016-11-07 17:41:49 +00:00
|
|
|
|
2016-11-10 13:09:25 +00:00
|
|
|
with pytest.raises(ValidationError):
|
2022-05-06 15:25:14 +01:00
|
|
|
user_update_schema_load_json.load(update_dict)
|
2016-11-10 13:09:25 +00:00
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@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",
|
|
|
|
|
],
|
|
|
|
|
)
|
2016-11-10 13:09:25 +00:00
|
|
|
def test_user_update_schema_rejects_disallowed_attribute_keys(user_attribute):
|
2023-08-29 14:54:30 -07:00
|
|
|
update_dict = {user_attribute: "not important"}
|
2016-11-10 13:09:25 +00:00
|
|
|
from app.schemas import user_update_schema_load_json
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValidationError) as excinfo:
|
2022-05-06 15:25:14 +01:00
|
|
|
user_update_schema_load_json.load(update_dict)
|
2016-11-10 13:09:25 +00:00
|
|
|
|
2023-08-29 14:54:30 -07: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(
|
2023-08-29 14:54:30 -07:00
|
|
|
mocker, sample_user, restore_provider_details
|
2017-03-08 23:15:56 +00:00
|
|
|
):
|
|
|
|
|
from app.schemas import provider_details_schema
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
|
|
|
current_sms_provider = get_provider_details_by_identifier("sns")
|
2017-03-08 23:15:56 +00:00
|
|
|
current_sms_provider.created_by = sample_user
|
2022-05-06 15:52:44 +01:00
|
|
|
data = provider_details_schema.dump(current_sms_provider)
|
2017-03-08 23:15:56 +00:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
assert sorted(data["created_by"].keys()) == sorted(["id", "email_address", "name"])
|
2017-03-08 23:15:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_provider_details_history_schema_returns_user_details(
|
|
|
|
|
mocker,
|
|
|
|
|
sample_user,
|
|
|
|
|
restore_provider_details,
|
|
|
|
|
):
|
|
|
|
|
from app.schemas import provider_details_schema
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
|
|
|
current_sms_provider = get_provider_details_by_identifier("sns")
|
2017-03-08 23:15:56 +00:00
|
|
|
current_sms_provider.created_by_id = sample_user.id
|
2022-05-06 15:52:44 +01:00
|
|
|
data = provider_details_schema.dump(current_sms_provider)
|
2017-03-08 23:15:56 +00:00
|
|
|
|
|
|
|
|
dao_update_provider_details(current_sms_provider)
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
current_sms_provider_in_history = (
|
|
|
|
|
ProviderDetailsHistory.query.filter(
|
|
|
|
|
ProviderDetailsHistory.id == current_sms_provider.id
|
|
|
|
|
)
|
|
|
|
|
.order_by(desc(ProviderDetailsHistory.version))
|
|
|
|
|
.first()
|
|
|
|
|
)
|
2022-05-06 15:52:44 +01:00
|
|
|
data = provider_details_schema.dump(current_sms_provider_in_history)
|
2017-03-08 23:15:56 +00:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
assert sorted(data["created_by"].keys()) == sorted(["id", "email_address", "name"])
|