2018-02-20 11:22:17 +00:00
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
2016-04-06 11:01:22 +01:00
|
|
|
|
import pytest
|
|
|
|
|
|
from wtforms import ValidationError
|
2018-02-20 11:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
from app.main.validators import (
|
2019-11-08 17:12:32 +00:00
|
|
|
|
MustContainAlphanumericCharacters,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
NoCommasInPlaceHolders,
|
2019-05-03 15:13:39 +01:00
|
|
|
|
OnlySMSCharacters,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
ValidGovEmail,
|
|
|
|
|
|
)
|
2015-12-01 15:51:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-04-06 11:01:22 +01:00
|
|
|
|
def _gen_mock_field(x):
|
|
|
|
|
|
return Mock(data=x)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"email",
|
|
|
|
|
|
[ # TODO: update with email_domains.txt
|
|
|
|
|
|
"test@gsa.gov",
|
|
|
|
|
|
"test@abc.gov",
|
|
|
|
|
|
"test@xyz.gov",
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_valid_list_of_white_list_email_domains(
|
2022-01-04 18:33:23 +00:00
|
|
|
|
client_request,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
email,
|
|
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
email_domain_validators = ValidGovEmail()
|
|
|
|
|
|
email_domain_validators(None, _gen_mock_field(email))
|
2016-04-06 11:01:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"email",
|
|
|
|
|
|
[ # TODO: update with email_domains.txt
|
|
|
|
|
|
"test@gov.gsa",
|
|
|
|
|
|
"test@gmail.co",
|
|
|
|
|
|
"test@mail.co",
|
|
|
|
|
|
"test@amazonses.co",
|
|
|
|
|
|
"test@amazon.com",
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_invalid_list_of_white_list_email_domains(
|
2022-01-04 18:33:23 +00:00
|
|
|
|
client_request,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
email,
|
2023-07-12 12:09:44 -04:00
|
|
|
|
mock_get_organizations,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
email_domain_validators = ValidGovEmail()
|
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
|
email_domain_validators(None, _gen_mock_field(email))
|
2016-04-07 16:02:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_for_commas_in_placeholders(
|
2022-01-04 18:33:23 +00:00
|
|
|
|
client_request,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
with pytest.raises(ValidationError) as error:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
NoCommasInPlaceHolders()(None, _gen_mock_field("Hello ((name,date))"))
|
|
|
|
|
|
assert str(error.value) == "You cannot put commas between double brackets"
|
|
|
|
|
|
NoCommasInPlaceHolders()(None, _gen_mock_field("Hello ((name))"))
|
2016-07-01 13:47:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@pytest.mark.parametrize("msg", ["The quick brown fox", "Thé “quick” bröwn fox\u200B"])
|
2022-01-04 18:33:23 +00:00
|
|
|
|
def test_sms_character_validation(client_request, msg):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
OnlySMSCharacters(template_type="sms")(None, _gen_mock_field(msg))
|
2017-02-14 17:06:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
2023-09-29 17:28:58 -04:00
|
|
|
|
("data", "err_msg"),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
[
|
2017-02-15 16:21:14 +00:00
|
|
|
|
(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"∆ abc 📲 def 📵 ghi",
|
|
|
|
|
|
(
|
|
|
|
|
|
"You cannot use ∆, 📲 or 📵 in text messages. "
|
|
|
|
|
|
"They will not show up properly on everyone’s phones."
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
2017-02-15 16:21:14 +00:00
|
|
|
|
(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"📵",
|
|
|
|
|
|
(
|
|
|
|
|
|
"You cannot use 📵 in text messages. "
|
|
|
|
|
|
"It will not show up properly on everyone’s phones."
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2022-01-04 18:33:23 +00:00
|
|
|
|
def test_non_sms_character_validation(data, err_msg, client_request):
|
2017-02-14 17:06:32 +00:00
|
|
|
|
with pytest.raises(ValidationError) as error:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
OnlySMSCharacters(template_type="sms")(None, _gen_mock_field(data))
|
2017-02-14 17:06:32 +00:00
|
|
|
|
|
2017-02-15 16:21:14 +00:00
|
|
|
|
assert str(error.value) == err_msg
|
2017-02-14 17:06:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
2019-11-08 17:12:32 +00:00
|
|
|
|
@pytest.mark.parametrize("string", [".", "A.", ".8...."])
|
|
|
|
|
|
def test_if_string_does_not_contain_alphanumeric_characters_raises(string):
|
|
|
|
|
|
with pytest.raises(ValidationError) as error:
|
|
|
|
|
|
MustContainAlphanumericCharacters()(None, _gen_mock_field(string))
|
|
|
|
|
|
|
|
|
|
|
|
assert str(error.value) == "Must include at least two alphanumeric characters"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("string", [".A8", "AB.", ".42...."])
|
|
|
|
|
|
def test_if_string_contains_alphanumeric_characters_does_not_raise(string):
|
|
|
|
|
|
MustContainAlphanumericCharacters()(None, _gen_mock_field(string))
|