2020-02-05 18:35:38 +08:00
|
|
|
from flask import url_for
|
|
|
|
|
2020-10-09 17:37:56 +08:00
|
|
|
from app.alias_utils import delete_alias
|
2020-02-05 18:35:38 +08:00
|
|
|
from app.config import EMAIL_DOMAIN
|
2020-05-02 18:34:11 +08:00
|
|
|
from app.dashboard.views.custom_alias import (
|
|
|
|
signer,
|
|
|
|
verify_prefix_suffix,
|
|
|
|
available_suffixes,
|
|
|
|
)
|
2020-02-05 18:35:38 +08:00
|
|
|
from app.extensions import db
|
2020-10-09 17:37:56 +08:00
|
|
|
from app.models import (
|
|
|
|
Mailbox,
|
|
|
|
CustomDomain,
|
|
|
|
Alias,
|
|
|
|
User,
|
|
|
|
DomainDeletedAlias,
|
|
|
|
DeletedAlias,
|
|
|
|
)
|
2020-02-05 18:35:38 +08:00
|
|
|
from app.utils import random_word
|
|
|
|
from tests.utils import login
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_alias_success(flask_client):
|
2020-02-11 00:30:04 +08:00
|
|
|
user = login(flask_client)
|
2020-02-05 18:35:38 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
word = random_word()
|
2020-05-02 18:15:03 +08:00
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
suffix = signer.sign(suffix).decode()
|
2020-02-05 18:35:38 +08:00
|
|
|
|
2020-05-03 22:50:39 +08:00
|
|
|
# create with a single mailbox
|
2020-02-05 18:35:38 +08:00
|
|
|
r = flask_client.post(
|
|
|
|
url_for("dashboard.custom_alias"),
|
2020-05-03 22:50:39 +08:00
|
|
|
data={
|
|
|
|
"prefix": "prefix",
|
|
|
|
"suffix": suffix,
|
|
|
|
"mailboxes": [user.default_mailbox_id],
|
|
|
|
},
|
2020-02-05 18:35:38 +08:00
|
|
|
follow_redirects=True,
|
|
|
|
)
|
2020-05-03 22:50:39 +08:00
|
|
|
assert r.status_code == 200
|
|
|
|
assert f"Alias prefix.{word}@{EMAIL_DOMAIN} has been created" in str(r.data)
|
|
|
|
|
|
|
|
alias = Alias.query.order_by(Alias.created_at.desc()).first()
|
|
|
|
assert not alias._mailboxes
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_alias_multiple_mailboxes(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
suffix = signer.sign(suffix).decode()
|
2020-02-05 18:35:38 +08:00
|
|
|
|
2020-05-03 22:50:39 +08:00
|
|
|
# create with a multiple mailboxes
|
|
|
|
mb1 = Mailbox.create(user_id=user.id, email="m1@example.com", verified=True)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("dashboard.custom_alias"),
|
|
|
|
data={
|
|
|
|
"prefix": "prefix",
|
|
|
|
"suffix": suffix,
|
|
|
|
"mailboxes": [user.default_mailbox_id, mb1.id],
|
|
|
|
},
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
2020-02-05 18:35:38 +08:00
|
|
|
assert r.status_code == 200
|
|
|
|
assert f"Alias prefix.{word}@{EMAIL_DOMAIN} has been created" in str(r.data)
|
2020-02-21 22:59:13 +08:00
|
|
|
|
2020-05-03 22:50:39 +08:00
|
|
|
alias = Alias.query.order_by(Alias.created_at.desc()).first()
|
|
|
|
assert alias._mailboxes
|
|
|
|
|
2020-02-21 22:59:13 +08:00
|
|
|
|
|
|
|
def test_not_show_unverified_mailbox(flask_client):
|
|
|
|
"""make sure user unverified mailbox is not shown to user"""
|
|
|
|
user = login(flask_client)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
Mailbox.create(user_id=user.id, email="m1@example.com", verified=True)
|
|
|
|
Mailbox.create(user_id=user.id, email="m2@example.com", verified=False)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-05 18:00:58 +08:00
|
|
|
r = flask_client.get(url_for("dashboard.custom_alias"))
|
2020-02-21 22:59:13 +08:00
|
|
|
|
|
|
|
assert "m1@example.com" in str(r.data)
|
|
|
|
assert "m2@example.com" not in str(r.data)
|
2020-05-02 18:27:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_verify_prefix_suffix(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
CustomDomain.create(user_id=user.id, domain="test.com", verified=True)
|
|
|
|
|
|
|
|
assert verify_prefix_suffix(user, "prefix", "@test.com")
|
|
|
|
assert not verify_prefix_suffix(user, "prefix", "@abcd.com")
|
|
|
|
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
assert verify_prefix_suffix(user, "prefix", suffix)
|
2020-05-02 18:34:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_available_suffixes(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
CustomDomain.create(user_id=user.id, domain="test.com", verified=True)
|
|
|
|
|
|
|
|
assert len(available_suffixes(user)) > 0
|
|
|
|
|
|
|
|
# first suffix is custom domain
|
|
|
|
first_suffix = available_suffixes(user)[0]
|
|
|
|
assert first_suffix[0]
|
|
|
|
assert first_suffix[1] == "@test.com"
|
|
|
|
assert first_suffix[2].startswith("@test.com")
|
2020-10-09 17:37:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_already_existed_alias(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
another_user = User.create(
|
|
|
|
email="a2@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
|
|
|
|
|
|
|
# alias already exist
|
|
|
|
Alias.create(
|
|
|
|
user_id=another_user.id,
|
|
|
|
email=f"prefix{suffix}",
|
|
|
|
mailbox_id=another_user.default_mailbox_id,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# create the same alias, should return error
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("dashboard.custom_alias"),
|
|
|
|
data={
|
|
|
|
"prefix": "prefix",
|
|
|
|
"suffix": signed_suffix,
|
|
|
|
"mailboxes": [user.default_mailbox_id],
|
|
|
|
},
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
2020-10-09 17:48:52 +08:00
|
|
|
assert f"prefix{suffix} cannot be used" in r.get_data(True)
|
2020-10-09 17:37:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_alias_in_global_trash(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
another_user = User.create(
|
|
|
|
email="a2@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
word = random_word()
|
|
|
|
suffix = f".{word}@{EMAIL_DOMAIN}"
|
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
|
|
|
|
|
|
|
# delete an alias: alias should go the DeletedAlias
|
|
|
|
alias = Alias.create(
|
|
|
|
user_id=another_user.id,
|
|
|
|
email=f"prefix{suffix}",
|
|
|
|
mailbox_id=another_user.default_mailbox_id,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert DeletedAlias.query.count() == 0
|
|
|
|
delete_alias(alias, another_user)
|
|
|
|
assert DeletedAlias.query.count() == 1
|
|
|
|
|
|
|
|
# create the same alias, should return error
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("dashboard.custom_alias"),
|
|
|
|
data={
|
|
|
|
"prefix": "prefix",
|
|
|
|
"suffix": signed_suffix,
|
|
|
|
"mailboxes": [user.default_mailbox_id],
|
|
|
|
},
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
2020-10-09 17:48:52 +08:00
|
|
|
assert f"prefix{suffix} cannot be used" in r.get_data(True)
|
2020-10-09 17:37:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_alias_in_custom_domain_trash(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
custom_domain = CustomDomain.create(
|
|
|
|
user_id=user.id, domain="ab.cd", verified=True, commit=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# delete a custom-domain alias: alias should go the DomainDeletedAlias
|
|
|
|
alias = Alias.create(
|
|
|
|
user_id=user.id,
|
2020-12-07 00:46:10 +08:00
|
|
|
email="prefix@ab.cd",
|
2020-10-09 17:37:56 +08:00
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
mailbox_id=user.default_mailbox_id,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert DomainDeletedAlias.query.count() == 0
|
|
|
|
delete_alias(alias, user)
|
|
|
|
assert DomainDeletedAlias.query.count() == 1
|
|
|
|
|
|
|
|
# create the same alias, should return error
|
2020-12-07 00:46:10 +08:00
|
|
|
suffix = "@ab.cd"
|
2020-10-09 17:37:56 +08:00
|
|
|
signed_suffix = signer.sign(suffix).decode()
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("dashboard.custom_alias"),
|
|
|
|
data={
|
|
|
|
"prefix": "prefix",
|
|
|
|
"suffix": signed_suffix,
|
|
|
|
"mailboxes": [user.default_mailbox_id],
|
|
|
|
},
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
2020-10-09 17:48:52 +08:00
|
|
|
assert "You have deleted this alias before. You can restore it on" in r.get_data(
|
|
|
|
True
|
|
|
|
)
|