From c1df61a7d4c74116414c492ff1acac46bb3f84f9 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 18 Feb 2016 09:52:27 +0000 Subject: [PATCH] Use celery to send the email verification code. --- app/celery/tasks.py | 13 ++++++------- app/user/rest.py | 11 +++++------ scripts/run_single_test.sh | 4 ++++ tests/app/celery/test_tasks.py | 14 +++++++------- tests/app/conftest.py | 5 +++++ tests/app/user/test_rest_verify.py | 23 +++++++++-------------- 6 files changed, 36 insertions(+), 34 deletions(-) create mode 100755 scripts/run_single_test.sh diff --git a/app/celery/tasks.py b/app/celery/tasks.py index f5fd07e95..511be190a 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -45,13 +45,12 @@ def send_sms_code(encrypted_notification): @notify_celery.task(name='send-email-code') -def send_email_code(encrypted_notification): - content = encryption.decrypt(encrypted_notification) - +def send_email_code(encrypted_verification_message): + verification_message = encryption.decrypt(encrypted_verification_message) try: - aws_ses_client.send_email(content['from_address'], - content['to_address'], - content['subject'], - content['body']) + aws_ses_client.send_email(verification_message['from_address'], + verification_message['to_address'], + verification_message['subject'], + verification_message['body']) except AwsSesClientException as e: current_app.logger.error(e) diff --git a/app/user/rest.py b/app/user/rest.py index b951b4c4f..734c01214 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -135,17 +135,16 @@ def send_user_code(user_id): create_user_code(user, secret_code, verify_code.get('code_type')) if verify_code.get('code_type') == 'sms': mobile = user.mobile_number if verify_code.get('to', None) is None else verify_code.get('to') - notification = {'to': mobile, 'secret_code': secret_code} - send_sms_code.apply_async([encryption.encrypt(notification)], queue='sms_code') + verification_message = {'to': mobile, 'secret_code': secret_code} + send_sms_code.apply_async([encryption.encrypt(verification_message)], queue='sms_code') elif verify_code.get('code_type') == 'email': email = user.email_address if verify_code.get('to', None) is None else verify_code.get('to') - import json - notification = json.dumps({ + verification_message = { 'to_address': email, 'from_address': current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'], 'subject': 'Verification code', - 'body': secret_code}) - send_email_code.apply_async([encryption.encrypt(notification)], queue='email_code') + 'body': secret_code} + send_email_code.apply_async([encryption.encrypt(verification_message)], queue='email_code') else: abort(500) return jsonify({}), 204 diff --git a/scripts/run_single_test.sh b/scripts/run_single_test.sh new file mode 100755 index 000000000..663e2e098 --- /dev/null +++ b/scripts/run_single_test.sh @@ -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 \ No newline at end of file diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 320f2f19a..8e67fa79b 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -98,17 +98,17 @@ def test_should_log_firetext_client_exception(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', 'subject': 'Verification code', 'body': 11111} - encrypted_notification = encryption.encrypt(notification) + encrypted_verification = encryption.encrypt(verification) 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'], - notification['to_address'], - notification['subject'], - notification['body'])) + aws_ses_client.send_email.assert_called_once_with(verification['from_address'], + verification['to_address'], + verification['subject'], + verification['body']) diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 356d851b1..309f43871 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -212,3 +212,8 @@ def mock_celery_send_sms_code(mocker): @pytest.fixture(scope='function') def mock_celery_send_email_code(mocker): 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") diff --git a/tests/app/user/test_rest_verify.py b/tests/app/user/test_rest_verify.py index 40643a2cc..938e6599d 100644 --- a/tests/app/user/test_rest_verify.py +++ b/tests/app/user/test_rest_verify.py @@ -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, sample_email_code, mock_secret_code, - mock_celery_send_email_code): + mock_celery_send_email_code, + mock_encryption): """ Tests POST endpoint '//code' successful email """ @@ -317,18 +318,16 @@ def test_send_user_code_for_email(notify_api, data=data, headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 204 - notification = {'to_address': sample_email_code.user.email_address, - 'from_address': notify_api.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'], - 'subject': 'Verification code', - 'body': 11111} - encrypted = encryption.encrypt([notification]) - app.celery.tasks.send_email_code.apply_async.assert_called_once_with([encrypted], queue='email_code') + + app.celery.tasks.send_email_code.apply_async.assert_called_once_with(['something_encrypted'], + queue='email_code') def test_send_user_code_for_email_uses_optional_to_field(notify_api, sample_email_code, mock_secret_code, - mock_celery_send_email_code): + mock_celery_send_email_code, + mock_encryption): """ Tests POST endpoint '//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, headers=[('Content-Type', 'application/json'), auth_header]) 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([encrypted], queue='email_code') + app.celery.tasks.send_email_code.apply_async.assert_called_once_with(['something_encrypted'], + queue='email_code') def test_request_verify_code_schema_invalid_code_type(notify_api, notify_db, notify_db_session, sample_user):