def testInit_invalid(self): """Tests that build channel initialization validates the provider.""" config = ndb_models.BuildChannelConfig( id='channel_id', provider_name='unknown_provider') channel = build.BuildChannel(config) self.assertEqual(channel.id, 'channel_id') self.assertFalse(channel.is_valid) self.assertEqual(channel.auth_state, ndb_models.AuthorizationState.NOT_APPLICABLE)
def testInit(self): """Tests that build channel can be initialized.""" config = ndb_models.BuildChannelConfig(id='channel_id', provider_name='url_provider') # Channel is valid and has instantiated its provider channel = build.BuildChannel(config) self.assertTrue(channel.is_valid) self.assertIsInstance(channel.provider, UrlBuildProvider) self.assertEqual(channel.auth_state, ndb_models.AuthorizationState.NOT_APPLICABLE)
def testInit_unauthorized(self): """Tests that build channel can detect its authorization state.""" config = ndb_models.BuildChannelConfig(id='channel_id', provider_name='oauth2_provider') # Channel is valid but unauthorized channel = build.BuildChannel(config) self.assertEqual(channel.id, 'channel_id') self.assertTrue(channel.is_valid) self.assertIsInstance(channel.provider, OAuth2BuildProvider) self.assertEqual(channel.auth_state, ndb_models.AuthorizationState.UNAUTHORIZED) self.assertIsNone(channel.provider.GetCredentials())
def testListBuildChannels(self, mock_get_build_channel): config = ndb_models.BuildChannelConfig( id='google_cloud_storage', name='GCS', provider_name='Google Cloud Storage') channel = build.BuildChannel(config) mock_get_build_channel.return_value = channel res = self.app.get('/_ah/api/mtt/v1/config_sets/build_channels') res_msg = protojson.decode_message(messages.BuildChannelList, res.body) self.assertEqual(len(res_msg.build_channels), 1) self.assertEqual(res_msg.build_channels[0].id, 'google_cloud_storage') self.assertEqual(res_msg.build_channels[0].name, 'GCS')
def testAuthMethods(self): config = ndb_models.BuildChannelConfig(id='channel_id', provider_name='oauth2_provider') # Channel is valid but unauthorized channel = build.BuildChannel(config) self.assertEqual(channel.id, 'channel_id') self.assertTrue(channel.is_valid) self.assertIsInstance(channel.provider, OAuth2BuildProvider) self.assertEqual([ ndb_models.AuthorizationMethod.OAUTH2_AUTHORIZATION_CODE, ndb_models.AuthorizationMethod.OAUTH2_SERVICE_ACCOUNT ], channel.auth_methods) self.assertEqual(channel.auth_state, ndb_models.AuthorizationState.UNAUTHORIZED) self.assertIsNone(channel.provider.GetCredentials())
def testInit_withDefaultCredentials(self): """Tests that build channels inherits the default auth credentials.""" credentials = authorized_user.Credentials(None) private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.default_credentials = credentials private_node_config.put() config = ndb_models.BuildChannelConfig(id='channel_id', provider_name='oauth2_provider') channel = build.BuildChannel(config) self.assertEqual(channel.id, 'channel_id') self.assertTrue(channel.is_valid) self.assertIsInstance(channel.provider, OAuth2BuildProvider) self.assertEqual(channel.auth_state, ndb_models.AuthorizationState.AUTHORIZED) self.assertIsNotNone(channel.provider.GetCredentials())