1
0

test_register.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 mock import Mock
  16. from twisted.internet import defer
  17. from synapse.api.errors import RegistrationError
  18. from synapse.handlers.register import RegistrationHandler
  19. from synapse.types import UserID, create_requester
  20. from tests.utils import setup_test_homeserver
  21. from .. import unittest
  22. class RegistrationHandlers(object):
  23. def __init__(self, hs):
  24. self.registration_handler = RegistrationHandler(hs)
  25. class RegistrationTestCase(unittest.TestCase):
  26. """ Tests the RegistrationHandler. """
  27. @defer.inlineCallbacks
  28. def setUp(self):
  29. self.mock_distributor = Mock()
  30. self.mock_distributor.declare("registered_user")
  31. self.mock_captcha_client = Mock()
  32. self.hs = yield setup_test_homeserver(
  33. handlers=None,
  34. http_client=None,
  35. expire_access_token=True,
  36. profile_handler=Mock(),
  37. )
  38. self.macaroon_generator = Mock(
  39. generate_access_token=Mock(return_value='secret'))
  40. self.hs.get_macaroon_generator = Mock(return_value=self.macaroon_generator)
  41. self.hs.handlers = RegistrationHandlers(self.hs)
  42. self.handler = self.hs.get_handlers().registration_handler
  43. @defer.inlineCallbacks
  44. def test_user_is_created_and_logged_in_if_doesnt_exist(self):
  45. local_part = "someone"
  46. display_name = "someone"
  47. user_id = "@someone:test"
  48. requester = create_requester("@as:test")
  49. result_user_id, result_token = yield self.handler.get_or_create_user(
  50. requester, local_part, display_name)
  51. self.assertEquals(result_user_id, user_id)
  52. self.assertEquals(result_token, 'secret')
  53. @defer.inlineCallbacks
  54. def test_if_user_exists(self):
  55. store = self.hs.get_datastore()
  56. frank = UserID.from_string("@frank:test")
  57. yield store.register(
  58. user_id=frank.to_string(),
  59. token="jkv;g498752-43gj['eamb!-5",
  60. password_hash=None)
  61. local_part = "frank"
  62. display_name = "Frank"
  63. user_id = "@frank:test"
  64. requester = create_requester("@as:test")
  65. result_user_id, result_token = yield self.handler.get_or_create_user(
  66. requester, local_part, display_name)
  67. self.assertEquals(result_user_id, user_id)
  68. self.assertEquals(result_token, 'secret')
  69. @defer.inlineCallbacks
  70. def test_cannot_register_when_mau_limits_exceeded(self):
  71. local_part = "someone"
  72. display_name = "someone"
  73. requester = create_requester("@as:test")
  74. store = self.hs.get_datastore()
  75. self.hs.config.limit_usage_by_mau = False
  76. self.hs.config.max_mau_value = 50
  77. lots_of_users = 100
  78. small_number_users = 1
  79. store.count_monthly_users = Mock(return_value=defer.succeed(lots_of_users))
  80. # Ensure does not throw exception
  81. yield self.handler.get_or_create_user(requester, 'a', display_name)
  82. self.hs.config.limit_usage_by_mau = True
  83. with self.assertRaises(RegistrationError):
  84. yield self.handler.get_or_create_user(requester, 'b', display_name)
  85. store.count_monthly_users = Mock(return_value=defer.succeed(small_number_users))
  86. self._macaroon_mock_generator("another_secret")
  87. # Ensure does not throw exception
  88. yield self.handler.get_or_create_user("@neil:matrix.org", 'c', "Neil")
  89. self._macaroon_mock_generator("another another secret")
  90. store.count_monthly_users = Mock(return_value=defer.succeed(lots_of_users))
  91. with self.assertRaises(RegistrationError):
  92. yield self.handler.register(localpart=local_part)
  93. self._macaroon_mock_generator("another another secret")
  94. store.count_monthly_users = Mock(return_value=defer.succeed(lots_of_users))
  95. with self.assertRaises(RegistrationError):
  96. yield self.handler.register_saml2(local_part)
  97. def _macaroon_mock_generator(self, secret):
  98. """
  99. Reset macaroon generator in the case where the test creates multiple users
  100. """
  101. macaroon_generator = Mock(
  102. generate_access_token=Mock(return_value=secret))
  103. self.hs.get_macaroon_generator = Mock(return_value=macaroon_generator)
  104. self.hs.handlers = RegistrationHandlers(self.hs)
  105. self.handler = self.hs.get_handlers().registration_handler