From aad06f73e9f1aa7e8abdb5007f9396f15016e3d5 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 11 Mar 2020 12:18:27 +0100 Subject: [PATCH] Support note in POST /api/alias/custom/new --- README.md | 1 + app/api/views/new_custom_alias.py | 4 +++- tests/api/test_new_custom_alias.py | 25 +++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e90c698d..e088f459 100644 --- a/README.md +++ b/README.md @@ -680,6 +680,7 @@ Input: - Request Message Body in json (`Content-Type` is `application/json`) - alias_prefix: string. The first part of the alias that user can choose. - alias_suffix: should be one of the suffixes returned in the `GET /api/v2/alias/options` endpoint. + - (Optional) note: alias note Output: If success, 201 with the new alias, for example diff --git a/app/api/views/new_custom_alias.py b/app/api/views/new_custom_alias.py index 5e739692..b6f77c11 100644 --- a/app/api/views/new_custom_alias.py +++ b/app/api/views/new_custom_alias.py @@ -21,6 +21,7 @@ def new_custom_alias(): alias_prefix, for ex "www_groupon_com" alias_suffix, either .random_letters@simplelogin.co or @my-domain.com optional "hostname" in args + optional "note" Output: 201 if success 409 if the alias already exists @@ -46,6 +47,7 @@ def new_custom_alias(): alias_prefix = data.get("alias_prefix", "").strip() alias_suffix = data.get("alias_suffix", "").strip() + note = data.get("note") alias_prefix = convert_to_id(alias_prefix) if not verify_prefix_suffix(user, alias_prefix, alias_suffix, user_custom_domains): @@ -57,7 +59,7 @@ def new_custom_alias(): return jsonify(error=f"alias {full_alias} already exists"), 409 gen_email = GenEmail.create( - user_id=user.id, email=full_alias, mailbox_id=user.default_mailbox_id + user_id=user.id, email=full_alias, mailbox_id=user.default_mailbox_id, note=note ) db.session.commit() diff --git a/tests/api/test_new_custom_alias.py b/tests/api/test_new_custom_alias.py index 40172ba2..1881d79c 100644 --- a/tests/api/test_new_custom_alias.py +++ b/tests/api/test_new_custom_alias.py @@ -16,17 +16,38 @@ def test_success(flask_client): api_key = ApiKey.create(user.id, "for test") db.session.commit() + # create new alias with note word = random_word() - r = flask_client.post( url_for("api.new_custom_alias", hostname="www.test.com"), headers={"Authentication": api_key.code}, - json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"}, + json={ + "alias_prefix": "prefix", + "alias_suffix": f".{word}@{EMAIL_DOMAIN}", + "note": "test note", + }, ) assert r.status_code == 201 assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}" + new_ge = GenEmail.get_by(email=r.json["alias"]) + assert new_ge.note == "test note" + + # create alias without note + word = random_word() + r = flask_client.post( + url_for("api.new_custom_alias", hostname="www.test.com"), + headers={"Authentication": api_key.code}, + json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}",}, + ) + + assert r.status_code == 201 + assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}" + + new_ge = GenEmail.get_by(email=r.json["alias"]) + assert new_ge.note is None + def test_out_of_quota(flask_client): user = User.create(