mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-23 15:57:23 -04:00
Make whitespace stripping work for whitelists too
It’s a bit hacky, but it fixes a potential issue for users.
Code adapted from:
2c34f678ab
This commit is contained in:
@@ -4,6 +4,7 @@ import weakref
|
|||||||
|
|
||||||
from flask_wtf import FlaskForm as Form
|
from flask_wtf import FlaskForm as Form
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from itertools import chain
|
||||||
|
|
||||||
from notifications_utils.recipients import (
|
from notifications_utils.recipients import (
|
||||||
validate_phone_number,
|
validate_phone_number,
|
||||||
@@ -174,6 +175,17 @@ class StripWhitespaceForm(Form):
|
|||||||
return bound
|
return bound
|
||||||
|
|
||||||
|
|
||||||
|
class StripWhitespaceStringField(StringField):
|
||||||
|
def __init__(self, label=None, **kwargs):
|
||||||
|
kwargs['filters'] = tuple(chain(
|
||||||
|
kwargs.get('filters', ()),
|
||||||
|
(
|
||||||
|
strip_whitespace,
|
||||||
|
),
|
||||||
|
))
|
||||||
|
super(StringField, self).__init__(label, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class LoginForm(StripWhitespaceForm):
|
class LoginForm(StripWhitespaceForm):
|
||||||
email_address = StringField('Email address', validators=[
|
email_address = StringField('Email address', validators=[
|
||||||
Length(min=5, max=255),
|
Length(min=5, max=255),
|
||||||
@@ -641,6 +653,14 @@ class LetterBranding(StripWhitespaceForm):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class EmailFieldInWhitelist(EmailField, StripWhitespaceStringField):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InternationalPhoneNumberInWhitelist(InternationalPhoneNumber, StripWhitespaceStringField):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Whitelist(StripWhitespaceForm):
|
class Whitelist(StripWhitespaceForm):
|
||||||
|
|
||||||
def populate(self, email_addresses, phone_numbers):
|
def populate(self, email_addresses, phone_numbers):
|
||||||
@@ -652,7 +672,7 @@ class Whitelist(StripWhitespaceForm):
|
|||||||
form_field[index].data = value
|
form_field[index].data = value
|
||||||
|
|
||||||
email_addresses = FieldList(
|
email_addresses = FieldList(
|
||||||
EmailField(
|
EmailFieldInWhitelist(
|
||||||
'',
|
'',
|
||||||
validators=[
|
validators=[
|
||||||
Optional(),
|
Optional(),
|
||||||
@@ -666,7 +686,7 @@ class Whitelist(StripWhitespaceForm):
|
|||||||
)
|
)
|
||||||
|
|
||||||
phone_numbers = FieldList(
|
phone_numbers = FieldList(
|
||||||
InternationalPhoneNumber(
|
InternationalPhoneNumberInWhitelist(
|
||||||
'',
|
'',
|
||||||
validators=[
|
validators=[
|
||||||
Optional()
|
Optional()
|
||||||
|
|||||||
@@ -351,8 +351,8 @@ def test_should_update_whitelist(
|
|||||||
service_id = str(uuid.uuid4())
|
service_id = str(uuid.uuid4())
|
||||||
data = OrderedDict([
|
data = OrderedDict([
|
||||||
('email_addresses-1', 'test@example.com'),
|
('email_addresses-1', 'test@example.com'),
|
||||||
('email_addresses-3', 'test@example.com'),
|
('email_addresses-3', ' test@example.com '),
|
||||||
('phone_numbers-0', '07900900000'),
|
('phone_numbers-0', '07900900000 '),
|
||||||
('phone_numbers-2', '+1800-555-555'),
|
('phone_numbers-2', '+1800-555-555'),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user