mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-10 18:23:01 -04:00
Merge pull request #411 from alphagov/use-notify-to-send-email-verification-links
Use notify to send email verification links
This commit is contained in:
+4
-7
@@ -180,16 +180,10 @@ def process_job(job_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if template.template_type == 'email':
|
if template.template_type == 'email':
|
||||||
from_email = '"{}" <{}@{}>'.format(
|
|
||||||
service.name,
|
|
||||||
service.email_from,
|
|
||||||
current_app.config['NOTIFY_EMAIL_DOMAIN']
|
|
||||||
)
|
|
||||||
|
|
||||||
send_email.apply_async((
|
send_email.apply_async((
|
||||||
str(job.service_id),
|
str(job.service_id),
|
||||||
create_uuid(),
|
create_uuid(),
|
||||||
from_email.encode('ascii', 'ignore').decode('ascii'),
|
'',
|
||||||
encrypted,
|
encrypted,
|
||||||
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
||||||
{'reply_to_addresses': service.reply_to_email_address},
|
{'reply_to_addresses': service.reply_to_email_address},
|
||||||
@@ -343,6 +337,9 @@ def send_email(service_id, notification_id, from_address, encrypted_notification
|
|||||||
(provider.get_name(), str(reference), notification['to']), queue='research-mode'
|
(provider.get_name(), str(reference), notification['to']), queue='research-mode'
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
# First step setting the from_address here rather than the method creating the task
|
||||||
|
from_address = '"{}" <{}@{}>'.format(service.name, service.email_from, current_app.config[
|
||||||
|
'NOTIFY_EMAIL_DOMAIN']) if from_address == "" else from_address
|
||||||
reference = provider.send_email(
|
reference = provider.send_email(
|
||||||
from_address,
|
from_address,
|
||||||
notification['to'],
|
notification['to'],
|
||||||
|
|||||||
@@ -387,7 +387,7 @@ def send_notification(notification_type):
|
|||||||
send_email.apply_async((
|
send_email.apply_async((
|
||||||
service_id,
|
service_id,
|
||||||
notification_id,
|
notification_id,
|
||||||
'"{}" <{}@{}>'.format(service.name, service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
'',
|
||||||
encryption.encrypt(notification),
|
encryption.encrypt(notification),
|
||||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||||
), queue='email')
|
), queue='email')
|
||||||
|
|||||||
+19
-9
@@ -28,8 +28,8 @@ from app.schemas import (
|
|||||||
from app.celery.tasks import (
|
from app.celery.tasks import (
|
||||||
send_sms,
|
send_sms,
|
||||||
email_reset_password,
|
email_reset_password,
|
||||||
email_registration_verification
|
email_registration_verification,
|
||||||
)
|
send_email)
|
||||||
|
|
||||||
from app.errors import register_errors
|
from app.errors import register_errors
|
||||||
|
|
||||||
@@ -157,13 +157,23 @@ def send_user_email_verification(user_id):
|
|||||||
secret_code = create_secret_code()
|
secret_code = create_secret_code()
|
||||||
create_user_code(user_to_send_to, secret_code, 'email')
|
create_user_code(user_to_send_to, secret_code, 'email')
|
||||||
|
|
||||||
email = user_to_send_to.email_address
|
template = dao_get_template_by_id(current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID'])
|
||||||
verification_message = {'to': email,
|
message = {
|
||||||
'name': user_to_send_to.name,
|
'template': str(template.id),
|
||||||
'url': _create_verification_url(user_to_send_to, secret_code)}
|
'template_version': template.version,
|
||||||
|
'to': user_to_send_to.email_address,
|
||||||
email_registration_verification.apply_async([encryption.encrypt(verification_message)],
|
'personalisation': {
|
||||||
queue='email-registration-verification')
|
'name': user_to_send_to.name,
|
||||||
|
'url': _create_verification_url(user_to_send_to, secret_code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
send_email.apply_async((
|
||||||
|
current_app.config['NOTIFY_SERVICE_ID'],
|
||||||
|
str(uuid.uuid4()),
|
||||||
|
'',
|
||||||
|
encryption.encrypt(message),
|
||||||
|
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||||
|
), queue='email-registration-verification')
|
||||||
|
|
||||||
return jsonify({}), 204
|
return jsonify({}), 204
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 0028_fix_reg_template_history
|
||||||
|
Revises: 0027_update_provider_rates
|
||||||
|
Create Date: 2016-06-13 11:04:15.888017
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
revision = '0028_fix_reg_template_history'
|
||||||
|
down_revision = '0027_update_provider_rates'
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
service_id = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553'
|
||||||
|
user_id= '6af522d0-2915-4e52-83a3-3690455a5fe6'
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.get_bind()
|
||||||
|
op.execute("delete from templates_history where name = 'Notify email verification code'")
|
||||||
|
|
||||||
|
template_history_insert = """INSERT INTO templates_history (id, name, template_type, created_at,
|
||||||
|
content, archived, service_id,
|
||||||
|
subject, created_by_id, version)
|
||||||
|
VALUES ('{}', '{}', '{}', '{}', '{}', False, '{}', '{}', '{}', 1)
|
||||||
|
"""
|
||||||
|
email_verification_content = \
|
||||||
|
"""Hi ((name)),\n\nTo complete your registration for GOV.UK Notify please click the link below\n\n((url))"""
|
||||||
|
op.execute(template_history_insert.format('ece42649-22a8-4d06-b87f-d52d5d3f0a27',
|
||||||
|
'Notify email verification code', 'email',
|
||||||
|
datetime.utcnow(), email_verification_content, service_id,
|
||||||
|
'Confirm GOV.UK Notify registration', user_id))
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
### commands auto generated by Alembic - please adjust! ###
|
||||||
|
pass
|
||||||
|
### end Alembic commands ###
|
||||||
@@ -248,11 +248,7 @@ def test_should_process_email_job_if_exactly_on_send_limits(notify_db,
|
|||||||
(
|
(
|
||||||
str(job.service_id),
|
str(job.service_id),
|
||||||
"uuid",
|
"uuid",
|
||||||
"\"{}\" <{}@{}>".format(
|
"",
|
||||||
service.name,
|
|
||||||
service.email_from,
|
|
||||||
"test.notify.com"
|
|
||||||
),
|
|
||||||
"something_encrypted",
|
"something_encrypted",
|
||||||
"2016-01-01T11:09:00.061258"
|
"2016-01-01T11:09:00.061258"
|
||||||
),
|
),
|
||||||
@@ -298,11 +294,7 @@ def test_should_process_email_job(sample_email_job, mocker, mock_celery_remove_j
|
|||||||
(
|
(
|
||||||
str(sample_email_job.service_id),
|
str(sample_email_job.service_id),
|
||||||
"uuid",
|
"uuid",
|
||||||
"\"{}\" <{}@{}>".format(
|
"",
|
||||||
sample_email_job.service.name,
|
|
||||||
sample_email_job.service.email_from,
|
|
||||||
"test.notify.com"
|
|
||||||
),
|
|
||||||
"something_encrypted",
|
"something_encrypted",
|
||||||
"2016-01-01T11:09:00.061258"
|
"2016-01-01T11:09:00.061258"
|
||||||
),
|
),
|
||||||
@@ -960,11 +952,7 @@ def test_process_email_job_should_use_reply_to_email_if_present(sample_email_job
|
|||||||
(
|
(
|
||||||
str(sample_email_job.service_id),
|
str(sample_email_job.service_id),
|
||||||
"uuid",
|
"uuid",
|
||||||
"\"{}\" <{}@{}>".format(
|
"",
|
||||||
sample_email_job.service.name,
|
|
||||||
sample_email_job.service.email_from,
|
|
||||||
"test.notify.com"
|
|
||||||
),
|
|
||||||
"something_encrypted",
|
"something_encrypted",
|
||||||
ANY
|
ANY
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -589,3 +589,37 @@ def sms_code_template(notify_db,
|
|||||||
template = Template(**data)
|
template = Template(**data)
|
||||||
db.session.add(template)
|
db.session.add(template)
|
||||||
return template
|
return template
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def email_verification_template(notify_db,
|
||||||
|
notify_db_session):
|
||||||
|
user = sample_user(notify_db, notify_db_session)
|
||||||
|
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
||||||
|
if not service:
|
||||||
|
data = {
|
||||||
|
'id': current_app.config['NOTIFY_SERVICE_ID'],
|
||||||
|
'name': 'Notify Service',
|
||||||
|
'message_limit': 1000,
|
||||||
|
'active': True,
|
||||||
|
'restricted': False,
|
||||||
|
'email_from': 'notify.service',
|
||||||
|
'created_by': user
|
||||||
|
}
|
||||||
|
service = Service(**data)
|
||||||
|
db.session.add(service)
|
||||||
|
|
||||||
|
template = Template.query.get(current_app.config['SMS_CODE_TEMPLATE_ID'])
|
||||||
|
if not template:
|
||||||
|
data = {
|
||||||
|
'id': current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID'],
|
||||||
|
'name': 'Email verification template',
|
||||||
|
'template_type': 'email',
|
||||||
|
'content': '((user_name)) use ((url)) to complete registration',
|
||||||
|
'service': service,
|
||||||
|
'created_by': user,
|
||||||
|
'archived': False
|
||||||
|
}
|
||||||
|
template = Template(**data)
|
||||||
|
db.session.add(template)
|
||||||
|
return template
|
||||||
|
|||||||
@@ -946,7 +946,7 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
|
|||||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||||
(str(sample_email_template.service_id),
|
(str(sample_email_template.service_id),
|
||||||
notification_id,
|
notification_id,
|
||||||
"\"Sample service\" <sample.service@test.notify.com>",
|
"",
|
||||||
"something_encrypted",
|
"something_encrypted",
|
||||||
"2016-01-01T11:09:00.061258"),
|
"2016-01-01T11:09:00.061258"),
|
||||||
queue="email"
|
queue="email"
|
||||||
|
|||||||
@@ -309,18 +309,47 @@ def test_send_sms_code_returns_404_for_bad_input_data(notify_api, notify_db, not
|
|||||||
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
|
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2016-01-01 11:09:00.061258")
|
||||||
def test_send_user_email_verification(notify_api,
|
def test_send_user_email_verification(notify_api,
|
||||||
sample_email_code,
|
sample_user,
|
||||||
mock_celery_email_registration_verification,
|
mocker,
|
||||||
mock_encryption):
|
email_verification_template):
|
||||||
|
|
||||||
with notify_api.test_request_context():
|
with notify_api.test_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
data = json.dumps({})
|
data = json.dumps({})
|
||||||
|
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
|
||||||
|
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||||
|
mocked = mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
resp = client.post(
|
resp = client.post(
|
||||||
url_for('user.send_user_email_verification', user_id=str(sample_email_code.user.id)),
|
url_for('user.send_user_email_verification', user_id=str(sample_user.id)),
|
||||||
data=data,
|
data=data,
|
||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
assert resp.status_code == 204
|
assert resp.status_code == 204
|
||||||
app.celery.tasks.email_registration_verification.apply_async.assert_called_once_with(['something_encrypted'], queue='email-registration-verification') # noqa
|
assert mocked.call_count == 1
|
||||||
|
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||||
|
(str(current_app.config['NOTIFY_SERVICE_ID']),
|
||||||
|
'some_uuid',
|
||||||
|
'',
|
||||||
|
"something_encrypted",
|
||||||
|
"2016-01-01T11:09:00.061258"),
|
||||||
|
queue="email-registration-verification")
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_email_verification_returns_404_for_bad_input_data(notify_api, notify_db, notify_db_session):
|
||||||
|
"""
|
||||||
|
Tests POST endpoint /user/<user_id>/sms-code return 404 for bad input data
|
||||||
|
"""
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
data = json.dumps({})
|
||||||
|
import uuid
|
||||||
|
uuid_ = uuid.uuid4()
|
||||||
|
auth_header = create_authorization_header()
|
||||||
|
resp = client.post(
|
||||||
|
url_for('user.send_user_email_verification', user_id=uuid_),
|
||||||
|
data=data,
|
||||||
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
|
assert resp.status_code == 404
|
||||||
|
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
|
||||||
|
|||||||
Reference in New Issue
Block a user