test_api.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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.module_api import ModuleApi
  16. from tests.unittest import HomeserverTestCase
  17. class ModuleApiTestCase(HomeserverTestCase):
  18. def prepare(self, reactor, clock, homeserver):
  19. self.store = homeserver.get_datastore()
  20. self.module_api = ModuleApi(homeserver, homeserver.get_auth_handler())
  21. def test_can_register_user(self):
  22. """Tests that an external module can register a user"""
  23. # Register a new user
  24. user_id, access_token = self.get_success(
  25. self.module_api.register(
  26. "bob", displayname="Bobberino", emails=["bob@bobinator.bob"]
  27. )
  28. )
  29. # Check that the new user exists with all provided attributes
  30. self.assertEqual(user_id, "@bob:test")
  31. self.assertTrue(access_token)
  32. self.assertTrue(self.store.get_user_by_id(user_id))
  33. # Check that the email was assigned
  34. emails = self.get_success(self.store.user_get_threepids(user_id))
  35. self.assertEqual(len(emails), 1)
  36. email = emails[0]
  37. self.assertEqual(email["medium"], "email")
  38. self.assertEqual(email["address"], "bob@bobinator.bob")
  39. # Should these be 0?
  40. self.assertEqual(email["validated_at"], 0)
  41. self.assertEqual(email["added_at"], 0)
  42. # Check that the displayname was assigned
  43. displayname = self.get_success(self.store.get_profile_displayname("bob"))
  44. self.assertEqual(displayname, "Bobberino")