2019-12-08 02:04:42 +08:00
|
|
|
from flask import url_for
|
|
|
|
|
2020-01-22 17:22:59 +08:00
|
|
|
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
|
2019-12-08 02:04:42 +08:00
|
|
|
from app.extensions import db
|
2020-03-17 18:51:40 +08:00
|
|
|
from app.models import User, ApiKey, Alias
|
2020-01-22 17:22:59 +08:00
|
|
|
from app.utils import random_word
|
2019-12-08 02:04:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2020-03-11 19:18:27 +08:00
|
|
|
# create new alias with note
|
2020-01-22 17:22:59 +08:00
|
|
|
word = random_word()
|
2020-03-11 19:18:27 +08:00
|
|
|
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".{word}@{EMAIL_DOMAIN}",
|
|
|
|
"note": "test note",
|
|
|
|
},
|
|
|
|
)
|
2020-01-22 17:22:59 +08:00
|
|
|
|
2020-03-11 19:18:27 +08:00
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
|
|
|
|
|
2020-03-17 18:51:40 +08:00
|
|
|
new_ge = Alias.get_by(email=r.json["alias"])
|
2020-03-11 19:18:27 +08:00
|
|
|
assert new_ge.note == "test note"
|
|
|
|
|
2020-03-11 20:35:56 +08:00
|
|
|
|
|
|
|
def test_create_custom_alias_without_note(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()
|
|
|
|
|
2020-03-11 19:18:27 +08:00
|
|
|
# create alias without note
|
|
|
|
word = random_word()
|
2019-12-08 02:04:42 +08:00
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_custom_alias", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
2020-03-13 17:34:41 +08:00
|
|
|
json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
|
2019-12-08 02:04:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
2020-01-22 17:22:59 +08:00
|
|
|
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
|
2019-12-08 02:04:42 +08:00
|
|
|
|
2020-03-17 18:51:40 +08:00
|
|
|
new_ge = Alias.get_by(email=r.json["alias"])
|
2020-03-11 19:18:27 +08:00
|
|
|
assert new_ge.note is None
|
|
|
|
|
2019-12-08 02:04:42 +08:00
|
|
|
|
|
|
|
def test_out_of_quota(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
2020-01-30 14:20:32 +08:00
|
|
|
user.trial_end = None
|
2019-12-08 02:04:42 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-01-22 17:22:59 +08:00
|
|
|
# create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota
|
|
|
|
for _ in range(MAX_NB_EMAIL_FREE_PLAN):
|
2020-03-17 18:51:40 +08:00
|
|
|
Alias.create_new(user, prefix="test")
|
2019-12-08 02:04:42 +08:00
|
|
|
|
2020-01-22 17:22:59 +08:00
|
|
|
word = random_word()
|
2019-12-08 02:04:42 +08:00
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_custom_alias", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
2020-01-22 17:22:59 +08:00
|
|
|
json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
|
2019-12-08 02:04:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json == {
|
2020-01-06 04:14:40 +08:00
|
|
|
"error": "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
|
2019-12-08 02:04:42 +08:00
|
|
|
}
|