Refactor send_already_registered_email to persist and send message to the notify queue.

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.

4th task for story: https://www.pivotaltracker.com/story/show/135839709
This commit is contained in:
Rebecca Law
2016-12-19 17:35:13 +00:00
parent ab1326b97e
commit 813947e7e4
2 changed files with 36 additions and 54 deletions

View File

@@ -87,7 +87,6 @@ def update_user_attribute(user_id):
def verify_user_password(user_id): def verify_user_password(user_id):
user_to_verify = get_user_by_id(user_id=user_id) user_to_verify = get_user_by_id(user_id=user_id)
txt_pwd = None
try: try:
txt_pwd = request.get_json()['password'] txt_pwd = request.get_json()['password']
except KeyError: except KeyError:
@@ -227,22 +226,22 @@ def send_already_registered_email(user_id):
to, errors = email_data_request_schema.load(request.get_json()) to, errors = email_data_request_schema.load(request.get_json())
template = dao_get_template_by_id(current_app.config['ALREADY_REGISTERED_EMAIL_TEMPLATE_ID']) template = dao_get_template_by_id(current_app.config['ALREADY_REGISTERED_EMAIL_TEMPLATE_ID'])
message = { saved_notification = persist_notification(
'template': str(template.id), template_id=template.id,
'template_version': template.version, template_version=template.version,
'to': to['email'], recipient=to['email'],
'personalisation': { service_id=current_app.config['NOTIFY_SERVICE_ID'],
personalisation={
'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in', 'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in',
'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password', 'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password',
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback' 'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback'
} },
} 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

@@ -496,50 +496,33 @@ def test_send_user_reset_password_should_return_400_when_data_is_not_email_addre
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Not a valid email address.']} assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Not a valid email address.']}
@freeze_time("2016-01-01 11:09:00.061258") def test_send_already_registered_email(client, sample_user, already_registered_template, mocker):
def test_send_already_registered_email(notify_api, sample_user, already_registered_template, mocker): data = json.dumps({'email': sample_user.email_address})
with notify_api.test_request_context(): auth_header = create_authorization_header()
with notify_api.test_client() as client: mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
data = json.dumps({'email': sample_user.email_address})
auth_header = create_authorization_header()
mocker.patch('app.celery.tasks.send_email.apply_async')
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
resp = client.post( resp = client.post(
url_for('user.send_already_registered_email', user_id=str(sample_user.id)), url_for('user.send_already_registered_email', 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
message = {
'template': str(already_registered_template.id), notification = Notification.query.first()
'template_version': already_registered_template.version, mocked.assert_called_once_with(
'to': sample_user.email_address, ([str(notification.id)]),
'personalisation': { queue="notify")
'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in',
'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password',
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback'
}
}
app.celery.tasks.send_email.apply_async.assert_called_once_with(
(str(current_app.config['NOTIFY_SERVICE_ID']),
'some_uuid',
app.encryption.encrypt(message),
"2016-01-01T11:09:00.061258Z"),
queue="notify")
def test_send_already_registered_email_returns_400_when_data_is_missing(notify_api, sample_user): def test_send_already_registered_email_returns_400_when_data_is_missing(client, sample_user):
with notify_api.test_request_context(): data = json.dumps({})
with notify_api.test_client() as client: auth_header = create_authorization_header()
data = json.dumps({})
auth_header = create_authorization_header()
resp = client.post( resp = client.post(
url_for('user.send_already_registered_email', user_id=str(sample_user.id)), url_for('user.send_already_registered_email', 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 == 400 assert resp.status_code == 400
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']} assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']}
def test_send_user_confirm_new_email_returns_204(client, sample_user, change_email_confirmation_template, mocker): def test_send_user_confirm_new_email_returns_204(client, sample_user, change_email_confirmation_template, mocker):