Support note in POST /api/alias/random/new

This commit is contained in:
Son NK 2020-03-11 12:24:30 +01:00
parent aad06f73e9
commit 1098f17c0c
4 changed files with 32 additions and 3 deletions

View file

@ -699,6 +699,8 @@ Input:
- `Authentication` header that contains the api key - `Authentication` header that contains the api key
- (Optional but recommended) `hostname` passed in query string - (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. - (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: Output:
If success, 201 with the new alias, for example If success, 201 with the new alias, for example

View file

@ -15,6 +15,8 @@ from app.models import GenEmail, AliasUsedOn, AliasGeneratorEnum
def new_random_alias(): def new_random_alias():
""" """
Create a new random alias Create a new random alias
Input:
(Optional) note
Output: Output:
201 if success 201 if success
@ -30,6 +32,11 @@ def new_random_alias():
400, 400,
) )
note = None
data = request.get_json()
if data:
note = data.get("note")
scheme = user.alias_generator scheme = user.alias_generator
mode = request.args.get("mode") mode = request.args.get("mode")
if mode: if mode:
@ -40,7 +47,7 @@ def new_random_alias():
else: else:
return jsonify(error=f"{mode} must be either word or alias"), 400 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() db.session.commit()
hostname = request.args.get("hostname") hostname = request.args.get("hostname")

View file

@ -581,12 +581,19 @@ class GenEmail(db.Model, ModelMixin):
@classmethod @classmethod
def create_new_random( 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""" """create a new random alias"""
random_email = generate_email(scheme=scheme, in_hex=in_hex) random_email = generate_email(scheme=scheme, in_hex=in_hex)
return GenEmail.create( 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): def mailbox_email(self):

View file

@ -36,6 +36,7 @@ def test_custom_mode(flask_client):
api_key = ApiKey.create(user.id, "for test") api_key = ApiKey.create(user.id, "for test")
db.session.commit() db.session.commit()
# without note
r = flask_client.post( r = flask_client.post(
url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"), url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"),
headers={"Authentication": api_key.code}, headers={"Authentication": api_key.code},
@ -47,6 +48,18 @@ def test_custom_mode(flask_client):
uuid_part = alias[: len(alias) - len(EMAIL_DOMAIN) - 1] uuid_part = alias[: len(alias) - len(EMAIL_DOMAIN) - 1]
assert is_valid_uuid(uuid_part) 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): def test_out_of_quota(flask_client):
user = User.create( user = User.create(