Example #1
0
    def test_publish_notifications(self):
        """Test getting the pubsub service"""
        self.mox.StubOutWithMock(os.path, 'isfile')
        self.mox.StubOutWithMock(GoogleCredentials, 'from_stream')
        os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True)
        credentials = self.mox.CreateMock(GoogleCredentials)
        GoogleCredentials.from_stream(
            _TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(credentials)
        credentials.create_scoped_required().AndReturn(True)
        credentials.create_scoped(
            pubsub_utils.PUBSUB_SCOPES).AndReturn(credentials)
        self.mox.StubOutWithMock(discovery, 'build')
        msg = _create_sample_message()
        discovery.build(
            pubsub_utils.PUBSUB_SERVICE_NAME,
            pubsub_utils.PUBSUB_VERSION,
            credentials=credentials
        ).AndReturn(
            MockedPubSub(
                self,
                'test_topic',
                msg,
                pubsub_utils.DEFAULT_PUBSUB_NUM_RETRIES,
                # use tuple ('123') instead of list just for easy to
                # write the test.
                ret_val={'messageIds': ('123')}))

        self.mox.ReplayAll()
        pubsub_client = pubsub_utils.PubSubClient(
            _TEST_CLOUD_SERVICE_ACCOUNT_FILE)
        msg_ids = pubsub_client.publish_notifications('test_topic', [msg])
        self.assertEquals(('123'), msg_ids)

        self.mox.VerifyAll()
Example #2
0
 def test_pubsub_with_no_service_account(self):
     """Test getting the pubsub service"""
     self.mox.StubOutWithMock(os.path, 'isfile')
     self.mox.ReplayAll()
     with self.assertRaises(pubsub_utils.PubSubException):
         pubsub_utils.PubSubClient()
     self.mox.VerifyAll()
Example #3
0
 def test_pubsub_with_non_existing_service_account(self):
     """Test getting the pubsub service"""
     self.mox.StubOutWithMock(os.path, 'isfile')
     os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(False)
     self.mox.ReplayAll()
     with self.assertRaises(pubsub_utils.PubSubException):
         pubsub_utils.PubSubClient(_TEST_CLOUD_SERVICE_ACCOUNT_FILE)
     self.mox.VerifyAll()
Example #4
0
 def test_pubsub_with_corrupted_service_account(self):
     """Test pubsub with corrupted service account."""
     self.mox.StubOutWithMock(os.path, 'isfile')
     self.mox.StubOutWithMock(GoogleCredentials, 'from_stream')
     os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True)
     GoogleCredentials.from_stream(
         _TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndRaise(
             ApplicationDefaultCredentialsError())
     self.mox.ReplayAll()
     with self.assertRaises(pubsub_utils.PubSubException):
         pubsub_utils.PubSubClient(_TEST_CLOUD_SERVICE_ACCOUNT_FILE)
     self.mox.VerifyAll()
Example #5
0
 def test_pubsub_with_invalid_service_account(self):
     """Test pubsubwith invalid service account."""
     self.mox.StubOutWithMock(os.path, 'isfile')
     self.mox.StubOutWithMock(GoogleCredentials, 'from_stream')
     os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True)
     credentials = self.mox.CreateMock(GoogleCredentials)
     GoogleCredentials.from_stream(
         _TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(credentials)
     credentials.create_scoped_required().AndReturn(True)
     credentials.create_scoped(
         pubsub_utils.PUBSUB_SCOPES).AndReturn(credentials)
     self.mox.StubOutWithMock(discovery, 'build')
     discovery.build(pubsub_utils.PUBSUB_SERVICE_NAME,
                     pubsub_utils.PUBSUB_VERSION,
                     credentials=credentials).AndRaise(
                         UnknownApiNameOrVersion())
     self.mox.ReplayAll()
     with self.assertRaises(pubsub_utils.PubSubException):
         msg = _create_sample_message()
         pubsub_client = pubsub_utils.PubSubClient(
             _TEST_CLOUD_SERVICE_ACCOUNT_FILE)
         pubsub_client.publish_notifications('test_topic', [msg])
     self.mox.VerifyAll()