test_types.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-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 tests import unittest
  16. from synapse.api.errors import SynapseError
  17. from synapse.server import HomeServer
  18. from synapse.types import UserID, RoomAlias
  19. mock_homeserver = HomeServer(hostname="my.domain")
  20. class UserIDTestCase(unittest.TestCase):
  21. def test_parse(self):
  22. user = UserID.from_string("@1234abcd:my.domain")
  23. self.assertEquals("1234abcd", user.localpart)
  24. self.assertEquals("my.domain", user.domain)
  25. self.assertEquals(True, mock_homeserver.is_mine(user))
  26. def test_pase_empty(self):
  27. with self.assertRaises(SynapseError):
  28. UserID.from_string("")
  29. def test_build(self):
  30. user = UserID("5678efgh", "my.domain")
  31. self.assertEquals(user.to_string(), "@5678efgh:my.domain")
  32. def test_compare(self):
  33. userA = UserID.from_string("@userA:my.domain")
  34. userAagain = UserID.from_string("@userA:my.domain")
  35. userB = UserID.from_string("@userB:my.domain")
  36. self.assertTrue(userA == userAagain)
  37. self.assertTrue(userA != userB)
  38. class RoomAliasTestCase(unittest.TestCase):
  39. def test_parse(self):
  40. room = RoomAlias.from_string("#channel:my.domain")
  41. self.assertEquals("channel", room.localpart)
  42. self.assertEquals("my.domain", room.domain)
  43. self.assertEquals(True, mock_homeserver.is_mine(room))
  44. def test_build(self):
  45. room = RoomAlias("channel", "my.domain")
  46. self.assertEquals(room.to_string(), "#channel:my.domain")