test_types.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.api.errors import SynapseError
  15. from synapse.types import RoomAlias, UserID, map_username_to_mxid_localpart
  16. from tests import unittest
  17. class UserIDTestCase(unittest.HomeserverTestCase):
  18. def test_parse(self):
  19. user = UserID.from_string("@1234abcd:test")
  20. self.assertEqual("1234abcd", user.localpart)
  21. self.assertEqual("test", user.domain)
  22. self.assertEqual(True, self.hs.is_mine(user))
  23. def test_parse_rejects_empty_id(self):
  24. with self.assertRaises(SynapseError):
  25. UserID.from_string("")
  26. def test_parse_rejects_missing_sigil(self):
  27. with self.assertRaises(SynapseError):
  28. UserID.from_string("alice:example.com")
  29. def test_parse_rejects_missing_separator(self):
  30. with self.assertRaises(SynapseError):
  31. UserID.from_string("@alice.example.com")
  32. def test_validation_rejects_missing_domain(self):
  33. self.assertFalse(UserID.is_valid("@alice:"))
  34. def test_build(self):
  35. user = UserID("5678efgh", "my.domain")
  36. self.assertEqual(user.to_string(), "@5678efgh:my.domain")
  37. def test_compare(self):
  38. userA = UserID.from_string("@userA:my.domain")
  39. userAagain = UserID.from_string("@userA:my.domain")
  40. userB = UserID.from_string("@userB:my.domain")
  41. self.assertTrue(userA == userAagain)
  42. self.assertTrue(userA != userB)
  43. class RoomAliasTestCase(unittest.HomeserverTestCase):
  44. def test_parse(self):
  45. room = RoomAlias.from_string("#channel:test")
  46. self.assertEqual("channel", room.localpart)
  47. self.assertEqual("test", room.domain)
  48. self.assertEqual(True, self.hs.is_mine(room))
  49. def test_build(self):
  50. room = RoomAlias("channel", "my.domain")
  51. self.assertEqual(room.to_string(), "#channel:my.domain")
  52. def test_validate(self):
  53. id_string = "#test:domain,test"
  54. self.assertFalse(RoomAlias.is_valid(id_string))
  55. class MapUsernameTestCase(unittest.TestCase):
  56. def testPassThrough(self):
  57. self.assertEqual(map_username_to_mxid_localpart("test1234"), "test1234")
  58. def testUpperCase(self):
  59. self.assertEqual(map_username_to_mxid_localpart("tEST_1234"), "test_1234")
  60. self.assertEqual(
  61. map_username_to_mxid_localpart("tEST_1234", case_sensitive=True),
  62. "t_e_s_t__1234",
  63. )
  64. def testSymbols(self):
  65. self.assertEqual(
  66. map_username_to_mxid_localpart("test=$?_1234"), "test=3d=24=3f_1234"
  67. )
  68. def testLeadingUnderscore(self):
  69. self.assertEqual(map_username_to_mxid_localpart("_test_1234"), "=5ftest_1234")
  70. def testNonAscii(self):
  71. # this should work with either a unicode or a bytes
  72. self.assertEqual(map_username_to_mxid_localpart("têst"), "t=c3=aast")
  73. self.assertEqual(map_username_to_mxid_localpart("têst".encode()), "t=c3=aast")