test_register.py 2.8 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 twisted.internet import defer
  16. from .. import unittest
  17. from synapse.handlers.register import RegistrationHandler
  18. from synapse.types import UserID, create_requester
  19. from tests.utils import setup_test_homeserver
  20. from mock import Mock
  21. class RegistrationHandlers(object):
  22. def __init__(self, hs):
  23. self.registration_handler = RegistrationHandler(hs)
  24. class RegistrationTestCase(unittest.TestCase):
  25. """ Tests the RegistrationHandler. """
  26. @defer.inlineCallbacks
  27. def setUp(self):
  28. self.mock_distributor = Mock()
  29. self.mock_distributor.declare("registered_user")
  30. self.mock_captcha_client = Mock()
  31. self.hs = yield setup_test_homeserver(
  32. handlers=None,
  33. http_client=None,
  34. expire_access_token=True,
  35. profile_handler=Mock(),
  36. )
  37. self.macaroon_generator = Mock(
  38. generate_access_token=Mock(return_value='secret'))
  39. self.hs.get_macaroon_generator = Mock(return_value=self.macaroon_generator)
  40. self.hs.handlers = RegistrationHandlers(self.hs)
  41. self.handler = self.hs.get_handlers().registration_handler
  42. @defer.inlineCallbacks
  43. def test_user_is_created_and_logged_in_if_doesnt_exist(self):
  44. local_part = "someone"
  45. display_name = "someone"
  46. user_id = "@someone:test"
  47. requester = create_requester("@as:test")
  48. result_user_id, result_token = yield self.handler.get_or_create_user(
  49. requester, local_part, display_name)
  50. self.assertEquals(result_user_id, user_id)
  51. self.assertEquals(result_token, 'secret')
  52. @defer.inlineCallbacks
  53. def test_if_user_exists(self):
  54. store = self.hs.get_datastore()
  55. frank = UserID.from_string("@frank:test")
  56. yield store.register(
  57. user_id=frank.to_string(),
  58. token="jkv;g498752-43gj['eamb!-5",
  59. password_hash=None)
  60. local_part = "frank"
  61. display_name = "Frank"
  62. user_id = "@frank:test"
  63. requester = create_requester("@as:test")
  64. result_user_id, result_token = yield self.handler.get_or_create_user(
  65. requester, local_part, display_name)
  66. self.assertEquals(result_user_id, user_id)
  67. self.assertEquals(result_token, 'secret')