2020-02-07 15:30:46 +01:00
|
|
|
import uuid
|
|
|
|
|
2021-03-24 16:26:42 +01:00
|
|
|
from flask import url_for, g
|
2020-01-05 21:15:16 +01:00
|
|
|
|
|
|
|
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2021-11-12 11:04:00 +01:00
|
|
|
from app.models import Alias, CustomDomain, AliasUsedOn
|
2021-03-24 15:54:03 +01:00
|
|
|
from tests.utils import login
|
2020-01-05 21:15:16 +01:00
|
|
|
|
|
|
|
|
2021-11-12 09:45:31 +01:00
|
|
|
def test_with_hostname(flask_client):
|
2021-03-24 15:54:03 +01:00
|
|
|
login(flask_client)
|
2020-01-05 21:15:16 +01:00
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_random_alias", hostname="www.test.com"),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
2021-11-12 11:04:00 +01:00
|
|
|
assert r.json["alias"].endswith("d1.test")
|
2020-01-05 21:15:16 +01:00
|
|
|
|
2021-11-12 09:45:31 +01:00
|
|
|
# make sure alias starts with the suggested prefix
|
|
|
|
assert r.json["alias"].startswith("test")
|
|
|
|
|
2020-03-26 19:50:22 +01:00
|
|
|
# assert returned field
|
|
|
|
res = r.json
|
|
|
|
assert "id" in res
|
|
|
|
assert "email" in res
|
|
|
|
assert "creation_date" in res
|
|
|
|
assert "creation_timestamp" in res
|
|
|
|
assert "nb_forward" in res
|
|
|
|
assert "nb_block" in res
|
|
|
|
assert "nb_reply" in res
|
|
|
|
assert "enabled" in res
|
|
|
|
assert "note" in res
|
|
|
|
|
2021-11-12 11:04:00 +01:00
|
|
|
alias_used_on: AliasUsedOn = AliasUsedOn.first()
|
|
|
|
assert alias_used_on.hostname == "www.test.com"
|
|
|
|
assert alias_used_on.alias_id == res["id"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_with_custom_domain(flask_client):
|
|
|
|
user = login(flask_client)
|
2021-12-29 10:23:29 +01:00
|
|
|
CustomDomain.create(
|
|
|
|
user_id=user.id, domain="ab.cd", ownership_verified=True, commit=True
|
|
|
|
)
|
2021-11-12 11:04:00 +01:00
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_random_alias", hostname="www.test.com"),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json["alias"] == "test@ab.cd"
|
|
|
|
assert Alias.count() == 2
|
|
|
|
|
|
|
|
# call the endpoint again, should return the same alias
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_random_alias", hostname="www.test.com"),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json["alias"] == "test@ab.cd"
|
|
|
|
# no new alias is created
|
|
|
|
assert Alias.count() == 2
|
|
|
|
|
2020-01-05 21:15:16 +01:00
|
|
|
|
2021-11-12 09:45:31 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-02-07 15:30:46 +01:00
|
|
|
def test_custom_mode(flask_client):
|
2021-03-24 15:54:03 +01:00
|
|
|
login(flask_client)
|
2020-02-07 15:30:46 +01:00
|
|
|
|
2020-03-11 12:24:30 +01:00
|
|
|
# without note
|
2020-02-07 15:30:46 +01:00
|
|
|
r = flask_client.post(
|
2021-11-12 10:00:01 +01:00
|
|
|
url_for("api.new_random_alias", mode="uuid"),
|
2020-02-07 15:30:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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 12:24:30 +01:00
|
|
|
# with note
|
|
|
|
r = flask_client.post(
|
2021-11-12 10:00:01 +01:00
|
|
|
url_for("api.new_random_alias", mode="uuid"),
|
2020-03-13 10:34:41 +01:00
|
|
|
json={"note": "test note"},
|
2020-03-11 12:24:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
alias = r.json["alias"]
|
2020-03-17 11:51:40 +01:00
|
|
|
ge = Alias.get_by(email=alias)
|
2020-03-11 12:24:30 +01:00
|
|
|
assert ge.note == "test note"
|
|
|
|
|
2020-02-07 15:30:46 +01:00
|
|
|
|
2020-01-05 21:15:16 +01:00
|
|
|
def test_out_of_quota(flask_client):
|
2021-03-24 15:54:03 +01:00
|
|
|
user = login(flask_client)
|
2021-03-24 16:51:23 +01:00
|
|
|
user.trial_end = None
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-01-05 21:15:16 +01:00
|
|
|
|
2020-01-30 07:20:32 +01:00
|
|
|
# create MAX_NB_EMAIL_FREE_PLAN random alias to run out of quota
|
2020-01-05 21:15:16 +01:00
|
|
|
for _ in range(MAX_NB_EMAIL_FREE_PLAN):
|
2020-03-17 11:51:40 +01:00
|
|
|
Alias.create_new(user, prefix="test1")
|
2020-01-05 21:15:16 +01:00
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_random_alias", hostname="www.test.com"),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert (
|
2021-03-24 16:26:42 +01:00
|
|
|
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-01-05 21:15:16 +01:00
|
|
|
)
|
2020-02-07 15:30:46 +01:00
|
|
|
|
|
|
|
|
2021-03-24 16:26:42 +01:00
|
|
|
def test_too_many_requests(flask_client):
|
|
|
|
login(flask_client)
|
|
|
|
|
|
|
|
# can't create more than 5 aliases in 1 minute
|
|
|
|
for _ in range(7):
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"),
|
|
|
|
)
|
|
|
|
# to make flask-limiter work with unit test
|
|
|
|
# https://github.com/alisaifee/flask-limiter/issues/147#issuecomment-642683820
|
|
|
|
g._rate_limiting_complete = False
|
|
|
|
else:
|
|
|
|
# last request
|
|
|
|
assert r.status_code == 429
|
|
|
|
assert r.json == {"error": "Rate limit exceeded"}
|
|
|
|
|
|
|
|
|
2020-02-07 15:30:46 +01:00
|
|
|
def is_valid_uuid(val):
|
|
|
|
try:
|
|
|
|
uuid.UUID(str(val))
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|