def setUp(self): super(BuildTest, self).setUp() # Create two build channel configurations (one valid and one invalid) ndb_models.BuildChannelConfig(id='channel_id', name='valid', provider_name='url_provider').put() ndb_models.BuildChannelConfig(id='invalid_id', name='invalid', provider_name='unknown_provider').put()
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())
def setUp(self): super(TestPlanApiTest, self).setUp(test_plan_api.TestPlanApi) self.mock_test = ndb_models.Test(name='mock_test', command='command') self.mock_test.put() self.mock_test_id = str(self.mock_test.key.id()) self.mock_build_channel = ndb_models.BuildChannelConfig( id=str(uuid.uuid4()), name='mock_build_channel', provider_name='mock_build_provider') self.mock_build_channel.put() self.mock_build_channel_id = self.mock_build_channel.key.id() self.mock_device_action = ndb_models.DeviceAction( name='mock_device_action') self.mock_device_action.put() self.mock_device_action_id = str(self.mock_device_action.key.id()) build_item = base.BuildItem(name='zz', path='/foo/bar/zz', is_file=True) self.mock_test_resource_url = build.BuildUrl( self.mock_build_channel_id, build_item)
def AddBuildChannel(name, provider_name, options): """Adds a new build channel. Args: name: a build channel name. provider_name: a build provider name. options: a option dict. Returns: a newly created ndb_models.BuildChannelConfig object. """ provider_class = GetBuildProviderClass(provider_name) if not provider_class: raise errors.PluginError( 'Unknown provider %s' % provider_name, http_status=400) provider = provider_class() # Validate options. provider.UpdateOptions(**options) new_config = ndb_models.BuildChannelConfig( id=str(uuid.uuid4()), name=name, provider_name=provider_name, options=ndb_models.NameValuePair.FromDict(options)) new_config.put() return new_config
def _BuildChannelConfigMessageConverter(msg): return ndb_models.BuildChannelConfig( key=ConvertToKeyOrNone(ndb_models.BuildChannelConfig, msg.id), name=msg.name, provider_name=msg.provider_name, options=ConvertNameValuePairs(msg.options, ndb_models.NameValuePair))