From 1afd392e5c7a1bae6b3eea8eba70d43ac4a3ebea Mon Sep 17 00:00:00 2001 From: Carlos Quintana <74399022+cquintana92@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:06:20 +0200 Subject: [PATCH] fix: crash on empty mailbox email (#2283) --- app/api/views/mailbox.py | 6 +++++- tests/api/test_mailbox.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/api/views/mailbox.py b/app/api/views/mailbox.py index 10bba2c2..a8787afb 100644 --- a/app/api/views/mailbox.py +++ b/app/api/views/mailbox.py @@ -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 diff --git a/tests/api/test_mailbox.py b/tests/api/test_mailbox.py index 085a42aa..9cbdfc6f 100644 --- a/tests/api/test_mailbox.py +++ b/tests/api/test_mailbox.py @@ -5,7 +5,7 @@ from app.models import Mailbox 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( @@ -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"}, + 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": ""}, ) assert r.status_code == 400