2020-02-05 19:57:11 +08:00
|
|
|
import json
|
|
|
|
|
2020-02-05 00:26:59 +08:00
|
|
|
from flask import url_for
|
|
|
|
|
|
|
|
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN, PAGE_LIMIT
|
|
|
|
from app.extensions import db
|
2020-03-17 18:51:40 +08:00
|
|
|
from app.models import User, ApiKey, Alias, Contact, EmailLog
|
2020-02-05 00:26:59 +08:00
|
|
|
from app.utils import random_word
|
|
|
|
|
|
|
|
|
|
|
|
def test_error_without_pagination(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()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
2020-03-05 18:00:58 +08:00
|
|
|
url_for("api.get_aliases"), headers={"Authentication": api_key.code}
|
2020-02-05 00:26:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.json["error"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_success_with_pagination(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
|
|
|
|
for _ in range(PAGE_LIMIT + 1):
|
2020-03-17 18:51:40 +08:00
|
|
|
Alias.create_new_random(user)
|
2020-02-05 00:26:59 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# get aliases on the 1st page, should return PAGE_LIMIT aliases
|
|
|
|
r = flask_client.get(
|
2020-03-05 18:00:58 +08:00
|
|
|
url_for("api.get_aliases", page_id=0), headers={"Authentication": api_key.code}
|
2020-02-05 00:26:59 +08:00
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["aliases"]) == PAGE_LIMIT
|
|
|
|
|
2020-03-11 19:13:38 +08: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-05 00:26:59 +08: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 18:00:58 +08:00
|
|
|
url_for("api.get_aliases", page_id=1), headers={"Authentication": api_key.code}
|
2020-02-05 00:26:59 +08:00
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert len(r.json["aliases"]) == 2
|
2020-02-05 19:21:17 +08: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 18:51:40 +08:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-02-05 19:21:17 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.delete(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.delete_alias", alias_id=alias.id),
|
2020-02-05 19:21:17 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"deleted": True}
|
2020-02-05 19:28:54 +08: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 18:51:40 +08:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-02-05 19:28:54 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.toggle_alias", alias_id=alias.id),
|
2020-02-05 19:28:54 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"enabled": False}
|
2020-02-05 19:57:11 +08: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 18:51:40 +08:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-02-05 19:57:11 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create some alias log
|
2020-03-17 17:56:59 +08:00
|
|
|
contact = Contact.create(
|
2020-02-05 19:57:11 +08:00
|
|
|
website_email="marketing@example.com",
|
|
|
|
reply_email="reply@a.b",
|
2020-03-17 18:51:40 +08:00
|
|
|
gen_email_id=alias.id,
|
2020-02-05 19:57:11 +08:00
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
for _ in range(int(PAGE_LIMIT / 2)):
|
2020-03-17 18:10:50 +08:00
|
|
|
EmailLog.create(contact_id=contact.id, is_reply=True)
|
2020-02-05 19:57:11 +08:00
|
|
|
|
|
|
|
for _ in range(int(PAGE_LIMIT / 2) + 2):
|
2020-03-17 18:10:50 +08:00
|
|
|
EmailLog.create(contact_id=contact.id, blocked=True)
|
2020-02-05 19:57:11 +08:00
|
|
|
|
|
|
|
r = flask_client.get(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.get_alias_activities", alias_id=alias.id, page_id=0),
|
2020-02-05 19:57:11 +08: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["action"]
|
|
|
|
assert ac["from"]
|
|
|
|
assert ac["action"]
|
|
|
|
assert ac["action"]
|
|
|
|
|
|
|
|
# second page, should return 1 or 2 results only
|
|
|
|
r = flask_client.get(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.get_alias_activities", alias_id=alias.id, page_id=1),
|
2020-02-05 19:57:11 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert len(r.json["activities"]) < 3
|
2020-03-14 18:38:39 +08: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 18:51:40 +08:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-03-14 18:38:39 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.put(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.update_alias", alias_id=alias.id),
|
2020-03-14 18:38:39 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"note": "test note"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json == {"note": "test note"}
|
2020-03-14 19:22:43 +08: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 18:51:40 +08:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-03-14 19:22:43 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create some alias log
|
|
|
|
for i in range(PAGE_LIMIT + 1):
|
2020-03-17 17:56:59 +08:00
|
|
|
contact = Contact.create(
|
2020-03-14 19:22:43 +08:00
|
|
|
website_email=f"marketing-{i}@example.com",
|
|
|
|
reply_email=f"reply-{i}@a.b",
|
2020-03-17 18:51:40 +08:00
|
|
|
gen_email_id=alias.id,
|
2020-03-14 19:22:43 +08:00
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-17 18:10:50 +08:00
|
|
|
EmailLog.create(contact_id=contact.id, is_reply=True)
|
2020-03-14 19:22:43 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.get_alias_contacts_route", alias_id=alias.id, page_id=0),
|
2020-03-14 19:22:43 +08: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"]
|
|
|
|
|
|
|
|
# second page, should return 1 result only
|
|
|
|
r = flask_client.get(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.get_alias_contacts_route", alias_id=alias.id, page_id=1),
|
2020-03-14 19:22:43 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert len(r.json["contacts"]) == 1
|
2020-03-14 19:55:38 +08: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 18:51:40 +08:00
|
|
|
alias = Alias.create_new_random(user)
|
2020-03-14 19:55:38 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.post(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
2020-03-14 19:55:38 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"contact": "First Last <first@example.com>"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json["contact"] == "First Last <first@example.com>"
|
|
|
|
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"]
|
|
|
|
|
|
|
|
# re-add a contact, should return 409
|
|
|
|
r = flask_client.post(
|
2020-03-17 18:51:40 +08:00
|
|
|
url_for("api.create_contact_route", alias_id=alias.id),
|
2020-03-14 19:55:38 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
json={"contact": "First2 Last2 <first@example.com>"},
|
|
|
|
)
|
|
|
|
assert r.status_code == 409
|