test_rooms.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket 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. """Tests REST events for /rooms paths."""
  16. import json
  17. from mock import Mock, NonCallableMock
  18. from six.moves.urllib import parse as urlparse
  19. from twisted.internet import defer
  20. from synapse.api.constants import Membership
  21. from synapse.rest.client.v1 import room
  22. from tests import unittest
  23. PATH_PREFIX = b"/_matrix/client/api/v1"
  24. class RoomBase(unittest.HomeserverTestCase):
  25. rmcreator_id = None
  26. servlets = [room.register_servlets, room.register_deprecated_servlets]
  27. def make_homeserver(self, reactor, clock):
  28. self.hs = self.setup_test_homeserver(
  29. "red",
  30. http_client=None,
  31. federation_client=Mock(),
  32. ratelimiter=NonCallableMock(spec_set=["send_message"]),
  33. )
  34. self.ratelimiter = self.hs.get_ratelimiter()
  35. self.ratelimiter.send_message.return_value = (True, 0)
  36. self.hs.get_federation_handler = Mock(return_value=Mock())
  37. def _insert_client_ip(*args, **kwargs):
  38. return defer.succeed(None)
  39. self.hs.get_datastore().insert_client_ip = _insert_client_ip
  40. return self.hs
  41. class RoomPermissionsTestCase(RoomBase):
  42. """ Tests room permissions. """
  43. user_id = "@sid1:red"
  44. rmcreator_id = "@notme:red"
  45. def prepare(self, reactor, clock, hs):
  46. self.helper.auth_user_id = self.rmcreator_id
  47. # create some rooms under the name rmcreator_id
  48. self.uncreated_rmid = "!aa:test"
  49. self.created_rmid = self.helper.create_room_as(
  50. self.rmcreator_id, is_public=False
  51. )
  52. self.created_public_rmid = self.helper.create_room_as(
  53. self.rmcreator_id, is_public=True
  54. )
  55. # send a message in one of the rooms
  56. self.created_rmid_msg_path = (
  57. "rooms/%s/send/m.room.message/a1" % (self.created_rmid)
  58. ).encode('ascii')
  59. request, channel = self.make_request(
  60. "PUT", self.created_rmid_msg_path, b'{"msgtype":"m.text","body":"test msg"}'
  61. )
  62. self.render(request)
  63. self.assertEquals(200, channel.code, channel.result)
  64. # set topic for public room
  65. request, channel = self.make_request(
  66. "PUT",
  67. ("rooms/%s/state/m.room.topic" % self.created_public_rmid).encode('ascii'),
  68. b'{"topic":"Public Room Topic"}',
  69. )
  70. self.render(request)
  71. self.assertEquals(200, channel.code, channel.result)
  72. # auth as user_id now
  73. self.helper.auth_user_id = self.user_id
  74. def test_send_message(self):
  75. msg_content = b'{"msgtype":"m.text","body":"hello"}'
  76. seq = iter(range(100))
  77. def send_msg_path():
  78. return "/rooms/%s/send/m.room.message/mid%s" % (
  79. self.created_rmid,
  80. str(next(seq)),
  81. )
  82. # send message in uncreated room, expect 403
  83. request, channel = self.make_request(
  84. "PUT",
  85. "/rooms/%s/send/m.room.message/mid2" % (self.uncreated_rmid,),
  86. msg_content,
  87. )
  88. self.render(request)
  89. self.assertEquals(403, channel.code, msg=channel.result["body"])
  90. # send message in created room not joined (no state), expect 403
  91. request, channel = self.make_request("PUT", send_msg_path(), msg_content)
  92. self.render(request)
  93. self.assertEquals(403, channel.code, msg=channel.result["body"])
  94. # send message in created room and invited, expect 403
  95. self.helper.invite(
  96. room=self.created_rmid, src=self.rmcreator_id, targ=self.user_id
  97. )
  98. request, channel = self.make_request("PUT", send_msg_path(), msg_content)
  99. self.render(request)
  100. self.assertEquals(403, channel.code, msg=channel.result["body"])
  101. # send message in created room and joined, expect 200
  102. self.helper.join(room=self.created_rmid, user=self.user_id)
  103. request, channel = self.make_request("PUT", send_msg_path(), msg_content)
  104. self.render(request)
  105. self.assertEquals(200, channel.code, msg=channel.result["body"])
  106. # send message in created room and left, expect 403
  107. self.helper.leave(room=self.created_rmid, user=self.user_id)
  108. request, channel = self.make_request("PUT", send_msg_path(), msg_content)
  109. self.render(request)
  110. self.assertEquals(403, channel.code, msg=channel.result["body"])
  111. def test_topic_perms(self):
  112. topic_content = b'{"topic":"My Topic Name"}'
  113. topic_path = "/rooms/%s/state/m.room.topic" % self.created_rmid
  114. # set/get topic in uncreated room, expect 403
  115. request, channel = self.make_request(
  116. "PUT", "/rooms/%s/state/m.room.topic" % self.uncreated_rmid, topic_content
  117. )
  118. self.render(request)
  119. self.assertEquals(403, channel.code, msg=channel.result["body"])
  120. request, channel = self.make_request(
  121. "GET", "/rooms/%s/state/m.room.topic" % self.uncreated_rmid
  122. )
  123. self.render(request)
  124. self.assertEquals(403, channel.code, msg=channel.result["body"])
  125. # set/get topic in created PRIVATE room not joined, expect 403
  126. request, channel = self.make_request("PUT", topic_path, topic_content)
  127. self.render(request)
  128. self.assertEquals(403, channel.code, msg=channel.result["body"])
  129. request, channel = self.make_request("GET", topic_path)
  130. self.render(request)
  131. self.assertEquals(403, channel.code, msg=channel.result["body"])
  132. # set topic in created PRIVATE room and invited, expect 403
  133. self.helper.invite(
  134. room=self.created_rmid, src=self.rmcreator_id, targ=self.user_id
  135. )
  136. request, channel = self.make_request("PUT", topic_path, topic_content)
  137. self.render(request)
  138. self.assertEquals(403, channel.code, msg=channel.result["body"])
  139. # get topic in created PRIVATE room and invited, expect 403
  140. request, channel = self.make_request("GET", topic_path)
  141. self.render(request)
  142. self.assertEquals(403, channel.code, msg=channel.result["body"])
  143. # set/get topic in created PRIVATE room and joined, expect 200
  144. self.helper.join(room=self.created_rmid, user=self.user_id)
  145. # Only room ops can set topic by default
  146. self.helper.auth_user_id = self.rmcreator_id
  147. request, channel = self.make_request("PUT", topic_path, topic_content)
  148. self.render(request)
  149. self.assertEquals(200, channel.code, msg=channel.result["body"])
  150. self.helper.auth_user_id = self.user_id
  151. request, channel = self.make_request("GET", topic_path)
  152. self.render(request)
  153. self.assertEquals(200, channel.code, msg=channel.result["body"])
  154. self.assert_dict(json.loads(topic_content.decode('utf8')), channel.json_body)
  155. # set/get topic in created PRIVATE room and left, expect 403
  156. self.helper.leave(room=self.created_rmid, user=self.user_id)
  157. request, channel = self.make_request("PUT", topic_path, topic_content)
  158. self.render(request)
  159. self.assertEquals(403, channel.code, msg=channel.result["body"])
  160. request, channel = self.make_request("GET", topic_path)
  161. self.render(request)
  162. self.assertEquals(200, channel.code, msg=channel.result["body"])
  163. # get topic in PUBLIC room, not joined, expect 403
  164. request, channel = self.make_request(
  165. "GET", "/rooms/%s/state/m.room.topic" % self.created_public_rmid
  166. )
  167. self.render(request)
  168. self.assertEquals(403, channel.code, msg=channel.result["body"])
  169. # set topic in PUBLIC room, not joined, expect 403
  170. request, channel = self.make_request(
  171. "PUT",
  172. "/rooms/%s/state/m.room.topic" % self.created_public_rmid,
  173. topic_content,
  174. )
  175. self.render(request)
  176. self.assertEquals(403, channel.code, msg=channel.result["body"])
  177. def _test_get_membership(self, room=None, members=[], expect_code=None):
  178. for member in members:
  179. path = "/rooms/%s/state/m.room.member/%s" % (room, member)
  180. request, channel = self.make_request("GET", path)
  181. self.render(request)
  182. self.assertEquals(expect_code, channel.code)
  183. def test_membership_basic_room_perms(self):
  184. # === room does not exist ===
  185. room = self.uncreated_rmid
  186. # get membership of self, get membership of other, uncreated room
  187. # expect all 403s
  188. self._test_get_membership(
  189. members=[self.user_id, self.rmcreator_id], room=room, expect_code=403
  190. )
  191. # trying to invite people to this room should 403
  192. self.helper.invite(
  193. room=room, src=self.user_id, targ=self.rmcreator_id, expect_code=403
  194. )
  195. # set [invite/join/left] of self, set [invite/join/left] of other,
  196. # expect all 404s because room doesn't exist on any server
  197. for usr in [self.user_id, self.rmcreator_id]:
  198. self.helper.join(room=room, user=usr, expect_code=404)
  199. self.helper.leave(room=room, user=usr, expect_code=404)
  200. def test_membership_private_room_perms(self):
  201. room = self.created_rmid
  202. # get membership of self, get membership of other, private room + invite
  203. # expect all 403s
  204. self.helper.invite(room=room, src=self.rmcreator_id, targ=self.user_id)
  205. self._test_get_membership(
  206. members=[self.user_id, self.rmcreator_id], room=room, expect_code=403
  207. )
  208. # get membership of self, get membership of other, private room + joined
  209. # expect all 200s
  210. self.helper.join(room=room, user=self.user_id)
  211. self._test_get_membership(
  212. members=[self.user_id, self.rmcreator_id], room=room, expect_code=200
  213. )
  214. # get membership of self, get membership of other, private room + left
  215. # expect all 200s
  216. self.helper.leave(room=room, user=self.user_id)
  217. self._test_get_membership(
  218. members=[self.user_id, self.rmcreator_id], room=room, expect_code=200
  219. )
  220. def test_membership_public_room_perms(self):
  221. room = self.created_public_rmid
  222. # get membership of self, get membership of other, public room + invite
  223. # expect 403
  224. self.helper.invite(room=room, src=self.rmcreator_id, targ=self.user_id)
  225. self._test_get_membership(
  226. members=[self.user_id, self.rmcreator_id], room=room, expect_code=403
  227. )
  228. # get membership of self, get membership of other, public room + joined
  229. # expect all 200s
  230. self.helper.join(room=room, user=self.user_id)
  231. self._test_get_membership(
  232. members=[self.user_id, self.rmcreator_id], room=room, expect_code=200
  233. )
  234. # get membership of self, get membership of other, public room + left
  235. # expect 200.
  236. self.helper.leave(room=room, user=self.user_id)
  237. self._test_get_membership(
  238. members=[self.user_id, self.rmcreator_id], room=room, expect_code=200
  239. )
  240. def test_invited_permissions(self):
  241. room = self.created_rmid
  242. self.helper.invite(room=room, src=self.rmcreator_id, targ=self.user_id)
  243. # set [invite/join/left] of other user, expect 403s
  244. self.helper.invite(
  245. room=room, src=self.user_id, targ=self.rmcreator_id, expect_code=403
  246. )
  247. self.helper.change_membership(
  248. room=room,
  249. src=self.user_id,
  250. targ=self.rmcreator_id,
  251. membership=Membership.JOIN,
  252. expect_code=403,
  253. )
  254. self.helper.change_membership(
  255. room=room,
  256. src=self.user_id,
  257. targ=self.rmcreator_id,
  258. membership=Membership.LEAVE,
  259. expect_code=403,
  260. )
  261. def test_joined_permissions(self):
  262. room = self.created_rmid
  263. self.helper.invite(room=room, src=self.rmcreator_id, targ=self.user_id)
  264. self.helper.join(room=room, user=self.user_id)
  265. # set invited of self, expect 403
  266. self.helper.invite(
  267. room=room, src=self.user_id, targ=self.user_id, expect_code=403
  268. )
  269. # set joined of self, expect 200 (NOOP)
  270. self.helper.join(room=room, user=self.user_id)
  271. other = "@burgundy:red"
  272. # set invited of other, expect 200
  273. self.helper.invite(room=room, src=self.user_id, targ=other, expect_code=200)
  274. # set joined of other, expect 403
  275. self.helper.change_membership(
  276. room=room,
  277. src=self.user_id,
  278. targ=other,
  279. membership=Membership.JOIN,
  280. expect_code=403,
  281. )
  282. # set left of other, expect 403
  283. self.helper.change_membership(
  284. room=room,
  285. src=self.user_id,
  286. targ=other,
  287. membership=Membership.LEAVE,
  288. expect_code=403,
  289. )
  290. # set left of self, expect 200
  291. self.helper.leave(room=room, user=self.user_id)
  292. def test_leave_permissions(self):
  293. room = self.created_rmid
  294. self.helper.invite(room=room, src=self.rmcreator_id, targ=self.user_id)
  295. self.helper.join(room=room, user=self.user_id)
  296. self.helper.leave(room=room, user=self.user_id)
  297. # set [invite/join/left] of self, set [invite/join/left] of other,
  298. # expect all 403s
  299. for usr in [self.user_id, self.rmcreator_id]:
  300. self.helper.change_membership(
  301. room=room,
  302. src=self.user_id,
  303. targ=usr,
  304. membership=Membership.INVITE,
  305. expect_code=403,
  306. )
  307. self.helper.change_membership(
  308. room=room,
  309. src=self.user_id,
  310. targ=usr,
  311. membership=Membership.JOIN,
  312. expect_code=403,
  313. )
  314. # It is always valid to LEAVE if you've already left (currently.)
  315. self.helper.change_membership(
  316. room=room,
  317. src=self.user_id,
  318. targ=self.rmcreator_id,
  319. membership=Membership.LEAVE,
  320. expect_code=403,
  321. )
  322. class RoomsMemberListTestCase(RoomBase):
  323. """ Tests /rooms/$room_id/members/list REST events."""
  324. user_id = "@sid1:red"
  325. def test_get_member_list(self):
  326. room_id = self.helper.create_room_as(self.user_id)
  327. request, channel = self.make_request("GET", "/rooms/%s/members" % room_id)
  328. self.render(request)
  329. self.assertEquals(200, channel.code, msg=channel.result["body"])
  330. def test_get_member_list_no_room(self):
  331. request, channel = self.make_request("GET", "/rooms/roomdoesnotexist/members")
  332. self.render(request)
  333. self.assertEquals(403, channel.code, msg=channel.result["body"])
  334. def test_get_member_list_no_permission(self):
  335. room_id = self.helper.create_room_as("@some_other_guy:red")
  336. request, channel = self.make_request("GET", "/rooms/%s/members" % room_id)
  337. self.render(request)
  338. self.assertEquals(403, channel.code, msg=channel.result["body"])
  339. def test_get_member_list_mixed_memberships(self):
  340. room_creator = "@some_other_guy:red"
  341. room_id = self.helper.create_room_as(room_creator)
  342. room_path = "/rooms/%s/members" % room_id
  343. self.helper.invite(room=room_id, src=room_creator, targ=self.user_id)
  344. # can't see list if you're just invited.
  345. request, channel = self.make_request("GET", room_path)
  346. self.render(request)
  347. self.assertEquals(403, channel.code, msg=channel.result["body"])
  348. self.helper.join(room=room_id, user=self.user_id)
  349. # can see list now joined
  350. request, channel = self.make_request("GET", room_path)
  351. self.render(request)
  352. self.assertEquals(200, channel.code, msg=channel.result["body"])
  353. self.helper.leave(room=room_id, user=self.user_id)
  354. # can see old list once left
  355. request, channel = self.make_request("GET", room_path)
  356. self.render(request)
  357. self.assertEquals(200, channel.code, msg=channel.result["body"])
  358. class RoomsCreateTestCase(RoomBase):
  359. """ Tests /rooms and /rooms/$room_id REST events. """
  360. user_id = "@sid1:red"
  361. def test_post_room_no_keys(self):
  362. # POST with no config keys, expect new room id
  363. request, channel = self.make_request("POST", "/createRoom", "{}")
  364. self.render(request)
  365. self.assertEquals(200, channel.code, channel.result)
  366. self.assertTrue("room_id" in channel.json_body)
  367. def test_post_room_visibility_key(self):
  368. # POST with visibility config key, expect new room id
  369. request, channel = self.make_request(
  370. "POST", "/createRoom", b'{"visibility":"private"}'
  371. )
  372. self.render(request)
  373. self.assertEquals(200, channel.code)
  374. self.assertTrue("room_id" in channel.json_body)
  375. def test_post_room_custom_key(self):
  376. # POST with custom config keys, expect new room id
  377. request, channel = self.make_request(
  378. "POST", "/createRoom", b'{"custom":"stuff"}'
  379. )
  380. self.render(request)
  381. self.assertEquals(200, channel.code)
  382. self.assertTrue("room_id" in channel.json_body)
  383. def test_post_room_known_and_unknown_keys(self):
  384. # POST with custom + known config keys, expect new room id
  385. request, channel = self.make_request(
  386. "POST", "/createRoom", b'{"visibility":"private","custom":"things"}'
  387. )
  388. self.render(request)
  389. self.assertEquals(200, channel.code)
  390. self.assertTrue("room_id" in channel.json_body)
  391. def test_post_room_invalid_content(self):
  392. # POST with invalid content / paths, expect 400
  393. request, channel = self.make_request("POST", "/createRoom", b'{"visibili')
  394. self.render(request)
  395. self.assertEquals(400, channel.code)
  396. request, channel = self.make_request("POST", "/createRoom", b'["hello"]')
  397. self.render(request)
  398. self.assertEquals(400, channel.code)
  399. class RoomTopicTestCase(RoomBase):
  400. """ Tests /rooms/$room_id/topic REST events. """
  401. user_id = "@sid1:red"
  402. def prepare(self, reactor, clock, hs):
  403. # create the room
  404. self.room_id = self.helper.create_room_as(self.user_id)
  405. self.path = "/rooms/%s/state/m.room.topic" % (self.room_id,)
  406. def test_invalid_puts(self):
  407. # missing keys or invalid json
  408. request, channel = self.make_request("PUT", self.path, '{}')
  409. self.render(request)
  410. self.assertEquals(400, channel.code, msg=channel.result["body"])
  411. request, channel = self.make_request("PUT", self.path, '{"_name":"bo"}')
  412. self.render(request)
  413. self.assertEquals(400, channel.code, msg=channel.result["body"])
  414. request, channel = self.make_request("PUT", self.path, '{"nao')
  415. self.render(request)
  416. self.assertEquals(400, channel.code, msg=channel.result["body"])
  417. request, channel = self.make_request(
  418. "PUT", self.path, '[{"_name":"bo"},{"_name":"jill"}]'
  419. )
  420. self.render(request)
  421. self.assertEquals(400, channel.code, msg=channel.result["body"])
  422. request, channel = self.make_request("PUT", self.path, 'text only')
  423. self.render(request)
  424. self.assertEquals(400, channel.code, msg=channel.result["body"])
  425. request, channel = self.make_request("PUT", self.path, '')
  426. self.render(request)
  427. self.assertEquals(400, channel.code, msg=channel.result["body"])
  428. # valid key, wrong type
  429. content = '{"topic":["Topic name"]}'
  430. request, channel = self.make_request("PUT", self.path, content)
  431. self.render(request)
  432. self.assertEquals(400, channel.code, msg=channel.result["body"])
  433. def test_rooms_topic(self):
  434. # nothing should be there
  435. request, channel = self.make_request("GET", self.path)
  436. self.render(request)
  437. self.assertEquals(404, channel.code, msg=channel.result["body"])
  438. # valid put
  439. content = '{"topic":"Topic name"}'
  440. request, channel = self.make_request("PUT", self.path, content)
  441. self.render(request)
  442. self.assertEquals(200, channel.code, msg=channel.result["body"])
  443. # valid get
  444. request, channel = self.make_request("GET", self.path)
  445. self.render(request)
  446. self.assertEquals(200, channel.code, msg=channel.result["body"])
  447. self.assert_dict(json.loads(content), channel.json_body)
  448. def test_rooms_topic_with_extra_keys(self):
  449. # valid put with extra keys
  450. content = '{"topic":"Seasons","subtopic":"Summer"}'
  451. request, channel = self.make_request("PUT", self.path, content)
  452. self.render(request)
  453. self.assertEquals(200, channel.code, msg=channel.result["body"])
  454. # valid get
  455. request, channel = self.make_request("GET", self.path)
  456. self.render(request)
  457. self.assertEquals(200, channel.code, msg=channel.result["body"])
  458. self.assert_dict(json.loads(content), channel.json_body)
  459. class RoomMemberStateTestCase(RoomBase):
  460. """ Tests /rooms/$room_id/members/$user_id/state REST events. """
  461. user_id = "@sid1:red"
  462. def prepare(self, reactor, clock, hs):
  463. self.room_id = self.helper.create_room_as(self.user_id)
  464. def test_invalid_puts(self):
  465. path = "/rooms/%s/state/m.room.member/%s" % (self.room_id, self.user_id)
  466. # missing keys or invalid json
  467. request, channel = self.make_request("PUT", path, '{}')
  468. self.render(request)
  469. self.assertEquals(400, channel.code, msg=channel.result["body"])
  470. request, channel = self.make_request("PUT", path, '{"_name":"bo"}')
  471. self.render(request)
  472. self.assertEquals(400, channel.code, msg=channel.result["body"])
  473. request, channel = self.make_request("PUT", path, '{"nao')
  474. self.render(request)
  475. self.assertEquals(400, channel.code, msg=channel.result["body"])
  476. request, channel = self.make_request(
  477. "PUT", path, b'[{"_name":"bo"},{"_name":"jill"}]'
  478. )
  479. self.render(request)
  480. self.assertEquals(400, channel.code, msg=channel.result["body"])
  481. request, channel = self.make_request("PUT", path, 'text only')
  482. self.render(request)
  483. self.assertEquals(400, channel.code, msg=channel.result["body"])
  484. request, channel = self.make_request("PUT", path, '')
  485. self.render(request)
  486. self.assertEquals(400, channel.code, msg=channel.result["body"])
  487. # valid keys, wrong types
  488. content = '{"membership":["%s","%s","%s"]}' % (
  489. Membership.INVITE,
  490. Membership.JOIN,
  491. Membership.LEAVE,
  492. )
  493. request, channel = self.make_request("PUT", path, content.encode('ascii'))
  494. self.render(request)
  495. self.assertEquals(400, channel.code, msg=channel.result["body"])
  496. def test_rooms_members_self(self):
  497. path = "/rooms/%s/state/m.room.member/%s" % (
  498. urlparse.quote(self.room_id),
  499. self.user_id,
  500. )
  501. # valid join message (NOOP since we made the room)
  502. content = '{"membership":"%s"}' % Membership.JOIN
  503. request, channel = self.make_request("PUT", path, content.encode('ascii'))
  504. self.render(request)
  505. self.assertEquals(200, channel.code, msg=channel.result["body"])
  506. request, channel = self.make_request("GET", path, None)
  507. self.render(request)
  508. self.assertEquals(200, channel.code, msg=channel.result["body"])
  509. expected_response = {"membership": Membership.JOIN}
  510. self.assertEquals(expected_response, channel.json_body)
  511. def test_rooms_members_other(self):
  512. self.other_id = "@zzsid1:red"
  513. path = "/rooms/%s/state/m.room.member/%s" % (
  514. urlparse.quote(self.room_id),
  515. self.other_id,
  516. )
  517. # valid invite message
  518. content = '{"membership":"%s"}' % Membership.INVITE
  519. request, channel = self.make_request("PUT", path, content)
  520. self.render(request)
  521. self.assertEquals(200, channel.code, msg=channel.result["body"])
  522. request, channel = self.make_request("GET", path, None)
  523. self.render(request)
  524. self.assertEquals(200, channel.code, msg=channel.result["body"])
  525. self.assertEquals(json.loads(content), channel.json_body)
  526. def test_rooms_members_other_custom_keys(self):
  527. self.other_id = "@zzsid1:red"
  528. path = "/rooms/%s/state/m.room.member/%s" % (
  529. urlparse.quote(self.room_id),
  530. self.other_id,
  531. )
  532. # valid invite message with custom key
  533. content = '{"membership":"%s","invite_text":"%s"}' % (
  534. Membership.INVITE,
  535. "Join us!",
  536. )
  537. request, channel = self.make_request("PUT", path, content)
  538. self.render(request)
  539. self.assertEquals(200, channel.code, msg=channel.result["body"])
  540. request, channel = self.make_request("GET", path, None)
  541. self.render(request)
  542. self.assertEquals(200, channel.code, msg=channel.result["body"])
  543. self.assertEquals(json.loads(content), channel.json_body)
  544. class RoomMessagesTestCase(RoomBase):
  545. """ Tests /rooms/$room_id/messages/$user_id/$msg_id REST events. """
  546. user_id = "@sid1:red"
  547. def prepare(self, reactor, clock, hs):
  548. self.room_id = self.helper.create_room_as(self.user_id)
  549. def test_invalid_puts(self):
  550. path = "/rooms/%s/send/m.room.message/mid1" % (urlparse.quote(self.room_id))
  551. # missing keys or invalid json
  552. request, channel = self.make_request("PUT", path, b'{}')
  553. self.render(request)
  554. self.assertEquals(400, channel.code, msg=channel.result["body"])
  555. request, channel = self.make_request("PUT", path, b'{"_name":"bo"}')
  556. self.render(request)
  557. self.assertEquals(400, channel.code, msg=channel.result["body"])
  558. request, channel = self.make_request("PUT", path, b'{"nao')
  559. self.render(request)
  560. self.assertEquals(400, channel.code, msg=channel.result["body"])
  561. request, channel = self.make_request(
  562. "PUT", path, b'[{"_name":"bo"},{"_name":"jill"}]'
  563. )
  564. self.render(request)
  565. self.assertEquals(400, channel.code, msg=channel.result["body"])
  566. request, channel = self.make_request("PUT", path, b'text only')
  567. self.render(request)
  568. self.assertEquals(400, channel.code, msg=channel.result["body"])
  569. request, channel = self.make_request("PUT", path, b'')
  570. self.render(request)
  571. self.assertEquals(400, channel.code, msg=channel.result["body"])
  572. def test_rooms_messages_sent(self):
  573. path = "/rooms/%s/send/m.room.message/mid1" % (urlparse.quote(self.room_id))
  574. content = b'{"body":"test","msgtype":{"type":"a"}}'
  575. request, channel = self.make_request("PUT", path, content)
  576. self.render(request)
  577. self.assertEquals(400, channel.code, msg=channel.result["body"])
  578. # custom message types
  579. content = b'{"body":"test","msgtype":"test.custom.text"}'
  580. request, channel = self.make_request("PUT", path, content)
  581. self.render(request)
  582. self.assertEquals(200, channel.code, msg=channel.result["body"])
  583. # m.text message type
  584. path = "/rooms/%s/send/m.room.message/mid2" % (urlparse.quote(self.room_id))
  585. content = b'{"body":"test2","msgtype":"m.text"}'
  586. request, channel = self.make_request("PUT", path, content)
  587. self.render(request)
  588. self.assertEquals(200, channel.code, msg=channel.result["body"])
  589. class RoomInitialSyncTestCase(RoomBase):
  590. """ Tests /rooms/$room_id/initialSync. """
  591. user_id = "@sid1:red"
  592. def prepare(self, reactor, clock, hs):
  593. # create the room
  594. self.room_id = self.helper.create_room_as(self.user_id)
  595. def test_initial_sync(self):
  596. request, channel = self.make_request(
  597. "GET", "/rooms/%s/initialSync" % self.room_id
  598. )
  599. self.render(request)
  600. self.assertEquals(200, channel.code)
  601. self.assertEquals(self.room_id, channel.json_body["room_id"])
  602. self.assertEquals("join", channel.json_body["membership"])
  603. # Room state is easier to assert on if we unpack it into a dict
  604. state = {}
  605. for event in channel.json_body["state"]:
  606. if "state_key" not in event:
  607. continue
  608. t = event["type"]
  609. if t not in state:
  610. state[t] = []
  611. state[t].append(event)
  612. self.assertTrue("m.room.create" in state)
  613. self.assertTrue("messages" in channel.json_body)
  614. self.assertTrue("chunk" in channel.json_body["messages"])
  615. self.assertTrue("end" in channel.json_body["messages"])
  616. self.assertTrue("presence" in channel.json_body)
  617. presence_by_user = {
  618. e["content"]["user_id"]: e for e in channel.json_body["presence"]
  619. }
  620. self.assertTrue(self.user_id in presence_by_user)
  621. self.assertEquals("m.presence", presence_by_user[self.user_id]["type"])
  622. class RoomMessageListTestCase(RoomBase):
  623. """ Tests /rooms/$room_id/messages REST events. """
  624. user_id = "@sid1:red"
  625. def prepare(self, reactor, clock, hs):
  626. self.room_id = self.helper.create_room_as(self.user_id)
  627. def test_topo_token_is_accepted(self):
  628. token = "t1-0_0_0_0_0_0_0_0_0"
  629. request, channel = self.make_request(
  630. "GET", "/rooms/%s/messages?access_token=x&from=%s" % (self.room_id, token)
  631. )
  632. self.render(request)
  633. self.assertEquals(200, channel.code)
  634. self.assertTrue("start" in channel.json_body)
  635. self.assertEquals(token, channel.json_body['start'])
  636. self.assertTrue("chunk" in channel.json_body)
  637. self.assertTrue("end" in channel.json_body)
  638. def test_stream_token_is_accepted_for_fwd_pagianation(self):
  639. token = "s0_0_0_0_0_0_0_0_0"
  640. request, channel = self.make_request(
  641. "GET", "/rooms/%s/messages?access_token=x&from=%s" % (self.room_id, token)
  642. )
  643. self.render(request)
  644. self.assertEquals(200, channel.code)
  645. self.assertTrue("start" in channel.json_body)
  646. self.assertEquals(token, channel.json_body['start'])
  647. self.assertTrue("chunk" in channel.json_body)
  648. self.assertTrue("end" in channel.json_body)