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: crash on empty mailbox email #2283

Merged
merged 1 commit into from
Oct 22, 2024
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
6 changes: 5 additions & 1 deletion app/api/views/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def create_mailbox():
the new mailbox dict
"""
user = g.user
mailbox_email = sanitize_email(request.get_json().get("email"))
email = request.get_json().get("email")
if not email:
return jsonify(error="Invalid email"), 400

mailbox_email = sanitize_email(email)

try:
new_mailbox = mailbox_utils.create_mailbox(user, mailbox_email).mailbox
Expand Down
30 changes: 27 additions & 3 deletions tests/api/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tests.utils import login


def test_create_mailbox(flask_client):
def test_create_mailbox_valid(flask_client):
login(flask_client)

r = flask_client.post(
Expand All @@ -21,10 +21,34 @@ def test_create_mailbox(flask_client):
assert r.json["default"] is False
assert r.json["nb_alias"] == 0

# invalid email address

def test_create_mailbox_invalid_email(flask_client):
login(flask_client)
r = flask_client.post(
"/api/mailboxes",
json={"email": "gmail.com"}, # not an email address
)

assert r.status_code == 400
assert r.json == {"error": "Invalid email"}


def test_create_mailbox_empty_payload(flask_client):
login(flask_client)
r = flask_client.post(
"/api/mailboxes",
json={},
)

assert r.status_code == 400
assert r.json == {"error": "Invalid email"}


def test_create_mailbox_empty_email(flask_client):
login(flask_client)
r = flask_client.post(
"/api/mailboxes",
json={"email": "gmail.com"},
json={"email": ""},
)

assert r.status_code == 400
Expand Down
Loading