From 1098f17c0c66ebc289a766ee0f9296e5c3dfd54d Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 11 Mar 2020 12:24:30 +0100 Subject: [PATCH] Support note in POST /api/alias/random/new --- README.md | 2 ++ app/api/views/new_random_alias.py | 9 ++++++++- app/models.py | 11 +++++++++-- tests/api/test_new_random_alias.py | 13 +++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e088f459..0052732e 100644 --- a/README.md +++ b/README.md @@ -699,6 +699,8 @@ Input: - `Authentication` header that contains the api key - (Optional but recommended) `hostname` passed in query string - (Optional) mode: either `uuid` or `word`. By default, use the user setting when creating new random alias. +- Request Message Body in json (`Content-Type` is `application/json`) + - (Optional) note: alias note Output: If success, 201 with the new alias, for example diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py index 5cc30d60..4d097b0c 100644 --- a/app/api/views/new_random_alias.py +++ b/app/api/views/new_random_alias.py @@ -15,6 +15,8 @@ from app.models import GenEmail, AliasUsedOn, AliasGeneratorEnum def new_random_alias(): """ Create a new random alias + Input: + (Optional) note Output: 201 if success @@ -30,6 +32,11 @@ def new_random_alias(): 400, ) + note = None + data = request.get_json() + if data: + note = data.get("note") + scheme = user.alias_generator mode = request.args.get("mode") if mode: @@ -40,7 +47,7 @@ def new_random_alias(): else: return jsonify(error=f"{mode} must be either word or alias"), 400 - gen_email = GenEmail.create_new_random(user=user, scheme=scheme) + gen_email = GenEmail.create_new_random(user=user, scheme=scheme, note=note) db.session.commit() hostname = request.args.get("hostname") diff --git a/app/models.py b/app/models.py index 2b2fe6e6..a7cd1771 100644 --- a/app/models.py +++ b/app/models.py @@ -581,12 +581,19 @@ class GenEmail(db.Model, ModelMixin): @classmethod def create_new_random( - cls, user, scheme: int = AliasGeneratorEnum.word.value, in_hex: bool = False + cls, + user, + scheme: int = AliasGeneratorEnum.word.value, + in_hex: bool = False, + note: str = None, ): """create a new random alias""" random_email = generate_email(scheme=scheme, in_hex=in_hex) return GenEmail.create( - user_id=user.id, email=random_email, mailbox_id=user.default_mailbox_id + user_id=user.id, + email=random_email, + mailbox_id=user.default_mailbox_id, + note=note, ) def mailbox_email(self): diff --git a/tests/api/test_new_random_alias.py b/tests/api/test_new_random_alias.py index eeb54a90..885d30dc 100644 --- a/tests/api/test_new_random_alias.py +++ b/tests/api/test_new_random_alias.py @@ -36,6 +36,7 @@ def test_custom_mode(flask_client): api_key = ApiKey.create(user.id, "for test") db.session.commit() + # without note r = flask_client.post( url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"), headers={"Authentication": api_key.code}, @@ -47,6 +48,18 @@ def test_custom_mode(flask_client): uuid_part = alias[: len(alias) - len(EMAIL_DOMAIN) - 1] assert is_valid_uuid(uuid_part) + # with note + r = flask_client.post( + url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"), + headers={"Authentication": api_key.code}, + json={"note": "test note",}, + ) + + assert r.status_code == 201 + alias = r.json["alias"] + ge = GenEmail.get_by(email=alias) + assert ge.note == "test note" + def test_out_of_quota(flask_client): user = User.create(