test_api.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 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 mock import Mock
  16. from synapse.events import EventBase
  17. from synapse.rest import admin
  18. from synapse.rest.client.v1 import login, room
  19. from synapse.types import create_requester
  20. from tests.unittest import HomeserverTestCase
  21. class ModuleApiTestCase(HomeserverTestCase):
  22. servlets = [
  23. admin.register_servlets,
  24. login.register_servlets,
  25. room.register_servlets,
  26. ]
  27. def prepare(self, reactor, clock, homeserver):
  28. self.store = homeserver.get_datastore()
  29. self.module_api = homeserver.get_module_api()
  30. self.event_creation_handler = homeserver.get_event_creation_handler()
  31. def test_can_register_user(self):
  32. """Tests that an external module can register a user"""
  33. # Register a new user
  34. user_id, access_token = self.get_success(
  35. self.module_api.register(
  36. "bob", displayname="Bobberino", emails=["bob@bobinator.bob"]
  37. )
  38. )
  39. # Check that the new user exists with all provided attributes
  40. self.assertEqual(user_id, "@bob:test")
  41. self.assertTrue(access_token)
  42. self.assertTrue(self.get_success(self.store.get_user_by_id(user_id)))
  43. # Check that the email was assigned
  44. emails = self.get_success(self.store.user_get_threepids(user_id))
  45. self.assertEqual(len(emails), 1)
  46. email = emails[0]
  47. self.assertEqual(email["medium"], "email")
  48. self.assertEqual(email["address"], "bob@bobinator.bob")
  49. # Should these be 0?
  50. self.assertEqual(email["validated_at"], 0)
  51. self.assertEqual(email["added_at"], 0)
  52. # Check that the displayname was assigned
  53. displayname = self.get_success(self.store.get_profile_displayname("bob"))
  54. self.assertEqual(displayname, "Bobberino")
  55. def test_sending_events_into_room(self):
  56. """Tests that a module can send events into a room"""
  57. # Mock out create_and_send_nonmember_event to check whether events are being sent
  58. self.event_creation_handler.create_and_send_nonmember_event = Mock(
  59. spec=[],
  60. side_effect=self.event_creation_handler.create_and_send_nonmember_event,
  61. )
  62. # Create a user and room to play with
  63. user_id = self.register_user("summer", "monkey")
  64. tok = self.login("summer", "monkey")
  65. room_id = self.helper.create_room_as(user_id, tok=tok)
  66. # Create and send a non-state event
  67. content = {"body": "I am a puppet", "msgtype": "m.text"}
  68. event_dict = {
  69. "room_id": room_id,
  70. "type": "m.room.message",
  71. "content": content,
  72. "sender": user_id,
  73. }
  74. event = self.get_success(
  75. self.module_api.create_and_send_event_into_room(event_dict)
  76. ) # type: EventBase
  77. self.assertEqual(event.sender, user_id)
  78. self.assertEqual(event.type, "m.room.message")
  79. self.assertEqual(event.room_id, room_id)
  80. self.assertFalse(hasattr(event, "state_key"))
  81. self.assertDictEqual(event.content, content)
  82. expected_requester = create_requester(
  83. user_id, authenticated_entity=self.hs.hostname
  84. )
  85. # Check that the event was sent
  86. self.event_creation_handler.create_and_send_nonmember_event.assert_called_with(
  87. expected_requester, event_dict, ratelimit=False, ignore_shadow_ban=True,
  88. )
  89. # Create and send a state event
  90. content = {
  91. "events_default": 0,
  92. "users": {user_id: 100},
  93. "state_default": 50,
  94. "users_default": 0,
  95. "events": {"test.event.type": 25},
  96. }
  97. event_dict = {
  98. "room_id": room_id,
  99. "type": "m.room.power_levels",
  100. "content": content,
  101. "sender": user_id,
  102. "state_key": "",
  103. }
  104. event = self.get_success(
  105. self.module_api.create_and_send_event_into_room(event_dict)
  106. ) # type: EventBase
  107. self.assertEqual(event.sender, user_id)
  108. self.assertEqual(event.type, "m.room.power_levels")
  109. self.assertEqual(event.room_id, room_id)
  110. self.assertEqual(event.state_key, "")
  111. self.assertDictEqual(event.content, content)
  112. # Check that the event was sent
  113. self.event_creation_handler.create_and_send_nonmember_event.assert_called_with(
  114. expected_requester,
  115. {
  116. "type": "m.room.power_levels",
  117. "content": content,
  118. "room_id": room_id,
  119. "sender": user_id,
  120. "state_key": "",
  121. },
  122. ratelimit=False,
  123. ignore_shadow_ban=True,
  124. )
  125. # Check that we can't send membership events
  126. content = {
  127. "membership": "leave",
  128. }
  129. event_dict = {
  130. "room_id": room_id,
  131. "type": "m.room.member",
  132. "content": content,
  133. "sender": user_id,
  134. "state_key": user_id,
  135. }
  136. self.get_failure(
  137. self.module_api.create_and_send_event_into_room(event_dict), Exception
  138. )
  139. def test_public_rooms(self):
  140. """Tests that a room can be added and removed from the public rooms list,
  141. as well as have its public rooms directory state queried.
  142. """
  143. # Create a user and room to play with
  144. user_id = self.register_user("kermit", "monkey")
  145. tok = self.login("kermit", "monkey")
  146. room_id = self.helper.create_room_as(user_id, tok=tok)
  147. # The room should not currently be in the public rooms directory
  148. is_in_public_rooms = self.get_success(
  149. self.module_api.public_room_list_manager.room_is_in_public_room_list(
  150. room_id
  151. )
  152. )
  153. self.assertFalse(is_in_public_rooms)
  154. # Let's try adding it to the public rooms directory
  155. self.get_success(
  156. self.module_api.public_room_list_manager.add_room_to_public_room_list(
  157. room_id
  158. )
  159. )
  160. # And checking whether it's in there...
  161. is_in_public_rooms = self.get_success(
  162. self.module_api.public_room_list_manager.room_is_in_public_room_list(
  163. room_id
  164. )
  165. )
  166. self.assertTrue(is_in_public_rooms)
  167. # Let's remove it again
  168. self.get_success(
  169. self.module_api.public_room_list_manager.remove_room_from_public_room_list(
  170. room_id
  171. )
  172. )
  173. # Should be gone
  174. is_in_public_rooms = self.get_success(
  175. self.module_api.public_room_list_manager.room_is_in_public_room_list(
  176. room_id
  177. )
  178. )
  179. self.assertFalse(is_in_public_rooms)