def test_list_user_excludes_secret(self): user = factory.make_User() config_keys = list(CONFIG_ITEMS.keys()) + ["maas_url", "uuid"] handler = ConfigHandler(user, {}, None) self.assertNotIn("rpc_shared_secret", config_keys) self.assertCountEqual(config_keys, [item["name"] for item in handler.list({})])
def test_get_must_be_in_config_items(self): allowed_keys = set(CONFIG_ITEMS.keys()) all_keys = set(get_default_config().keys()) not_allowed_keys = all_keys.difference(allowed_keys) key = random.choice(list(not_allowed_keys)) user = factory.make_User() handler = ConfigHandler(user, {}, None) self.assertRaises(HandlerDoesNotExistError, handler.get, {"name": key})
def test_list_admin_includes_all_config(self): admin = factory.make_admin() config_keys = list(CONFIG_ITEMS.keys()) + [ "maas_url", "uuid", "rpc_shared_secret", ] handler = ConfigHandler(admin, {}, None) self.assertCountEqual(config_keys, [item["name"] for item in handler.list({})])
def test_dehydrate_no_choice_config(self): no_choice_name = random.choice( list(name for name in CONFIG_ITEMS.keys() if not hasattr(get_config_field(name), "choices"))) Config.objects.set_config(no_choice_name, "myvalue") user = factory.make_User() handler = ConfigHandler(user, {}, None) dehydrated = handler.dehydrate_configs([no_choice_name])[0] self.assertEqual(no_choice_name, dehydrated["name"]) self.assertEqual("myvalue", dehydrated["value"]) self.assertNotIn("choices", dehydrated)
def list(self, params): """List all the configuration values.""" defaults = get_default_config() config_keys = CONFIG_ITEMS.keys() config_objs = Config.objects.filter(name__in=config_keys) config_objs = {obj.name: obj for obj in config_objs} self.cache["loaded_pks"].update(config_keys) config_keys = [{ 'name': key, 'value': (config_objs[key].value if key in config_objs else defaults.get(key, '')), } for key in config_keys] return self._include_choices(config_keys)
def test_dehydrate_choice_config(self): choice_name, choices = random.choice( list((name, get_config_field(name).choices) for name in CONFIG_ITEMS.keys() if hasattr(get_config_field(name), "choices"))) choice_value = random.choice([value for value, _ in choices]) Config.objects.set_config(choice_name, choice_value) user = factory.make_User() handler = ConfigHandler(user, {}, None) dehydrated = handler.dehydrate_configs([choice_name])[0] self.assertEqual(choice_name, dehydrated["name"]) self.assertEqual(choice_value, dehydrated["value"]) self.assertEqual(choices, dehydrated["choices"])
def dehydrated_all_configs(self): defaults = get_default_config() config_keys = CONFIG_ITEMS.keys() config_objs = Config.objects.filter(name__in=config_keys) config_objs = {obj.name: obj for obj in config_objs} config_keys = [{ "name": key, "value": (config_objs[key].value if key in config_objs else defaults.get(key, "")), } for key in config_keys] for config_key in config_keys: config_field = get_config_field(config_key["name"]) if hasattr(config_field, "choices"): config_key["choices"] = config_field.choices return config_keys
def dehydrated_all_configs(self): defaults = get_default_config() config_keys = CONFIG_ITEMS.keys() config_objs = Config.objects.filter(name__in=config_keys) config_objs = {obj.name: obj for obj in config_objs} config_keys = [{ 'name': key, 'value': (config_objs[key].value if key in config_objs else defaults.get(key, '')) } for key in config_keys] for config_key in config_keys: config_field = get_config_field(config_key['name']) if hasattr(config_field, 'choices'): config_key['choices'] = config_field.choices return config_keys
def get_config_keys(user): config_keys = list(CONFIG_ITEMS.keys()) + ["uuid", "maas_url"] if user.is_superuser: config_keys.append("rpc_shared_secret") return config_keys