Prechádzať zdrojové kódy

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

Son NK 5 rokov pred
rodič
commit
1098f17c0c

+ 2 - 0
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

+ 8 - 1
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")

+ 9 - 2
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):

+ 13 - 0
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(