mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
I have an issue with the test, not sure why?
This commit is contained in:
@@ -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
|
||||
@@ -37,6 +38,20 @@ def send_sms_code(encrypted_notification):
|
||||
notification = encryption.decrypt(encrypted_notification)
|
||||
|
||||
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:
|
||||
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()
|
||||
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",
|
||||
|
||||
@@ -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__)
|
||||
|
||||
@@ -141,12 +139,13 @@ def send_user_code(user_id):
|
||||
send_sms_code.apply_async([encryption.encrypt(notification)], 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 = {
|
||||
import json
|
||||
notification = json.dumps({
|
||||
'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)
|
||||
'body': secret_code})
|
||||
send_email_code.apply_async([encryption.encrypt(notification)], queue='email_code')
|
||||
else:
|
||||
abort(500)
|
||||
return jsonify({}), 204
|
||||
|
||||
Reference in New Issue
Block a user