test_util.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.config import ConfigError
  15. from synapse.config._util import validate_config
  16. from tests.unittest import TestCase
  17. class ValidateConfigTestCase(TestCase):
  18. """Test cases for synapse.config._util.validate_config"""
  19. def test_bad_object_in_array(self) -> None:
  20. """malformed objects within an array should be validated correctly"""
  21. # consider a structure:
  22. #
  23. # array_of_objs:
  24. # - r: 1
  25. # foo: 2
  26. #
  27. # - r: 2
  28. # bar: 3
  29. #
  30. # ... where each entry must contain an "r": check that the path
  31. # to the required item is correclty reported.
  32. schema = {
  33. "type": "object",
  34. "properties": {
  35. "array_of_objs": {
  36. "type": "array",
  37. "items": {"type": "object", "required": ["r"]},
  38. },
  39. },
  40. }
  41. with self.assertRaises(ConfigError) as c:
  42. validate_config(schema, {"array_of_objs": [{}]}, ("base",))
  43. self.assertEqual(c.exception.path, ["base", "array_of_objs", "<item 0>"])