def test_publish_notifications(self): """Tests publish notifications.""" self.mox.StubOutWithMock(pubsub_utils, '_get_pubsub_service') msg = _create_sample_message() pubsub_utils._get_pubsub_service().AndReturn(MockedPubSub( self, 'test_topic', msg, pubsub_utils._PUBSUB_NUM_RETRIES, # use tuple ('123') instead of list just for easy to write the test. ret_val = {'messageIds', ('123')})) self.mox.ReplayAll() pubsub_utils.publish_notifications( 'test_topic', [msg]) self.mox.VerifyAll()
def test_get_pubsub_service_no_service_account(self): """Test getting the pubsub service""" self.mox.StubOutWithMock(os.path, 'isfile') os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(False) self.mox.ReplayAll() pubsub = pubsub_utils._get_pubsub_service() self.assertIsNone(pubsub) self.mox.VerifyAll()
def test_get_pubsub_service_with_invalid_service_account(self): """Test getting the pubsub service""" self.mox.StubOutWithMock(os.path, 'isfile') self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) credentials = self.mox.CreateMock(GoogleCredentials) GoogleCredentials.from_stream( pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndRaise( ApplicationDefaultCredentialsError()) self.mox.ReplayAll() pubsub = pubsub_utils._get_pubsub_service() self.assertIsNone(pubsub) self.mox.VerifyAll()
def test_get_pubsub_service_with_invalid_service_account(self): """Test getting the pubsub service""" self.mox.StubOutWithMock(os.path, 'isfile') self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) credentials = self.mox.CreateMock(GoogleCredentials) GoogleCredentials.from_stream( pubsub_utils.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() pubsub = pubsub_utils._get_pubsub_service() self.assertIsNone(pubsub) self.mox.VerifyAll()