def test_fetch_configs_ok(self): fetches = { 'imports.cfg': ('imports_cfg_rev', 'tarball{url:"a" systems:"b"}'), 'ip_whitelist.cfg': ('ip_whitelist_cfg_rev', config_pb2.IPWhitelistConfig()), 'oauth.cfg': ('oauth_cfg_rev', config_pb2.OAuthConfig(primary_client_id='a')), } @ndb.tasklet def get_self_config_mock(path, *_args, **_kwargs): self.assertIn(path, fetches) raise ndb.Return(fetches.pop(path)) self.mock(config_component, 'get_self_config_async', get_self_config_mock) self.mock(config, '_get_configs_url', lambda: 'http://url') result = config._fetch_configs(fetches.keys()) self.assertFalse(fetches) self.assertEqual( { 'imports.cfg': (config.Revision('imports_cfg_rev', 'http://url'), 'tarball{url:"a" systems:"b"}'), 'ip_whitelist.cfg': (config.Revision('ip_whitelist_cfg_rev', 'http://url'), config_pb2.IPWhitelistConfig()), 'oauth.cfg': (config.Revision('oauth_cfg_rev', 'http://url'), config_pb2.OAuthConfig(primary_client_id='a')), }, result)
def test_update_oauth_config(self): self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5)) @ndb.transactional def run(conf): return config._update_oauth_config( config.Revision('oauth_cfg_rev', 'http://url'), conf) model.AuthGlobalConfig(key=model.root_key()).put() # Pushing empty config to empty state -> no changes. self.assertFalse(run(config_pb2.OAuthConfig())) # Updating config. self.assertTrue( run( config_pb2.OAuthConfig(primary_client_id='a', primary_client_secret='b', client_ids=['c', 'd']))) self.assertEqual( { 'auth_db_rev': 1, 'auth_db_prev_rev': None, 'modified_by': model.get_service_self_identity(), 'modified_ts': datetime.datetime(2014, 1, 2, 3, 4, 5), 'oauth_additional_client_ids': ['c', 'd'], 'oauth_client_id': 'a', 'oauth_client_secret': 'b', }, model.root_key().get().to_dict()) # Same config again -> no changes. self.assertFalse( run( config_pb2.OAuthConfig(primary_client_id='a', primary_client_secret='b', client_ids=['c', 'd'])))
def test_validate_oauth_config(self): with self.assertRaises(ValueError): config._validate_oauth_config( config_pb2.OAuthConfig( primary_client_id='a', primary_client_secret='b', client_ids=['c', 'd'], token_server_url='https://not-root-url/abc/def'))
def test_update_oauth_config(self): def run(conf): return config._update_authdb_configs({ 'oauth.cfg': (config.Revision('oauth_cfg_rev', 'http://url'), conf), }) # Pushing empty config to empty state -> no changes. self.assertFalse(run(config_pb2.OAuthConfig())) # Updating config. self.assertTrue( run( config_pb2.OAuthConfig( primary_client_id='a', primary_client_secret='b', client_ids=['c', 'd'], token_server_url='https://token-server'))) self.assertEqual( { 'auth_db_rev': 1, 'auth_db_prev_rev': 0, 'modified_by': model.get_service_self_identity(), 'modified_ts': datetime.datetime(2014, 1, 2, 3, 4, 5), 'oauth_additional_client_ids': [u'c', u'd'], 'oauth_client_id': u'a', 'oauth_client_secret': u'b', 'security_config': None, 'token_server_url': u'https://token-server', }, model.root_key().get().to_dict()) # Same config again -> no changes. self.assertFalse( run( config_pb2.OAuthConfig( primary_client_id='a', primary_client_secret='b', client_ids=['c', 'd'], token_server_url='https://token-server')))
def oauth_cfg(client_id): return config_pb2.OAuthConfig(primary_client_id=client_id)