Merge pull request #1283 from terrycain/fix_1276

Fixes #1276 SNS Subject validation
This commit is contained in:
Jack Danger 2017-10-23 09:41:17 -07:00 committed by GitHub
commit 462eceae6f
3 changed files with 42 additions and 2 deletions

View File

@ -256,7 +256,10 @@ class SNSBackend(BaseBackend):
else:
return self._get_values_nexttoken(self.subscriptions, next_token)
def publish(self, arn, message):
def publish(self, arn, message, subject=None):
if subject is not None and len(subject) >= 100:
raise ValueError('Subject must be less than 100 characters')
try:
topic = self.get_topic(arn)
message_id = topic.publish(message)

View File

@ -239,6 +239,8 @@ class SNSResponse(BaseResponse):
target_arn = self._get_param('TargetArn')
topic_arn = self._get_param('TopicArn')
phone_number = self._get_param('PhoneNumber')
subject = self._get_param('Subject')
if phone_number is not None:
# Check phone is correct syntax (e164)
if not is_e164(phone_number):
@ -261,7 +263,12 @@ class SNSResponse(BaseResponse):
arn = topic_arn
message = self._get_param('Message')
message_id = self.backend.publish(arn, message)
try:
message_id = self.backend.publish(arn, message, subject=subject)
except ValueError as err:
error_response = self._error('InvalidParameter', str(err))
return error_response, dict(status=400)
if self.request_json:
return json.dumps({

View File

@ -177,3 +177,33 @@ def test_publish_to_http():
response = conn.publish(
TopicArn=topic_arn, Message="my message", Subject="my subject")
message_id = response['MessageId']
@mock_sqs
@mock_sns
def test_publish_subject():
conn = boto3.client('sns', region_name='us-east-1')
conn.create_topic(Name="some-topic")
response = conn.list_topics()
topic_arn = response["Topics"][0]['TopicArn']
sqs_conn = boto3.resource('sqs', region_name='us-east-1')
sqs_conn.create_queue(QueueName="test-queue")
conn.subscribe(TopicArn=topic_arn,
Protocol="sqs",
Endpoint="arn:aws:sqs:us-east-1:123456789012:test-queue")
message = 'my message'
subject1 = 'test subject'
subject2 = 'test subject' * 20
with freeze_time("2015-01-01 12:00:00"):
conn.publish(TopicArn=topic_arn, Message=message, Subject=subject1)
# Just that it doesnt error is a pass
try:
with freeze_time("2015-01-01 12:00:00"):
conn.publish(TopicArn=topic_arn, Message=message, Subject=subject2)
except ClientError as err:
err.response['Error']['Code'].should.equal('InvalidParameter')
else:
raise RuntimeError('Should have raised an InvalidParameter exception')