test_api.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from unittest import TestCase as StdlibTestCase
  2. import yaml
  3. from synapse.config import ConfigError
  4. from synapse.config.api import ApiConfig
  5. from synapse.types.state import StateFilter
  6. DEFAULT_PREJOIN_STATE_PAIRS = {
  7. ("m.room.join_rules", ""),
  8. ("m.room.canonical_alias", ""),
  9. ("m.room.avatar", ""),
  10. ("m.room.encryption", ""),
  11. ("m.room.name", ""),
  12. ("m.room.create", ""),
  13. ("m.room.topic", ""),
  14. }
  15. class TestRoomPrejoinState(StdlibTestCase):
  16. def read_config(self, source: str) -> ApiConfig:
  17. config = ApiConfig()
  18. config.read_config(yaml.safe_load(source))
  19. return config
  20. def test_no_prejoin_state(self) -> None:
  21. config = self.read_config("foo: bar")
  22. self.assertFalse(config.room_prejoin_state.has_wildcards())
  23. self.assertEqual(
  24. set(config.room_prejoin_state.concrete_types()), DEFAULT_PREJOIN_STATE_PAIRS
  25. )
  26. def test_disable_default_event_types(self) -> None:
  27. config = self.read_config(
  28. """
  29. room_prejoin_state:
  30. disable_default_event_types: true
  31. """
  32. )
  33. self.assertEqual(config.room_prejoin_state, StateFilter.none())
  34. def test_event_without_state_key(self) -> None:
  35. config = self.read_config(
  36. """
  37. room_prejoin_state:
  38. disable_default_event_types: true
  39. additional_event_types:
  40. - foo
  41. """
  42. )
  43. self.assertEqual(config.room_prejoin_state.wildcard_types(), ["foo"])
  44. self.assertEqual(config.room_prejoin_state.concrete_types(), [])
  45. def test_event_with_specific_state_key(self) -> None:
  46. config = self.read_config(
  47. """
  48. room_prejoin_state:
  49. disable_default_event_types: true
  50. additional_event_types:
  51. - [foo, bar]
  52. """
  53. )
  54. self.assertFalse(config.room_prejoin_state.has_wildcards())
  55. self.assertEqual(
  56. set(config.room_prejoin_state.concrete_types()),
  57. {("foo", "bar")},
  58. )
  59. def test_repeated_event_with_specific_state_key(self) -> None:
  60. config = self.read_config(
  61. """
  62. room_prejoin_state:
  63. disable_default_event_types: true
  64. additional_event_types:
  65. - [foo, bar]
  66. - [foo, baz]
  67. """
  68. )
  69. self.assertFalse(config.room_prejoin_state.has_wildcards())
  70. self.assertEqual(
  71. set(config.room_prejoin_state.concrete_types()),
  72. {("foo", "bar"), ("foo", "baz")},
  73. )
  74. def test_no_specific_state_key_overrides_specific_state_key(self) -> None:
  75. config = self.read_config(
  76. """
  77. room_prejoin_state:
  78. disable_default_event_types: true
  79. additional_event_types:
  80. - [foo, bar]
  81. - foo
  82. """
  83. )
  84. self.assertEqual(config.room_prejoin_state.wildcard_types(), ["foo"])
  85. self.assertEqual(config.room_prejoin_state.concrete_types(), [])
  86. config = self.read_config(
  87. """
  88. room_prejoin_state:
  89. disable_default_event_types: true
  90. additional_event_types:
  91. - foo
  92. - [foo, bar]
  93. """
  94. )
  95. self.assertEqual(config.room_prejoin_state.wildcard_types(), ["foo"])
  96. self.assertEqual(config.room_prejoin_state.concrete_types(), [])
  97. def test_bad_event_type_entry_raises(self) -> None:
  98. with self.assertRaises(ConfigError):
  99. self.read_config(
  100. """
  101. room_prejoin_state:
  102. additional_event_types:
  103. - []
  104. """
  105. )
  106. with self.assertRaises(ConfigError):
  107. self.read_config(
  108. """
  109. room_prejoin_state:
  110. additional_event_types:
  111. - [a]
  112. """
  113. )
  114. with self.assertRaises(ConfigError):
  115. self.read_config(
  116. """
  117. room_prejoin_state:
  118. additional_event_types:
  119. - [a, b, c]
  120. """
  121. )
  122. with self.assertRaises(ConfigError):
  123. self.read_config(
  124. """
  125. room_prejoin_state:
  126. additional_event_types:
  127. - [true, 1.23]
  128. """
  129. )