test_register.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. self.addCleanup,
  34. handlers=None,
  35. http_client=None,
  36. expire_access_token=True,
  37. profile_handler=Mock(),
  38. )
  39. self.macaroon_generator = Mock(
  40. generate_access_token=Mock(return_value='secret')
  41. )
  42. self.hs.get_macaroon_generator = Mock(return_value=self.macaroon_generator)
  43. self.hs.handlers = RegistrationHandlers(self.hs)
  44. self.handler = self.hs.get_handlers().registration_handler
  45. self.store = self.hs.get_datastore()
  46. self.hs.config.max_mau_value = 50
  47. self.lots_of_users = 100
  48. self.small_number_of_users = 1
  49. @defer.inlineCallbacks
  50. def test_user_is_created_and_logged_in_if_doesnt_exist(self):
  51. local_part = "someone"
  52. display_name = "someone"
  53. user_id = "@someone:test"
  54. requester = create_requester("@as:test")
  55. result_user_id, result_token = yield self.handler.get_or_create_user(
  56. requester, local_part, display_name
  57. )
  58. self.assertEquals(result_user_id, user_id)
  59. self.assertEquals(result_token, 'secret')
  60. @defer.inlineCallbacks
  61. def test_if_user_exists(self):
  62. store = self.hs.get_datastore()
  63. frank = UserID.from_string("@frank:test")
  64. yield store.register(
  65. user_id=frank.to_string(),
  66. token="jkv;g498752-43gj['eamb!-5",
  67. password_hash=None,
  68. )
  69. local_part = "frank"
  70. display_name = "Frank"
  71. user_id = "@frank:test"
  72. requester = create_requester("@as:test")
  73. result_user_id, result_token = yield self.handler.get_or_create_user(
  74. requester, local_part, display_name
  75. )
  76. self.assertEquals(result_user_id, user_id)
  77. self.assertEquals(result_token, 'secret')
  78. @defer.inlineCallbacks
  79. def test_mau_limits_when_disabled(self):
  80. self.hs.config.limit_usage_by_mau = False
  81. # Ensure does not throw exception
  82. yield self.handler.get_or_create_user("requester", 'a', "display_name")
  83. @defer.inlineCallbacks
  84. def test_get_or_create_user_mau_not_blocked(self):
  85. self.hs.config.limit_usage_by_mau = True
  86. self.store.count_monthly_users = Mock(
  87. return_value=defer.succeed(self.small_number_of_users)
  88. )
  89. # Ensure does not throw exception
  90. yield self.handler.get_or_create_user("@user:server", 'c', "User")
  91. @defer.inlineCallbacks
  92. def test_get_or_create_user_mau_blocked(self):
  93. self.hs.config.limit_usage_by_mau = True
  94. self.store.get_monthly_active_count = Mock(
  95. return_value=defer.succeed(self.lots_of_users)
  96. )
  97. with self.assertRaises(RegistrationError):
  98. yield self.handler.get_or_create_user("requester", 'b', "display_name")
  99. @defer.inlineCallbacks
  100. def test_register_mau_blocked(self):
  101. self.hs.config.limit_usage_by_mau = True
  102. self.store.get_monthly_active_count = Mock(
  103. return_value=defer.succeed(self.lots_of_users)
  104. )
  105. with self.assertRaises(RegistrationError):
  106. yield self.handler.register(localpart="local_part")
  107. @defer.inlineCallbacks
  108. def test_register_saml2_mau_blocked(self):
  109. self.hs.config.limit_usage_by_mau = True
  110. self.store.get_monthly_active_count = Mock(
  111. return_value=defer.succeed(self.lots_of_users)
  112. )
  113. with self.assertRaises(RegistrationError):
  114. yield self.handler.register_saml2(localpart="local_part")