Browse Source

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

Son NK 5 years ago
parent
commit
aad06f73e9
3 changed files with 26 additions and 2 deletions
  1. 1 0
      README.md
  2. 3 1
      app/api/views/new_custom_alias.py
  3. 22 1
      tests/api/test_new_custom_alias.py

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

+ 3 - 1
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()
 

+ 22 - 1
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}",
+            "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}"},
+        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(