2020-02-07 14:30:46 +00:00
|
|
|
import uuid
|
|
|
|
|
2020-01-05 20:15:16 +00:00
|
|
|
from flask import url_for
|
|
|
|
|
|
|
|
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-02-07 14:30:46 +00:00
|
|
|
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()
|
|
|
|
|
2020-03-11 11:24:30 +00:00
|
|
|
# without note
|
2020-02-07 14:30:46 +00:00
|
|
|
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)
|
|
|
|
|
2020-03-11 11:24:30 +00:00
|
|
|
# with note
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"note": "test note",},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
alias = r.json["alias"]
|
|
|
|
ge = GenEmail.get_by(email=alias)
|
|
|
|
assert ge.note == "test note"
|
|
|
|
|
2020-02-07 14:30:46 +00:00
|
|
|
|
2020-01-05 20:15:16 +00: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 06:20:32 +00:00
|
|
|
user.trial_end = None
|
2020-01-05 20:15:16 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-01-30 06:20:32 +00:00
|
|
|
# create MAX_NB_EMAIL_FREE_PLAN random alias to run out of quota
|
2020-01-05 20:15:16 +00:00
|
|
|
for _ in range(MAX_NB_EMAIL_FREE_PLAN):
|
2020-03-05 19:32:08 +00:00
|
|
|
GenEmail.create_new(user, prefix="test1")
|
2020-01-05 20:15:16 +00:00
|
|
|
|
|
|
|
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 reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
|
|
|
|
)
|
2020-02-07 14:30:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_valid_uuid(val):
|
|
|
|
try:
|
|
|
|
uuid.UUID(str(val))
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|