app-MAIL-temp/tests/api/test_import_export.py

142 lines
4.4 KiB
Python
Raw Normal View History

from app.db import Session
from app.import_utils import import_from_csv
2021-03-13 15:37:28 +01:00
from app.models import (
CustomDomain,
Mailbox,
Alias,
BatchImport,
2021-08-05 19:49:36 +02:00
File,
2021-03-13 15:37:28 +01:00
)
from tests.utils_test_alias import alias_export
from tests.utils import login, random_domain, random_token
2021-03-10 22:35:04 +01:00
2021-03-13 15:37:28 +01:00
2021-03-10 22:35:04 +01:00
def test_export(flask_client):
alias_export(flask_client, "api.export_aliases")
2021-03-13 15:37:28 +01:00
2021-03-10 23:08:33 +01:00
2021-03-13 00:13:33 +01:00
def test_import_no_mailboxes_no_domains(flask_client):
# Create user
user = login(flask_client)
# Check start state
2021-03-13 15:37:28 +01:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # Onboarding alias
2021-03-13 00:13:33 +01:00
alias_data = [
2021-03-13 15:37:28 +01:00
"alias,note",
"ebay@my-domain.com,Used on eBay",
'facebook@my-domain.com,"Used on Facebook, Instagram."',
2021-03-13 00:13:33 +01:00
]
file = File.create(path=f"/{random_token()}", commit=True)
2021-08-05 19:49:36 +02:00
batch_import = BatchImport.create(user_id=user.id, file_id=file.id, commit=True)
2021-03-13 00:13:33 +01:00
import_from_csv(batch_import, user, alias_data)
# Should have failed to import anything new because my-domain.com isn't registered
2021-03-13 15:37:28 +01:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # +0
2021-03-13 00:13:33 +01:00
2021-03-10 23:08:33 +01:00
def test_import_no_mailboxes(flask_client):
# Create user
2021-03-13 00:13:33 +01:00
user = login(flask_client)
# Check start state
2021-03-13 15:37:28 +01:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # Onboarding alias
2021-03-13 00:13:33 +01:00
domain = random_domain()
2021-03-13 00:13:33 +01:00
# Create domain
CustomDomain.create(user_id=user.id, domain=domain, ownership_verified=True)
Session.commit()
2021-03-10 23:08:33 +01:00
2021-03-13 00:13:33 +01:00
alias_data = [
2021-03-13 15:37:28 +01:00
"alias,note",
f"ebay@{domain},Used on eBay",
f'facebook@{domain},"Used on Facebook, Instagram."',
2021-03-13 00:13:33 +01:00
]
2021-03-10 23:08:33 +01:00
file = File.create(path=f"/{random_token()}", commit=True)
2021-08-05 19:49:36 +02:00
batch_import = BatchImport.create(user_id=user.id, file_id=file.id)
2021-03-10 23:08:33 +01:00
2021-03-13 00:13:33 +01:00
import_from_csv(batch_import, user, alias_data)
2021-03-13 15:37:28 +01:00
assert len(Alias.filter_by(user_id=user.id).all()) == 3 # +2
2021-03-13 15:36:25 +01:00
def test_import_no_domains(flask_client):
# Create user
user = login(flask_client)
# Check start state
2021-03-13 15:37:28 +01:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # Onboarding alias
2021-03-13 15:36:25 +01:00
alias_data = [
2021-03-13 15:37:28 +01:00
"alias,note,mailboxes",
"ebay@my-domain.com,Used on eBay,destination@my-destination-domain.com",
'facebook@my-domain.com,"Used on Facebook, Instagram.",destination1@my-destination-domain.com destination2@my-destination-domain.com',
2021-03-13 15:36:25 +01:00
]
file = File.create(path=f"/{random_token()}", commit=True)
2021-08-05 19:49:36 +02:00
batch_import = BatchImport.create(user_id=user.id, file_id=file.id)
2021-03-13 15:36:25 +01:00
import_from_csv(batch_import, user, alias_data)
# Should have failed to import anything new because my-domain.com isn't registered
2021-03-13 15:37:28 +01:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # +0
2021-03-13 15:36:25 +01:00
def test_import(flask_client):
# Create user
user = login(flask_client)
# Check start state
2021-03-13 15:37:28 +01:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # Onboarding alias
2021-03-13 15:36:25 +01:00
domain1 = random_domain()
domain2 = random_domain()
2021-03-13 15:36:25 +01:00
# Create domains
CustomDomain.create(user_id=user.id, domain=domain1, ownership_verified=True)
CustomDomain.create(user_id=user.id, domain=domain2, ownership_verified=True)
Session.commit()
2021-03-13 15:36:25 +01:00
# Create mailboxes
mailbox1 = Mailbox.create(
user_id=user.id, email=f"destination@{domain2}", verified=True
2021-03-13 15:36:25 +01:00
)
mailbox2 = Mailbox.create(
user_id=user.id, email=f"destination2@{domain2}", verified=True
2021-03-13 15:36:25 +01:00
)
Session.commit()
2021-03-13 15:36:25 +01:00
alias_data = [
2021-03-13 15:37:28 +01:00
"alias,note,mailboxes",
f"ebay@{domain1},Used on eBay,destination@{domain2}",
f'facebook@{domain1},"Used on Facebook, Instagram.",destination@{domain2} destination2@{domain2}',
2021-03-13 15:36:25 +01:00
]
file = File.create(path=f"/{random_token()}", commit=True)
2021-08-05 19:49:36 +02:00
batch_import = BatchImport.create(user_id=user.id, file_id=file.id)
2021-03-13 15:36:25 +01:00
import_from_csv(batch_import, user, alias_data)
2021-11-08 12:57:03 +01:00
aliases = Alias.filter_by(user_id=user.id).order_by(Alias.id).all()
2021-03-13 15:37:28 +01:00
assert len(aliases) == 3 # +2
2021-03-13 15:36:25 +01:00
# aliases[0] is the onboarding alias, skip it
# eBay alias
assert aliases[1].email == f"ebay@{domain1}"
2021-03-13 15:37:28 +01:00
assert len(aliases[1].mailboxes) == 1
2021-03-13 15:36:25 +01:00
# First one should be primary
2021-03-13 15:37:28 +01:00
assert aliases[1].mailbox_id == mailbox1.id
2021-03-13 15:36:25 +01:00
# Others are sorted
2021-03-13 15:37:28 +01:00
assert aliases[1].mailboxes[0] == mailbox1
2021-03-13 15:36:25 +01:00
# Facebook alias
assert aliases[2].email == f"facebook@{domain1}"
2021-03-13 15:37:28 +01:00
assert len(aliases[2].mailboxes) == 2
2021-03-13 15:36:25 +01:00
# First one should be primary
2021-03-13 15:37:28 +01:00
assert aliases[2].mailbox_id == mailbox1.id
2021-03-13 15:36:25 +01:00
# Others are sorted
2021-03-13 15:37:28 +01:00
assert aliases[2].mailboxes[0] == mailbox2
assert aliases[2].mailboxes[1] == mailbox1