-
Bjarke Madsen authoredBjarke Madsen authored
test_send_mail.py 2.25 KiB
from typing import List
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from contextlib import contextmanager
from compendium_v2.db.auth_model import User
from compendium_v2.email import send_admin_signup_notification, send_user_signup_notification
class MockedThread(object):
def __init__(self, target, args):
self.target = target
self.args = args
def start(self):
self.target(*self.args)
def decode_message(msg: MIMEMultipart):
message: List[MIMEText] = msg.get_payload()
text: MIMEText = message[0]
decoded = text.get_payload(decode=True).decode('utf-8')
return decoded
@contextmanager
def test_user(app):
with app.app_context():
app.config['MAIL_ENABLE'] = True
app.config['MAIL_SERVER'] = 'localhost'
app.config['MAIL_PORT'] = 54655
app.config['MAIL_SENDER_EMAIL'] = 'fakesender123@test.local'
app.config['MAIL_EXCLUDED_ADMINS'] = []
user = User(fullname='testname', email='testmail321@email.com', oidc_sub='testsub')
yield user
def test_signup_email_admin(app, mocked_admin_user, mocker):
def _send_mail(*args, **kwargs):
_msg = args[-1]
decoded = decode_message(_msg)
_from = _msg['From']
_to = _msg['To']
assert _from == 'fakesender123@test.local'
assert _to == 'testemail123@email.local'
message = 'testname has just signed up with the email testmail321@email.com and provider ID testsub'
assert decoded == message
mocker.patch('compendium_v2.email._send_mail', _send_mail)
mocker.patch('threading.Thread', MockedThread)
with test_user(app) as user:
send_admin_signup_notification(user)
def test_signup_email_user(app, mocker):
def _send_mail(*args, **kwargs):
_msg = args[-1]
_from = _msg['From']
_to = _msg['To']
assert _from == 'fakesender123@test.local'
assert _to == 'testmail321@email.com'
decoded = decode_message(_msg)
assert len(decoded) > 50 # check that there's a message
mocker.patch('compendium_v2.email._send_mail', _send_mail)
mocker.patch('threading.Thread', MockedThread)
with test_user(app) as user:
send_user_signup_notification(user, 'testname')