diff --git a/app/celery/tasks.py b/app/celery/tasks.py index a94da9f44..88f23aea3 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -13,7 +13,7 @@ from app.dao.templates_dao import dao_get_template_by_id from app.dao.provider_details_dao import get_provider_details_by_notification_type from app.celery.research_mode_tasks import send_email_response, send_sms_response -from notifications_utils.template import Template, unlink_govuk_escaped +from notifications_utils.template import Template from notifications_utils.recipients import ( RecipientCSV, @@ -370,99 +370,6 @@ def send_email(service_id, notification_id, encrypted_notification, created_at, current_app.logger.exception(e) -# TODO: when placeholders in templates work, this will be a real template -def invitation_template(user_name, service_name, url, expiry_date): - from string import Template - t = Template( - '$user_name has invited you to collaborate on $service_name on GOV.UK Notify.\n\n' - 'GOV.UK Notify makes it easy to keep people updated by helping you send text messages, emails and letters.\n\n' - 'Click this link to create an account on GOV.UK Notify:\n$url\n\n' - 'This invitation will stop working at midnight tomorrow. This is to keep $service_name secure.') - return unlink_govuk_escaped( - t.substitute(user_name=user_name, service_name=service_name, url=url, expiry_date=expiry_date) - ) - - -def invitation_subject_line(user_name, service_name): - from string import Template - t = Template('$user_name has invited you to collaborate on $service_name on GOV.UK Notify') - return unlink_govuk_escaped( - t.substitute(user_name=user_name, service_name=service_name) - ) - - -def invited_user_url(base_url, token): - return '{0}/invitation/{1}'.format(base_url, token) - - -@notify_celery.task(name='email-invited-user') -def email_invited_user(encrypted_invitation): - provider = provider_to_use('email', 'email-invited-user') - - invitation = encryption.decrypt(encrypted_invitation) - url = invited_user_url(current_app.config['ADMIN_BASE_URL'], - invitation['token']) - invitation_content = invitation_template(invitation['user_name'], - invitation['service_name'], - url, - invitation['expiry_date']) - try: - email_from = '"GOV.UK Notify" <{}@{}>'.format( - current_app.config['INVITATION_EMAIL_FROM'], - current_app.config['NOTIFY_EMAIL_DOMAIN'] - ) - subject_line = invitation_subject_line(invitation['user_name'], invitation['service_name']) - provider.send_email(email_from, - invitation['to'], - subject_line, - invitation_content) - except EmailClientException as e: - current_app.logger.exception(e) - - -def password_reset_message(name, url): - from string import Template - t = Template("Hi $user_name,\n\n" - "We received a request to reset your password on GOV.UK Notify.\n\n" - "If you didn't request this email, you can ignore it – your password has not been changed.\n\n" - "To reset your password, click this link:\n\n" - "$url") - return unlink_govuk_escaped( - t.substitute(user_name=name, url=url) - ) - - -@notify_celery.task(name='email-reset-password') -def email_reset_password(encrypted_reset_password_message): - provider = provider_to_use('email', 'email-reset-password') - - reset_password_message = encryption.decrypt(encrypted_reset_password_message) - try: - email_from = '"GOV.UK Notify" <{}>'.format( - current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'] - ) - provider.send_email( - email_from, - reset_password_message['to'], - "Reset your GOV.UK Notify password", - password_reset_message( - name=reset_password_message['name'], - url=reset_password_message['reset_password_url'] - ) - ) - except EmailClientException as e: - current_app.logger.exception(e) - - -def registration_verification_template(name, url): - from string import Template - t = Template("Hi $name,\n\n" - "To complete your registration for GOV.UK Notify please click the link below\n\n $url") - return unlink_govuk_escaped( - t.substitute(name=name, url=url) - ) - - def service_allowed_to_send_to(recipient, service): if not service.restricted: return True diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 01173ccd5..5bfef5377 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -7,8 +7,6 @@ from notifications_utils.recipients import validate_phone_number, format_phone_n from app.celery.tasks import ( send_sms, process_job, - email_invited_user, - email_reset_password, delete_verify_codes, delete_invitations, delete_failed_notifications, @@ -877,55 +875,6 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo assert 'No row was found for one' in str(e.value) -def test_email_invited_user_should_send_email(notify_api, mocker): - with notify_api.test_request_context(): - invitation = {'to': 'new_person@it.gov.uk', - 'user_name': 'John Smith', - 'service_id': '123123', - 'service_name': 'Blacksmith Service', - 'token': 'the-token', - 'expiry_date': str(datetime.utcnow() + timedelta(days=1)) - } - - mocker.patch('app.aws_ses_client.send_email') - mocker.patch('app.encryption.decrypt', return_value=invitation) - url = tasks.invited_user_url(current_app.config['ADMIN_BASE_URL'], invitation['token']) - expected_content = tasks.invitation_template(invitation['user_name'], - invitation['service_name'], - url, - invitation['expiry_date']) - - email_invited_user(encryption.encrypt(invitation)) - email_from = '"GOV.UK Notify" <{}@{}>'.format( - current_app.config['INVITATION_EMAIL_FROM'], - current_app.config['NOTIFY_EMAIL_DOMAIN'] - ) - expected_subject = tasks.invitation_subject_line(invitation['user_name'], invitation['service_name']) - aws_ses_client.send_email.assert_called_once_with(email_from, - invitation['to'], - expected_subject, - expected_content) - - -def test_email_reset_password_should_send_email(notify_db, notify_db_session, notify_api, mocker): - with notify_api.test_request_context(): - reset_password_message = {'to': 'someone@it.gov.uk', - 'name': 'Some One', - 'reset_password_url': 'bah'} - - mocker.patch('app.aws_ses_client.send_email') - mocker.patch('app.encryption.decrypt', return_value=reset_password_message) - - encrypted_message = encryption.encrypt(reset_password_message) - email_reset_password(encrypted_message) - message = tasks.password_reset_message(reset_password_message['name'], - reset_password_message['reset_password_url']) - aws_ses_client.send_email(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'], - reset_password_message['to'], - "Reset password for GOV.UK Notify", - message) - - def test_process_email_job_should_use_reply_to_email_if_present(sample_email_job, mocker, mock_celery_remove_job): mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('email')) mocker.patch('app.celery.tasks.send_email.apply_async')