2022-03-17 18:03:36 +00:00
|
|
|
import email
|
2020-11-15 18:34:13 +00:00
|
|
|
import json
|
2022-03-17 18:03:36 +00:00
|
|
|
import os
|
2022-03-18 14:44:07 +00:00
|
|
|
import random
|
|
|
|
import string
|
2022-03-17 18:03:36 +00:00
|
|
|
from email.message import EmailMessage
|
2022-03-18 14:55:59 +00:00
|
|
|
from typing import Optional, Dict
|
2020-11-15 18:34:13 +00:00
|
|
|
|
2022-03-18 14:44:07 +00:00
|
|
|
import jinja2
|
2019-08-10 15:03:51 +00:00
|
|
|
from flask import url_for
|
|
|
|
|
2022-03-21 11:03:11 +00:00
|
|
|
from app.models import User
|
2022-06-09 08:19:49 +00:00
|
|
|
from app.utils import random_string
|
2019-08-10 15:03:51 +00:00
|
|
|
|
2022-04-15 14:59:44 +00:00
|
|
|
|
2022-11-30 16:19:55 +00:00
|
|
|
def create_new_user(email: Optional[str] = None, name: Optional[str] = None) -> User:
|
|
|
|
if not email:
|
|
|
|
email = f"user_{random_token(10)}@mailbox.test"
|
|
|
|
if not name:
|
|
|
|
name = f"Test User"
|
2022-04-15 14:59:44 +00:00
|
|
|
# new user has a different email address
|
2019-08-10 15:03:51 +00:00
|
|
|
user = User.create(
|
2022-11-30 16:19:55 +00:00
|
|
|
email=email,
|
2020-12-31 12:59:03 +00:00
|
|
|
password="password",
|
2022-11-30 16:19:55 +00:00
|
|
|
name=name,
|
2020-12-31 12:59:03 +00:00
|
|
|
activated=True,
|
2022-04-15 14:59:44 +00:00
|
|
|
flush=True,
|
2019-08-10 15:03:51 +00:00
|
|
|
)
|
|
|
|
|
2022-04-15 14:59:44 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
2022-11-30 16:19:55 +00:00
|
|
|
def login(flask_client, user: Optional[User] = None) -> User:
|
|
|
|
if not user:
|
|
|
|
user = create_new_user()
|
2022-04-15 14:59:44 +00:00
|
|
|
|
2019-08-10 15:03:51 +00:00
|
|
|
r = flask_client.post(
|
|
|
|
url_for("auth.login"),
|
2022-04-15 14:59:44 +00:00
|
|
|
data={"email": user.email, "password": "password"},
|
2019-08-10 15:03:51 +00:00
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert b"/auth/logout" in r.data
|
|
|
|
|
|
|
|
return user
|
2020-11-15 18:34:13 +00:00
|
|
|
|
|
|
|
|
2022-04-18 09:55:14 +00:00
|
|
|
def random_domain() -> str:
|
|
|
|
return random_token() + ".test"
|
|
|
|
|
|
|
|
|
2022-03-18 14:44:07 +00:00
|
|
|
def random_token(length: int = 10) -> str:
|
|
|
|
return "".join(random.choices(string.ascii_lowercase + string.digits, k=length))
|
|
|
|
|
|
|
|
|
2020-11-15 18:34:13 +00:00
|
|
|
def pretty(d):
|
|
|
|
"""pretty print as json"""
|
|
|
|
print(json.dumps(d, indent=2))
|
2022-03-17 18:03:36 +00:00
|
|
|
|
|
|
|
|
2022-03-18 14:55:59 +00:00
|
|
|
def load_eml_file(
|
|
|
|
filename: str, template_values: Optional[Dict[str, str]] = None
|
|
|
|
) -> EmailMessage:
|
2022-03-17 20:36:25 +00:00
|
|
|
emails_dir = os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)), "example_emls"
|
|
|
|
)
|
2022-03-17 18:03:36 +00:00
|
|
|
fullpath = os.path.join(emails_dir, filename)
|
2022-03-21 11:03:11 +00:00
|
|
|
with open(fullpath) as fd:
|
|
|
|
template = jinja2.Template(fd.read())
|
|
|
|
if not template_values:
|
|
|
|
template_values = {}
|
|
|
|
rendered = template.render(**template_values)
|
|
|
|
return email.message_from_string(rendered)
|
2022-06-09 08:19:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def random_email() -> str:
|
|
|
|
return "{rand}@{rand}.com".format(rand=random_string(20))
|