Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #730: Add default schema for waitlists #731

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions ctms/schemas/waitlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def __lt__(self, other):

@root_validator
def check_fields(cls, values): # pylint:disable = no-self-argument
if "subscribed" in values and values["subscribed"] and "name" in values:
validate_waitlist_fields(values["name"], values.get("fields", {}))
# If subscribed is False, we don't need to validate fields.
validate_waitlist_fields(values.get("name"), values.get("fields", {}))
return values

class Config:
Expand Down Expand Up @@ -88,20 +86,33 @@ class Config:
extra = "forbid"


def validate_waitlist_fields(name: str, fields: dict):
def CountryField(): # pylint:disable = invalid-name
leplatrem marked this conversation as resolved.
Show resolved Hide resolved
return Field(
default=None,
max_length=100,
leplatrem marked this conversation as resolved.
Show resolved Hide resolved
description="Waitlist country",
example="fr",
)


def PlatformField(): # pylint:disable = invalid-name
return Field(
default=None,
max_length=100,
description="VPN waitlist platforms as comma-separated list",
example="ios,mac",
)


def validate_waitlist_fields(name: Optional[str], fields: dict):
"""
Once waitlists will have been migrated to a full N-N relationship,
this will be the only remaining VPN specific piece of code.
"""
if name == "relay":

class RelayFieldsSchema(ComparableBase):
geo: Optional[str] = Field(
default=None,
max_length=100,
description="Waitlist country",
example="fr",
)
geo: Optional[str] = CountryField()
leplatrem marked this conversation as resolved.
Show resolved Hide resolved

class Config:
extra = "forbid"
Expand All @@ -111,24 +122,26 @@ class Config:
elif name == "vpn":

class VPNFieldsSchema(ComparableBase):
geo: Optional[str] = Field(
default=None,
max_length=100,
description="Waitlist country",
example="fr",
)
platform: Optional[str] = Field(
default=None,
max_length=100,
description="VPN waitlist platforms as comma-separated list",
example="ios,mac",
)
geo: Optional[str] = CountryField()
platform: Optional[str] = PlatformField()

class Config:
extra = "forbid"

VPNFieldsSchema(**fields)

else:
# Default schema for any waitlist.
# Only the known fields are validated. Any extra field would
# be accepted as is.
# This should allow us to onboard most waitlists without specific
# code change and service redeployment.
class DefaultFieldsSchema(ComparableBase):
geo: Optional[str] = CountryField()
platform: Optional[str] = PlatformField()

DefaultFieldsSchema(**fields)


def validate_waitlist_newsletters(values):
"""
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/schemas/test_waitlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"name": "a",
"source": "http://website.com",
"fields": {"foo": "bar", "geo": "b"},
"fields": {"foo": "bar", "geo": "br"},
},
],
)
Expand All @@ -31,6 +31,16 @@ def test_waitlist_with_valid_input_data(data):
{"name": "a", "fields": None},
{"name": "a", "fields": "foo"},
{"name": "a", "fields": [{}]},
{
"name": "a",
"source": "http://website.com",
"fields": {"geo": "s" * 101},
},
{
"name": "a",
"source": "http://website.com",
"fields": {"platform": "s" * 101},
},
],
)
def test_waitlist_with_invalid_input_data(data):
Expand Down