Merge pull request #119 from simple-login/api-alias

Api alias
This commit is contained in:
Son Nguyen Kim 2020-03-26 22:14:17 +01:00 committed by GitHub
commit b3d99cc010
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 149 additions and 18 deletions

View file

@ -714,7 +714,16 @@ If success, 201 with the new alias, for example
```json
{
"alias": "www_groupon_com@my_domain.com"
"alias": "www_groupon_com@my_domain.com",
"creation_date": "2020-02-04 16:23:02+00:00",
"creation_timestamp": 1580833382,
"email": "www_groupon_com@my_domain.com",
"id": 4,
"nb_block": 0,
"nb_forward": 0,
"nb_reply": 0,
"enabled": true,
"note": "This is a note"
}
```
@ -734,7 +743,16 @@ If success, 201 with the new alias, for example
```json
{
"alias": "www_groupon_com@my_domain.com"
"alias": "prefix.suffix@my_domain.com",
"creation_date": "2020-02-04 16:23:02+00:00",
"creation_timestamp": 1580833382,
"email": "www_groupon_com@my_domain.com",
"id": 4,
"nb_block": 0,
"nb_forward": 0,
"nb_reply": 0,
"enabled": true,
"note": "This is a note"
}
```
@ -861,6 +879,31 @@ If success, 200 with the list of aliases, for example:
}
```
#### GET /api/aliases/:alias_id
Get alias info
Input:
- `Authentication` header that contains the api key
- `alias_id` in url
Output:
Alias info
```json
{
"creation_date": "2020-02-04 16:23:02+00:00",
"creation_timestamp": 1580833382,
"email": "e3@.alo@sl.local",
"id": 4,
"nb_block": 0,
"nb_forward": 0,
"nb_reply": 0,
"enabled": true,
"note": "This is a note"
}
```
#### DELETE /api/aliases/:alias_id
Delete an alias

View file

@ -12,6 +12,7 @@ from app.dashboard.views.alias_log import get_alias_log
from app.dashboard.views.index import (
AliasInfo,
get_alias_infos_with_pagination,
get_alias_info,
)
from app.extensions import db
from app.log import LOG
@ -57,25 +58,26 @@ def get_aliases():
return (
jsonify(
aliases=[
{
"id": alias_info.id,
"email": alias_info.alias.email,
"creation_date": alias_info.alias.created_at.format(),
"creation_timestamp": alias_info.alias.created_at.timestamp,
"nb_forward": alias_info.nb_forward,
"nb_block": alias_info.nb_blocked,
"nb_reply": alias_info.nb_reply,
"enabled": alias_info.alias.enabled,
"note": alias_info.note,
}
for alias_info in alias_infos
]
aliases=[serialize_alias_info(alias_info) for alias_info in alias_infos]
),
200,
)
def serialize_alias_info(alias_info: AliasInfo) -> dict:
return {
"id": alias_info.id,
"email": alias_info.alias.email,
"creation_date": alias_info.alias.created_at.format(),
"creation_timestamp": alias_info.alias.created_at.timestamp,
"nb_forward": alias_info.nb_forward,
"nb_block": alias_info.nb_blocked,
"nb_reply": alias_info.nb_reply,
"enabled": alias_info.alias.enabled,
"note": alias_info.note,
}
@api_bp.route("/aliases/<int:alias_id>", methods=["DELETE"])
@cross_origin()
@verify_api_key
@ -209,6 +211,27 @@ def update_alias(alias_id):
return jsonify(note=new_note), 200
@api_bp.route("/aliases/<int:alias_id>", methods=["GET"])
@cross_origin()
@verify_api_key
def get_alias(alias_id):
"""
Get alias
Input:
alias_id: in url
Output:
Alias info, same as in get_aliases
"""
user = g.user
alias: Alias = Alias.get(alias_id)
if alias.user_id != user.id:
return jsonify(error="Forbidden"), 403
return jsonify(**serialize_alias_info(get_alias_info(alias))), 200
def serialize_contact(fe: Contact) -> dict:
res = {

View file

@ -3,8 +3,10 @@ from flask import jsonify, request
from flask_cors import cross_origin
from app.api.base import api_bp, verify_api_key
from app.api.views.alias import serialize_alias_info
from app.config import MAX_NB_EMAIL_FREE_PLAN, ALIAS_DOMAINS
from app.dashboard.views.custom_alias import verify_prefix_suffix
from app.dashboard.views.index import get_alias_info
from app.extensions import db
from app.log import LOG
from app.models import Alias, AliasUsedOn, User, CustomDomain
@ -75,4 +77,4 @@ def new_custom_alias():
AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id)
db.session.commit()
return jsonify(alias=full_alias), 201
return jsonify(alias=full_alias, **serialize_alias_info(get_alias_info(alias))), 201

View file

@ -3,7 +3,9 @@ from flask import jsonify, request
from flask_cors import cross_origin
from app.api.base import api_bp, verify_api_key
from app.api.views.alias import serialize_alias_info
from app.config import MAX_NB_EMAIL_FREE_PLAN
from app.dashboard.views.index import get_alias_info
from app.extensions import db
from app.log import LOG
from app.models import Alias, AliasUsedOn, AliasGeneratorEnum
@ -55,4 +57,7 @@ def new_random_alias():
AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id)
db.session.commit()
return jsonify(alias=alias.email), 201
return (
jsonify(alias=alias.email, **serialize_alias_info(get_alias_info(alias))),
201,
)

View file

@ -331,3 +331,37 @@ def test_delete_contact(flask_client):
assert r.status_code == 200
assert r.json == {"deleted": True}
def test_get_alias(flask_client):
user = User.create(
email="a@b.c", password="password", name="Test User", activated=True
)
db.session.commit()
# create api_key
api_key = ApiKey.create(user.id, "for test")
db.session.commit()
# create more aliases than PAGE_LIMIT
alias = Alias.create_new_random(user)
db.session.commit()
# get aliases on the 1st page, should return PAGE_LIMIT aliases
r = flask_client.get(
url_for("api.get_alias", alias_id=alias.id),
headers={"Authentication": api_key.code},
)
assert r.status_code == 200
# assert returned field
res = r.json
assert "id" in res
assert "email" in res
assert "creation_date" in res
assert "creation_timestamp" in res
assert "nb_forward" in res
assert "nb_block" in res
assert "nb_reply" in res
assert "enabled" in res
assert "note" in res

View file

@ -31,6 +31,18 @@ def test_success(flask_client):
assert r.status_code == 201
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
# assert returned field
res = r.json
assert "id" in res
assert "email" in res
assert "creation_date" in res
assert "creation_timestamp" in res
assert "nb_forward" in res
assert "nb_block" in res
assert "nb_reply" in res
assert "enabled" in res
assert "note" in res
new_ge = Alias.get_by(email=r.json["alias"])
assert new_ge.note == "test note"

View file

@ -25,6 +25,18 @@ def test_success(flask_client):
assert r.status_code == 201
assert r.json["alias"].endswith(EMAIL_DOMAIN)
# assert returned field
res = r.json
assert "id" in res
assert "email" in res
assert "creation_date" in res
assert "creation_timestamp" in res
assert "nb_forward" in res
assert "nb_block" in res
assert "nb_reply" in res
assert "enabled" in res
assert "note" in res
def test_custom_mode(flask_client):
user = User.create(