mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-23 07:46:06 -04:00
Added base object for response statuses, and tests around it's behaviour
This commit is contained in:
@@ -11,3 +11,24 @@ class Client(object):
|
|||||||
Base client for sending notifications.
|
Base client for sending notifications.
|
||||||
'''
|
'''
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ClientResponse:
|
||||||
|
def __init__(self):
|
||||||
|
self.__response_model__ = None
|
||||||
|
|
||||||
|
def response_code_to_object(self, response_code):
|
||||||
|
return self.__response_model__[response_code]
|
||||||
|
|
||||||
|
def response_code_to_message(self, response_code):
|
||||||
|
return self.response_code_to_object(response_code)['message']
|
||||||
|
|
||||||
|
def response_code_to_notification_status(self, response_code):
|
||||||
|
print(response_code)
|
||||||
|
return self.response_code_to_object(response_code)['notification_status']
|
||||||
|
|
||||||
|
def response_code_to_notification_statistics_status(self, response_code):
|
||||||
|
return self.response_code_to_object(response_code)['notification_statistics_status']
|
||||||
|
|
||||||
|
def response_code_to_notification_success(self, response_code):
|
||||||
|
return self.response_code_to_object(response_code)['success']
|
||||||
|
|||||||
@@ -1,22 +1,33 @@
|
|||||||
import boto3
|
import boto3
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from monotonic import monotonic
|
from monotonic import monotonic
|
||||||
|
from app.clients import ClientResponse
|
||||||
from app.clients.email import (EmailClientException, EmailClient)
|
from app.clients.email import (EmailClientException, EmailClient)
|
||||||
|
|
||||||
ses_response_status = {
|
|
||||||
'Bounce': {
|
class AwsSesResponses(ClientResponse):
|
||||||
"success": False,
|
def __init__(self):
|
||||||
"notify_status": 'bounce'
|
ClientResponse.__init__(self)
|
||||||
},
|
self.__response_model__ = {
|
||||||
'Delivery': {
|
'Bounce': {
|
||||||
"success": True,
|
"message": 'Bounced',
|
||||||
"notify_status": 'delivered'
|
"success": False,
|
||||||
},
|
"notification_status": 'bounce',
|
||||||
'Complaint': {
|
"notification_statistics_status": 'failed'
|
||||||
"success": False,
|
},
|
||||||
"notify_status": 'complaint'
|
'Delivery': {
|
||||||
}
|
"message": 'Delivered',
|
||||||
}
|
"success": True,
|
||||||
|
"notification_status": 'delivered',
|
||||||
|
"notification_statistics_status": 'delivered'
|
||||||
|
},
|
||||||
|
'Complaint': {
|
||||||
|
"message": 'Complaint',
|
||||||
|
"success": False,
|
||||||
|
"notification_status": 'complaint',
|
||||||
|
"notification_statistics_status": 'failed'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class AwsSesClientException(EmailClientException):
|
class AwsSesClientException(EmailClientException):
|
||||||
|
|||||||
@@ -6,58 +6,34 @@ from app.clients.sms import (
|
|||||||
)
|
)
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from requests import request, RequestException, HTTPError
|
from requests import request, RequestException, HTTPError
|
||||||
|
from app.clients import ClientResponse
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
firetext_response_status = {
|
|
||||||
'0': {
|
|
||||||
"firetext_message": 'delivered',
|
|
||||||
"success": True,
|
|
||||||
"notify_status": 'delivered'
|
|
||||||
},
|
|
||||||
'1': {
|
|
||||||
"firetext_message": 'declined',
|
|
||||||
"success": False,
|
|
||||||
"notify_status": 'failed'
|
|
||||||
},
|
|
||||||
'2': {
|
|
||||||
"firetext_message": 'Undelivered (Pending with Network)',
|
|
||||||
"success": False,
|
|
||||||
"notify_status": 'sent'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
class FiretextResponses(ClientResponse):
|
||||||
class FiretextResponses:
|
def __init__(self):
|
||||||
response_model = {
|
ClientResponse.__init__(self)
|
||||||
'0': {
|
self.__response_model__ = {
|
||||||
"firetext_message": 'delivered',
|
'0': {
|
||||||
"stats_mapping": 'delivered',
|
"message": 'Delivered',
|
||||||
"success": True,
|
"notification_statistics_status": 'delivered',
|
||||||
"notify_status": 'delivered'
|
"success": True,
|
||||||
},
|
"notification_status": 'delivered'
|
||||||
'1': {
|
},
|
||||||
"firetext_message": 'declined',
|
'1': {
|
||||||
"success": False,
|
"message": 'Declined',
|
||||||
"stats_mapping": 'failure',
|
"success": False,
|
||||||
"notify_status": 'failed'
|
"notification_statistics_status": 'failed',
|
||||||
},
|
"notification_status": 'failed'
|
||||||
'2': {
|
},
|
||||||
"firetext_message": 'Undelivered (Pending with Network)',
|
'2': {
|
||||||
"success": False,
|
"message": 'Undelivered (Pending with Network)',
|
||||||
"stats_mapping": None,
|
"success": False,
|
||||||
"notify_status": 'sent'
|
"notification_statistics_status": None,
|
||||||
|
"notification_status": 'sent'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
def response_code_to_message(self, response_code):
|
|
||||||
return self.response_model.get(response_code)['firetext_message']
|
|
||||||
|
|
||||||
def response_code_to_notify_status(self, response_code):
|
|
||||||
return self.response_model.get(response_code)['notify_status']
|
|
||||||
|
|
||||||
def response_code_to_notify_stats(self, response_code):
|
|
||||||
return self.response_model.get(response_code)['stats_mapping']
|
|
||||||
|
|
||||||
|
|
||||||
class FiretextClientException(SmsClientException):
|
class FiretextClientException(SmsClientException):
|
||||||
|
|||||||
0
tests/app/clients/__init__.py
Normal file
0
tests/app/clients/__init__.py
Normal file
32
tests/app/clients/test_aws_ses.py
Normal file
32
tests/app/clients/test_aws_ses.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from app.clients.email import aws_ses
|
||||||
|
|
||||||
|
aws_responses = aws_ses.AwsSesResponses()
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_correct_details_for_delivery():
|
||||||
|
assert aws_responses.response_code_to_message('Delivery') == 'Delivered'
|
||||||
|
assert aws_responses.response_code_to_notification_status('Delivery') == 'delivered'
|
||||||
|
assert aws_responses.response_code_to_notification_statistics_status('Delivery') == 'delivered'
|
||||||
|
assert aws_responses.response_code_to_notification_success('Delivery')
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_correct_details_for_bounced():
|
||||||
|
assert aws_responses.response_code_to_message('Bounce') == 'Bounced'
|
||||||
|
assert aws_responses.response_code_to_notification_status('Bounce') == 'bounce'
|
||||||
|
assert aws_responses.response_code_to_notification_statistics_status('Bounce') == 'failed'
|
||||||
|
assert not aws_responses.response_code_to_notification_success('Bounce')
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_correct_details_for_complaint():
|
||||||
|
assert aws_responses.response_code_to_message('Complaint') == 'Complaint'
|
||||||
|
assert aws_responses.response_code_to_notification_status('Complaint') == 'complaint'
|
||||||
|
assert aws_responses.response_code_to_notification_statistics_status('Complaint') == 'failed'
|
||||||
|
assert not aws_responses.response_code_to_notification_success('Complaint')
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_be_none_if_unrecognised_status_code():
|
||||||
|
with pytest.raises(KeyError) as e:
|
||||||
|
aws_responses.response_code_to_object('99')
|
||||||
|
assert '99' in str(e.value)
|
||||||
32
tests/app/clients/test_firetext.py
Normal file
32
tests/app/clients/test_firetext.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from app.clients.sms import firetext
|
||||||
|
|
||||||
|
responses = firetext.FiretextResponses()
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_correct_details_for_delivery():
|
||||||
|
assert responses.response_code_to_message('0') == 'Delivered'
|
||||||
|
assert responses.response_code_to_notification_status('0') == 'delivered'
|
||||||
|
assert responses.response_code_to_notification_statistics_status('0') == 'delivered'
|
||||||
|
assert responses.response_code_to_notification_success('0')
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_correct_details_for_bounced():
|
||||||
|
assert responses.response_code_to_message('1') == 'Declined'
|
||||||
|
assert responses.response_code_to_notification_status('1') == 'failed'
|
||||||
|
assert responses.response_code_to_notification_statistics_status('1') == 'failed'
|
||||||
|
assert not responses.response_code_to_notification_success('1')
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_correct_details_for_complaint():
|
||||||
|
assert responses.response_code_to_message('2') == 'Undelivered (Pending with Network)'
|
||||||
|
assert responses.response_code_to_notification_status('2') == 'sent'
|
||||||
|
assert not responses.response_code_to_notification_statistics_status('2')
|
||||||
|
assert not responses.response_code_to_notification_success('2')
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_be_none_if_unrecognised_status_code():
|
||||||
|
with pytest.raises(KeyError) as e:
|
||||||
|
responses.response_code_to_object('99')
|
||||||
|
assert '99' in str(e.value)
|
||||||
Reference in New Issue
Block a user