diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py index aef5fd08..fd1d5636 100644 --- a/app/api/views/new_random_alias.py +++ b/app/api/views/new_random_alias.py @@ -1,3 +1,4 @@ +import tldextract from flask import g from flask import jsonify, request @@ -11,6 +12,7 @@ from app.db import Session from app.extensions import limiter from app.log import LOG from app.models import Alias, AliasUsedOn, AliasGeneratorEnum +from app.utils import convert_to_id @api_bp.route("/alias/random/new", methods=["POST"]) @@ -41,20 +43,33 @@ def new_random_alias(): if data: note = data.get("note") - scheme = user.alias_generator - mode = request.args.get("mode") - if mode: - if mode == "word": - scheme = AliasGeneratorEnum.word.value - elif mode == "uuid": - scheme = AliasGeneratorEnum.uuid.value - else: - return jsonify(error=f"{mode} must be either word or uuid"), 400 - - alias = Alias.create_new_random(user=user, scheme=scheme, note=note) - Session.commit() - + # custom alias suggestion and suffix hostname = request.args.get("hostname") + if hostname and user.include_website_in_one_click_alias: + LOG.d("Use %s to create new alias", hostname) + # keep only the domain name of hostname, ignore TLD and subdomain + # for ex www.groupon.com -> groupon + ext = tldextract.extract(hostname) + prefix_suggestion = ext.domain + prefix_suggestion = convert_to_id(prefix_suggestion) + + alias = Alias.create_new(user, prefix_suggestion, note=note) + Session.commit() + + else: + scheme = user.alias_generator + mode = request.args.get("mode") + if mode: + if mode == "word": + scheme = AliasGeneratorEnum.word.value + elif mode == "uuid": + scheme = AliasGeneratorEnum.uuid.value + else: + return jsonify(error=f"{mode} must be either word or uuid"), 400 + + alias = Alias.create_new_random(user=user, scheme=scheme, note=note) + Session.commit() + if hostname: AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id) Session.commit() diff --git a/tests/api/test_new_random_alias.py b/tests/api/test_new_random_alias.py index c0894a77..c8c46a15 100644 --- a/tests/api/test_new_random_alias.py +++ b/tests/api/test_new_random_alias.py @@ -8,7 +8,7 @@ from app.models import Alias from tests.utils import login -def test_success(flask_client): +def test_with_hostname(flask_client): login(flask_client) r = flask_client.post( @@ -18,6 +18,9 @@ def test_success(flask_client): assert r.status_code == 201 assert r.json["alias"].endswith(EMAIL_DOMAIN) + # make sure alias starts with the suggested prefix + assert r.json["alias"].startswith("test") + # assert returned field res = r.json assert "id" in res @@ -31,6 +34,17 @@ def test_success(flask_client): assert "note" in res +def test_without_hostname(flask_client): + login(flask_client) + + r = flask_client.post( + url_for("api.new_random_alias"), + ) + + assert r.status_code == 201 + assert r.json["alias"].endswith(EMAIL_DOMAIN) + + def test_custom_mode(flask_client): login(flask_client)