Refactor send_user_email_verification to persist the notification then put on the "notify" queue for delivery.

The reason for doing this is to ensure the tasks performed for the Notify users are not queued behind a large job, a way to
ensure priority for messages.
This commit is contained in:
Rebecca Law
2016-12-19 15:31:54 +00:00
parent fb114f4c80
commit 741cbd1741
2 changed files with 39 additions and 58 deletions

View File

@@ -18,7 +18,7 @@ from app.dao.users_dao import (
from app.dao.permissions_dao import permission_dao from app.dao.permissions_dao import permission_dao
from app.dao.services_dao import dao_fetch_service_by_id from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id from app.dao.templates_dao import dao_get_template_by_id
from app.models import SMS_TYPE, KEY_TYPE_NORMAL from app.models import SMS_TYPE, KEY_TYPE_NORMAL, EMAIL_TYPE
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue send_notification_to_queue
@@ -203,21 +203,22 @@ def send_user_email_verification(user_id):
create_user_code(user_to_send_to, secret_code, 'email') create_user_code(user_to_send_to, secret_code, 'email')
template = dao_get_template_by_id(current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID']) template = dao_get_template_by_id(current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID'])
message = {
'template': str(template.id), saved_notification = persist_notification(
'template_version': template.version, template_id=template.id,
'to': user_to_send_to.email_address, template_version=template.version,
'personalisation': { recipient=user_to_send_to.email_address,
service_id=current_app.config['NOTIFY_SERVICE_ID'],
personalisation={
'name': user_to_send_to.name, 'name': user_to_send_to.name,
'url': _create_verification_url(user_to_send_to, secret_code) 'url': _create_verification_url(user_to_send_to, secret_code)
} },
} notification_type=EMAIL_TYPE,
send_email.apply_async(( api_key_id=None,
current_app.config['NOTIFY_SERVICE_ID'], key_type=KEY_TYPE_NORMAL
str(uuid.uuid4()), )
encryption.encrypt(message),
datetime.utcnow().strftime(DATETIME_FORMAT) send_notification_to_queue(saved_notification, False, queue="notify")
), queue='notify')
return jsonify({}), 204 return jsonify({}), 204

View File

@@ -311,55 +311,35 @@ 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(client,
def test_send_user_email_verification(notify_api,
sample_user, sample_user,
mocker, mocker,
email_verification_template): email_verification_template):
data = json.dumps({})
with notify_api.test_request_context(): mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
with notify_api.test_client() as client: auth_header = create_authorization_header()
data = json.dumps({}) resp = client.post(
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id url_for('user.send_user_email_verification', user_id=str(sample_user.id)),
mocked = mocker.patch('app.celery.tasks.send_email.apply_async') data=data,
mocker.patch('notifications_utils.url_safe_token.generate_token', return_value='the-token') headers=[('Content-Type', 'application/json'), auth_header])
auth_header = create_authorization_header() assert resp.status_code == 204
resp = client.post( notification = Notification.query.first()
url_for('user.send_user_email_verification', user_id=str(sample_user.id)), mocked.assert_called_once_with(
data=data, ([str(notification.id)]),
headers=[('Content-Type', 'application/json'), auth_header]) queue="notify")
assert resp.status_code == 204
assert mocked.call_count == 1
message = {
'template': str(email_verification_template.id),
'template_version': email_verification_template.version,
'to': sample_user.email_address,
'personalisation': {
'name': sample_user.name,
'url': current_app.config['ADMIN_BASE_URL'] + '/verify-email/' + 'the-token'
}
}
app.celery.tasks.send_email.apply_async.assert_called_once_with(
(str(current_app.config['NOTIFY_SERVICE_ID']),
'some_uuid',
encryption.encrypt(message),
"2016-01-01T11:09:00.061258Z"),
queue="notify")
def test_send_email_verification_returns_404_for_bad_input_data(notify_api, notify_db, notify_db_session): def test_send_email_verification_returns_404_for_bad_input_data(client, notify_db, notify_db_session):
""" """
Tests POST endpoint /user/<user_id>/sms-code return 404 for bad input data Tests POST endpoint /user/<user_id>/sms-code return 404 for bad input data
""" """
with notify_api.test_request_context(): data = json.dumps({})
with notify_api.test_client() as client: import uuid
data = json.dumps({}) uuid_ = uuid.uuid4()
import uuid auth_header = create_authorization_header()
uuid_ = uuid.uuid4() resp = client.post(
auth_header = create_authorization_header() url_for('user.send_user_email_verification', user_id=uuid_),
resp = client.post( data=data,
url_for('user.send_user_email_verification', user_id=uuid_), headers=[('Content-Type', 'application/json'), auth_header])
data=data, assert resp.status_code == 404
headers=[('Content-Type', 'application/json'), auth_header]) assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
assert resp.status_code == 404
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'