Skip to content
Snippets Groups Projects
Commit 48f9c191 authored by Remco Tukker's avatar Remco Tukker
Browse files

Merge branch 'send-user-signup-email' into 'develop'

Send email to user when they sign up

See merge request !72
parents 7df83420 69c88c1a
No related branches found
No related tags found
1 merge request!72Send email to user when they sign up
...@@ -5,7 +5,7 @@ from datetime import datetime ...@@ -5,7 +5,7 @@ from datetime import datetime
from flask_login import LoginManager, current_user # type: ignore from flask_login import LoginManager, current_user # type: ignore
from compendium_v2.db import session_scope from compendium_v2.db import session_scope
from compendium_v2.db.auth_model import User, ROLES 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): def admin_required(func):
...@@ -33,20 +33,22 @@ def admin_required(func): ...@@ -33,20 +33,22 @@ def admin_required(func):
return wraps(func)(wrapper) return wraps(func)(wrapper)
def create_user(email: str, fullname: str, oidc_sub: str): def create_user(email: str, fullname: str, oidc_sub: str, given_name: str):
""" """
Function used to create a new user in the database. Function used to create a new user in the database.
:param email: The email of the user :param email: The email of the user
:param fullname: The full name of the user :param fullname: The full name of the user
:param oidc_sub: The OIDC subject identifier (ID) of the user :param oidc_sub: The OIDC subject identifier (ID) of the user
:param given_name: The given name of the user
:return: The user object :return: The user object
""" """
with session_scope() as session: with session_scope() as session:
user = User(email=email, fullname=fullname, oidc_sub=oidc_sub) user = User(email=email, fullname=fullname, oidc_sub=oidc_sub)
session.add(user) 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, given_name)
return user return user
......
...@@ -10,6 +10,18 @@ from compendium_v2.db.auth_model import User, ROLES ...@@ -10,6 +10,18 @@ from compendium_v2.db.auth_model import User, ROLES
logger = logging.getLogger(__name__) 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): def _send_mail(smtp_server, port, sender_email, recipients, message):
try: try:
...@@ -22,7 +34,7 @@ def _send_mail(smtp_server, port, sender_email, recipients, message): ...@@ -22,7 +34,7 @@ def _send_mail(smtp_server, port, sender_email, recipients, message):
def send_mail( def send_mail(
contents: str, contents: str,
subject: str = 'New user signed up for Compendium', subject: str,
recipients: Union[str, Sequence[str]] = '' recipients: Union[str, Sequence[str]] = ''
): ):
if not current_app.config['MAIL_ENABLE']: if not current_app.config['MAIL_ENABLE']:
...@@ -52,3 +64,20 @@ def send_mail( ...@@ -52,3 +64,20 @@ def send_mail(
logger.debug('Sending email') logger.debug('Sending email')
thread = threading.Thread(target=_send_mail, args=(smtp_server, port, sender_email, recipients, message)) thread = threading.Thread(target=_send_mail, args=(smtp_server, port, sender_email, recipients, message))
thread.start() 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, given_name: str):
email = user.email
name = f' {given_name}' if given_name else ''
contents = USER_NOTIFICATION_TEMPLATE.format(name=name)
send_mail(contents=contents, subject='You have signed up for the Compendium Survey', recipients=email)
...@@ -36,7 +36,7 @@ def authorize(): ...@@ -36,7 +36,7 @@ def authorize():
user = fetch_user(profile) user = fetch_user(profile)
if user is None: if user is None:
# create a new user # create a new user
user = create_user(profile['email'], profile['name'], profile['sub']) user = create_user(profile['email'], profile['name'], profile['sub'], profile['given_name'])
login_user(user) login_user(user)
# redirect to / # redirect to /
......
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): @contextmanager
def test_user(app):
def _send_mail(*args, **kwargs):
pass
mocker.patch('compendium_v2.email._send_mail', _send_mail)
with app.app_context(): with app.app_context():
app.config['MAIL_ENABLE'] = True app.config['MAIL_ENABLE'] = True
app.config['MAIL_SERVER'] = 'localhost' app.config['MAIL_SERVER'] = 'localhost'
app.config['MAIL_PORT'] = 54655 app.config['MAIL_PORT'] = 54655
app.config['MAIL_SENDER_EMAIL'] = 'fakesender123@test.local' app.config['MAIL_SENDER_EMAIL'] = 'fakesender123@test.local'
app.config['MAIL_EXCLUDED_ADMINS'] = [] 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, 'testname')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment