test_room.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import synapse
  2. from synapse.api.constants import EventTypes, RoomEncryptionAlgorithms
  3. from synapse.rest.client import login, room
  4. from tests import unittest
  5. from tests.unittest import override_config
  6. class EncryptedByDefaultTestCase(unittest.HomeserverTestCase):
  7. servlets = [
  8. login.register_servlets,
  9. synapse.rest.admin.register_servlets_for_client_rest_resource,
  10. room.register_servlets,
  11. ]
  12. @override_config({"encryption_enabled_by_default_for_room_type": "all"})
  13. def test_encrypted_by_default_config_option_all(self):
  14. """Tests that invite-only and non-invite-only rooms have encryption enabled by
  15. default when the config option encryption_enabled_by_default_for_room_type is "all".
  16. """
  17. # Create a user
  18. user = self.register_user("user", "pass")
  19. user_token = self.login(user, "pass")
  20. # Create an invite-only room as that user
  21. room_id = self.helper.create_room_as(user, is_public=False, tok=user_token)
  22. # Check that the room has an encryption state event
  23. event_content = self.helper.get_state(
  24. room_id=room_id,
  25. event_type=EventTypes.RoomEncryption,
  26. tok=user_token,
  27. )
  28. self.assertEqual(event_content, {"algorithm": RoomEncryptionAlgorithms.DEFAULT})
  29. # Create a non invite-only room as that user
  30. room_id = self.helper.create_room_as(user, is_public=True, tok=user_token)
  31. # Check that the room has an encryption state event
  32. event_content = self.helper.get_state(
  33. room_id=room_id,
  34. event_type=EventTypes.RoomEncryption,
  35. tok=user_token,
  36. )
  37. self.assertEqual(event_content, {"algorithm": RoomEncryptionAlgorithms.DEFAULT})
  38. @override_config({"encryption_enabled_by_default_for_room_type": "invite"})
  39. def test_encrypted_by_default_config_option_invite(self):
  40. """Tests that only new, invite-only rooms have encryption enabled by default when
  41. the config option encryption_enabled_by_default_for_room_type is "invite".
  42. """
  43. # Create a user
  44. user = self.register_user("user", "pass")
  45. user_token = self.login(user, "pass")
  46. # Create an invite-only room as that user
  47. room_id = self.helper.create_room_as(user, is_public=False, tok=user_token)
  48. # Check that the room has an encryption state event
  49. event_content = self.helper.get_state(
  50. room_id=room_id,
  51. event_type=EventTypes.RoomEncryption,
  52. tok=user_token,
  53. )
  54. self.assertEqual(event_content, {"algorithm": RoomEncryptionAlgorithms.DEFAULT})
  55. # Create a non invite-only room as that user
  56. room_id = self.helper.create_room_as(user, is_public=True, tok=user_token)
  57. # Check that the room does not have an encryption state event
  58. self.helper.get_state(
  59. room_id=room_id,
  60. event_type=EventTypes.RoomEncryption,
  61. tok=user_token,
  62. expect_code=404,
  63. )
  64. @override_config({"encryption_enabled_by_default_for_room_type": "off"})
  65. def test_encrypted_by_default_config_option_off(self):
  66. """Tests that neither new invite-only nor non-invite-only rooms have encryption
  67. enabled by default when the config option
  68. encryption_enabled_by_default_for_room_type is "off".
  69. """
  70. # Create a user
  71. user = self.register_user("user", "pass")
  72. user_token = self.login(user, "pass")
  73. # Create an invite-only room as that user
  74. room_id = self.helper.create_room_as(user, is_public=False, tok=user_token)
  75. # Check that the room does not have an encryption state event
  76. self.helper.get_state(
  77. room_id=room_id,
  78. event_type=EventTypes.RoomEncryption,
  79. tok=user_token,
  80. expect_code=404,
  81. )
  82. # Create a non invite-only room as that user
  83. room_id = self.helper.create_room_as(user, is_public=True, tok=user_token)
  84. # Check that the room does not have an encryption state event
  85. self.helper.get_state(
  86. room_id=room_id,
  87. event_type=EventTypes.RoomEncryption,
  88. tok=user_token,
  89. expect_code=404,
  90. )