utils.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. # twisted imports
  16. from twisted.internet import defer
  17. # trial imports
  18. from tests import unittest
  19. from synapse.api.constants import Membership
  20. import json
  21. import time
  22. class RestTestCase(unittest.TestCase):
  23. """Contains extra helper functions to quickly and clearly perform a given
  24. REST action, which isn't the focus of the test.
  25. This subclass assumes there are mock_resource and auth_user_id attributes.
  26. """
  27. def __init__(self, *args, **kwargs):
  28. super(RestTestCase, self).__init__(*args, **kwargs)
  29. self.mock_resource = None
  30. self.auth_user_id = None
  31. @defer.inlineCallbacks
  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 = "/createRoom"
  36. content = "{}"
  37. if not is_public:
  38. content = '{"visibility":"private"}'
  39. if tok:
  40. path = path + "?access_token=%s" % tok
  41. (code, response) = yield self.mock_resource.trigger("POST", path, content)
  42. self.assertEquals(200, code, msg=str(response))
  43. self.auth_user_id = temp_id
  44. defer.returnValue(response["room_id"])
  45. @defer.inlineCallbacks
  46. def invite(self, room=None, src=None, targ=None, expect_code=200, tok=None):
  47. yield self.change_membership(room=room, src=src, targ=targ, tok=tok,
  48. membership=Membership.INVITE,
  49. expect_code=expect_code)
  50. @defer.inlineCallbacks
  51. def join(self, room=None, user=None, expect_code=200, tok=None):
  52. yield self.change_membership(room=room, src=user, targ=user, tok=tok,
  53. membership=Membership.JOIN,
  54. expect_code=expect_code)
  55. @defer.inlineCallbacks
  56. def leave(self, room=None, user=None, expect_code=200, tok=None):
  57. yield self.change_membership(room=room, src=user, targ=user, tok=tok,
  58. membership=Membership.LEAVE,
  59. expect_code=expect_code)
  60. @defer.inlineCallbacks
  61. def change_membership(self, room, src, targ, membership, tok=None,
  62. expect_code=200):
  63. temp_id = self.auth_user_id
  64. self.auth_user_id = src
  65. path = "/rooms/%s/state/m.room.member/%s" % (room, targ)
  66. if tok:
  67. path = path + "?access_token=%s" % tok
  68. data = {
  69. "membership": membership
  70. }
  71. (code, response) = yield self.mock_resource.trigger(
  72. "PUT", path, json.dumps(data)
  73. )
  74. self.assertEquals(expect_code, code, msg=str(response))
  75. self.auth_user_id = temp_id
  76. @defer.inlineCallbacks
  77. def register(self, user_id):
  78. (code, response) = yield self.mock_resource.trigger(
  79. "POST",
  80. "/register",
  81. json.dumps({
  82. "user": user_id,
  83. "password": "test",
  84. "type": "m.login.password"
  85. }))
  86. self.assertEquals(200, code)
  87. defer.returnValue(response)
  88. @defer.inlineCallbacks
  89. def send(self, room_id, body=None, txn_id=None, tok=None,
  90. expect_code=200):
  91. if txn_id is None:
  92. txn_id = "m%s" % (str(time.time()))
  93. if body is None:
  94. body = "body_text_here"
  95. path = "/rooms/%s/send/m.room.message/%s" % (room_id, txn_id)
  96. content = '{"msgtype":"m.text","body":"%s"}' % body
  97. if tok:
  98. path = path + "?access_token=%s" % tok
  99. (code, response) = yield self.mock_resource.trigger("PUT", path, content)
  100. self.assertEquals(expect_code, code, msg=str(response))
  101. def assert_dict(self, required, actual):
  102. """Does a partial assert of a dict.
  103. Args:
  104. required (dict): The keys and value which MUST be in 'actual'.
  105. actual (dict): The test result. Extra keys will not be checked.
  106. """
  107. for key in required:
  108. self.assertEquals(required[key], actual[key],
  109. msg="%s mismatch. %s" % (key, actual))