mirror of
https://github.com/simple-login/app.git
synced 2025-10-06 05:17:41 +08:00
Merge pull request #104 from simple-login/api-note
Support alias note in API
This commit is contained in:
commit
9bcec60736
9 changed files with 98 additions and 27 deletions
|
@ -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
|
||||
|
@ -698,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
|
||||
|
@ -806,7 +809,8 @@ If success, 200 with the list of aliases, for example:
|
|||
"nb_block": 0,
|
||||
"nb_forward": 0,
|
||||
"nb_reply": 0,
|
||||
"enabled": true
|
||||
"enabled": true,
|
||||
"note": "This is a note"
|
||||
},
|
||||
{
|
||||
"creation_date": "2020-02-04 16:23:02+00:00",
|
||||
|
@ -816,7 +820,8 @@ If success, 200 with the list of aliases, for example:
|
|||
"nb_block": 0,
|
||||
"nb_forward": 0,
|
||||
"nb_reply": 0,
|
||||
"enabled": false
|
||||
"enabled": false,
|
||||
"note": null
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ def get_aliases():
|
|||
- nb_forward
|
||||
- nb_block
|
||||
- nb_reply
|
||||
- note
|
||||
|
||||
"""
|
||||
user = g.user
|
||||
|
@ -48,6 +49,7 @@ def get_aliases():
|
|||
"nb_block": alias.nb_blocked,
|
||||
"nb_reply": alias.nb_reply,
|
||||
"enabled": alias.gen_email.enabled,
|
||||
"note": alias.note,
|
||||
}
|
||||
for alias in aliases
|
||||
]
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -30,6 +30,7 @@ class AliasInfo:
|
|||
|
||||
show_intro_test_send_email: bool = False
|
||||
highlight: bool = False
|
||||
note: str
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
for k, v in kwargs.items():
|
||||
|
@ -230,6 +231,7 @@ def get_alias_info(
|
|||
nb_reply=0,
|
||||
highlight=ge.id == highlight_gen_email_id,
|
||||
mailbox=mb,
|
||||
note=ge.note,
|
||||
)
|
||||
|
||||
alias_info = aliases[ge.email]
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -48,6 +48,18 @@ def test_success_with_pagination(flask_client):
|
|||
assert r.status_code == 200
|
||||
assert len(r.json["aliases"]) == PAGE_LIMIT
|
||||
|
||||
# assert returned field
|
||||
for a in r.json["aliases"]:
|
||||
assert "id" in a
|
||||
assert "email" in a
|
||||
assert "creation_date" in a
|
||||
assert "creation_timestamp" in a
|
||||
assert "nb_forward" in a
|
||||
assert "nb_block" in a
|
||||
assert "nb_reply" in a
|
||||
assert "enabled" in a
|
||||
assert "note" in a
|
||||
|
||||
# get aliases on the 2nd page, should return 2 aliases
|
||||
# as the total number of aliases is PAGE_LIMIT +2
|
||||
# 1 alias is created when user is created
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Reference in a new issue