app/tests/api/test_import_export.py

222 lines
6.6 KiB
Python
Raw Normal View History

2021-03-11 05:35:04 +08:00
from flask import url_for
2021-03-13 07:13:33 +08:00
from app import alias_utils
2021-03-11 05:35:04 +08:00
from app.extensions import db
2021-03-13 22:37:28 +08:00
from app.models import (
User,
CustomDomain,
Mailbox,
Alias,
AliasMailbox,
BatchImport,
2021-08-06 01:49:36 +08:00
File,
2021-03-13 22:37:28 +08:00
)
2021-03-13 07:13:33 +08:00
from app.import_utils import import_from_csv
from tests.utils import login
2021-03-11 05:35:04 +08:00
2021-03-13 22:37:28 +08:00
2021-03-11 05:35:04 +08:00
def test_export(flask_client):
# Create users
2021-03-13 07:13:33 +08:00
user1 = login(flask_client)
2021-03-11 05:35:04 +08:00
user2 = User.create(
2021-03-13 22:37:28 +08:00
email="x@y.z", password="password", name="Wrong user", activated=True
2021-03-11 05:35:04 +08:00
)
db.session.commit()
# Remove onboarding aliases
for alias in Alias.filter_by(user_id=user1.id).all():
alias_utils.delete_alias(alias, user1)
for alias in Alias.filter_by(user_id=user2.id).all():
alias_utils.delete_alias(alias, user2)
db.session.commit()
# Create domains
CustomDomain.create(
2021-03-13 22:37:28 +08:00
user_id=user1.id, domain="my-destination-domain.com", verified=True
2021-03-11 05:35:04 +08:00
)
CustomDomain.create(
2021-03-13 22:37:28 +08:00
user_id=user2.id, domain="bad-destionation-domain.com", verified=True
2021-03-11 05:35:04 +08:00
)
db.session.commit()
# Create mailboxes
mailbox1 = Mailbox.create(
2021-03-13 22:37:28 +08:00
user_id=user1.id, email="destination@my-destination-domain.com", verified=True
2021-03-11 05:35:04 +08:00
)
mailbox2 = Mailbox.create(
2021-03-13 22:37:28 +08:00
user_id=user1.id, email="destination2@my-destination-domain.com", verified=True
2021-03-11 05:35:04 +08:00
)
badmailbox1 = Mailbox.create(
user_id=user2.id,
email="baddestination@bad-destination-domain.com",
2021-03-13 22:37:28 +08:00
verified=True,
2021-03-11 05:35:04 +08:00
)
db.session.commit()
# Create aliases
Alias.create(
2021-03-11 05:35:04 +08:00
user_id=user1.id,
email="ebay@my-domain.com",
note="Used on eBay",
2021-03-13 22:37:28 +08:00
mailbox_id=mailbox1.id,
2021-03-11 05:35:04 +08:00
)
alias2 = Alias.create(
user_id=user1.id,
email="facebook@my-domain.com",
note="Used on Facebook, Instagram.",
2021-03-13 22:37:28 +08:00
mailbox_id=mailbox1.id,
2021-03-11 05:35:04 +08:00
)
Alias.create(
2021-03-11 05:35:04 +08:00
user_id=user2.id,
email="notmine@my-domain.com",
note="Should not appear",
2021-03-13 22:37:28 +08:00
mailbox_id=badmailbox1.id,
2021-03-11 05:35:04 +08:00
)
db.session.commit()
# Add second mailbox to an alias
AliasMailbox.create(
2021-03-11 05:35:04 +08:00
alias_id=alias2.id,
mailbox_id=mailbox2.id,
)
db.session.commit()
# Export
2021-03-13 22:37:28 +08:00
r = flask_client.get(url_for("api.export_aliases"))
2021-03-11 05:35:04 +08:00
assert r.status_code == 200
assert r.mimetype == "text/csv"
2021-03-13 22:37:28 +08:00
assert (
r.data
== """alias,note,enabled,mailboxes
2021-03-11 05:35:04 +08:00
ebay@my-domain.com,Used on eBay,True,destination@my-destination-domain.com
facebook@my-domain.com,"Used on Facebook, Instagram.",True,destination@my-destination-domain.com destination2@my-destination-domain.com
2021-03-13 22:37:28 +08:00
""".replace(
"\n", "\r\n"
).encode()
)
2021-03-11 06:08:33 +08:00
2021-03-13 07:13:33 +08:00
def test_import_no_mailboxes_no_domains(flask_client):
# Create user
user = login(flask_client)
# Check start state
2021-03-13 22:37:28 +08:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # Onboarding alias
2021-03-13 07:13:33 +08:00
alias_data = [
2021-03-13 22:37:28 +08:00
"alias,note",
"ebay@my-domain.com,Used on eBay",
'facebook@my-domain.com,"Used on Facebook, Instagram."',
2021-03-13 07:13:33 +08:00
]
2021-08-06 01:49:36 +08:00
file = File.create(path="/test", commit=True)
batch_import = BatchImport.create(user_id=user.id, file_id=file.id, commit=True)
2021-03-13 07:13:33 +08: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 22:37:28 +08:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # +0
2021-03-13 07:13:33 +08:00
2021-03-11 06:08:33 +08:00
def test_import_no_mailboxes(flask_client):
# Create user
2021-03-13 07:13:33 +08:00
user = login(flask_client)
# Check start state
2021-03-13 22:37:28 +08:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # Onboarding alias
2021-03-13 07:13:33 +08:00
# Create domain
2021-03-13 22:37:28 +08:00
CustomDomain.create(user_id=user.id, domain="my-domain.com", verified=True)
2021-03-11 06:08:33 +08:00
db.session.commit()
2021-03-13 07:13:33 +08:00
alias_data = [
2021-03-13 22:37:28 +08:00
"alias,note",
"ebay@my-domain.com,Used on eBay",
'facebook@my-domain.com,"Used on Facebook, Instagram."',
2021-03-13 07:13:33 +08:00
]
2021-03-11 06:08:33 +08:00
2021-08-06 01:49:36 +08:00
file = File.create(path="/test", commit=True)
batch_import = BatchImport.create(user_id=user.id, file_id=file.id)
2021-03-11 06:08:33 +08:00
2021-03-13 07:13:33 +08:00
import_from_csv(batch_import, user, alias_data)
2021-03-13 22:37:28 +08:00
assert len(Alias.filter_by(user_id=user.id).all()) == 3 # +2
2021-03-13 22:36:25 +08:00
def test_import_no_domains(flask_client):
# Create user
user = login(flask_client)
# Check start state
2021-03-13 22:37:28 +08:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # Onboarding alias
2021-03-13 22:36:25 +08:00
alias_data = [
2021-03-13 22:37:28 +08: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 22:36:25 +08:00
]
2021-08-06 01:49:36 +08:00
file = File.create(path="/test", commit=True)
batch_import = BatchImport.create(user_id=user.id, file_id=file.id)
2021-03-13 22:36:25 +08: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 22:37:28 +08:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # +0
2021-03-13 22:36:25 +08:00
def test_import(flask_client):
# Create user
user = login(flask_client)
# Check start state
2021-03-13 22:37:28 +08:00
assert len(Alias.filter_by(user_id=user.id).all()) == 1 # Onboarding alias
2021-03-13 22:36:25 +08:00
# Create domains
CustomDomain.create(user_id=user.id, domain="my-domain.com", verified=True)
CustomDomain.create(
2021-03-13 22:37:28 +08:00
user_id=user.id, domain="my-destination-domain.com", verified=True
2021-03-13 22:36:25 +08:00
)
db.session.commit()
# Create mailboxes
mailbox1 = Mailbox.create(
2021-03-13 22:37:28 +08:00
user_id=user.id, email="destination@my-destination-domain.com", verified=True
2021-03-13 22:36:25 +08:00
)
mailbox2 = Mailbox.create(
2021-03-13 22:37:28 +08:00
user_id=user.id, email="destination2@my-destination-domain.com", verified=True
2021-03-13 22:36:25 +08:00
)
db.session.commit()
alias_data = [
2021-03-13 22:37:28 +08:00
"alias,note,mailboxes",
"ebay@my-domain.com,Used on eBay,destination@my-destination-domain.com",
'facebook@my-domain.com,"Used on Facebook, Instagram.",destination@my-destination-domain.com destination2@my-destination-domain.com',
2021-03-13 22:36:25 +08:00
]
2021-08-06 01:49:36 +08:00
file = File.create(path="/test", commit=True)
batch_import = BatchImport.create(user_id=user.id, file_id=file.id)
2021-03-13 22:36:25 +08:00
import_from_csv(batch_import, user, alias_data)
aliases = Alias.filter_by(user_id=user.id).all()
2021-03-13 22:37:28 +08:00
assert len(aliases) == 3 # +2
2021-03-13 22:36:25 +08:00
# aliases[0] is the onboarding alias, skip it
# eBay alias
2021-03-13 22:37:28 +08:00
assert aliases[1].email == "ebay@my-domain.com"
assert len(aliases[1].mailboxes) == 1
2021-03-13 22:36:25 +08:00
# First one should be primary
2021-03-13 22:37:28 +08:00
assert aliases[1].mailbox_id == mailbox1.id
2021-03-13 22:36:25 +08:00
# Others are sorted
2021-03-13 22:37:28 +08:00
assert aliases[1].mailboxes[0] == mailbox1
2021-03-13 22:36:25 +08:00
# Facebook alias
2021-03-13 22:37:28 +08:00
assert aliases[2].email == "facebook@my-domain.com"
assert len(aliases[2].mailboxes) == 2
2021-03-13 22:36:25 +08:00
# First one should be primary
2021-03-13 22:37:28 +08:00
assert aliases[2].mailbox_id == mailbox1.id
2021-03-13 22:36:25 +08:00
# Others are sorted
2021-03-13 22:37:28 +08:00
assert aliases[2].mailboxes[0] == mailbox2
assert aliases[2].mailboxes[1] == mailbox1