mirror of
https://github.com/simple-login/app.git
synced 2024-11-14 08:01:13 +01:00
69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
import email
|
|
import json
|
|
import os
|
|
import random
|
|
import string
|
|
from email.message import EmailMessage
|
|
from typing import Optional, Dict
|
|
|
|
import jinja2
|
|
from flask import url_for
|
|
|
|
from app.models import User
|
|
|
|
|
|
def login(flask_client) -> User:
|
|
# create user, user is activated
|
|
user = User.create(
|
|
email="a@b.c",
|
|
password="password",
|
|
name="Test User",
|
|
activated=True,
|
|
commit=True,
|
|
)
|
|
|
|
r = flask_client.post(
|
|
url_for("auth.login"),
|
|
data={"email": "a@b.c", "password": "password"},
|
|
follow_redirects=True,
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
assert b"/auth/logout" in r.data
|
|
|
|
return user
|
|
|
|
|
|
def random_token(length: int = 10) -> str:
|
|
return "".join(random.choices(string.ascii_lowercase + string.digits, k=length))
|
|
|
|
|
|
def create_random_user() -> User:
|
|
random_email = "{}@{}.com".format(random_token(), random_token())
|
|
return User.create(
|
|
email=random_email,
|
|
password="password",
|
|
name="Test {}".format(random_token()),
|
|
activated=True,
|
|
commit=True,
|
|
)
|
|
|
|
|
|
def pretty(d):
|
|
"""pretty print as json"""
|
|
print(json.dumps(d, indent=2))
|
|
|
|
|
|
def load_eml_file(
|
|
filename: str, template_values: Optional[Dict[str, str]] = None
|
|
) -> EmailMessage:
|
|
emails_dir = os.path.join(
|
|
os.path.dirname(os.path.realpath(__file__)), "example_emls"
|
|
)
|
|
fullpath = os.path.join(emails_dir, filename)
|
|
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)
|