From e10981f85ed58a4a8ab5f0f51944e6e7d53f73f9 Mon Sep 17 00:00:00 2001 From: Son NK Date: Fri, 7 Feb 2020 21:30:46 +0700 Subject: [PATCH] able to create word-based or uuid-based in /api/alias/random/new endpoint --- README.md | 1 + app/api/views/new_random_alias.py | 14 ++++++++++--- tests/api/test_new_random_alias.py | 32 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 98a3429a..890f8ad5 100644 --- a/README.md +++ b/README.md @@ -697,6 +697,7 @@ Create a new random alias. 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. Output: If success, 201 with the new alias, for example diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py index 7afe1f6d..15ff5d3a 100644 --- a/app/api/views/new_random_alias.py +++ b/app/api/views/new_random_alias.py @@ -3,11 +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.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN +from app.config import MAX_NB_EMAIL_FREE_PLAN from app.extensions import db from app.log import LOG -from app.models import GenEmail, AliasUsedOn -from app.utils import convert_to_id +from app.models import GenEmail, AliasUsedOn, AliasGeneratorEnum @api_bp.route("/alias/random/new", methods=["POST"]) @@ -32,6 +31,15 @@ def new_random_alias(): ) 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 alias"), 400 + gen_email = GenEmail.create_new_random(user_id=user.id, scheme=scheme) db.session.commit() diff --git a/tests/api/test_new_random_alias.py b/tests/api/test_new_random_alias.py index 6eb692b5..8cbbaaa6 100644 --- a/tests/api/test_new_random_alias.py +++ b/tests/api/test_new_random_alias.py @@ -1,3 +1,5 @@ +import uuid + from flask import url_for from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN @@ -24,6 +26,28 @@ def test_success(flask_client): assert r.json["alias"].endswith(EMAIL_DOMAIN) +def test_custom_mode(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() + + r = flask_client.post( + url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"), + headers={"Authentication": api_key.code}, + ) + + assert r.status_code == 201 + # extract the uuid part + alias = r.json["alias"] + uuid_part = alias[: len(alias) - len(EMAIL_DOMAIN) - 1] + assert is_valid_uuid(uuid_part) + + def test_out_of_quota(flask_client): user = User.create( email="a@b.c", password="password", name="Test User", activated=True @@ -49,3 +73,11 @@ def test_out_of_quota(flask_client): r.json["error"] == "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases" ) + + +def is_valid_uuid(val): + try: + uuid.UUID(str(val)) + return True + except ValueError: + return False