utils.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. import json
  16. import time
  17. import attr
  18. from synapse.api.constants import Membership
  19. from tests.server import make_request, render
  20. @attr.s
  21. class RestHelper(object):
  22. """Contains extra helper functions to quickly and clearly perform a given
  23. REST action, which isn't the focus of the test.
  24. """
  25. hs = attr.ib()
  26. resource = attr.ib()
  27. auth_user_id = attr.ib()
  28. def create_room_as(self, room_creator, is_public=True, tok=None):
  29. temp_id = self.auth_user_id
  30. self.auth_user_id = room_creator
  31. path = "/_matrix/client/r0/createRoom"
  32. content = {}
  33. if not is_public:
  34. content["visibility"] = "private"
  35. if tok:
  36. path = path + "?access_token=%s" % tok
  37. request, channel = make_request(
  38. self.hs.get_reactor(), "POST", path, json.dumps(content).encode("utf8")
  39. )
  40. render(request, self.resource, self.hs.get_reactor())
  41. assert channel.result["code"] == b"200", channel.result
  42. self.auth_user_id = temp_id
  43. return channel.json_body["room_id"]
  44. def invite(self, room=None, src=None, targ=None, expect_code=200, tok=None):
  45. self.change_membership(
  46. room=room,
  47. src=src,
  48. targ=targ,
  49. tok=tok,
  50. membership=Membership.INVITE,
  51. expect_code=expect_code,
  52. )
  53. def join(self, room=None, user=None, expect_code=200, tok=None):
  54. self.change_membership(
  55. room=room,
  56. src=user,
  57. targ=user,
  58. tok=tok,
  59. membership=Membership.JOIN,
  60. expect_code=expect_code,
  61. )
  62. def leave(self, room=None, user=None, expect_code=200, tok=None):
  63. self.change_membership(
  64. room=room,
  65. src=user,
  66. targ=user,
  67. tok=tok,
  68. membership=Membership.LEAVE,
  69. expect_code=expect_code,
  70. )
  71. def change_membership(self, room, src, targ, membership, tok=None, expect_code=200):
  72. temp_id = self.auth_user_id
  73. self.auth_user_id = src
  74. path = "/_matrix/client/r0/rooms/%s/state/m.room.member/%s" % (room, targ)
  75. if tok:
  76. path = path + "?access_token=%s" % tok
  77. data = {"membership": membership}
  78. request, channel = make_request(
  79. self.hs.get_reactor(), "PUT", path, json.dumps(data).encode("utf8")
  80. )
  81. render(request, self.resource, self.hs.get_reactor())
  82. assert int(channel.result["code"]) == expect_code, (
  83. "Expected: %d, got: %d, resp: %r"
  84. % (expect_code, int(channel.result["code"]), channel.result["body"])
  85. )
  86. self.auth_user_id = temp_id
  87. def send(self, room_id, body=None, txn_id=None, tok=None, expect_code=200):
  88. if txn_id is None:
  89. txn_id = "m%s" % (str(time.time()))
  90. if body is None:
  91. body = "body_text_here"
  92. path = "/_matrix/client/r0/rooms/%s/send/m.room.message/%s" % (room_id, txn_id)
  93. content = {"msgtype": "m.text", "body": body}
  94. if tok:
  95. path = path + "?access_token=%s" % tok
  96. request, channel = make_request(
  97. self.hs.get_reactor(), "PUT", path, json.dumps(content).encode("utf8")
  98. )
  99. render(request, self.resource, self.hs.get_reactor())
  100. assert int(channel.result["code"]) == expect_code, (
  101. "Expected: %d, got: %d, resp: %r"
  102. % (expect_code, int(channel.result["code"]), channel.result["body"])
  103. )
  104. return channel.json_body
  105. def send_state(self, room_id, event_type, body, tok, expect_code=200, state_key=""):
  106. path = "/_matrix/client/r0/rooms/%s/state/%s/%s" % (
  107. room_id,
  108. event_type,
  109. state_key,
  110. )
  111. if tok:
  112. path = path + "?access_token=%s" % tok
  113. request, channel = make_request(
  114. self.hs.get_reactor(), "PUT", path, json.dumps(body).encode("utf8")
  115. )
  116. render(request, self.resource, self.hs.get_reactor())
  117. assert int(channel.result["code"]) == expect_code, (
  118. "Expected: %d, got: %d, resp: %r"
  119. % (expect_code, int(channel.result["code"]), channel.result["body"])
  120. )
  121. return channel.json_body