Skip to content

Commit

Permalink
Restore /alias/custom/new as currently used by safari
Browse files Browse the repository at this point in the history
  • Loading branch information
Son NK committed Mar 31, 2021
1 parent e31e190 commit d5de99a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/api/views/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
is_valid_email,
generate_reply_email,
)
from app.utils import sanitize_email
from app.extensions import db
from app.log import LOG
from app.models import Alias, Contact, Mailbox, AliasMailbox
from app.utils import sanitize_email


@api_bp.route("/aliases", methods=["GET", "POST"])
Expand Down
2 changes: 1 addition & 1 deletion app/api/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
send_email,
render,
)
from app.utils import sanitize_email
from app.extensions import db, limiter
from app.log import LOG
from app.models import User, ApiKey, SocialAuth, AccountActivation
from app.utils import sanitize_email


@api_bp.route("/auth/login", methods=["POST"])
Expand Down
2 changes: 1 addition & 1 deletion app/api/views/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
email_can_be_used_as_mailbox,
is_valid_email,
)
from app.utils import sanitize_email
from app.extensions import db
from app.models import Mailbox
from app.utils import sanitize_email


def mailbox_to_dict(mailbox: Mailbox):
Expand Down
74 changes: 74 additions & 0 deletions app/api/views/new_custom_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from app.api.serializer import (
serialize_alias_info_v2,
get_alias_info_v2,
serialize_alias_info,
get_alias_info,
)
from app.config import MAX_NB_EMAIL_FREE_PLAN, ALIAS_LIMIT
from app.dashboard.views.custom_alias import verify_prefix_suffix, signer
Expand All @@ -25,6 +27,78 @@
from app.utils import convert_to_id


@api_bp.route("/alias/custom/new", methods=["POST"])
@limiter.limit(ALIAS_LIMIT)
@require_api_auth
def new_custom_alias():
"""
Currently used by Safari extension.
Create a new custom alias
Input:
alias_prefix, for ex "www_groupon_com"
alias_suffix, either [email protected] or @my-domain.com
optional "hostname" in args
optional "note"
Output:
201 if success
409 if the alias already exists
"""
LOG.warning("/alias/custom/new is obsolete")
user: User = g.user
if not user.can_create_new_alias():
LOG.d("user %s cannot create any custom alias", user)
return (
jsonify(
error="You have reached the limitation of a free account with the maximum of "
f"{MAX_NB_EMAIL_FREE_PLAN} aliases, please upgrade your plan to create more aliases"
),
400,
)

hostname = request.args.get("hostname")

data = request.get_json()
if not data:
return jsonify(error="request body cannot be empty"), 400

alias_prefix = data.get("alias_prefix", "").strip().lower().replace(" ", "")
alias_suffix = data.get("alias_suffix", "").strip().lower().replace(" ", "")
note = data.get("note")
alias_prefix = convert_to_id(alias_prefix)

if not verify_prefix_suffix(user, alias_prefix, alias_suffix):
return jsonify(error="wrong alias prefix or suffix"), 400

full_alias = alias_prefix + alias_suffix
if (
Alias.get_by(email=full_alias)
or DeletedAlias.get_by(email=full_alias)
or DomainDeletedAlias.get_by(email=full_alias)
):
LOG.d("full alias already used %s", full_alias)
return jsonify(error=f"alias {full_alias} already exists"), 409

alias = Alias.create(
user_id=user.id, email=full_alias, mailbox_id=user.default_mailbox_id, note=note
)

if alias_suffix.startswith("@"):
alias_domain = alias_suffix[1:]
domain = CustomDomain.get_by(domain=alias_domain)
if domain:
LOG.d("set alias %s to domain %s", full_alias, domain)
alias.custom_domain_id = domain.id

db.session.commit()

if hostname:
AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id)
db.session.commit()

return jsonify(alias=full_alias, **serialize_alias_info(get_alias_info(alias))), 201


@api_bp.route("/v2/alias/custom/new", methods=["POST"])
@limiter.limit(ALIAS_LIMIT)
@require_api_auth
Expand Down
31 changes: 31 additions & 0 deletions tests/api/test_new_custom_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@
from tests.utils import login


def test_v1(flask_client):
login(flask_client)

word = random_word()
suffix = f".{word}@{EMAIL_DOMAIN}"

r = flask_client.post(
"/api/alias/custom/new",
json={
"alias_prefix": "prefix",
"alias_suffix": suffix,
},
)

assert r.status_code == 201
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"

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

new_alias: Alias = Alias.get_by(email=r.json["alias"])
assert len(new_alias.mailboxes) == 1


def test_v2(flask_client):
login(flask_client)

Expand Down

0 comments on commit d5de99a

Please sign in to comment.