1
0

test_register.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 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. from synapse.rest.client.v1.register import CreateUserRestServlet
  16. from twisted.internet import defer
  17. from mock import Mock
  18. from tests import unittest
  19. from tests.utils import mock_getRawHeaders
  20. import json
  21. class CreateUserServletTestCase(unittest.TestCase):
  22. def setUp(self):
  23. # do the dance to hook up request data to self.request_data
  24. self.request_data = ""
  25. self.request = Mock(
  26. content=Mock(read=Mock(side_effect=lambda: self.request_data)),
  27. path='/_matrix/client/api/v1/createUser'
  28. )
  29. self.request.args = {}
  30. self.request.requestHeaders.getRawHeaders = mock_getRawHeaders()
  31. self.registration_handler = Mock()
  32. self.appservice = Mock(sender="@as:test")
  33. self.datastore = Mock(
  34. get_app_service_by_token=Mock(return_value=self.appservice)
  35. )
  36. # do the dance to hook things up to the hs global
  37. handlers = Mock(
  38. registration_handler=self.registration_handler,
  39. )
  40. self.hs = Mock()
  41. self.hs.hostname = "superbig~testing~thing.com"
  42. self.hs.get_datastore = Mock(return_value=self.datastore)
  43. self.hs.get_handlers = Mock(return_value=handlers)
  44. self.servlet = CreateUserRestServlet(self.hs)
  45. @defer.inlineCallbacks
  46. def test_POST_createuser_with_valid_user(self):
  47. user_id = "@someone:interesting"
  48. token = "my token"
  49. self.request.args = {
  50. "access_token": "i_am_an_app_service"
  51. }
  52. self.request_data = json.dumps({
  53. "localpart": "someone",
  54. "displayname": "someone interesting",
  55. "duration_seconds": 200
  56. })
  57. self.registration_handler.get_or_create_user = Mock(
  58. return_value=(user_id, token)
  59. )
  60. (code, result) = yield self.servlet.on_POST(self.request)
  61. self.assertEquals(code, 200)
  62. det_data = {
  63. "user_id": user_id,
  64. "access_token": token,
  65. "home_server": self.hs.hostname
  66. }
  67. self.assertDictContainsSubset(det_data, result)