utils.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2017 Vector Creations Ltd
  4. # Copyright 2018-2019 New Vector Ltd
  5. # Copyright 2019 The Matrix.org Foundation C.I.C.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. import json
  19. import time
  20. import attr
  21. from twisted.web.resource import Resource
  22. from synapse.api.constants import Membership
  23. from tests.server import make_request, render
  24. @attr.s
  25. class RestHelper(object):
  26. """Contains extra helper functions to quickly and clearly perform a given
  27. REST action, which isn't the focus of the test.
  28. """
  29. hs = attr.ib()
  30. resource = attr.ib()
  31. auth_user_id = attr.ib()
  32. def create_room_as(self, room_creator, is_public=True, tok=None):
  33. temp_id = self.auth_user_id
  34. self.auth_user_id = room_creator
  35. path = "/_matrix/client/r0/createRoom"
  36. content = {}
  37. if not is_public:
  38. content["visibility"] = "private"
  39. if tok:
  40. path = path + "?access_token=%s" % tok
  41. request, channel = make_request(
  42. self.hs.get_reactor(), "POST", path, json.dumps(content).encode("utf8")
  43. )
  44. render(request, self.resource, self.hs.get_reactor())
  45. assert channel.result["code"] == b"200", channel.result
  46. self.auth_user_id = temp_id
  47. return channel.json_body["room_id"]
  48. def invite(self, room=None, src=None, targ=None, expect_code=200, tok=None):
  49. self.change_membership(
  50. room=room,
  51. src=src,
  52. targ=targ,
  53. tok=tok,
  54. membership=Membership.INVITE,
  55. expect_code=expect_code,
  56. )
  57. def join(self, room=None, user=None, expect_code=200, tok=None):
  58. self.change_membership(
  59. room=room,
  60. src=user,
  61. targ=user,
  62. tok=tok,
  63. membership=Membership.JOIN,
  64. expect_code=expect_code,
  65. )
  66. def leave(self, room=None, user=None, expect_code=200, tok=None):
  67. self.change_membership(
  68. room=room,
  69. src=user,
  70. targ=user,
  71. tok=tok,
  72. membership=Membership.LEAVE,
  73. expect_code=expect_code,
  74. )
  75. def change_membership(self, room, src, targ, membership, tok=None, expect_code=200):
  76. temp_id = self.auth_user_id
  77. self.auth_user_id = src
  78. path = "/_matrix/client/r0/rooms/%s/state/m.room.member/%s" % (room, targ)
  79. if tok:
  80. path = path + "?access_token=%s" % tok
  81. data = {"membership": membership}
  82. request, channel = make_request(
  83. self.hs.get_reactor(), "PUT", path, json.dumps(data).encode("utf8")
  84. )
  85. render(request, self.resource, self.hs.get_reactor())
  86. assert int(channel.result["code"]) == expect_code, (
  87. "Expected: %d, got: %d, resp: %r"
  88. % (expect_code, int(channel.result["code"]), channel.result["body"])
  89. )
  90. self.auth_user_id = temp_id
  91. def send(self, room_id, body=None, txn_id=None, tok=None, expect_code=200):
  92. if body is None:
  93. body = "body_text_here"
  94. content = {"msgtype": "m.text", "body": body}
  95. return self.send_event(
  96. room_id, "m.room.message", content, txn_id, tok, expect_code
  97. )
  98. def send_event(
  99. self, room_id, type, content={}, txn_id=None, tok=None, expect_code=200
  100. ):
  101. if txn_id is None:
  102. txn_id = "m%s" % (str(time.time()))
  103. path = "/_matrix/client/r0/rooms/%s/send/%s/%s" % (room_id, type, txn_id)
  104. if tok:
  105. path = path + "?access_token=%s" % tok
  106. request, channel = make_request(
  107. self.hs.get_reactor(), "PUT", path, json.dumps(content).encode("utf8")
  108. )
  109. render(request, self.resource, self.hs.get_reactor())
  110. assert int(channel.result["code"]) == expect_code, (
  111. "Expected: %d, got: %d, resp: %r"
  112. % (expect_code, int(channel.result["code"]), channel.result["body"])
  113. )
  114. return channel.json_body
  115. def send_state(self, room_id, event_type, body, tok, expect_code=200, state_key=""):
  116. path = "/_matrix/client/r0/rooms/%s/state/%s/%s" % (
  117. room_id,
  118. event_type,
  119. state_key,
  120. )
  121. if tok:
  122. path = path + "?access_token=%s" % tok
  123. request, channel = make_request(
  124. self.hs.get_reactor(), "PUT", path, json.dumps(body).encode("utf8")
  125. )
  126. render(request, self.resource, self.hs.get_reactor())
  127. assert int(channel.result["code"]) == expect_code, (
  128. "Expected: %d, got: %d, resp: %r"
  129. % (expect_code, int(channel.result["code"]), channel.result["body"])
  130. )
  131. return channel.json_body
  132. def upload_media(
  133. self,
  134. resource: Resource,
  135. image_data: bytes,
  136. tok: str,
  137. filename: str = "test.png",
  138. expect_code: int = 200,
  139. ) -> dict:
  140. """Upload a piece of test media to the media repo
  141. Args:
  142. resource: The resource that will handle the upload request
  143. image_data: The image data to upload
  144. tok: The user token to use during the upload
  145. filename: The filename of the media to be uploaded
  146. expect_code: The return code to expect from attempting to upload the media
  147. """
  148. image_length = len(image_data)
  149. path = "/_matrix/media/r0/upload?filename=%s" % (filename,)
  150. request, channel = make_request(
  151. self.hs.get_reactor(), "POST", path, content=image_data, access_token=tok
  152. )
  153. request.requestHeaders.addRawHeader(
  154. b"Content-Length", str(image_length).encode("UTF-8")
  155. )
  156. request.render(resource)
  157. self.hs.get_reactor().pump([100])
  158. assert channel.code == expect_code, "Expected: %d, got: %d, resp: %r" % (
  159. expect_code,
  160. int(channel.result["code"]),
  161. channel.result["body"],
  162. )
  163. return channel.json_body