Skip to content

Commit

Permalink
Enforce EmailStr for email fields on waiting list, and fix emails wit…
Browse files Browse the repository at this point in the history
…h unicode characters.
  • Loading branch information
MelissaAutumn committed Sep 3, 2024
1 parent 39112a1 commit e210920
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/src/appointment/controller/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def build(self):
filename=a.filename
)

return message.as_string()
return message.as_string().encode('utf-8')

def send(self):
"""actually send the email"""
Expand Down
4 changes: 2 additions & 2 deletions backend/src/appointment/database/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ class TokenData(BaseModel):


class SendInviteEmailIn(BaseModel):
email: str = Field(title='Email', min_length=1)
email: EmailStr = Field(title='Email', min_length=1)


class JoinTheWaitingList(BaseModel):
email: str = Field(title='Email', min_length=1)
email: EmailStr = Field(title='Email', min_length=1)


class TokenForWaitingList(BaseModel):
Expand Down
15 changes: 15 additions & 0 deletions backend/test/integration/test_waiting_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ def test_already_in_list(self, with_db, with_client, make_waiting_list):
# Ensure we did not send out an email
mock.assert_not_called()

def test_bad_emails(self, with_db, with_client, make_waiting_list):
# Variety of bad emails
emails = ['', 'test', 'test@', '@example.org']

for email in emails:
with patch('fastapi.BackgroundTasks.add_task') as mock:
response = with_client.post('/waiting-list/join', json={'email': email})

# Ensure we hit the email validation error
assert response.status_code == 422, response.json()
assert 'value is not a valid email address' in response.json()['detail'][0]['msg']

# Ensure we did not send out an email
mock.assert_not_called()


class TestWaitingListActionConfirm:
def assert_email_verified(self, db, waiting_list, success=True):
Expand Down

0 comments on commit e210920

Please sign in to comment.