mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
I have an issue with the test, not sure why?
This commit is contained in:
@@ -27,6 +27,8 @@ export AWS_REGION='eu-west-1'
|
|||||||
export DANGEROUS_SALT='dev-notify-salt'
|
export DANGEROUS_SALT='dev-notify-salt'
|
||||||
export DELIVERY_CLIENT_USER_NAME='dev-notify-delivery'
|
export DELIVERY_CLIENT_USER_NAME='dev-notify-delivery'
|
||||||
export DELIVERY_CLIENT_SECRET='dev-notify-secret-key'
|
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 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 NOTIFICATION_QUEUE_PREFIX='[unique-to-environment]-notification_development' # NOTE unique prefix
|
||||||
export SECRET_KEY='dev-notify-secret-key'
|
export SECRET_KEY='dev-notify-secret-key'
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from utils import logging
|
|||||||
from app.celery.celery import NotifyCelery
|
from app.celery.celery import NotifyCelery
|
||||||
from app.clients.sms.twilio import TwilioClient
|
from app.clients.sms.twilio import TwilioClient
|
||||||
from app.clients.sms.firetext import FiretextClient
|
from app.clients.sms.firetext import FiretextClient
|
||||||
|
from app.clients.email.aws_ses import AwsSesClient
|
||||||
from app.encryption import Encryption
|
from app.encryption import Encryption
|
||||||
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
@@ -16,6 +17,7 @@ ma = Marshmallow()
|
|||||||
notify_celery = NotifyCelery()
|
notify_celery = NotifyCelery()
|
||||||
twilio_client = TwilioClient()
|
twilio_client = TwilioClient()
|
||||||
firetext_client = FiretextClient()
|
firetext_client = FiretextClient()
|
||||||
|
aws_ses_client = AwsSesClient()
|
||||||
encryption = Encryption()
|
encryption = Encryption()
|
||||||
|
|
||||||
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
|
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
|
||||||
@@ -33,6 +35,7 @@ def create_app():
|
|||||||
logging.init_app(application)
|
logging.init_app(application)
|
||||||
twilio_client.init_app(application)
|
twilio_client.init_app(application)
|
||||||
firetext_client.init_app(application)
|
firetext_client.init_app(application)
|
||||||
|
aws_ses_client.init_app(application.config['AWS_REGION'])
|
||||||
notify_celery.init_app(application)
|
notify_celery.init_app(application)
|
||||||
encryption.init_app(application)
|
encryption.init_app(application)
|
||||||
|
|
||||||
|
|||||||
@@ -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.clients.sms.firetext import FiretextClientException
|
||||||
from app.dao.templates_dao import get_model_templates
|
from app.dao.templates_dao import get_model_templates
|
||||||
from app.dao.notifications_dao import save_notification
|
from app.dao.notifications_dao import save_notification
|
||||||
@@ -37,6 +38,20 @@ def send_sms_code(encrypted_notification):
|
|||||||
notification = encryption.decrypt(encrypted_notification)
|
notification = encryption.decrypt(encrypted_notification)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
firetext_client.send_sms(notification['to'], notification['secret_code'])
|
response = firetext_client.send_sms(notification['to'], notification['secret_code'])
|
||||||
|
current_app.logger.info(response)
|
||||||
except FiretextClientException as e:
|
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_notification):
|
||||||
|
content = encryption.decrypt(encrypted_notification)
|
||||||
|
|
||||||
|
try:
|
||||||
|
aws_ses_client.send_email(content['from_address'],
|
||||||
|
content['to_address'],
|
||||||
|
content['subject'],
|
||||||
|
content['body'])
|
||||||
|
except AwsSesClientException as e:
|
||||||
|
current_app.logger.error(e)
|
||||||
|
|||||||
17
app/clients/email/__init__.py
Normal file
17
app/clients/email/__init__.py
Normal 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.')
|
||||||
52
app/clients/email/aws_ses.py
Normal file
52
app/clients/email/aws_ses.py
Normal 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))
|
||||||
@@ -40,7 +40,7 @@ class FiretextClient(SmsClient):
|
|||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except RequestException as e:
|
except RequestException as e:
|
||||||
api_error = HTTPError.create(e)
|
api_error = HTTPError.create(e)
|
||||||
print(
|
logger.error(
|
||||||
"API {} request on {} failed with {} '{}'".format(
|
"API {} request on {} failed with {} '{}'".format(
|
||||||
"POST",
|
"POST",
|
||||||
"https://www.firetext.co.uk/api/sendsms",
|
"https://www.firetext.co.uk/api/sendsms",
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ from sqlalchemy.orm.exc import NoResultFound
|
|||||||
|
|
||||||
from app import encryption
|
from app import encryption
|
||||||
from app.dao.services_dao import get_model_services
|
from app.dao.services_dao import get_model_services
|
||||||
from app.aws_sqs import add_notification_to_queue
|
|
||||||
from app.dao.users_dao import (
|
from app.dao.users_dao import (
|
||||||
get_model_users,
|
get_model_users,
|
||||||
save_model_user,
|
save_model_user,
|
||||||
@@ -19,8 +18,7 @@ from app.dao.users_dao import (
|
|||||||
from app.schemas import (
|
from app.schemas import (
|
||||||
user_schema, users_schema, service_schema, services_schema,
|
user_schema, users_schema, service_schema, services_schema,
|
||||||
request_verify_code_schema, user_schema_load_json)
|
request_verify_code_schema, user_schema_load_json)
|
||||||
from app import api_user
|
from app.celery.tasks import (send_sms_code, send_email_code)
|
||||||
from app.celery.tasks import send_sms_code
|
|
||||||
|
|
||||||
user = Blueprint('user', __name__)
|
user = Blueprint('user', __name__)
|
||||||
|
|
||||||
@@ -141,12 +139,13 @@ def send_user_code(user_id):
|
|||||||
send_sms_code.apply_async([encryption.encrypt(notification)], queue='sms_code')
|
send_sms_code.apply_async([encryption.encrypt(notification)], 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')
|
||||||
notification = {
|
import json
|
||||||
|
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})
|
||||||
add_notification_to_queue(api_user['client'], 'admin', 'email', notification)
|
send_email_code.apply_async([encryption.encrypt(notification)], queue='email_code')
|
||||||
else:
|
else:
|
||||||
abort(500)
|
abort(500)
|
||||||
return jsonify({}), 204
|
return jsonify({}), 204
|
||||||
|
|||||||
@@ -3,4 +3,4 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
source environment.sh
|
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
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
import uuid
|
import uuid
|
||||||
import pytest
|
import pytest
|
||||||
from app.celery.tasks import (send_sms, send_sms_code)
|
from app.celery.tasks import (send_sms, send_sms_code, send_email_code)
|
||||||
from app import twilio_client, encryption
|
from app import (firetext_client, aws_ses_client, encryption)
|
||||||
from app.clients.sms.twilio import TwilioClientException
|
|
||||||
from app.celery.tasks import send_sms
|
|
||||||
from app import firetext_client
|
|
||||||
from app.clients.sms.firetext import FiretextClientException
|
from app.clients.sms.firetext import FiretextClientException
|
||||||
from app.dao import notifications_dao
|
from app.dao import notifications_dao
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
@@ -98,3 +95,20 @@ def test_should_log_firetext_client_exception(mocker):
|
|||||||
mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException)
|
mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException)
|
||||||
send_sms_code(encrypted_notification)
|
send_sms_code(encrypted_notification)
|
||||||
firetext_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'])
|
firetext_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'])
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_send_email_code(mocker):
|
||||||
|
notification = {'to_address': 'someone@it.gov.uk',
|
||||||
|
'from_address': 'no-reply@notify.gov.uk',
|
||||||
|
'subject': 'Verification code',
|
||||||
|
'body': 11111}
|
||||||
|
|
||||||
|
encrypted_notification = encryption.encrypt(notification)
|
||||||
|
mocker.patch('app.aws_ses_client.send_email')
|
||||||
|
|
||||||
|
send_email_code(encrypted_notification)
|
||||||
|
|
||||||
|
aws_ses_client.send_email.assert_called_once_with((notification['from_address'],
|
||||||
|
notification['to_address'],
|
||||||
|
notification['subject'],
|
||||||
|
notification['body']))
|
||||||
|
|||||||
@@ -207,3 +207,8 @@ def sample_notification(notify_db,
|
|||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
def mock_celery_send_sms_code(mocker):
|
def mock_celery_send_sms_code(mocker):
|
||||||
return mocker.patch('app.celery.tasks.send_sms_code.apply_async')
|
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')
|
||||||
|
|||||||
@@ -294,17 +294,14 @@ def test_send_user_code_for_sms_with_optional_to_field(notify_api,
|
|||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
|
|
||||||
assert resp.status_code == 204
|
assert resp.status_code == 204
|
||||||
encrpyted = encryption.encrypt({'to': '+441119876757', 'secret_code': '11111'})
|
encrypted = encryption.encrypt({'to': '+441119876757', 'secret_code': '11111'})
|
||||||
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with([encrpyted], queue='sms_code')
|
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,
|
def test_send_user_code_for_email(notify_api,
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_email_code,
|
sample_email_code,
|
||||||
sqs_client_conn,
|
mock_secret_code,
|
||||||
mock_secret_code):
|
mock_celery_send_email_code):
|
||||||
"""
|
"""
|
||||||
Tests POST endpoint '/<user_id>/code' successful email
|
Tests POST endpoint '/<user_id>/code' successful email
|
||||||
"""
|
"""
|
||||||
@@ -320,15 +317,18 @@ 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'],
|
||||||
|
'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')
|
||||||
|
|
||||||
|
|
||||||
@moto.mock_sqs
|
|
||||||
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,
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_email_code,
|
sample_email_code,
|
||||||
sqs_client_conn,
|
mock_secret_code,
|
||||||
mock_secret_code):
|
mock_celery_send_email_code):
|
||||||
"""
|
"""
|
||||||
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,6 +344,13 @@ 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([encrypted], 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):
|
||||||
|
|||||||
@@ -59,13 +59,6 @@ def notify_db_session(request):
|
|||||||
request.addfinalizer(teardown)
|
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')
|
@pytest.fixture(scope='function')
|
||||||
def os_environ(request):
|
def os_environ(request):
|
||||||
env_patch = mock.patch('os.environ', {})
|
env_patch = mock.patch('os.environ', {})
|
||||||
|
|||||||
Reference in New Issue
Block a user