Merge pull request #83 from alphagov/celery-send-email-code

Celery send email code
This commit is contained in:
minglis
2016-02-18 11:03:23 +00:00
13 changed files with 140 additions and 33 deletions

View File

@@ -27,6 +27,8 @@ export AWS_REGION='eu-west-1'
export DANGEROUS_SALT='dev-notify-salt'
export DELIVERY_CLIENT_USER_NAME='dev-notify-delivery'
export DELIVERY_CLIENT_SECRET='dev-notify-secret-key'
export FIRETEXT_API_KEY="secret-fire-text"
export FIRETEXT_NUMBER="Firetext"
export NOTIFY_JOB_QUEUE='[unique-to-environment]-notify-jobs-queue' # NOTE unique prefix
export NOTIFICATION_QUEUE_PREFIX='[unique-to-environment]-notification_development' # NOTE unique prefix
export SECRET_KEY='dev-notify-secret-key'

View File

@@ -9,6 +9,7 @@ from utils import logging
from app.celery.celery import NotifyCelery
from app.clients.sms.twilio import TwilioClient
from app.clients.sms.firetext import FiretextClient
from app.clients.email.aws_ses import AwsSesClient
from app.encryption import Encryption
db = SQLAlchemy()
@@ -16,6 +17,7 @@ ma = Marshmallow()
notify_celery = NotifyCelery()
twilio_client = TwilioClient()
firetext_client = FiretextClient()
aws_ses_client = AwsSesClient()
encryption = Encryption()
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
@@ -33,6 +35,7 @@ def create_app():
logging.init_app(application)
twilio_client.init_app(application)
firetext_client.init_app(application)
aws_ses_client.init_app(application.config['AWS_REGION'])
notify_celery.init_app(application)
encryption.init_app(application)

View File

@@ -1,4 +1,5 @@
from app import notify_celery, encryption, firetext_client
from app import notify_celery, encryption, firetext_client, aws_ses_client
from app.clients.email.aws_ses import AwsSesClientException
from app.clients.sms.firetext import FiretextClientException
from app.dao.templates_dao import get_model_templates
from app.dao.notifications_dao import save_notification
@@ -35,8 +36,19 @@ def send_sms(service_id, notification_id, encrypted_notification):
@notify_celery.task(name='send-sms-code')
def send_sms_code(encrypted_verification):
verification_message = encryption.decrypt(encrypted_verification)
try:
firetext_client.send_sms(verification_message['to'], verification_message['secret_code'])
except FiretextClientException as e:
current_app.logger.debug(e)
current_app.logger.error(e)
@notify_celery.task(name='send-email-code')
def send_email_code(encrypted_verification_message):
verification_message = encryption.decrypt(encrypted_verification_message)
try:
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)

View File

@@ -0,0 +1,17 @@
from app.clients import ClientException, Client
class EmailClientException(ClientException):
'''
Base Exception for EmailClients
'''
pass
class EmailClient(Client):
'''
Base Email client for sending emails.
'''
def send_email(self, *args, **kwargs):
raise NotImplemented('TODO Need to implement.')

View File

@@ -0,0 +1,52 @@
import boto3
from app.clients.email import (EmailClientException, EmailClient)
class AwsSesClientException(EmailClientException):
pass
class AwsSesClient(EmailClient):
'''
Amazon SES email client.
'''
def init_app(self, region, *args, **kwargs):
self._client = boto3.client('ses', region_name=region)
super(AwsSesClient, self).__init__(*args, **kwargs)
def send_email(self,
source,
to_addresses,
subject,
body,
reply_to_addresses=None):
try:
if isinstance(to_addresses, str):
to_addresses = [to_addresses]
if reply_to_addresses and isinstance(reply_to_addresses, str):
reply_to_addresses = [reply_to_addresses]
elif reply_to_addresses is None:
reply_to_addresses = []
response = self._client.send_email(
Source=source,
Destination={
'ToAddresses': to_addresses,
'CcAddresses': [],
'BccAddresses': []
},
Message={
'Subject': {
'Data': subject,
},
'Body': {
'Text': {
'Data': body}}
},
ReplyToAddresses=reply_to_addresses)
return response['MessageId']
except Exception as e:
# TODO logging exceptions
raise AwsSesClientException(str(e))

View File

@@ -40,7 +40,7 @@ class FiretextClient(SmsClient):
response.raise_for_status()
except RequestException as e:
api_error = HTTPError.create(e)
print(
logger.error(
"API {} request on {} failed with {} '{}'".format(
"POST",
"https://www.firetext.co.uk/api/sendsms",

View File

@@ -5,7 +5,6 @@ from sqlalchemy.orm.exc import NoResultFound
from app import encryption
from app.dao.services_dao import get_model_services
from app.aws_sqs import add_notification_to_queue
from app.dao.users_dao import (
get_model_users,
save_model_user,
@@ -19,8 +18,7 @@ from app.dao.users_dao import (
from app.schemas import (
user_schema, users_schema, service_schema, services_schema,
request_verify_code_schema, user_schema_load_json)
from app import api_user
from app.celery.tasks import send_sms_code
from app.celery.tasks import (send_sms_code, send_email_code)
user = Blueprint('user', __name__)
@@ -149,7 +147,7 @@ def send_user_code(user_id):
'from_address': current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
'subject': 'Verification code',
'body': secret_code}
add_notification_to_queue(api_user['client'], 'admin', 'email', verification_message)
send_email_code.apply_async([encryption.encrypt(verification_message)], queue='email_code')
else:
abort(500)
return jsonify({}), 204

View File

@@ -3,4 +3,4 @@
set -e
source environment.sh
celery -A run_celery.notify_celery worker --loglevel=INFO --logfile=/var/log/notify/application.log --concurrency=4 -Q sms, sms-code
celery -A run_celery.notify_celery worker --loglevel=INFO --logfile=/var/log/notify/application.log --concurrency=4 -Q sms,sms-code,email-code

4
scripts/run_single_test.sh Executable file
View 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

View File

@@ -1,10 +1,7 @@
import uuid
import pytest
from app.celery.tasks import (send_sms, send_sms_code)
from app import twilio_client, encryption
from app.clients.sms.twilio import TwilioClientException
from app.celery.tasks import send_sms
from app import firetext_client
from app.celery.tasks import (send_sms, send_sms_code, send_email_code)
from app import (firetext_client, aws_ses_client, encryption)
from app.clients.sms.firetext import FiretextClientException
from app.dao import notifications_dao
from sqlalchemy.exc import SQLAlchemyError
@@ -98,3 +95,20 @@ def test_should_throw_firetext_client_exception(mocker):
mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException)
send_sms_code(encrypted_notification)
firetext_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'])
def test_should_send_email_code(mocker):
verification = {'to_address': 'someone@it.gov.uk',
'from_address': 'no-reply@notify.gov.uk',
'subject': 'Verification code',
'body': 11111}
encrypted_verification = encryption.encrypt(verification)
mocker.patch('app.aws_ses_client.send_email')
send_email_code(encrypted_verification)
aws_ses_client.send_email.assert_called_once_with(verification['from_address'],
verification['to_address'],
verification['subject'],
verification['body'])

View File

@@ -207,3 +207,13 @@ def sample_notification(notify_db,
@pytest.fixture(scope='function')
def mock_celery_send_sms_code(mocker):
return mocker.patch('app.celery.tasks.send_sms_code.apply_async')
@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")

View File

@@ -294,17 +294,15 @@ def test_send_user_code_for_sms_with_optional_to_field(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
encrpyted = encryption.encrypt({'to': '+441119876757', 'secret_code': '11111'})
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with([encrpyted], queue='sms_code')
encrypted = encryption.encrypt({'to': '+441119876757', 'secret_code': '11111'})
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with([encrypted], queue='sms_code')
@moto.mock_sqs
def test_send_user_code_for_email(notify_api,
notify_db,
notify_db_session,
sample_email_code,
sqs_client_conn,
mock_secret_code):
mock_secret_code,
mock_celery_send_email_code,
mock_encryption):
"""
Tests POST endpoint '/<user_id>/code' successful email
"""
@@ -321,14 +319,15 @@ def test_send_user_code_for_email(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
app.celery.tasks.send_email_code.apply_async.assert_called_once_with(['something_encrypted'],
queue='email_code')
@moto.mock_sqs
def test_send_user_code_for_email_uses_optional_to_field(notify_api,
notify_db,
notify_db_session,
sample_email_code,
sqs_client_conn,
mock_secret_code):
mock_secret_code,
mock_celery_send_email_code,
mock_encryption):
"""
Tests POST endpoint '/<user_id>/code' successful email with included in body
"""
@@ -345,6 +344,9 @@ def test_send_user_code_for_email_uses_optional_to_field(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
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):
from app.schemas import request_verify_code_schema

View File

@@ -59,13 +59,6 @@ def notify_db_session(request):
request.addfinalizer(teardown)
@pytest.fixture(scope='function')
def notify_config(notify_api):
notify_api.config['NOTIFY_API_ENVIRONMENT'] = 'test'
notify_api.config.from_object(configs['test'])
return notify_api.config
@pytest.fixture(scope='function')
def os_environ(request):
env_patch = mock.patch('os.environ', {})