def test_verify_subscription_notification(self, mock): """Test the verification of a valid subscription notification""" mock.return_value = self.pemfile notification = loader('subscriptionconfirmation') result = utils.verify_notification(notification) self.assertTrue(result)
def test_signal_sent(self, mock): """ Test that the subscription signal was sent Based on http://stackoverflow.com/questions/3817213/ """ # pylint: disable=attribute-defined-outside-init, unused-variable responsemock = Mock() responsemock.read.return_value = 'Return Value' mock.return_value = responsemock notification = loader('subscriptionconfirmation') self.signal_count = 0 @receiver(signals.subscription) def _signal_receiver(sender, **kwargs): """Signal Test Receiver""" self.signal_count += 1 self.signal_sender = sender self.signal_notification = kwargs['notification'] self.signal_result = kwargs['result'] response = utils.approve_subscription(notification) self.assertEqual(response.content.decode('ascii'), 'Return Value') self.assertEqual(self.signal_count, 1) self.assertEqual(self.signal_result, 'Return Value') self.assertEqual(self.signal_notification, notification)
def test_subscription_verification_failure(self, mock): """Test the failure of an invalid subscription notification""" mock.return_value = self.pemfile notification = loader('subscriptionconfirmation') notification['TopicArn'] = 'BadArn' result = utils.verify_notification(notification) self.assertFalse(result)
def test_unknown_message(self): """Test a JSON message without a type returns an error""" message = loader('bounce') message['notificationType'] = 'Not A Valid Notification' result = views.process_message(message, self.notification) self.assertEqual(result.status_code, 200) self.assertEqual( result.content.decode('ascii'), 'Unknown Notification Type')
def test_approve_subscription_called(self, mock): """Test that a approve_subscription is called""" mock.return_value = 'Test Return Value' notification = loader('subscriptionconfirmation') self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertTrue(mock.called) self.assertEqual(result, 'Test Return Value')
def test_missing_fields(self): """Test that missing vital fields returns an error""" message = loader('bounce') del(message['mail']) result = views.process_message(message, self.notification) self.assertEqual(result.status_code, 200) self.assertEqual( result.content.decode('ascii'), 'Missing Vital Fields')
def test_missing_fields(self): """Test that missing vital fields returns an error""" message = loader('bounce') del (message['mail']) result = views.process_message(message, self.notification) self.assertEqual(result.status_code, 200) self.assertEqual(result.content.decode('ascii'), 'Missing Vital Fields')
def test_unknown_message(self): """Test a JSON message without a type returns an error""" message = loader('bounce') message['notificationType'] = 'Not A Valid Notification' result = views.process_message(message, self.notification) self.assertEqual(result.status_code, 200) self.assertEqual(result.content.decode('ascii'), 'Unknown Notification Type')
def test_bad_certificate_url(self): """Test an unknown certificate hostname""" notification = loader('bounce_notification') notification['SigningCertURL'] = 'https://baddomain.com/cert.pem' self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertEqual(result.status_code, 400) self.assertEqual( result.content.decode('ascii'), 'Improper Certificate Location')
def test_unknown_notification_type(self): """Test an unknown notification type""" notification = loader('bounce_notification') notification['Type'] = 'NotAKnownType' self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertEqual(result.status_code, 400) self.assertEqual( result.content.decode('ascii'), 'Unknown Notification Type')
def test_unknown_notification_type(self): """Test an unknown notification type""" notification = loader('bounce_notification') notification['Type'] = 'NotAKnownType' self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertEqual(result.status_code, 400) self.assertEqual(result.content.decode('ascii'), 'Unknown Notification Type')
def test_non_json_message_not_allowed(self): """Test that a non-JSON message is properly ignored""" notification = loader('bounce_notification') notification['Message'] = 'Non JSON Message' self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertEqual(result.status_code, 200) self.assertEqual( result.content.decode('ascii'), 'Message is not valid JSON')
def test_non_json_message_not_allowed(self): """Test that a non-JSON message is properly ignored""" notification = loader('bounce_notification') notification['Message'] = 'Non JSON Message' self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertEqual(result.status_code, 200) self.assertEqual(result.content.decode('ascii'), 'Message is not valid JSON')
def test_unsubscribe_confirmation_not_handled(self): """Test that an unsubscribe notification is properly ignored""" notification = loader('bounce_notification') notification['Type'] = 'UnsubscribeConfirmation' self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertEqual(result.status_code, 200) self.assertEqual(result.content.decode('ascii'), 'UnsubscribeConfirmation Not Handled')
def test_bad_certificate_url(self): """Test an unknown certificate hostname""" notification = loader('bounce_notification') notification['SigningCertURL'] = 'https://baddomain.com/cert.pem' self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertEqual(result.status_code, 400) self.assertEqual(result.content.decode('ascii'), 'Improper Certificate Location')
def test_unsubscribe_confirmation_not_handled(self): """Test that an unsubscribe notification is properly ignored""" notification = loader('bounce_notification') notification['Type'] = 'UnsubscribeConfirmation' self.request._body = json.dumps(notification) result = views.endpoint(self.request) self.assertEqual(result.status_code, 200) self.assertEqual( result.content.decode('ascii'), 'UnsubscribeConfirmation Not Handled' )
def test_approve_subscription(self, mock): """Test the subscription approval mechanism""" responsemock = Mock() responsemock.read.return_value = 'Return Value' mock.return_value = responsemock notification = loader('subscriptionconfirmation') response = utils.approve_subscription(notification) mock.assert_called_with(notification['SubscribeURL']) self.assertEqual(response.status_code, 200) self.assertEqual(response.content.decode('ascii'), 'Return Value')
def test_subscription_throws_404(self): """ Test that a subscription request sent to bouncy throws a 404 if not permitted """ original_setting = getattr(settings, 'BOUNCY_AUTO_SUBSCRIBE', True) settings.BOUNCY_AUTO_SUBSCRIBE = False with self.assertRaises(Http404): notification = loader('subscriptionconfirmation') self.request._body = json.dumps(notification) views.endpoint(self.request) settings.BOUNCY_AUTO_SUBSCRIBE = original_setting
def test_bad_url(self): """Test to make sure an invalid URL isn't requested by our system""" old_setting = getattr(settings, 'BOUNCY_SUBSCRIBE_DOMAIN_REGEX', None) settings.BOUNCY_SUBSCRIBE_DOMAIN_REGEX = \ r"sns.[a-z0-9\-]+.amazonaws.com$" notification = loader('bounce_notification') notification['SubscribeURL'] = 'http://bucket.s3.amazonaws.com' result = utils.approve_subscription(notification) self.assertEqual(result.status_code, 400) self.assertEqual( result.content.decode('ascii'), 'Improper Subscription Domain') if old_setting is not None: settings.BOUNCY_SUBSCRIBE_DOMAIN_REGEX = old_setting
def test_bad_url(self): """Test to make sure an invalid URL isn't requested by our system""" old_setting = getattr(settings, 'BOUNCY_SUBSCRIBE_DOMAIN_REGEX', None) settings.BOUNCY_SUBSCRIBE_DOMAIN_REGEX = \ r"sns.[a-z0-9\-]+.amazonaws.com$" notification = loader('bounce_notification') notification['SubscribeURL'] = 'http://bucket.s3.amazonaws.com' result = utils.approve_subscription(notification) self.assertEqual(result.status_code, 400) self.assertEqual(result.content.decode('ascii'), 'Improper Subscription Domain') if old_setting is not None: settings.BOUNCY_SUBSCRIBE_DOMAIN_REGEX = old_setting
def setUp(self): """Setup the process delivery test""" self.delivery = loader('delivery') self.delivery_notification = loader('delivery_notification')
def setUp(self): self.complaint_notification = loader('complaint_notification')
def test_complaint(self, mock): """Test that a complaint is sent to process_complaint""" notification = loader('complaint_notification') views.process_message(self.complaint, notification) mock.assert_called_with(self.complaint, notification)