2020-04-04 17:18:07 +00:00
|
|
|
from flask import url_for
|
2020-02-05 11:57:11 +00:00
|
|
|
|
2020-04-04 17:18:07 +00:00
|
|
|
from app.config import PAGE_LIMIT
|
2021-10-12 12:36:47 +00:00
|
|
|
from app.db import Session
|
2021-10-19 10:14:16 +00:00
|
|
|
from app.email_utils import is_reverse_alias
|
2020-04-25 11:10:00 +00:00
|
|
|
from app.models import User, ApiKey, Alias, Contact, EmailLog, Mailbox
|
2022-04-15 14:59:44 +00:00
|
|
|
from tests.api.utils import get_new_user_and_api_key
|
2020-11-03 10:10:32 +00:00
|
|
|
from tests.utils import login
|
2020-02-04 16:26:59 +00:00
|
|
|
|
|
|
|
|
2020-03-17 18:32:45 +00:00
|
|
|
def test_get_aliases_error_without_pagination(flask_client):
|
2020-02-04 16:26:59 +00:00
|
|
|
user = User.create(
|
2020-09-28 15:40:30 +00:00
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
2020-02-04 16:26:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-02-04 16:26:59 +00:00
|
|
|
|
|
|
|
r = flask_client.get(
|
2020-03-05 10:00:58 +00:00
|
|
|
url_for("api.get_aliases"), headers={"Authentication": api_key.code}
|
2020-02-04 16:26:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json["error"]
|
|
|
|
|
|
|
|
|
2020-03-17 18:32:45 +00:00
|
|
|
def test_get_aliases_with_pagination(flask_client):
|
2020-02-04 16:26:59 +00:00
|
|
|
user = User.create(
|
2020-09-28 15:40:30 +00:00
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
2020-02-04 16:26:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-02-04 16:26:59 +00:00
|
|
|
|
|
|
|
# create more aliases than PAGE_LIMIT
|
|
|
|
for _ in range(PAGE_LIMIT + 1):
|
2020-03-17 10:51:40 +00:00
|
|
|
Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-02-04 16:26:59 +00:00
|
|
|
|
|
|
|
# get aliases on the 1st page, should return PAGE_LIMIT aliases
|
|
|
|
r = flask_client.get(
|
2020-03-05 10:00:58 +00:00
|
|
|
url_for("api.get_aliases", page_id=0), headers={"Authentication": api_key.code}
|
2020-02-04 16:26:59 +00:00
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["aliases"]) == PAGE_LIMIT
|
|
|
|
|
2020-03-11 11:13:38 +00: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 16:26:59 +00: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 10:00:58 +00:00
|
|
|
url_for("api.get_aliases", page_id=1), headers={"Authentication": api_key.code}
|
2020-02-04 16:26:59 +00:00
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["aliases"]) == 2
|
2020-02-05 11:21:17 +00:00
|
|
|
|
|
|
|
|
2020-11-01 11:29:15 +00:00
|
|
|
def test_get_aliases_query(flask_client):
|
2020-03-17 18:32:45 +00:00
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-17 18:32:45 +00:00
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-17 18:32:45 +00:00
|
|
|
|
|
|
|
# create more aliases than PAGE_LIMIT
|
|
|
|
Alias.create_new(user, "prefix1")
|
|
|
|
Alias.create_new(user, "prefix2")
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-17 18:32:45 +00:00
|
|
|
|
|
|
|
# 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 20:26:35 +00:00
|
|
|
def test_get_aliases_v2(flask_client):
|
2022-02-03 10:09:37 +00:00
|
|
|
user = login(flask_client)
|
2020-04-06 20:26:35 +00:00
|
|
|
|
|
|
|
a0 = Alias.create_new(user, "prefix0")
|
|
|
|
a1 = Alias.create_new(user, "prefix1")
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-04-06 20:26:35 +00:00
|
|
|
|
2020-11-01 11:32:20 +00:00
|
|
|
# << Aliases have no activity >>
|
2022-02-03 10:09:37 +00:00
|
|
|
r = flask_client.get("/api/v2/aliases?page_id=0")
|
2020-11-01 11:32:20 +00:00
|
|
|
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 20:26:35 +00:00
|
|
|
c0 = Contact.create(
|
|
|
|
user_id=user.id,
|
|
|
|
alias_id=a0.id,
|
|
|
|
website_email="c0@example.com",
|
|
|
|
reply_email="re0@SL",
|
2022-02-03 10:09:37 +00:00
|
|
|
commit=True,
|
2020-04-06 20:26:35 +00:00
|
|
|
)
|
2021-07-11 10:28:23 +00:00
|
|
|
EmailLog.create(
|
2022-02-03 10:09:37 +00:00
|
|
|
contact_id=c0.id, user_id=user.id, alias_id=c0.alias_id, commit=True
|
2021-07-11 10:28:23 +00:00
|
|
|
)
|
2020-04-06 20:26:35 +00:00
|
|
|
|
|
|
|
# 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",
|
2022-02-03 10:09:37 +00:00
|
|
|
commit=True,
|
2020-04-06 20:26:35 +00:00
|
|
|
)
|
2021-07-11 10:28:23 +00:00
|
|
|
EmailLog.create(
|
2022-02-03 10:09:37 +00:00
|
|
|
contact_id=c1.id, user_id=user.id, alias_id=c1.alias_id, commit=True
|
2021-07-11 10:28:23 +00:00
|
|
|
)
|
2020-04-06 20:26:35 +00:00
|
|
|
|
2022-02-03 10:09:37 +00:00
|
|
|
r = flask_client.get("/api/v2/aliases?page_id=0")
|
2020-04-06 20:26:35 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
r0 = r.json["aliases"][0]
|
2020-11-01 11:32:20 +00:00
|
|
|
|
2020-04-06 20:26:35 +00: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 18:01:11 +00:00
|
|
|
assert "pinned" in r0
|
2020-04-06 20:26:35 +00:00
|
|
|
|
|
|
|
|
2022-02-03 10:16:42 +00:00
|
|
|
def test_get_pinned_aliases_v2(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
|
|
|
|
a0 = Alias.create_new(user, "prefix0")
|
|
|
|
a0.pinned = True
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get("/api/v2/aliases?page_id=0")
|
|
|
|
assert r.status_code == 200
|
|
|
|
# the default alias (created when user is created) and a0 are returned
|
|
|
|
assert len(r.json["aliases"]) == 2
|
|
|
|
|
|
|
|
r = flask_client.get("/api/v2/aliases?page_id=0&pinned=true")
|
|
|
|
assert r.status_code == 200
|
|
|
|
# only a0 is returned
|
|
|
|
assert len(r.json["aliases"]) == 1
|
|
|
|
assert r.json["aliases"][0]["id"] == a0.id
|
|
|
|
|
|
|
|
|
2022-04-25 07:22:29 +00:00
|
|
|
def test_get_disabled_aliases_v2(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
|
|
|
|
a0 = Alias.create_new(user, "prefix0")
|
|
|
|
a0.enabled = False
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get("/api/v2/aliases?page_id=0")
|
|
|
|
assert r.status_code == 200
|
|
|
|
# the default alias (created when user is created) and a0 are returned
|
|
|
|
assert len(r.json["aliases"]) == 2
|
|
|
|
|
|
|
|
r = flask_client.get("/api/v2/aliases?page_id=0&disabled=true")
|
|
|
|
assert r.status_code == 200
|
|
|
|
# only a0 is returned
|
|
|
|
assert len(r.json["aliases"]) == 1
|
|
|
|
assert r.json["aliases"][0]["id"] == a0.id
|
|
|
|
|
|
|
|
|
2020-02-05 11:21:17 +00:00
|
|
|
def test_delete_alias(flask_client):
|
2022-01-06 14:29:37 +00:00
|
|
|
user = login(flask_client)
|
2020-02-05 11:21:17 +00:00
|
|
|
|
2020-03-17 10:51:40 +00:00
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-02-05 11:21:17 +00:00
|
|
|
|
|
|
|
r = flask_client.delete(
|
2020-03-17 10:51:40 +00:00
|
|
|
url_for("api.delete_alias", alias_id=alias.id),
|
2020-02-05 11:21:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"deleted": True}
|
2020-02-05 11:28:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_toggle_alias(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-02-05 11:28:54 +00:00
|
|
|
|
2020-03-17 10:51:40 +00:00
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-02-05 11:28:54 +00:00
|
|
|
|
|
|
|
r = flask_client.post(
|
2020-03-17 10:51:40 +00:00
|
|
|
url_for("api.toggle_alias", alias_id=alias.id),
|
2020-02-05 11:28:54 +00:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"enabled": False}
|
2020-02-05 11:57:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_alias_activities(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-02-05 11:57:11 +00:00
|
|
|
|
2020-03-17 10:51:40 +00:00
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-02-05 11:57:11 +00:00
|
|
|
|
|
|
|
# create some alias log
|
2020-03-17 09:56:59 +00:00
|
|
|
contact = Contact.create(
|
2020-02-05 11:57:11 +00:00
|
|
|
website_email="marketing@example.com",
|
|
|
|
reply_email="reply@a.b",
|
2020-03-17 11:01:18 +00:00
|
|
|
alias_id=alias.id,
|
2020-03-20 08:54:38 +00:00
|
|
|
user_id=alias.user_id,
|
2020-02-05 11:57:11 +00:00
|
|
|
)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-02-05 11:57:11 +00:00
|
|
|
|
|
|
|
for _ in range(int(PAGE_LIMIT / 2)):
|
2021-07-11 10:28:23 +00:00
|
|
|
EmailLog.create(
|
|
|
|
contact_id=contact.id,
|
|
|
|
is_reply=True,
|
|
|
|
user_id=contact.user_id,
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
)
|
2020-02-05 11:57:11 +00:00
|
|
|
|
|
|
|
for _ in range(int(PAGE_LIMIT / 2) + 2):
|
2021-07-11 10:28:23 +00:00
|
|
|
EmailLog.create(
|
|
|
|
contact_id=contact.id,
|
|
|
|
blocked=True,
|
|
|
|
user_id=contact.user_id,
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
)
|
2020-02-05 11:57:11 +00:00
|
|
|
|
|
|
|
r = flask_client.get(
|
2020-03-17 10:51:40 +00:00
|
|
|
url_for("api.get_alias_activities", alias_id=alias.id, page_id=0),
|
2020-02-05 11:57:11 +00: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 17:18:07 +00:00
|
|
|
assert ac["to"]
|
|
|
|
assert ac["timestamp"]
|
2020-02-05 11:57:11 +00:00
|
|
|
assert ac["action"]
|
2020-04-04 17:18:07 +00:00
|
|
|
assert ac["reverse_alias"]
|
2020-11-17 08:27:30 +00:00
|
|
|
assert ac["reverse_alias_address"]
|
2020-02-05 11:57:11 +00:00
|
|
|
|
|
|
|
# second page, should return 1 or 2 results only
|
|
|
|
r = flask_client.get(
|
2020-03-17 10:51:40 +00:00
|
|
|
url_for("api.get_alias_activities", alias_id=alias.id, page_id=1),
|
2020-02-05 11:57:11 +00:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert len(r.json["activities"]) < 3
|
2020-03-14 10:38:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_alias(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-03-14 10:38:39 +00:00
|
|
|
|
2020-03-17 10:51:40 +00:00
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-14 10:38:39 +00:00
|
|
|
|
|
|
|
r = flask_client.put(
|
2020-03-17 10:51:40 +00:00
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
2020-03-14 10:38:39 +00:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"note": "test note"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
2020-04-25 11:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_alias_mailbox(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-04-25 11:10:00 +00:00
|
|
|
|
|
|
|
mb = Mailbox.create(user_id=user.id, email="ab@cd.com", verified=True)
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-04-25 11:10:00 +00:00
|
|
|
|
|
|
|
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 11:22:43 +00:00
|
|
|
|
|
|
|
|
2020-04-26 08:38:16 +00:00
|
|
|
def test_update_alias_name(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-04-26 08:38:16 +00:00
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-04-26 08:38:16 +00:00
|
|
|
|
|
|
|
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 17:39:23 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
alias = Alias.get(alias.id)
|
|
|
|
assert alias.name == "Test Name"
|
2020-04-26 08:38:16 +00:00
|
|
|
|
2020-11-20 17:39:23 +00: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 08:38:16 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
alias = Alias.get(alias.id)
|
|
|
|
assert alias.name == "Test Name"
|
|
|
|
|
|
|
|
|
2020-05-03 14:02:46 +00:00
|
|
|
def test_update_alias_mailboxes(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-05-03 14:02:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-05-03 14:02:46 +00:00
|
|
|
|
|
|
|
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 10:55:21 +00:00
|
|
|
def test_update_disable_pgp(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-05-16 10:55:21 +00:00
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-05-16 10:55:21 +00:00
|
|
|
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 17:45:58 +00:00
|
|
|
def test_update_pinned(flask_client):
|
2022-04-18 09:17:10 +00:00
|
|
|
user = login(flask_client)
|
2020-11-15 17:45:58 +00:00
|
|
|
|
2022-04-18 09:17:10 +00:00
|
|
|
alias = Alias.filter_by(user_id=user.id).first()
|
2020-11-15 17:45:58 +00:00
|
|
|
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 11:22:43 +00:00
|
|
|
def test_alias_contacts(flask_client):
|
2021-11-02 14:55:16 +00:00
|
|
|
user = login(flask_client)
|
2020-03-14 11:22:43 +00:00
|
|
|
|
2020-03-17 10:51:40 +00:00
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-14 11:22:43 +00:00
|
|
|
|
|
|
|
# create some alias log
|
|
|
|
for i in range(PAGE_LIMIT + 1):
|
2020-03-17 09:56:59 +00:00
|
|
|
contact = Contact.create(
|
2020-03-14 11:22:43 +00:00
|
|
|
website_email=f"marketing-{i}@example.com",
|
|
|
|
reply_email=f"reply-{i}@a.b",
|
2020-03-17 11:01:18 +00:00
|
|
|
alias_id=alias.id,
|
2020-03-20 08:54:38 +00:00
|
|
|
user_id=alias.user_id,
|
2020-03-14 11:22:43 +00:00
|
|
|
)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-14 11:22:43 +00:00
|
|
|
|
2021-07-11 10:28:23 +00:00
|
|
|
EmailLog.create(
|
|
|
|
contact_id=contact.id,
|
|
|
|
is_reply=True,
|
|
|
|
user_id=contact.user_id,
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-14 11:22:43 +00:00
|
|
|
|
2021-11-02 14:55:16 +00:00
|
|
|
r = flask_client.get(f"/api/aliases/{alias.id}/contacts?page_id=0")
|
2020-03-14 11:22:43 +00:00
|
|
|
|
|
|
|
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 18:39:00 +00:00
|
|
|
assert ac["reverse_alias_address"]
|
2021-11-02 14:55:16 +00:00
|
|
|
assert "block_forward" in ac
|
2020-03-14 11:22:43 +00:00
|
|
|
|
|
|
|
# second page, should return 1 result only
|
2021-11-02 14:55:16 +00:00
|
|
|
r = flask_client.get(f"/api/aliases/{alias.id}/contacts?page_id=1")
|
2020-03-14 11:22:43 +00:00
|
|
|
assert len(r.json["contacts"]) == 1
|
2020-03-14 11:55:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_contact_route(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-14 11:55:38 +00:00
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-14 11:55:38 +00:00
|
|
|
|
2020-03-17 10:51:40 +00:00
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-14 11:55:38 +00:00
|
|
|
|
|
|
|
r = flask_client.post(
|
2020-03-17 10:51:40 +00:00
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
2020-03-14 11:55:38 +00:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"contact": "First Last <first@example.com>"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
2020-04-05 10:58:06 +00:00
|
|
|
assert r.json["contact"] == "first@example.com"
|
2020-03-14 11:55:38 +00: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 18:39:00 +00:00
|
|
|
assert r.json["reverse_alias_address"]
|
2021-03-17 19:29:34 +00:00
|
|
|
assert r.json["existed"] is False
|
2020-03-14 11:55:38 +00:00
|
|
|
|
2021-03-17 19:29:34 +00:00
|
|
|
# re-add a contact, should return 200
|
2020-03-14 11:55:38 +00:00
|
|
|
r = flask_client.post(
|
2020-03-17 10:51:40 +00:00
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
2020-03-14 11:55:38 +00:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"contact": "First2 Last2 <first@example.com>"},
|
|
|
|
)
|
2021-03-17 19:29:34 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json["existed"]
|
2020-03-17 18:18:26 +00:00
|
|
|
|
|
|
|
|
2020-11-03 10:10:32 +00:00
|
|
|
def test_create_contact_route_empty_contact_address(flask_client):
|
2022-04-18 09:17:10 +00:00
|
|
|
user = login(flask_client)
|
|
|
|
alias = Alias.filter_by(user_id=user.id).first()
|
2020-11-03 10:10:32 +00:00
|
|
|
|
|
|
|
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):
|
2022-04-18 09:17:10 +00:00
|
|
|
user = login(flask_client)
|
|
|
|
alias = Alias.filter_by(user_id=user.id).first()
|
2020-11-03 10:10:32 +00:00
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
2021-09-10 15:10:05 +00:00
|
|
|
json={"contact": "@gmail.com"},
|
2020-11-03 10:10:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
2021-09-10 15:10:05 +00:00
|
|
|
assert r.json["error"] == "invalid contact email @gmail.com"
|
2020-11-03 10:10:32 +00:00
|
|
|
|
|
|
|
|
2020-03-17 18:18:26 +00:00
|
|
|
def test_delete_contact(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-03-17 18:18:26 +00:00
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-17 18:18:26 +00:00
|
|
|
|
|
|
|
contact = Contact.create(
|
|
|
|
alias_id=alias.id,
|
|
|
|
website_email="contact@example.com",
|
|
|
|
reply_email="reply+random@sl.io",
|
2020-03-20 08:54:38 +00:00
|
|
|
user_id=alias.user_id,
|
2020-03-17 18:18:26 +00:00
|
|
|
)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-17 18:18:26 +00:00
|
|
|
|
|
|
|
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 18:44:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_alias(flask_client):
|
2022-04-15 14:59:44 +00:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-03-26 18:44:00 +00:00
|
|
|
|
|
|
|
# create more aliases than PAGE_LIMIT
|
|
|
|
alias = Alias.create_new_random(user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-26 18:44:00 +00:00
|
|
|
|
|
|
|
# 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 18:01:11 +00:00
|
|
|
assert "pinned" in res
|
2020-11-16 18:22:19 +00:00
|
|
|
|
|
|
|
|
2021-10-19 10:14:16 +00:00
|
|
|
def test_is_reverse_alias(flask_client):
|
|
|
|
assert is_reverse_alias("ra+abcd@sl.local")
|
|
|
|
assert is_reverse_alias("reply+abcd@sl.local")
|
|
|
|
|
|
|
|
assert not is_reverse_alias("ra+abcd@test.org")
|
|
|
|
assert not is_reverse_alias("reply+abcd@test.org")
|
|
|
|
assert not is_reverse_alias("abcd@test.org")
|
2021-10-28 08:14:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_toggle_contact(flask_client):
|
|
|
|
user = login(flask_client)
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
contact = Contact.create(
|
|
|
|
alias_id=alias.id,
|
|
|
|
website_email="contact@example.com",
|
|
|
|
reply_email="reply+random@sl.io",
|
|
|
|
user_id=alias.user_id,
|
|
|
|
)
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
r = flask_client.post(f"/api/contacts/{contact.id}/toggle")
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"block_forward": True}
|
2022-04-27 14:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_aliases_disabled_account(flask_client):
|
|
|
|
user, api_key = get_new_user_and_api_key()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
|
|
|
"/api/v2/aliases?page_id=0",
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
user.disabled = True
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
|
|
|
"/api/v2/aliases?page_id=0",
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.status_code == 403
|