2019-12-07 19:04:42 +01:00
|
|
|
from flask import url_for
|
|
|
|
|
2020-05-23 12:02:01 +02:00
|
|
|
from app.alias_utils import delete_alias
|
2020-01-22 10:22:59 +01:00
|
|
|
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
|
2020-05-02 16:22:17 +02:00
|
|
|
from app.dashboard.views.custom_alias import signer
|
2019-12-07 19:04:42 +01:00
|
|
|
from app.extensions import db
|
2020-06-02 09:33:56 +02:00
|
|
|
from app.models import User, ApiKey, Alias, CustomDomain, Mailbox
|
2020-01-22 10:22:59 +01:00
|
|
|
from app.utils import random_word
|
2019-12-07 19:04:42 +01: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 12:18:27 +01:00
|
|
|
# create new alias with note
|
2020-01-22 10:22:59 +01:00
|
|
|
word = random_word()
|
2020-03-11 12:18:27 +01: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 10:22:59 +01:00
|
|
|
|
2020-03-11 12:18:27 +01:00
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
|
|
|
|
|
2020-03-26 19:48:36 +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
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
new_ge = Alias.get_by(email=r.json["alias"])
|
2020-03-11 12:18:27 +01:00
|
|
|
assert new_ge.note == "test note"
|
|
|
|
|
2020-03-11 13:35:56 +01: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 12:18:27 +01:00
|
|
|
# create alias without note
|
|
|
|
word = random_word()
|
2019-12-07 19:04:42 +01:00
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_custom_alias", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
2020-03-13 10:34:41 +01:00
|
|
|
json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
|
2019-12-07 19:04:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
2020-01-22 10:22:59 +01:00
|
|
|
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
|
2019-12-07 19:04:42 +01:00
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
new_ge = Alias.get_by(email=r.json["alias"])
|
2020-03-11 12:18:27 +01:00
|
|
|
assert new_ge.note is None
|
|
|
|
|
2019-12-07 19:04:42 +01: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 07:20:32 +01:00
|
|
|
user.trial_end = None
|
2019-12-07 19:04:42 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-01-22 10:22:59 +01: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 11:51:40 +01:00
|
|
|
Alias.create_new(user, prefix="test")
|
2019-12-07 19:04:42 +01:00
|
|
|
|
2020-01-22 10:22:59 +01:00
|
|
|
word = random_word()
|
2019-12-07 19:04:42 +01:00
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_custom_alias", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
2020-01-22 10:22:59 +01:00
|
|
|
json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
|
2019-12-07 19:04:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json == {
|
2020-01-05 21:14:40 +01: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-07 19:04:42 +01:00
|
|
|
}
|
2020-05-02 16:22:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_success_v2(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 new alias with note
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
suffix = signer.sign(suffix).decode()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_custom_alias_v2", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
2020-08-27 10:20:48 +02:00
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
|
|
|
"signed_suffix": suffix,
|
|
|
|
"note": "test note",
|
|
|
|
},
|
2020-05-02 16:22:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
new_ge = Alias.get_by(email=r.json["alias"])
|
|
|
|
assert new_ge.note == "test note"
|
2020-05-23 12:02:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_cannot_create_alias_in_trash(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 a custom domain
|
|
|
|
CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create new alias with note
|
2020-12-06 17:47:37 +01:00
|
|
|
suffix = "@ab.cd"
|
2020-05-23 12:02:01 +02:00
|
|
|
suffix = signer.sign(suffix).decode()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_custom_alias_v2", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
2020-08-27 10:20:48 +02:00
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
|
|
|
"signed_suffix": suffix,
|
|
|
|
"note": "test note",
|
|
|
|
},
|
2020-05-23 12:02:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# assert alias creation is successful
|
|
|
|
assert r.status_code == 201
|
2020-12-06 17:47:37 +01:00
|
|
|
assert r.json["alias"] == "prefix@ab.cd"
|
2020-05-23 12:02:01 +02:00
|
|
|
|
|
|
|
# delete alias: it's going to be moved to ab.cd trash
|
|
|
|
alias = Alias.get_by(email="prefix@ab.cd")
|
|
|
|
assert alias.custom_domain_id
|
|
|
|
delete_alias(alias, user)
|
|
|
|
|
|
|
|
# try to create the same alias, will fail as the alias is in trash
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_custom_alias_v2", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
2020-08-27 10:20:48 +02:00
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
|
|
|
"signed_suffix": suffix,
|
|
|
|
"note": "test note",
|
|
|
|
},
|
2020-05-23 12:02:01 +02:00
|
|
|
)
|
|
|
|
assert r.status_code == 409
|
2020-06-02 09:33:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_success_v3(flask_client):
|
|
|
|
user = User.create(
|
2020-08-27 10:20:48 +02:00
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
2020-06-02 09:33:56 +02:00
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create another mailbox
|
|
|
|
mb = Mailbox.create(user_id=user.id, email="abcd@gmail.com", verified=True)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create new alias with note
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
suffix = signer.sign(suffix).decode()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.new_custom_alias_v3", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
|
|
|
"signed_suffix": suffix,
|
|
|
|
"note": "test note",
|
2020-06-02 20:06:32 +02:00
|
|
|
"mailbox_ids": [user.default_mailbox_id, mb.id],
|
2020-06-03 21:32:37 +02:00
|
|
|
"name": "your name",
|
2020-06-02 09:33:56 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
|
|
|
|
|
|
|
|
# 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
|
2020-06-03 21:22:29 +02:00
|
|
|
assert res["name"] == "your name"
|
2020-06-02 09:33:56 +02:00
|
|
|
|
|
|
|
new_alias: Alias = Alias.get_by(email=r.json["alias"])
|
|
|
|
assert new_alias.note == "test note"
|
|
|
|
assert len(new_alias.mailboxes) == 2
|