2020-04-04 19:18:07 +02:00
|
|
|
from flask import url_for
|
2020-02-05 12:57:11 +01:00
|
|
|
|
2020-04-04 19:18:07 +02:00
|
|
|
from app.config import PAGE_LIMIT
|
2020-11-16 19:22:19 +01:00
|
|
|
from app.email_utils import is_reply_email
|
2020-02-04 17:26:59 +01:00
|
|
|
from app.extensions import db
|
2020-04-25 13:10:00 +02:00
|
|
|
from app.models import User, ApiKey, Alias, Contact, EmailLog, Mailbox
|
2020-11-03 11:10:32 +01:00
|
|
|
from tests.utils import login
|
2020-02-04 17:26:59 +01:00
|
|
|
|
|
|
|
|
2020-03-17 19:32:45 +01:00
|
|
|
def test_get_aliases_error_without_pagination(flask_client):
|
2020-02-04 17:26:59 +01:00
|
|
|
user = User.create(
|
2020-09-28 17:40:30 +02:00
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
2020-02-04 17:26:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
2020-03-05 11:00:58 +01:00
|
|
|
url_for("api.get_aliases"), headers={"Authentication": api_key.code}
|
2020-02-04 17:26:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json["error"]
|
|
|
|
|
|
|
|
|
2020-03-17 19:32:45 +01:00
|
|
|
def test_get_aliases_with_pagination(flask_client):
|
2020-02-04 17:26:59 +01:00
|
|
|
user = User.create(
|
2020-09-28 17:40:30 +02:00
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
2020-02-04 17:26:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create more aliases than PAGE_LIMIT
|
|
|
|
for _ in range(PAGE_LIMIT + 1):
|
2020-03-17 11:51:40 +01:00
|
|
|
Alias.create_new_random(user)
|
2020-02-04 17:26:59 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# get aliases on the 1st page, should return PAGE_LIMIT aliases
|
|
|
|
r = flask_client.get(
|
2020-03-05 11:00:58 +01:00
|
|
|
url_for("api.get_aliases", page_id=0), headers={"Authentication": api_key.code}
|
2020-02-04 17:26:59 +01:00
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["aliases"]) == PAGE_LIMIT
|
|
|
|
|
2020-03-11 12:13:38 +01:00
|
|
|
# assert returned field
|
|
|
|
for a in r.json["aliases"]:
|
|
|
|
assert "id" in a
|
|
|
|
assert "email" in a
|
|
|
|
assert "creation_date" in a
|
|
|
|
assert "creation_timestamp" in a
|
|
|
|
assert "nb_forward" in a
|
|
|
|
assert "nb_block" in a
|
|
|
|
assert "nb_reply" in a
|
|
|
|
assert "enabled" in a
|
|
|
|
assert "note" in a
|
|
|
|
|
2020-02-04 17:26:59 +01:00
|
|
|
# get aliases on the 2nd page, should return 2 aliases
|
|
|
|
# as the total number of aliases is PAGE_LIMIT +2
|
|
|
|
# 1 alias is created when user is created
|
|
|
|
r = flask_client.get(
|
2020-03-05 11:00:58 +01:00
|
|
|
url_for("api.get_aliases", page_id=1), headers={"Authentication": api_key.code}
|
2020-02-04 17:26:59 +01:00
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["aliases"]) == 2
|
2020-02-05 12:21:17 +01:00
|
|
|
|
|
|
|
|
2020-11-01 12:29:15 +01:00
|
|
|
def test_get_aliases_query(flask_client):
|
2020-03-17 19:32:45 +01:00
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create more aliases than PAGE_LIMIT
|
|
|
|
Alias.create_new(user, "prefix1")
|
|
|
|
Alias.create_new(user, "prefix2")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# get aliases without query, should return 3 aliases as one alias is created when user is created
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.get_aliases", page_id=0), headers={"Authentication": api_key.code}
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["aliases"]) == 3
|
|
|
|
|
|
|
|
# get aliases with "prefix1" query, should return 1 alias
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.get_aliases", page_id=0),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"query": "prefix1"},
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["aliases"]) == 1
|
|
|
|
|
|
|
|
|
2020-04-06 22:26:35 +02:00
|
|
|
def test_get_aliases_v2(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
a0 = Alias.create_new(user, "prefix0")
|
|
|
|
a1 = Alias.create_new(user, "prefix1")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-11-01 12:32:20 +01:00
|
|
|
# << Aliases have no activity >>
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.get_aliases_v2", page_id=0),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
r0 = r.json["aliases"][0]
|
|
|
|
assert "name" in r0
|
|
|
|
|
|
|
|
# make sure a1 is returned before a0
|
|
|
|
assert r0["email"].startswith("prefix1")
|
|
|
|
assert "id" in r0["mailbox"]
|
|
|
|
assert "email" in r0["mailbox"]
|
|
|
|
|
|
|
|
assert r0["mailboxes"]
|
|
|
|
for mailbox in r0["mailboxes"]:
|
|
|
|
assert "id" in mailbox
|
|
|
|
assert "email" in mailbox
|
|
|
|
|
|
|
|
assert "support_pgp" in r0
|
|
|
|
assert not r0["support_pgp"]
|
|
|
|
|
|
|
|
assert "disable_pgp" in r0
|
|
|
|
assert not r0["disable_pgp"]
|
|
|
|
|
|
|
|
# << Alias has some activities >>
|
2020-04-06 22:26:35 +02:00
|
|
|
c0 = Contact.create(
|
|
|
|
user_id=user.id,
|
|
|
|
alias_id=a0.id,
|
|
|
|
website_email="c0@example.com",
|
|
|
|
reply_email="re0@SL",
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
EmailLog.create(contact_id=c0.id, user_id=user.id)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# a1 has more recent activity
|
|
|
|
c1 = Contact.create(
|
|
|
|
user_id=user.id,
|
|
|
|
alias_id=a1.id,
|
|
|
|
website_email="c1@example.com",
|
|
|
|
reply_email="re1@SL",
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
EmailLog.create(contact_id=c1.id, user_id=user.id)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# get aliases v2
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.get_aliases_v2", page_id=0),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
r0 = r.json["aliases"][0]
|
2020-11-01 12:32:20 +01:00
|
|
|
|
2020-04-06 22:26:35 +02:00
|
|
|
assert r0["latest_activity"]["action"] == "forward"
|
|
|
|
assert "timestamp" in r0["latest_activity"]
|
|
|
|
|
|
|
|
assert r0["latest_activity"]["contact"]["email"] == "c1@example.com"
|
|
|
|
assert "name" in r0["latest_activity"]["contact"]
|
|
|
|
assert "reverse_alias" in r0["latest_activity"]["contact"]
|
2020-11-15 19:01:11 +01:00
|
|
|
assert "pinned" in r0
|
2020-04-06 22:26:35 +02:00
|
|
|
|
|
|
|
|
2020-02-05 12:21:17 +01:00
|
|
|
def test_delete_alias(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-02-05 12:21:17 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.delete(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.delete_alias", alias_id=alias.id),
|
2020-02-05 12:21:17 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"deleted": True}
|
2020-02-05 12:28:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_toggle_alias(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-02-05 12:28:54 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.toggle_alias", alias_id=alias.id),
|
2020-02-05 12:28:54 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"enabled": False}
|
2020-02-05 12:57:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_alias_activities(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-02-05 12:57:11 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create some alias log
|
2020-03-17 10:56:59 +01:00
|
|
|
contact = Contact.create(
|
2020-02-05 12:57:11 +01:00
|
|
|
website_email="marketing@example.com",
|
|
|
|
reply_email="reply@a.b",
|
2020-03-17 12:01:18 +01:00
|
|
|
alias_id=alias.id,
|
2020-03-20 09:54:38 +01:00
|
|
|
user_id=alias.user_id,
|
2020-02-05 12:57:11 +01:00
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
for _ in range(int(PAGE_LIMIT / 2)):
|
2020-03-20 09:55:52 +01:00
|
|
|
EmailLog.create(contact_id=contact.id, is_reply=True, user_id=contact.user_id)
|
2020-02-05 12:57:11 +01:00
|
|
|
|
|
|
|
for _ in range(int(PAGE_LIMIT / 2) + 2):
|
2020-03-20 09:55:52 +01:00
|
|
|
EmailLog.create(contact_id=contact.id, blocked=True, user_id=contact.user_id)
|
2020-02-05 12:57:11 +01:00
|
|
|
|
|
|
|
r = flask_client.get(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.get_alias_activities", alias_id=alias.id, page_id=0),
|
2020-02-05 12:57:11 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["activities"]) == PAGE_LIMIT
|
|
|
|
for ac in r.json["activities"]:
|
|
|
|
assert ac["from"]
|
2020-04-04 19:18:07 +02:00
|
|
|
assert ac["to"]
|
|
|
|
assert ac["timestamp"]
|
2020-02-05 12:57:11 +01:00
|
|
|
assert ac["action"]
|
2020-04-04 19:18:07 +02:00
|
|
|
assert ac["reverse_alias"]
|
2020-11-17 09:27:30 +01:00
|
|
|
assert ac["reverse_alias_address"]
|
2020-02-05 12:57:11 +01:00
|
|
|
|
|
|
|
# second page, should return 1 or 2 results only
|
|
|
|
r = flask_client.get(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.get_alias_activities", alias_id=alias.id, page_id=1),
|
2020-02-05 12:57:11 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert len(r.json["activities"]) < 3
|
2020-03-14 11:38:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_alias(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-03-14 11:38:39 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.put(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
2020-03-14 11:38:39 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"note": "test note"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
2020-04-25 13:10:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_alias_mailbox(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
mb = Mailbox.create(user_id=user.id, email="ab@cd.com", verified=True)
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.put(
|
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"mailbox_id": mb.id},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
# fail when update with non-existing mailbox
|
|
|
|
r = flask_client.put(
|
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"mailbox_id": -1},
|
|
|
|
)
|
|
|
|
assert r.status_code == 400
|
2020-03-14 12:22:43 +01:00
|
|
|
|
|
|
|
|
2020-04-26 10:38:16 +02:00
|
|
|
def test_update_alias_name(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.put(
|
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"name": "Test Name"},
|
|
|
|
)
|
2020-11-20 18:39:23 +01:00
|
|
|
assert r.status_code == 200
|
|
|
|
alias = Alias.get(alias.id)
|
|
|
|
assert alias.name == "Test Name"
|
2020-04-26 10:38:16 +02:00
|
|
|
|
2020-11-20 18:39:23 +01:00
|
|
|
# update name with linebreak
|
|
|
|
r = flask_client.put(
|
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"name": "Test \nName"},
|
|
|
|
)
|
2020-04-26 10:38:16 +02:00
|
|
|
assert r.status_code == 200
|
|
|
|
alias = Alias.get(alias.id)
|
|
|
|
assert alias.name == "Test Name"
|
|
|
|
|
|
|
|
|
2020-05-03 16:02:46 +02:00
|
|
|
def test_update_alias_mailboxes(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
mb1 = Mailbox.create(user_id=user.id, email="ab1@cd.com", verified=True)
|
|
|
|
mb2 = Mailbox.create(user_id=user.id, email="ab2@cd.com", verified=True)
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.put(
|
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"mailbox_ids": [mb1.id, mb2.id]},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
alias = Alias.get(alias.id)
|
|
|
|
|
|
|
|
assert alias.mailbox
|
|
|
|
assert len(alias._mailboxes) == 1
|
|
|
|
|
|
|
|
# fail when update with empty mailboxes
|
|
|
|
r = flask_client.put(
|
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"mailbox_ids": []},
|
|
|
|
)
|
|
|
|
assert r.status_code == 400
|
|
|
|
|
|
|
|
|
2020-05-16 12:55:21 +02:00
|
|
|
def test_update_disable_pgp(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
db.session.commit()
|
|
|
|
assert not alias.disable_pgp
|
|
|
|
|
|
|
|
r = flask_client.put(
|
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"disable_pgp": True},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
alias = Alias.get(alias.id)
|
|
|
|
assert alias.disable_pgp
|
|
|
|
|
|
|
|
|
2020-11-15 18:45:58 +01:00
|
|
|
def test_update_pinned(flask_client):
|
|
|
|
login(flask_client)
|
|
|
|
|
|
|
|
alias = Alias.first()
|
|
|
|
assert not alias.pinned
|
|
|
|
|
|
|
|
r = flask_client.patch(
|
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
|
|
|
json={"pinned": True},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert alias.pinned
|
|
|
|
|
|
|
|
|
2020-03-14 12:22:43 +01:00
|
|
|
def test_alias_contacts(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-03-14 12:22:43 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create some alias log
|
|
|
|
for i in range(PAGE_LIMIT + 1):
|
2020-03-17 10:56:59 +01:00
|
|
|
contact = Contact.create(
|
2020-03-14 12:22:43 +01:00
|
|
|
website_email=f"marketing-{i}@example.com",
|
|
|
|
reply_email=f"reply-{i}@a.b",
|
2020-03-17 12:01:18 +01:00
|
|
|
alias_id=alias.id,
|
2020-03-20 09:54:38 +01:00
|
|
|
user_id=alias.user_id,
|
2020-03-14 12:22:43 +01:00
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-20 09:55:52 +01:00
|
|
|
EmailLog.create(contact_id=contact.id, is_reply=True, user_id=contact.user_id)
|
2020-03-14 12:22:43 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.get_alias_contacts_route", alias_id=alias.id, page_id=0),
|
2020-03-14 12:22:43 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["contacts"]) == PAGE_LIMIT
|
|
|
|
for ac in r.json["contacts"]:
|
|
|
|
assert ac["creation_date"]
|
|
|
|
assert ac["creation_timestamp"]
|
|
|
|
assert ac["last_email_sent_date"]
|
|
|
|
assert ac["last_email_sent_timestamp"]
|
|
|
|
assert ac["contact"]
|
|
|
|
assert ac["reverse_alias"]
|
2020-11-16 19:39:00 +01:00
|
|
|
assert ac["reverse_alias_address"]
|
2020-03-14 12:22:43 +01:00
|
|
|
|
|
|
|
# second page, should return 1 result only
|
|
|
|
r = flask_client.get(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.get_alias_contacts_route", alias_id=alias.id, page_id=1),
|
2020-03-14 12:22:43 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert len(r.json["contacts"]) == 1
|
2020-03-14 12:55:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_contact_route(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-03-14 12:55:38 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
2020-03-14 12:55:38 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"contact": "First Last <first@example.com>"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
2020-04-05 12:58:06 +02:00
|
|
|
assert r.json["contact"] == "first@example.com"
|
2020-03-14 12:55:38 +01:00
|
|
|
assert "creation_date" in r.json
|
|
|
|
assert "creation_timestamp" in r.json
|
|
|
|
assert r.json["last_email_sent_date"] is None
|
|
|
|
assert r.json["last_email_sent_timestamp"] is None
|
|
|
|
assert r.json["reverse_alias"]
|
2020-11-16 19:39:00 +01:00
|
|
|
assert r.json["reverse_alias_address"]
|
2021-03-17 20:29:34 +01:00
|
|
|
assert r.json["existed"] is False
|
2020-03-14 12:55:38 +01:00
|
|
|
|
2021-03-17 20:29:34 +01:00
|
|
|
# re-add a contact, should return 200
|
2020-03-14 12:55:38 +01:00
|
|
|
r = flask_client.post(
|
2020-03-17 11:51:40 +01:00
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
2020-03-14 12:55:38 +01:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"contact": "First2 Last2 <first@example.com>"},
|
|
|
|
)
|
2021-03-17 20:29:34 +01:00
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json["existed"]
|
2020-03-17 19:18:26 +01:00
|
|
|
|
|
|
|
|
2020-11-03 11:10:32 +01:00
|
|
|
def test_create_contact_route_empty_contact_address(flask_client):
|
|
|
|
login(flask_client)
|
|
|
|
alias = Alias.query.first()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
|
|
|
json={"contact": ""},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json["error"] == "Contact cannot be empty"
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_contact_route_invalid_contact_email(flask_client):
|
|
|
|
login(flask_client)
|
|
|
|
alias = Alias.query.first()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
|
|
|
json={"contact": "with space@gmail.com"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json["error"] == "invalid contact email with space@gmail.com"
|
|
|
|
|
|
|
|
|
2020-03-17 19:18:26 +01:00
|
|
|
def test_delete_contact(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
contact = Contact.create(
|
|
|
|
alias_id=alias.id,
|
|
|
|
website_email="contact@example.com",
|
|
|
|
reply_email="reply+random@sl.io",
|
2020-03-20 09:54:38 +01:00
|
|
|
user_id=alias.user_id,
|
2020-03-17 19:18:26 +01:00
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.delete(
|
|
|
|
url_for("api.delete_contact", contact_id=contact.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"deleted": True}
|
2020-03-26 19:44:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_alias(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create more aliases than PAGE_LIMIT
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# get aliases on the 1st page, should return PAGE_LIMIT aliases
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.get_alias", alias_id=alias.id),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
# assert returned field
|
|
|
|
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
|
|
|
|
assert "note" in res
|
2020-11-15 19:01:11 +01:00
|
|
|
assert "pinned" in res
|
2020-11-16 19:22:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_is_reply_email(flask_client):
|
|
|
|
assert is_reply_email("ra+abcd@test.org")
|
|
|
|
assert is_reply_email("reply+abcd@test.org")
|
|
|
|
assert not is_reply_email("abcd@test.org")
|