third_party_rules.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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 synapse.rest import admin
  16. from synapse.rest.client.v1 import login, room
  17. from tests import unittest
  18. class ThirdPartyRulesTestModule(object):
  19. def __init__(self, config):
  20. pass
  21. def check_event_allowed(self, event, context):
  22. if event.type == "foo.bar.forbidden":
  23. return False
  24. else:
  25. return True
  26. @staticmethod
  27. def parse_config(config):
  28. return config
  29. class ThirdPartyRulesTestCase(unittest.HomeserverTestCase):
  30. servlets = [
  31. admin.register_servlets,
  32. login.register_servlets,
  33. room.register_servlets,
  34. ]
  35. def make_homeserver(self, reactor, clock):
  36. config = self.default_config()
  37. config["third_party_event_rules"] = {
  38. "module": "tests.rest.client.third_party_rules.ThirdPartyRulesTestModule",
  39. "config": {},
  40. }
  41. self.hs = self.setup_test_homeserver(config=config)
  42. return self.hs
  43. def test_third_party_rules(self):
  44. """Tests that a forbidden event is forbidden from being sent, but an allowed one
  45. can be sent.
  46. """
  47. user_id = self.register_user("kermit", "monkey")
  48. tok = self.login("kermit", "monkey")
  49. room_id = self.helper.create_room_as(user_id, tok=tok)
  50. request, channel = self.make_request(
  51. "PUT",
  52. "/_matrix/client/r0/rooms/%s/send/foo.bar.allowed/1" % room_id,
  53. {},
  54. access_token=tok,
  55. )
  56. self.render(request)
  57. self.assertEquals(channel.result["code"], b"200", channel.result)
  58. request, channel = self.make_request(
  59. "PUT",
  60. "/_matrix/client/r0/rooms/%s/send/foo.bar.forbidden/1" % room_id,
  61. {},
  62. access_token=tok,
  63. )
  64. self.render(request)
  65. self.assertEquals(channel.result["code"], b"403", channel.result)