2021-03-24 16:39:49 +01:00
|
|
|
from flask import g
|
2019-12-07 19:04:42 +01:00
|
|
|
|
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
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2021-03-24 16:39:49 +01:00
|
|
|
from app.models import Alias, CustomDomain, Mailbox, AliasUsedOn
|
2020-01-22 10:22:59 +01:00
|
|
|
from app.utils import random_word
|
2022-04-18 11:55:14 +02:00
|
|
|
from tests.utils import login, random_domain
|
2019-12-07 19:04:42 +01:00
|
|
|
|
|
|
|
|
2021-03-25 19:18:50 +01:00
|
|
|
def test_v2(flask_client):
|
|
|
|
login(flask_client)
|
|
|
|
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
"/api/v2/alias/custom/new",
|
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
|
|
|
"signed_suffix": signed_suffix,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
new_alias: Alias = Alias.get_by(email=r.json["alias"])
|
|
|
|
assert len(new_alias.mailboxes) == 1
|
|
|
|
|
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
def test_minimal_payload(flask_client):
|
|
|
|
user = login(flask_client)
|
2019-12-07 19:04:42 +01:00
|
|
|
|
2020-01-22 10:22:59 +01:00
|
|
|
word = random_word()
|
2021-03-24 16:39:49 +01:00
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
|
|
|
|
2020-03-11 12:18:27 +01:00
|
|
|
r = flask_client.post(
|
2021-03-24 16:39:49 +01:00
|
|
|
"/api/v3/alias/custom/new",
|
2020-03-11 12:18:27 +01:00
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
2021-03-24 16:39:49 +01:00
|
|
|
"signed_suffix": signed_suffix,
|
|
|
|
"mailbox_ids": [user.default_mailbox_id],
|
2020-03-11 12:18:27 +01:00
|
|
|
},
|
|
|
|
)
|
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
|
|
|
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
|
2020-03-11 12:18:27 +01:00
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
new_alias: Alias = Alias.get_by(email=r.json["alias"])
|
|
|
|
assert len(new_alias.mailboxes) == 1
|
2020-03-11 13:35:56 +01:00
|
|
|
|
2020-03-11 12:18:27 +01:00
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
def test_full_payload(flask_client):
|
|
|
|
"""Create alias with:
|
|
|
|
- additional mailbox
|
|
|
|
- note
|
|
|
|
- name
|
|
|
|
- hostname (in URL)
|
|
|
|
"""
|
2019-12-07 19:04:42 +01:00
|
|
|
|
2021-03-24 16:11:22 +01:00
|
|
|
user = login(flask_client)
|
2020-05-02 16:22:17 +02:00
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
# create another mailbox
|
|
|
|
mb = Mailbox.create(user_id=user.id, email="abcd@gmail.com", verified=True)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-05-02 16:22:17 +02:00
|
|
|
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
2021-03-24 16:39:49 +01:00
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
assert AliasUsedOn.count() == 0
|
2020-05-02 16:22:17 +02:00
|
|
|
|
|
|
|
r = flask_client.post(
|
2021-03-24 16:39:49 +01:00
|
|
|
"/api/v3/alias/custom/new?hostname=example.com",
|
2020-08-27 10:20:48 +02:00
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
2021-03-24 16:39:49 +01:00
|
|
|
"signed_suffix": signed_suffix,
|
2020-08-27 10:20:48 +02:00
|
|
|
"note": "test note",
|
2021-03-24 16:39:49 +01:00
|
|
|
"mailbox_ids": [user.default_mailbox_id, mb.id],
|
|
|
|
"name": "your name",
|
2020-08-27 10:20:48 +02:00
|
|
|
},
|
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
|
2021-03-24 16:39:49 +01:00
|
|
|
assert res["note"] == "test note"
|
|
|
|
assert res["name"] == "your name"
|
2020-05-02 16:22:17 +02:00
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
new_alias: Alias = Alias.get_by(email=r.json["alias"])
|
|
|
|
assert new_alias.note == "test note"
|
|
|
|
assert len(new_alias.mailboxes) == 2
|
2020-05-23 12:02:01 +02:00
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
alias_used_on = AliasUsedOn.first()
|
|
|
|
assert alias_used_on.alias_id == new_alias.id
|
|
|
|
assert alias_used_on.hostname == "example.com"
|
2020-05-23 12:02:01 +02:00
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
|
|
|
|
def test_custom_domain_alias(flask_client):
|
2021-03-24 16:11:22 +01:00
|
|
|
user = login(flask_client)
|
2020-05-23 12:02:01 +02:00
|
|
|
|
|
|
|
# create a custom domain
|
2022-04-18 11:55:14 +02:00
|
|
|
domain = random_domain()
|
2021-12-29 10:23:29 +01:00
|
|
|
CustomDomain.create(
|
2022-04-18 11:55:14 +02:00
|
|
|
user_id=user.id, domain=domain, ownership_verified=True, commit=True
|
2021-12-29 10:23:29 +01:00
|
|
|
)
|
2020-05-23 12:02:01 +02:00
|
|
|
|
2022-04-18 11:55:14 +02:00
|
|
|
signed_suffix = signer.sign(f"@{domain}").decode()
|
2020-05-23 12:02:01 +02:00
|
|
|
|
|
|
|
r = flask_client.post(
|
2021-03-24 16:39:49 +01:00
|
|
|
"/api/v3/alias/custom/new",
|
2020-08-27 10:20:48 +02:00
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
2021-03-24 16:39:49 +01:00
|
|
|
"signed_suffix": signed_suffix,
|
|
|
|
"mailbox_ids": [user.default_mailbox_id],
|
2020-08-27 10:20:48 +02:00
|
|
|
},
|
2020-05-23 12:02:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
2022-04-18 11:55:14 +02:00
|
|
|
assert r.json["alias"] == f"prefix@{domain}"
|
2020-05-23 12:02:01 +02:00
|
|
|
|
|
|
|
|
2021-10-23 15:52:17 +02:00
|
|
|
def test_wrongly_formatted_payload(flask_client):
|
|
|
|
login(flask_client)
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
"/api/v3/alias/custom/new",
|
|
|
|
json="string isn't a dict",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json == {"error": "request body does not follow the required format"}
|
|
|
|
|
|
|
|
|
2021-10-23 15:55:39 +02:00
|
|
|
def test_mailbox_ids_is_not_an_array(flask_client):
|
|
|
|
login(flask_client)
|
|
|
|
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
"/api/v3/alias/custom/new",
|
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
|
|
|
"signed_suffix": signed_suffix,
|
|
|
|
"mailbox_ids": "not an array",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json == {"error": "mailbox_ids must be an array of id"}
|
|
|
|
|
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
def test_out_of_quota(flask_client):
|
2021-03-24 16:11:22 +01:00
|
|
|
user = login(flask_client)
|
2021-03-24 16:39:49 +01:00
|
|
|
user.trial_end = None
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-06-02 09:33:56 +02:00
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
# create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota
|
|
|
|
for _ in range(MAX_NB_EMAIL_FREE_PLAN):
|
|
|
|
Alias.create_new(user, prefix="test")
|
|
|
|
|
2020-06-02 09:33:56 +02:00
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
2021-03-24 16:39:49 +01:00
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
2020-06-02 09:33:56 +02:00
|
|
|
|
|
|
|
r = flask_client.post(
|
2021-03-24 16:39:49 +01:00
|
|
|
"/api/v3/alias/custom/new",
|
2020-06-02 09:33:56 +02:00
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
2021-03-24 16:39:49 +01:00
|
|
|
"signed_suffix": signed_suffix,
|
2020-06-02 09:33:56 +02:00
|
|
|
"note": "test note",
|
2021-03-24 16:39:49 +01:00
|
|
|
"mailbox_ids": [user.default_mailbox_id],
|
2020-06-03 21:32:37 +02:00
|
|
|
"name": "your name",
|
2020-06-02 09:33:56 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
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"
|
|
|
|
}
|
2021-03-24 16:26:42 +01:00
|
|
|
|
|
|
|
|
2021-03-24 16:39:49 +01:00
|
|
|
def test_cannot_create_alias_in_trash(flask_client):
|
2021-03-24 16:26:42 +01:00
|
|
|
user = login(flask_client)
|
|
|
|
|
|
|
|
# create a custom domain
|
2022-04-18 11:55:14 +02:00
|
|
|
domain = random_domain()
|
2021-12-29 10:23:29 +01:00
|
|
|
CustomDomain.create(
|
2022-04-18 11:55:14 +02:00
|
|
|
user_id=user.id, domain=domain, ownership_verified=True, commit=True
|
2021-12-29 10:23:29 +01:00
|
|
|
)
|
2021-03-24 16:26:42 +01:00
|
|
|
|
2022-04-18 11:55:14 +02:00
|
|
|
signed_suffix = signer.sign(f"@{domain}").decode()
|
2021-03-24 16:26:42 +01:00
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
"/api/v3/alias/custom/new",
|
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
|
|
|
"signed_suffix": signed_suffix,
|
|
|
|
"mailbox_ids": [user.default_mailbox_id],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
2022-04-18 11:55:14 +02:00
|
|
|
assert r.json["alias"] == f"prefix@{domain}"
|
2021-03-24 16:26:42 +01:00
|
|
|
|
2022-04-18 11:55:14 +02:00
|
|
|
# delete alias: it's going to be moved to domain trash
|
|
|
|
alias = Alias.get_by(email=f"prefix@{domain}")
|
2021-03-24 16:39:49 +01:00
|
|
|
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(
|
|
|
|
"/api/v3/alias/custom/new",
|
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix",
|
|
|
|
"signed_suffix": signed_suffix,
|
|
|
|
"mailbox_ids": [user.default_mailbox_id],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert r.status_code == 409
|
|
|
|
|
2021-03-24 16:26:42 +01:00
|
|
|
|
|
|
|
def test_too_many_requests(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
|
|
|
|
# create a custom domain
|
2022-04-18 11:55:14 +02:00
|
|
|
domain = random_domain()
|
|
|
|
CustomDomain.create(user_id=user.id, domain=domain, verified=True, commit=True)
|
2021-03-24 16:26:42 +01:00
|
|
|
|
|
|
|
# can't create more than 5 aliases in 1 minute
|
|
|
|
for i in range(7):
|
2022-04-18 11:55:14 +02:00
|
|
|
signed_suffix = signer.sign(f"@{domain}").decode()
|
2021-03-24 16:26:42 +01:00
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
"/api/v3/alias/custom/new",
|
|
|
|
json={
|
|
|
|
"alias_prefix": f"prefix{i}",
|
|
|
|
"signed_suffix": signed_suffix,
|
|
|
|
"mailbox_ids": [user.default_mailbox_id],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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"}
|
2021-12-02 16:17:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_alias_2_consecutive_dots(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
"/api/v3/alias/custom/new",
|
|
|
|
json={
|
|
|
|
"alias_prefix": "prefix.", # with the trailing dot, the alias will have 2 consecutive dots
|
|
|
|
"signed_suffix": signed_suffix,
|
|
|
|
"mailbox_ids": [user.default_mailbox_id],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json == {
|
|
|
|
"error": "2 consecutive dot signs aren't allowed in an email address"
|
|
|
|
}
|