From ef888288a68375a322dcd707bce6c1994e4cdceb Mon Sep 17 00:00:00 2001 From: Son NK Date: Sat, 7 Dec 2019 19:04:42 +0100 Subject: [PATCH] add tests for api create custom/random alias --- tests/api/test_alias_options.py | 7 ++-- tests/api/test_new_custom_alias.py | 52 ++++++++++++++++++++++++++++++ tests/api/test_new_random_alias.py | 50 ++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 tests/api/test_new_custom_alias.py create mode 100644 tests/api/test_new_random_alias.py diff --git a/tests/api/test_alias_options.py b/tests/api/test_alias_options.py index e3956250..73208a24 100644 --- a/tests/api/test_alias_options.py +++ b/tests/api/test_alias_options.py @@ -5,10 +5,9 @@ from app.models import User, ApiKey, AliasUsedOn, GenEmail def test_different_scenarios(flask_client): - """Start with a blank database.""" - - # create user, user is not activated - user = User.create(email="a@b.c", password="password", name="Test User") + user = User.create( + email="a@b.c", password="password", name="Test User", activated=True + ) db.session.commit() # create api_key diff --git a/tests/api/test_new_custom_alias.py b/tests/api/test_new_custom_alias.py new file mode 100644 index 00000000..7801e036 --- /dev/null +++ b/tests/api/test_new_custom_alias.py @@ -0,0 +1,52 @@ +from flask import url_for + +from app.config import EMAIL_DOMAIN +from app.extensions import db +from app.models import User, ApiKey, GenEmail + + +def test_success(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_custom_alias", hostname="www.test.com"), + headers={"Authentication": api_key.code}, + json={"alias_prefix": "prefix", "alias_suffix": f".abcdef@{EMAIL_DOMAIN}"}, + ) + + assert r.status_code == 201 + assert r.json["alias"] == f"prefix.abcdef@{EMAIL_DOMAIN}" + + +def test_out_of_quota(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 3 custom alias to run out of quota + GenEmail.create_new_gen_email(user.id, custom=True) + GenEmail.create_new_gen_email(user.id, custom=True) + GenEmail.create_new_gen_email(user.id, custom=True) + + 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".abcdef@{EMAIL_DOMAIN}"}, + ) + + assert r.status_code == 400 + assert r.json == { + "error": "You have created 3 custom aliases, please upgrade to create more" + } diff --git a/tests/api/test_new_random_alias.py b/tests/api/test_new_random_alias.py new file mode 100644 index 00000000..5582824f --- /dev/null +++ b/tests/api/test_new_random_alias.py @@ -0,0 +1,50 @@ +from flask import url_for + +from app.config import EMAIL_DOMAIN +from app.extensions import db +from app.models import User, ApiKey, GenEmail + + +def test_success(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"), + headers={"Authentication": api_key.code}, + ) + + assert r.status_code == 201 + assert r.json["alias"].endswith(EMAIL_DOMAIN) + + +def test_out_of_quota(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 3 random alias to run out of quota + GenEmail.create_new_gen_email(user.id) + GenEmail.create_new_gen_email(user.id) + GenEmail.create_new_gen_email(user.id) + + r = flask_client.post( + url_for("api.new_random_alias", hostname="www.test.com"), + headers={"Authentication": api_key.code}, + ) + + assert r.status_code == 400 + assert r.json == { + "error": "You have created 3 random aliases, please upgrade to create more" + }