mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
Merge branch 'master' into email-templates
Conflicts: app/user/rest.py
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -30,3 +31,24 @@ def send_sms(service_id, notification_id, encrypted_notification):
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
current_app.logger.debug(e)
|
||||
|
||||
|
||||
@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.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)
|
||||
|
||||
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()
|
||||
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",
|
||||
|
||||
@@ -2,12 +2,11 @@ from datetime import datetime
|
||||
from flask import (jsonify, request, abort, Blueprint, current_app)
|
||||
from sqlalchemy.exc import DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from app.dao.services_dao import dao_fetch_service_by_id_and_user
|
||||
from app.aws_sqs import add_notification_to_queue
|
||||
from app import encryption
|
||||
|
||||
from app.dao.users_dao import (
|
||||
get_model_users,
|
||||
save_model_user,
|
||||
delete_model_user,
|
||||
create_user_code,
|
||||
get_user_code,
|
||||
use_user_code,
|
||||
@@ -15,14 +14,15 @@ from app.dao.users_dao import (
|
||||
reset_failed_login_count
|
||||
)
|
||||
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
|
||||
|
||||
user_schema,
|
||||
users_schema,
|
||||
request_verify_code_schema,
|
||||
user_schema_load_json
|
||||
)
|
||||
from app.celery.tasks import (send_sms_code, send_email_code)
|
||||
from app.errors import register_errors
|
||||
|
||||
user = Blueprint('user', __name__)
|
||||
|
||||
from app.errors import register_errors
|
||||
register_errors(user)
|
||||
|
||||
|
||||
@@ -131,16 +131,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, 'content': secret_code}
|
||||
add_notification_to_queue(api_user['client'], 'admin', 'sms', notification)
|
||||
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')
|
||||
notification = {
|
||||
verification_message = {
|
||||
'to_address': email,
|
||||
'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', notification)
|
||||
send_email_code.apply_async([encryption.encrypt(verification_message)], queue='email-code')
|
||||
else:
|
||||
abort(500)
|
||||
return jsonify({}), 204
|
||||
|
||||
@@ -25,7 +25,7 @@ class Config(object):
|
||||
'region': 'eu-west-1',
|
||||
'polling_interval': 1, # 1 second
|
||||
'visibility_timeout': 60, # 60 seconds
|
||||
'queue_name_prefix': os.environ['NOTIFICATION_QUEUE_PREFIX']
|
||||
'queue_name_prefix': os.environ['NOTIFICATION_QUEUE_PREFIX']+'-'
|
||||
}
|
||||
CELERY_ENABLE_UTC = True,
|
||||
CELERY_TIMEZONE = 'Europe/London'
|
||||
|
||||
@@ -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
|
||||
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
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
|
||||
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
import pytest
|
||||
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
|
||||
@@ -74,3 +74,41 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
|
||||
with pytest.raises(NoResultFound) as e:
|
||||
notifications_dao.get_notification(sample_template.service_id, notification_id)
|
||||
assert 'No row was found for one' in str(e.value)
|
||||
|
||||
|
||||
def test_should_send_sms_code(mocker):
|
||||
notification = {'to': '+441234123123',
|
||||
'secret_code': '12345'}
|
||||
|
||||
encrypted_notification = encryption.encrypt(notification)
|
||||
|
||||
mocker.patch('app.firetext_client.send_sms')
|
||||
send_sms_code(encrypted_notification)
|
||||
firetext_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'])
|
||||
|
||||
|
||||
def test_should_throw_firetext_client_exception(mocker):
|
||||
notification = {'to': '+441234123123',
|
||||
'secret_code': '12345'}
|
||||
|
||||
encrypted_notification = encryption.encrypt(notification)
|
||||
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'])
|
||||
|
||||
@@ -210,3 +210,18 @@ def sample_notification(notify_db,
|
||||
notification = Notification(**data)
|
||||
save_notification(notification)
|
||||
return notification
|
||||
|
||||
|
||||
@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")
|
||||
|
||||
@@ -4,7 +4,9 @@ from datetime import (datetime, timedelta)
|
||||
from flask import url_for
|
||||
|
||||
from app.models import (VerifyCode)
|
||||
from app import db
|
||||
|
||||
import app.celery.tasks
|
||||
from app import db, encryption
|
||||
from tests import create_authorization_header
|
||||
|
||||
|
||||
@@ -247,13 +249,10 @@ def test_user_verify_password_missing_password(notify_api,
|
||||
assert 'Required field missing data' in json_resp['message']['password']
|
||||
|
||||
|
||||
@moto.mock_sqs
|
||||
def test_send_user_code_for_sms(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_sms_code,
|
||||
sqs_client_conn,
|
||||
mock_secret_code):
|
||||
mock_secret_code,
|
||||
mock_celery_send_sms_code):
|
||||
"""
|
||||
Tests POST endpoint '/<user_id>/code' successful sms
|
||||
"""
|
||||
@@ -270,20 +269,20 @@ def test_send_user_code_for_sms(notify_api,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert resp.status_code == 204
|
||||
encrpyted = encryption.encrypt({'to': sample_sms_code.user.mobile_number, 'secret_code': '11111'})
|
||||
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with([encrpyted], queue='sms-code')
|
||||
|
||||
|
||||
@moto.mock_sqs
|
||||
def test_send_user_code_for_sms_with_optional_to_field(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_sms_code,
|
||||
sqs_client_conn,
|
||||
mock_secret_code):
|
||||
mock_secret_code,
|
||||
mock_celery_send_sms_code):
|
||||
"""
|
||||
Tests POST endpoint '/<user_id>/code' successful sms with optional to field
|
||||
"""
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
data = json.dumps({'code_type': 'sms', 'to': '+441119876757'})
|
||||
auth_header = create_authorization_header(
|
||||
path=url_for('user.send_user_code', user_id=sample_sms_code.user.id),
|
||||
@@ -295,15 +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
|
||||
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
|
||||
"""
|
||||
@@ -320,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
|
||||
"""
|
||||
@@ -344,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
|
||||
|
||||
@@ -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', {})
|
||||
|
||||
Reference in New Issue
Block a user