Implemented CheckIfPhoneNumberIsOptedOut + Tests + Error code
This commit is contained in:
parent
57d94d56c3
commit
ba8a2ccfc5
@ -9,11 +9,17 @@ from .models import sns_backends
|
|||||||
|
|
||||||
|
|
||||||
class SNSResponse(BaseResponse):
|
class SNSResponse(BaseResponse):
|
||||||
|
SMS_ATTR_REGEX = re.compile(r'^attributes\.entry\.(?P<index>\d+)\.(?P<type>key|value)$')
|
||||||
|
OPT_OUT_PHONE_NUMBER_REGEX = re.compile(r'^\+?\d+$')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def backend(self):
|
def backend(self):
|
||||||
return sns_backends[self.region]
|
return sns_backends[self.region]
|
||||||
|
|
||||||
|
def _error(self, code, message, sender='Sender'):
|
||||||
|
template = self.response_template(ERROR_RESPONSE)
|
||||||
|
return template.render(code=code, message=message, sender=sender)
|
||||||
|
|
||||||
def _get_attributes(self):
|
def _get_attributes(self):
|
||||||
attributes = self._get_list_prefix('Attributes.entry')
|
attributes = self._get_list_prefix('Attributes.entry')
|
||||||
return dict(
|
return dict(
|
||||||
@ -462,15 +468,13 @@ class SNSResponse(BaseResponse):
|
|||||||
return template.render()
|
return template.render()
|
||||||
|
|
||||||
def set_sms_attributes(self):
|
def set_sms_attributes(self):
|
||||||
attr_regex = re.compile(r'^attributes\.entry\.(?P<index>\d+)\.(?P<type>key|value)$')
|
|
||||||
|
|
||||||
# attributes.entry.1.key
|
# attributes.entry.1.key
|
||||||
# attributes.entry.1.value
|
# attributes.entry.1.value
|
||||||
# to
|
# to
|
||||||
# 1: {key:X, value:Y}
|
# 1: {key:X, value:Y}
|
||||||
temp_dict = defaultdict(dict)
|
temp_dict = defaultdict(dict)
|
||||||
for key, value in self.querystring.items():
|
for key, value in self.querystring.items():
|
||||||
match = attr_regex.match(key)
|
match = self.SMS_ATTR_REGEX.match(key)
|
||||||
if match is not None:
|
if match is not None:
|
||||||
temp_dict[match.group('index')][match.group('type')] = value[0]
|
temp_dict[match.group('index')][match.group('type')] = value[0]
|
||||||
|
|
||||||
@ -502,6 +506,19 @@ class SNSResponse(BaseResponse):
|
|||||||
template = self.response_template(GET_SMS_ATTRIBUTES_TEMPLATE)
|
template = self.response_template(GET_SMS_ATTRIBUTES_TEMPLATE)
|
||||||
return template.render(attributes=result)
|
return template.render(attributes=result)
|
||||||
|
|
||||||
|
def check_if_phone_number_is_opted_out(self):
|
||||||
|
number = self._get_param('phoneNumber')
|
||||||
|
if self.OPT_OUT_PHONE_NUMBER_REGEX.match(number) is None:
|
||||||
|
error_response = self._error(
|
||||||
|
code='InvalidParameter',
|
||||||
|
message='Invalid parameter: PhoneNumber Reason: input incorrectly formatted'
|
||||||
|
)
|
||||||
|
return error_response, dict(status=400)
|
||||||
|
|
||||||
|
# There should be a nicer way to set if a nubmer has opted out
|
||||||
|
template = self.response_template(CHECK_IF_OPTED_OUT_TEMPLATE)
|
||||||
|
return template.render(opt_out=str(number.endswith('99')).lower())
|
||||||
|
|
||||||
|
|
||||||
CREATE_TOPIC_TEMPLATE = """<CreateTopicResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
CREATE_TOPIC_TEMPLATE = """<CreateTopicResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
||||||
<CreateTopicResult>
|
<CreateTopicResult>
|
||||||
@ -824,3 +841,21 @@ GET_SMS_ATTRIBUTES_TEMPLATE = """<GetSMSAttributesResponse xmlns="http://sns.ama
|
|||||||
<RequestId>287f9554-8db3-5e66-8abc-c76f0186db7e</RequestId>
|
<RequestId>287f9554-8db3-5e66-8abc-c76f0186db7e</RequestId>
|
||||||
</ResponseMetadata>
|
</ResponseMetadata>
|
||||||
</GetSMSAttributesResponse>"""
|
</GetSMSAttributesResponse>"""
|
||||||
|
|
||||||
|
CHECK_IF_OPTED_OUT_TEMPLATE = """<CheckIfPhoneNumberIsOptedOutResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
||||||
|
<CheckIfPhoneNumberIsOptedOutResult>
|
||||||
|
<isOptedOut>{{ opt_out }}</isOptedOut>
|
||||||
|
</CheckIfPhoneNumberIsOptedOutResult>
|
||||||
|
<ResponseMetadata>
|
||||||
|
<RequestId>287f9554-8db3-5e66-8abc-c76f0186db7e</RequestId>
|
||||||
|
</ResponseMetadata>
|
||||||
|
</CheckIfPhoneNumberIsOptedOutResponse>"""
|
||||||
|
|
||||||
|
ERROR_RESPONSE = """<ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
||||||
|
<Error>
|
||||||
|
<Type>{{ sender }}</Type>
|
||||||
|
<Code>{{ code }}</Code>
|
||||||
|
<Message>{{ message }}</Message>
|
||||||
|
</Error>
|
||||||
|
<RequestId>9dd01905-5012-5f99-8663-4b3ecd0dfaef</RequestId>
|
||||||
|
</ErrorResponse>"""
|
@ -3,6 +3,8 @@ import boto3
|
|||||||
import sure # noqa
|
import sure # noqa
|
||||||
|
|
||||||
from moto import mock_sns
|
from moto import mock_sns
|
||||||
|
from botocore.exceptions import ClientError
|
||||||
|
from nose.tools import assert_raises
|
||||||
|
|
||||||
|
|
||||||
@mock_sns
|
@mock_sns
|
||||||
@ -30,3 +32,30 @@ def test_get_sms_attributes_filtered():
|
|||||||
response['attributes'].should.contain('DefaultSMSType')
|
response['attributes'].should.contain('DefaultSMSType')
|
||||||
response['attributes'].should_not.contain('test')
|
response['attributes'].should_not.contain('test')
|
||||||
response['attributes']['DefaultSMSType'].should.equal('Transactional')
|
response['attributes']['DefaultSMSType'].should.equal('Transactional')
|
||||||
|
|
||||||
|
|
||||||
|
@mock_sns
|
||||||
|
def test_check_not_opted_out():
|
||||||
|
conn = boto3.client('sns', region_name='us-east-1')
|
||||||
|
response = conn.check_if_phone_number_is_opted_out(phoneNumber='+447428545375')
|
||||||
|
|
||||||
|
response.should.contain('isOptedOut')
|
||||||
|
response['isOptedOut'].should.be(False)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_sns
|
||||||
|
def test_check_opted_out(): # Ends in 99 so is opted out
|
||||||
|
conn = boto3.client('sns', region_name='us-east-1')
|
||||||
|
response = conn.check_if_phone_number_is_opted_out(phoneNumber='+447428545399')
|
||||||
|
|
||||||
|
response.should.contain('isOptedOut')
|
||||||
|
response['isOptedOut'].should.be(True)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_sns
|
||||||
|
def test_check_opted_out_invalid():
|
||||||
|
conn = boto3.client('sns', region_name='us-east-1')
|
||||||
|
|
||||||
|
# Invalid phone number
|
||||||
|
with assert_raises(ClientError):
|
||||||
|
conn.check_if_phone_number_is_opted_out(phoneNumber='+44742LALALA')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user