test_domainrulecheck.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from tests import unittest
  16. from synapse.config._base import ConfigError
  17. from synapse.rulecheck.domain_rule_checker import DomainRuleChecker
  18. class DomainRuleCheckerTestCase(unittest.TestCase):
  19. def test_allowed(self):
  20. config = {
  21. "default": False,
  22. "domain_mapping": {
  23. "source_one": ["target_one", "target_two"],
  24. "source_two": ["target_two"]
  25. }
  26. }
  27. check = DomainRuleChecker(config)
  28. self.assertTrue(check.user_may_invite("test:source_one",
  29. "test:target_one", "room"))
  30. self.assertTrue(check.user_may_invite("test:source_one",
  31. "test:target_two", "room"))
  32. self.assertTrue(check.user_may_invite("test:source_two",
  33. "test:target_two", "room"))
  34. def test_disallowed(self):
  35. config = {
  36. "default": True,
  37. "domain_mapping": {
  38. "source_one": ["target_one", "target_two"],
  39. "source_two": ["target_two"],
  40. "source_four": []
  41. }
  42. }
  43. check = DomainRuleChecker(config)
  44. self.assertFalse(check.user_may_invite("test:source_one",
  45. "test:target_three", "room"))
  46. self.assertFalse(check.user_may_invite("test:source_two",
  47. "test:target_three", "room"))
  48. self.assertFalse(check.user_may_invite("test:source_two",
  49. "test:target_one", "room"))
  50. self.assertFalse(check.user_may_invite("test:source_four",
  51. "test:target_one", "room"))
  52. def test_default_allow(self):
  53. config = {
  54. "default": True,
  55. "domain_mapping": {
  56. "source_one": ["target_one", "target_two"],
  57. "source_two": ["target_two"]
  58. }
  59. }
  60. check = DomainRuleChecker(config)
  61. self.assertTrue(check.user_may_invite("test:source_three",
  62. "test:target_one", "room"))
  63. def test_default_deny(self):
  64. config = {
  65. "default": False,
  66. "domain_mapping": {
  67. "source_one": ["target_one", "target_two"],
  68. "source_two": ["target_two"]
  69. }
  70. }
  71. check = DomainRuleChecker(config)
  72. self.assertFalse(check.user_may_invite("test:source_three",
  73. "test:target_one", "room"))
  74. def test_config_parse(self):
  75. config = {
  76. "default": False,
  77. "domain_mapping": {
  78. "source_one": ["target_one", "target_two"],
  79. "source_two": ["target_two"]
  80. }
  81. }
  82. self.assertEquals(config, DomainRuleChecker.parse_config(config))
  83. def test_config_parse_failure(self):
  84. config = {
  85. "domain_mapping": {
  86. "source_one": ["target_one", "target_two"],
  87. "source_two": ["target_two"]
  88. }
  89. }
  90. self.assertRaises(ConfigError, DomainRuleChecker.parse_config, config)