mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Refactor send_user_reset_password 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. 5th task for story: https://www.pivotaltracker.com/story/show/135839709
This commit is contained in:
@@ -288,19 +288,22 @@ def send_user_reset_password():
|
|||||||
user_to_send_to = get_user_by_email(email['email'])
|
user_to_send_to = get_user_by_email(email['email'])
|
||||||
|
|
||||||
template = dao_get_template_by_id(current_app.config['PASSWORD_RESET_TEMPLATE_ID'])
|
template = dao_get_template_by_id(current_app.config['PASSWORD_RESET_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=email['email'],
|
||||||
|
service_id=current_app.config['NOTIFY_SERVICE_ID'],
|
||||||
|
personalisation={
|
||||||
'user_name': user_to_send_to.name,
|
'user_name': user_to_send_to.name,
|
||||||
'url': _create_reset_password_url(user_to_send_to.email_address)
|
'url': _create_reset_password_url(user_to_send_to.email_address)
|
||||||
}
|
},
|
||||||
}
|
notification_type=EMAIL_TYPE,
|
||||||
send_email.apply_async([current_app.config['NOTIFY_SERVICE_ID'],
|
api_key_id=None,
|
||||||
str(uuid.uuid4()),
|
key_type=KEY_TYPE_NORMAL
|
||||||
encryption.encrypt(message),
|
)
|
||||||
datetime.utcnow().strftime(DATETIME_FORMAT)], queue='notify')
|
|
||||||
|
send_notification_to_queue(saved_notification, False, queue="notify")
|
||||||
|
|
||||||
return jsonify({}), 204
|
return jsonify({}), 204
|
||||||
|
|
||||||
|
|||||||
@@ -414,86 +414,69 @@ def test_set_user_permissions_remove_old(notify_api,
|
|||||||
|
|
||||||
|
|
||||||
@freeze_time("2016-01-01 11:09:00.061258")
|
@freeze_time("2016-01-01 11:09:00.061258")
|
||||||
def test_send_user_reset_password_should_send_reset_password_link(notify_api,
|
def test_send_user_reset_password_should_send_reset_password_link(client,
|
||||||
sample_user,
|
sample_user,
|
||||||
mocker,
|
mocker,
|
||||||
password_reset_email_template):
|
password_reset_email_template):
|
||||||
with notify_api.test_request_context():
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||||
with notify_api.test_client() as client:
|
data = json.dumps({'email': sample_user.email_address})
|
||||||
mocker.patch('notifications_utils.url_safe_token.generate_token', return_value='the-token')
|
auth_header = create_authorization_header()
|
||||||
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
|
resp = client.post(
|
||||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
url_for('user.send_user_reset_password'),
|
||||||
data = json.dumps({'email': sample_user.email_address})
|
data=data,
|
||||||
auth_header = create_authorization_header()
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
resp = client.post(
|
|
||||||
url_for('user.send_user_reset_password'),
|
|
||||||
data=data,
|
|
||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
|
||||||
|
|
||||||
message = {
|
assert resp.status_code == 204
|
||||||
'template': str(password_reset_email_template.id),
|
notification = Notification.query.first()
|
||||||
'template_version': password_reset_email_template.version,
|
mocked.assert_called_once_with([str(notification.id)], queue="notify")
|
||||||
'to': sample_user.email_address,
|
|
||||||
'personalisation': {
|
|
||||||
'user_name': sample_user.name,
|
|
||||||
'url': current_app.config['ADMIN_BASE_URL'] + '/new-password/' + 'the-token'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert resp.status_code == 204
|
|
||||||
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_user_reset_password_should_return_400_when_email_is_missing(notify_api):
|
def test_send_user_reset_password_should_return_400_when_email_is_missing(client, mocker):
|
||||||
with notify_api.test_request_context():
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||||
with notify_api.test_client() as client:
|
data = json.dumps({})
|
||||||
data = json.dumps({})
|
auth_header = create_authorization_header()
|
||||||
auth_header = create_authorization_header()
|
|
||||||
|
|
||||||
resp = client.post(
|
resp = client.post(
|
||||||
url_for('user.send_user_reset_password'),
|
url_for('user.send_user_reset_password'),
|
||||||
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.']}
|
||||||
|
assert mocked.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
def test_send_user_reset_password_should_return_400_when_user_doesnot_exist(notify_api,
|
def test_send_user_reset_password_should_return_400_when_user_doesnot_exist(client,
|
||||||
mocker):
|
mocker):
|
||||||
with notify_api.test_request_context():
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||||
with notify_api.test_client() as client:
|
bad_email_address = 'bad@email.gov.uk'
|
||||||
bad_email_address = 'bad@email.gov.uk'
|
data = json.dumps({'email': bad_email_address})
|
||||||
data = json.dumps({'email': bad_email_address})
|
auth_header = create_authorization_header()
|
||||||
auth_header = create_authorization_header()
|
|
||||||
|
|
||||||
resp = client.post(
|
resp = client.post(
|
||||||
url_for('user.send_user_reset_password'),
|
url_for('user.send_user_reset_password'),
|
||||||
data=data,
|
data=data,
|
||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
|
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
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'
|
||||||
|
assert mocked.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
def test_send_user_reset_password_should_return_400_when_data_is_not_email_address(notify_api, mocker):
|
def test_send_user_reset_password_should_return_400_when_data_is_not_email_address(client, mocker):
|
||||||
with notify_api.test_request_context():
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||||
with notify_api.test_client() as client:
|
bad_email_address = 'bad.email.gov.uk'
|
||||||
bad_email_address = 'bad.email.gov.uk'
|
data = json.dumps({'email': bad_email_address})
|
||||||
data = json.dumps({'email': bad_email_address})
|
auth_header = create_authorization_header()
|
||||||
auth_header = create_authorization_header()
|
|
||||||
|
|
||||||
resp = client.post(
|
resp = client.post(
|
||||||
url_for('user.send_user_reset_password'),
|
url_for('user.send_user_reset_password'),
|
||||||
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': ['Not a valid email address.']}
|
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Not a valid email address.']}
|
||||||
|
assert mocked.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
def test_send_already_registered_email(client, sample_user, already_registered_template, mocker):
|
def test_send_already_registered_email(client, sample_user, already_registered_template, mocker):
|
||||||
@@ -508,9 +491,7 @@ def test_send_already_registered_email(client, sample_user, already_registered_t
|
|||||||
assert resp.status_code == 204
|
assert resp.status_code == 204
|
||||||
|
|
||||||
notification = Notification.query.first()
|
notification = Notification.query.first()
|
||||||
mocked.assert_called_once_with(
|
mocked.assert_called_once_with(([str(notification.id)]), queue="notify")
|
||||||
([str(notification.id)]),
|
|
||||||
queue="notify")
|
|
||||||
|
|
||||||
|
|
||||||
def test_send_already_registered_email_returns_400_when_data_is_missing(client, sample_user):
|
def test_send_already_registered_email_returns_400_when_data_is_missing(client, sample_user):
|
||||||
|
|||||||
@@ -324,15 +324,14 @@ def test_send_user_email_verification(client,
|
|||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
assert resp.status_code == 204
|
assert resp.status_code == 204
|
||||||
notification = Notification.query.first()
|
notification = Notification.query.first()
|
||||||
mocked.assert_called_once_with(
|
mocked.assert_called_once_with(([str(notification.id)]), queue="notify")
|
||||||
([str(notification.id)]),
|
|
||||||
queue="notify")
|
|
||||||
|
|
||||||
|
|
||||||
def test_send_email_verification_returns_404_for_bad_input_data(client, notify_db, notify_db_session):
|
def test_send_email_verification_returns_404_for_bad_input_data(client, notify_db, notify_db_session, mocker):
|
||||||
"""
|
"""
|
||||||
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
|
||||||
"""
|
"""
|
||||||
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||||
data = json.dumps({})
|
data = json.dumps({})
|
||||||
import uuid
|
import uuid
|
||||||
uuid_ = uuid.uuid4()
|
uuid_ = uuid.uuid4()
|
||||||
@@ -343,3 +342,4 @@ def test_send_email_verification_returns_404_for_bad_input_data(client, notify_d
|
|||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
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'
|
||||||
|
assert mocked.call_count == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user