2020-11-01 12:24:19 +01:00
|
|
|
from app.api.serializer import get_alias_infos_with_pagination_v3
|
|
|
|
from app.config import PAGE_LIMIT
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2021-03-01 18:45:15 +01:00
|
|
|
from app.models import User, Alias, Mailbox, Contact
|
2021-03-01 18:46:15 +01:00
|
|
|
from tests.utils import create_user
|
2020-11-01 12:24:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_alias_infos_with_pagination_v3(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# user has 1 alias that's automatically created when the account is created
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user)
|
|
|
|
assert len(alias_infos) == 1
|
|
|
|
alias_info = alias_infos[0]
|
|
|
|
|
2021-03-01 18:45:15 +01:00
|
|
|
alias = Alias.first()
|
2020-11-01 12:24:19 +01:00
|
|
|
assert alias_info.alias == alias
|
|
|
|
assert alias_info.mailbox == user.default_mailbox
|
|
|
|
assert alias_info.mailboxes == [user.default_mailbox]
|
|
|
|
assert alias_info.nb_forward == 0
|
|
|
|
assert alias_info.nb_blocked == 0
|
|
|
|
assert alias_info.nb_reply == 0
|
|
|
|
assert alias_info.latest_email_log is None
|
|
|
|
assert alias_info.latest_contact is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_alias_infos_with_pagination_v3_query_alias_email(flask_client):
|
|
|
|
"""test the query on the alias email"""
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
2021-03-01 18:45:15 +01:00
|
|
|
alias = Alias.first()
|
2020-11-01 12:24:19 +01:00
|
|
|
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user, query=alias.email)
|
|
|
|
assert len(alias_infos) == 1
|
|
|
|
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user, query="no match")
|
|
|
|
assert len(alias_infos) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_alias_infos_with_pagination_v3_query_alias_mailbox(flask_client):
|
|
|
|
"""test the query on the alias mailbox email"""
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
2021-03-01 18:45:15 +01:00
|
|
|
alias = Alias.first()
|
2021-08-20 12:21:27 +02:00
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user, mailbox_id=alias.mailbox_id)
|
2020-11-01 12:24:19 +01:00
|
|
|
assert len(alias_infos) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_alias_infos_with_pagination_v3_query_alias_mailboxes(flask_client):
|
|
|
|
"""test the query on the alias additional mailboxes"""
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
2021-03-01 18:45:15 +01:00
|
|
|
alias = Alias.first()
|
2020-11-01 12:24:19 +01:00
|
|
|
mb = Mailbox.create(user_id=user.id, email="mb@gmail.com")
|
|
|
|
alias._mailboxes.append(mb)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-11-01 12:24:19 +01:00
|
|
|
|
2021-08-20 12:21:27 +02:00
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user, mailbox_id=mb.id)
|
2020-11-01 12:24:19 +01:00
|
|
|
assert len(alias_infos) == 1
|
|
|
|
|
2020-11-03 15:10:57 +01:00
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user, query=alias.email)
|
|
|
|
assert len(alias_infos) == 1
|
|
|
|
|
2020-11-01 12:24:19 +01:00
|
|
|
|
|
|
|
def test_get_alias_infos_with_pagination_v3_query_alias_note(flask_client):
|
|
|
|
"""test the query on the alias note"""
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
2021-03-01 18:45:15 +01:00
|
|
|
alias = Alias.first()
|
2020-11-01 12:24:19 +01:00
|
|
|
alias.note = "test note"
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-11-01 12:24:19 +01:00
|
|
|
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user, query="test note")
|
|
|
|
assert len(alias_infos) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_alias_infos_with_pagination_v3_query_alias_name(flask_client):
|
|
|
|
"""test the query on the alias name"""
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
2021-03-01 18:45:15 +01:00
|
|
|
alias = Alias.first()
|
2020-11-01 12:24:19 +01:00
|
|
|
alias.name = "Test Name"
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-11-01 12:24:19 +01:00
|
|
|
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user, query="test name")
|
|
|
|
assert len(alias_infos) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_alias_infos_with_pagination_v3_no_duplicate(flask_client):
|
|
|
|
"""When an alias belongs to multiple mailboxes, make sure get_alias_infos_with_pagination_v3
|
|
|
|
returns no duplicates
|
|
|
|
"""
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
2021-03-01 18:45:15 +01:00
|
|
|
alias = Alias.first()
|
2020-11-01 12:24:19 +01:00
|
|
|
mb = Mailbox.create(user_id=user.id, email="mb@gmail.com")
|
|
|
|
alias._mailboxes.append(mb)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-11-01 12:24:19 +01:00
|
|
|
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user)
|
|
|
|
assert len(alias_infos) == 1
|
2020-11-15 18:42:29 +01:00
|
|
|
|
|
|
|
|
2021-03-01 18:45:15 +01:00
|
|
|
def test_get_alias_infos_with_pagination_v3_no_duplicate_when_empty_contact(
|
|
|
|
flask_client,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Make sure an alias is returned once when it has 2 contacts that have no email log activity
|
|
|
|
"""
|
|
|
|
user = create_user(flask_client)
|
|
|
|
alias = Alias.first()
|
|
|
|
|
|
|
|
Contact.create(
|
|
|
|
user_id=user.id,
|
|
|
|
alias_id=alias.id,
|
|
|
|
website_email="contact@example.com",
|
|
|
|
reply_email="rep@sl.local",
|
|
|
|
)
|
|
|
|
|
|
|
|
Contact.create(
|
|
|
|
user_id=user.id,
|
|
|
|
alias_id=alias.id,
|
|
|
|
website_email="contact2@example.com",
|
|
|
|
reply_email="rep2@sl.local",
|
|
|
|
)
|
|
|
|
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user)
|
|
|
|
assert len(alias_infos) == 1
|
|
|
|
|
|
|
|
|
2020-11-15 18:42:29 +01:00
|
|
|
def test_get_alias_infos_pinned_alias(flask_client):
|
|
|
|
"""Different scenarios with pinned alias"""
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# to have 3 pages: 2*PAGE_LIMIT + the alias automatically created for a new account
|
2022-02-06 21:37:43 +01:00
|
|
|
for _ in range(2 * PAGE_LIMIT):
|
2020-11-15 18:42:29 +01:00
|
|
|
Alias.create_new_random(user)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
first_alias = Alias.order_by(Alias.id).first()
|
2020-11-15 18:42:29 +01:00
|
|
|
|
|
|
|
# should return PAGE_LIMIT alias
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user)
|
|
|
|
assert len(alias_infos) == PAGE_LIMIT
|
|
|
|
# make sure first_alias is not returned as the default order is alias creation date
|
|
|
|
assert first_alias not in [ai.alias for ai in alias_infos]
|
|
|
|
|
|
|
|
# pin the first alias
|
|
|
|
first_alias.pinned = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-11-15 18:42:29 +01:00
|
|
|
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user)
|
|
|
|
# now first_alias is the first result
|
|
|
|
assert first_alias == alias_infos[0].alias
|
|
|
|
# and the page size is still the same
|
|
|
|
assert len(alias_infos) == PAGE_LIMIT
|
|
|
|
|
|
|
|
# pinned alias isn't included in the search
|
|
|
|
alias_infos = get_alias_infos_with_pagination_v3(user, query="no match")
|
|
|
|
assert len(alias_infos) == 0
|