From 7732e17c1d9474de58f6148fd830904baa46a562 Mon Sep 17 00:00:00 2001 From: Bjarke Madsen <bjarke@nordu.net> Date: Thu, 24 Aug 2023 17:36:57 +0200 Subject: [PATCH] Send email to user when they sign up --- compendium_v2/auth/session_management.py | 5 ++-- compendium_v2/email/__init__.py | 32 ++++++++++++++++++++- test/test_send_mail.py | 36 ++++++++++++++++++------ 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/compendium_v2/auth/session_management.py b/compendium_v2/auth/session_management.py index 4c713ede..5c6b016b 100644 --- a/compendium_v2/auth/session_management.py +++ b/compendium_v2/auth/session_management.py @@ -5,7 +5,7 @@ from datetime import datetime from flask_login import LoginManager, current_user # type: ignore from compendium_v2.db import session_scope from compendium_v2.db.auth_model import User, ROLES -from compendium_v2.email import send_mail +from compendium_v2.email import send_admin_signup_notification, send_user_signup_notification def admin_required(func): @@ -46,7 +46,8 @@ def create_user(email: str, fullname: str, oidc_sub: str): with session_scope() as session: user = User(email=email, fullname=fullname, oidc_sub=oidc_sub) session.add(user) - send_mail(f'{fullname} has just signed up with the email {email} and provider ID {oidc_sub}') + send_admin_signup_notification(user) + send_user_signup_notification(user) return user diff --git a/compendium_v2/email/__init__.py b/compendium_v2/email/__init__.py index 85008856..7edf05ca 100644 --- a/compendium_v2/email/__init__.py +++ b/compendium_v2/email/__init__.py @@ -10,6 +10,18 @@ from compendium_v2.db.auth_model import User, ROLES logger = logging.getLogger(__name__) +USER_NOTIFICATION_TEMPLATE = """Hello{name}, + +Thank you for registering for the new GÉANT Compendium Survey site. + +The Compendium team have been notified and will assign you to your NREN shortly. If you are a long-time contributor this should be fairly quick (same working day). For new users, we will have to check with your colleagues first to make sure you are authorised to answer on behalf of your NREN. + +Best regards, + +Daniel and Jennifer (the Compendium admins) +""" # noqa: E501 + + def _send_mail(smtp_server, port, sender_email, recipients, message): try: @@ -22,7 +34,7 @@ def _send_mail(smtp_server, port, sender_email, recipients, message): def send_mail( contents: str, - subject: str = 'New user signed up for Compendium', + subject: str, recipients: Union[str, Sequence[str]] = '' ): if not current_app.config['MAIL_ENABLE']: @@ -52,3 +64,21 @@ def send_mail( logger.debug('Sending email') thread = threading.Thread(target=_send_mail, args=(smtp_server, port, sender_email, recipients, message)) thread.start() + + +def send_admin_signup_notification(user: User): + fullname = user.fullname + email = user.email + oidc_sub = user.oidc_sub + + contents = f"""{fullname} has just signed up with the email {email} and provider ID {oidc_sub}""" + send_mail(contents=contents, subject='New user signed up for Compendium') + + +def send_user_signup_notification(user: User): + fullname = user.fullname + email = user.email + + first_name = ' ' + fullname.split()[0] if fullname else '' + contents = USER_NOTIFICATION_TEMPLATE.format(name=first_name) + send_mail(contents=contents, subject='You have signed up for the Compendium Survey', recipients=email) diff --git a/test/test_send_mail.py b/test/test_send_mail.py index a1725367..d68b067d 100644 --- a/test/test_send_mail.py +++ b/test/test_send_mail.py @@ -1,16 +1,36 @@ -from compendium_v2.email import send_mail +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 -def test_email(app, mocked_admin_user, mocker): - - def _send_mail(*args, **kwargs): - pass - - mocker.patch('compendium_v2.email._send_mail', _send_mail) +@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'] = [] - send_mail('testmail321') + 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): + message = 'testname has just signed up with the email testmail321@email.com and provider ID testsub' + assert args[-1].split('\n\n')[-1] == message + + mocker.patch('compendium_v2.email._send_mail', _send_mail) + with test_user(app) as user: + send_admin_signup_notification(user) + + +def test_signup_email_user(app, mocker): + + def _send_mail(*args, **kwargs): + assert len(args[-1].split('\n\n', 1)[-1]) > 50 # check that there's a message + + mocker.patch('compendium_v2.email._send_mail', _send_mail) + with test_user(app) as user: + send_user_signup_notification(user) -- GitLab