def test_missing_message_type(self): # Remove the Message-Type header from a notification header request. headers = sns_helper.modify_header_list(self.confirm_headers, 'X-Amz-Sns-Message-Type', None) body_dict = self.confirm_body_dict body = json.dumps(body_dict) with self.assertRaises(AWSNotificationHandlerError): AWSNotificationHandler(headers, body, validation=False)
def test_get_region_missing_header(self): # Missing X-Amz-Sns-Topic-Arn headers = sns_helper.modify_header_list(self.confirm_headers, 'X-Amz-Sns-Topic-Arn', None) body_dict = self.confirm_body_dict body = json.dumps(body_dict) with self.assertRaises(AWSNotificationHandlerError) as error: AWSNotificationHandler(headers, body, validation=False) self.assertTrue('Missing Subscription ARN' in str(error.exception))
def test_get_region_unknown_region(self): # Set the region to an unknown value (mars-east-1) in the Topic-Arn headers = sns_helper.modify_header_list( self.confirm_headers, 'X-Amz-Sns-Topic-Arn', 'arn:aws:sns:mars-east-1:123456789012:MyTopic') body_dict = self.confirm_body_dict body_dict['TopicArn'] = 'arn:aws:sns:mars-east-1:123456789012:MyTopic' body = json.dumps(body_dict) with self.assertRaises(AWSNotificationHandlerError) as error: AWSNotificationHandler(headers, body, validation=False) self.assertTrue('Unexpected region name.' in str(error.exception))
def test_get_region_invalid_arn(self): # Removed a ':' in Topic ARN headers = sns_helper.modify_header_list( self.confirm_headers, 'X-Amz-Sns-Topic-Arn', 'arnaws:sns:us-east-1:123456789012:MyTopic') body_dict = self.confirm_body_dict body_dict['TopicArn'] = 'arnaws:sns:us-east-1:123456789012:MyTopic' body = json.dumps(body_dict) with self.assertRaises(AWSNotificationHandlerError) as error: AWSNotificationHandler(headers, body, validation=False) self.assertTrue('Unexpected region name' in str(error.exception))
def test_confirm_subscription(self): # Setup conn = boto3.client('sns', region_name='us-east-1') response = conn.create_topic(Name='CostUsageNotificationDemo') body_dict = self.confirm_body_dict headers = sns_helper.modify_header_list(self.confirm_headers, 'X-Amz-Sns-Topic-Arn', response['TopicArn']) body_dict['TopicArn'] = response['TopicArn'] body = json.dumps(body_dict) try: AWSNotificationHandler(headers, body, validation=False) except Exception: self.fail('Unexpected exception')
def test_initializer_error_setting_handler(self): """Test to initializer error setting handler.""" # Use invalid type invalid_msg_type = 'InvalidType' headers = sns_helper.modify_header_list( self.notification_headers, 'X-Amz-Sns-Message-Type', invalid_msg_type ) body_dict = self.notification_body_dict body_dict['Type'] = invalid_msg_type body = json.dumps(body_dict) with self.assertRaises(NotificationHandlerError) as error: NotificationHandler(headers, body) self.assertTrue('Unexpected message type' in str(error.exception))
def test_confirm_subscription_not_successful(self): # Setup conn = boto3.client('sns', region_name='us-east-1') response = conn.create_topic(Name='CostUsageNotificationDemo') # Make the TopicArn into a non-standard, unexpected format corrupted_arn_name = 'Mangle{}'.format(response['TopicArn']) body_dict = self.confirm_body_dict headers = sns_helper.modify_header_list(self.confirm_headers, 'X-Amz-Sns-Topic-Arn', corrupted_arn_name) body_dict['TopicArn'] = corrupted_arn_name body = json.dumps(body_dict) with self.assertRaises(AWSNotificationHandlerError): AWSNotificationHandler(headers, body, validation=False)
def test_confirm_subscription_not_successful(self, mock_boto3_client): sns_client = Mock() sns_client.confirm_subscription.return_value = {} mock_boto3_client.return_value = sns_client # Make the TopicArn into a non-standard, unexpected format topic_arn = 'arn:aws:sns:us-east-1:123456789012:MyTopic' corrupted_arn_name = 'Mangle{}'.format(topic_arn) body_dict = self.confirm_body_dict headers = sns_helper.modify_header_list(self.confirm_headers, 'X-Amz-Sns-Topic-Arn', corrupted_arn_name) body_dict['TopicArn'] = corrupted_arn_name body = json.dumps(body_dict) with self.assertRaises(AWSNotificationHandlerError): AWSNotificationHandler(headers, body, validation=False)
def test_confirm_subscription(self, mock_boto3_client): sns_client = Mock() sns_client.confirm_subscription.return_value = { 'ResponseMetadata': { 'HTTPStatusCode': 201 } } mock_boto3_client.return_value = sns_client topic_arn = 'arn:aws:sns:us-east-1:123456789012:MyTopic' body_dict = self.confirm_body_dict headers = sns_helper.modify_header_list(self.confirm_headers, 'X-Amz-Sns-Topic-Arn', topic_arn) body_dict['TopicArn'] = topic_arn body = json.dumps(body_dict) try: AWSNotificationHandler(headers, body, validation=False) except Exception: self.fail('Unexpected exception')