mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Use celery to send the email verification code.
This commit is contained in:
@@ -45,13 +45,12 @@ def send_sms_code(encrypted_notification):
|
|||||||
|
|
||||||
|
|
||||||
@notify_celery.task(name='send-email-code')
|
@notify_celery.task(name='send-email-code')
|
||||||
def send_email_code(encrypted_notification):
|
def send_email_code(encrypted_verification_message):
|
||||||
content = encryption.decrypt(encrypted_notification)
|
verification_message = encryption.decrypt(encrypted_verification_message)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
aws_ses_client.send_email(content['from_address'],
|
aws_ses_client.send_email(verification_message['from_address'],
|
||||||
content['to_address'],
|
verification_message['to_address'],
|
||||||
content['subject'],
|
verification_message['subject'],
|
||||||
content['body'])
|
verification_message['body'])
|
||||||
except AwsSesClientException as e:
|
except AwsSesClientException as e:
|
||||||
current_app.logger.error(e)
|
current_app.logger.error(e)
|
||||||
|
|||||||
@@ -135,17 +135,16 @@ def send_user_code(user_id):
|
|||||||
create_user_code(user, secret_code, verify_code.get('code_type'))
|
create_user_code(user, secret_code, verify_code.get('code_type'))
|
||||||
if verify_code.get('code_type') == 'sms':
|
if verify_code.get('code_type') == 'sms':
|
||||||
mobile = user.mobile_number if verify_code.get('to', None) is None else verify_code.get('to')
|
mobile = user.mobile_number if verify_code.get('to', None) is None else verify_code.get('to')
|
||||||
notification = {'to': mobile, 'secret_code': secret_code}
|
verification_message = {'to': mobile, 'secret_code': secret_code}
|
||||||
send_sms_code.apply_async([encryption.encrypt(notification)], queue='sms_code')
|
send_sms_code.apply_async([encryption.encrypt(verification_message)], queue='sms_code')
|
||||||
elif verify_code.get('code_type') == 'email':
|
elif verify_code.get('code_type') == 'email':
|
||||||
email = user.email_address if verify_code.get('to', None) is None else verify_code.get('to')
|
email = user.email_address if verify_code.get('to', None) is None else verify_code.get('to')
|
||||||
import json
|
verification_message = {
|
||||||
notification = json.dumps({
|
|
||||||
'to_address': email,
|
'to_address': email,
|
||||||
'from_address': current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
|
'from_address': current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
|
||||||
'subject': 'Verification code',
|
'subject': 'Verification code',
|
||||||
'body': secret_code})
|
'body': secret_code}
|
||||||
send_email_code.apply_async([encryption.encrypt(notification)], queue='email_code')
|
send_email_code.apply_async([encryption.encrypt(verification_message)], queue='email_code')
|
||||||
else:
|
else:
|
||||||
abort(500)
|
abort(500)
|
||||||
return jsonify({}), 204
|
return jsonify({}), 204
|
||||||
|
|||||||
4
scripts/run_single_test.sh
Executable file
4
scripts/run_single_test.sh
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# run a single unit test, pass in the unit test name for example: tests/app/service/test_rest.py::test_get_template_list
|
||||||
|
source environment_test.sh
|
||||||
|
py.test -s $1
|
||||||
@@ -98,17 +98,17 @@ def test_should_log_firetext_client_exception(mocker):
|
|||||||
|
|
||||||
|
|
||||||
def test_should_send_email_code(mocker):
|
def test_should_send_email_code(mocker):
|
||||||
notification = {'to_address': 'someone@it.gov.uk',
|
verification = {'to_address': 'someone@it.gov.uk',
|
||||||
'from_address': 'no-reply@notify.gov.uk',
|
'from_address': 'no-reply@notify.gov.uk',
|
||||||
'subject': 'Verification code',
|
'subject': 'Verification code',
|
||||||
'body': 11111}
|
'body': 11111}
|
||||||
|
|
||||||
encrypted_notification = encryption.encrypt(notification)
|
encrypted_verification = encryption.encrypt(verification)
|
||||||
mocker.patch('app.aws_ses_client.send_email')
|
mocker.patch('app.aws_ses_client.send_email')
|
||||||
|
|
||||||
send_email_code(encrypted_notification)
|
send_email_code(encrypted_verification)
|
||||||
|
|
||||||
aws_ses_client.send_email.assert_called_once_with((notification['from_address'],
|
aws_ses_client.send_email.assert_called_once_with(verification['from_address'],
|
||||||
notification['to_address'],
|
verification['to_address'],
|
||||||
notification['subject'],
|
verification['subject'],
|
||||||
notification['body']))
|
verification['body'])
|
||||||
|
|||||||
@@ -212,3 +212,8 @@ def mock_celery_send_sms_code(mocker):
|
|||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
def mock_celery_send_email_code(mocker):
|
def mock_celery_send_email_code(mocker):
|
||||||
return mocker.patch('app.celery.tasks.send_email_code.apply_async')
|
return mocker.patch('app.celery.tasks.send_email_code.apply_async')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def mock_encryption(mocker):
|
||||||
|
return mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||||
|
|||||||
@@ -301,7 +301,8 @@ def test_send_user_code_for_sms_with_optional_to_field(notify_api,
|
|||||||
def test_send_user_code_for_email(notify_api,
|
def test_send_user_code_for_email(notify_api,
|
||||||
sample_email_code,
|
sample_email_code,
|
||||||
mock_secret_code,
|
mock_secret_code,
|
||||||
mock_celery_send_email_code):
|
mock_celery_send_email_code,
|
||||||
|
mock_encryption):
|
||||||
"""
|
"""
|
||||||
Tests POST endpoint '/<user_id>/code' successful email
|
Tests POST endpoint '/<user_id>/code' successful email
|
||||||
"""
|
"""
|
||||||
@@ -317,18 +318,16 @@ def test_send_user_code_for_email(notify_api,
|
|||||||
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
|
||||||
notification = {'to_address': sample_email_code.user.email_address,
|
|
||||||
'from_address': notify_api.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
|
app.celery.tasks.send_email_code.apply_async.assert_called_once_with(['something_encrypted'],
|
||||||
'subject': 'Verification code',
|
queue='email_code')
|
||||||
'body': 11111}
|
|
||||||
encrypted = encryption.encrypt([notification])
|
|
||||||
app.celery.tasks.send_email_code.apply_async.assert_called_once_with([encrypted], queue='email_code')
|
|
||||||
|
|
||||||
|
|
||||||
def test_send_user_code_for_email_uses_optional_to_field(notify_api,
|
def test_send_user_code_for_email_uses_optional_to_field(notify_api,
|
||||||
sample_email_code,
|
sample_email_code,
|
||||||
mock_secret_code,
|
mock_secret_code,
|
||||||
mock_celery_send_email_code):
|
mock_celery_send_email_code,
|
||||||
|
mock_encryption):
|
||||||
"""
|
"""
|
||||||
Tests POST endpoint '/<user_id>/code' successful email with included in body
|
Tests POST endpoint '/<user_id>/code' successful email with included in body
|
||||||
"""
|
"""
|
||||||
@@ -344,13 +343,9 @@ def test_send_user_code_for_email_uses_optional_to_field(notify_api,
|
|||||||
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
|
||||||
notification = json.dumps({'to_address': 'different@email.gov.uk',
|
|
||||||
'from_address': 'no-reply@notify.works',
|
|
||||||
'subject': 'Verification code',
|
|
||||||
'body': '11111'})
|
|
||||||
|
|
||||||
encrypted = encryption.encrypt([notification])
|
app.celery.tasks.send_email_code.apply_async.assert_called_once_with(['something_encrypted'],
|
||||||
app.celery.tasks.send_email_code.apply_async.assert_called_once_with([encrypted], queue='email_code')
|
queue='email_code')
|
||||||
|
|
||||||
|
|
||||||
def test_request_verify_code_schema_invalid_code_type(notify_api, notify_db, notify_db_session, sample_user):
|
def test_request_verify_code_schema_invalid_code_type(notify_api, notify_db, notify_db_session, sample_user):
|
||||||
|
|||||||
Reference in New Issue
Block a user