test_types.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 synapse.api.errors import SynapseError
  16. from synapse.types import GroupID, RoomAlias, UserID, map_username_to_mxid_localpart
  17. from tests import unittest
  18. class UserIDTestCase(unittest.HomeserverTestCase):
  19. def test_parse(self):
  20. user = UserID.from_string("@1234abcd:test")
  21. self.assertEquals("1234abcd", user.localpart)
  22. self.assertEquals("test", user.domain)
  23. self.assertEquals(True, self.hs.is_mine(user))
  24. def test_pase_empty(self):
  25. with self.assertRaises(SynapseError):
  26. UserID.from_string("")
  27. def test_build(self):
  28. user = UserID("5678efgh", "my.domain")
  29. self.assertEquals(user.to_string(), "@5678efgh:my.domain")
  30. def test_compare(self):
  31. userA = UserID.from_string("@userA:my.domain")
  32. userAagain = UserID.from_string("@userA:my.domain")
  33. userB = UserID.from_string("@userB:my.domain")
  34. self.assertTrue(userA == userAagain)
  35. self.assertTrue(userA != userB)
  36. class RoomAliasTestCase(unittest.HomeserverTestCase):
  37. def test_parse(self):
  38. room = RoomAlias.from_string("#channel:test")
  39. self.assertEquals("channel", room.localpart)
  40. self.assertEquals("test", room.domain)
  41. self.assertEquals(True, self.hs.is_mine(room))
  42. def test_build(self):
  43. room = RoomAlias("channel", "my.domain")
  44. self.assertEquals(room.to_string(), "#channel:my.domain")
  45. def test_validate(self):
  46. id_string = "#test:domain,test"
  47. self.assertFalse(RoomAlias.is_valid(id_string))
  48. class GroupIDTestCase(unittest.TestCase):
  49. def test_parse(self):
  50. group_id = GroupID.from_string("+group/=_-.123:my.domain")
  51. self.assertEqual("group/=_-.123", group_id.localpart)
  52. self.assertEqual("my.domain", group_id.domain)
  53. def test_validate(self):
  54. bad_ids = ["$badsigil:domain", "+:empty"] + [
  55. "+group" + c + ":domain" for c in "A%?æ£"
  56. ]
  57. for id_string in bad_ids:
  58. try:
  59. GroupID.from_string(id_string)
  60. self.fail("Parsing '%s' should raise exception" % id_string)
  61. except SynapseError as exc:
  62. self.assertEqual(400, exc.code)
  63. self.assertEqual("M_INVALID_PARAM", exc.errcode)
  64. class MapUsernameTestCase(unittest.TestCase):
  65. def testPassThrough(self):
  66. self.assertEqual(map_username_to_mxid_localpart("test1234"), "test1234")
  67. def testUpperCase(self):
  68. self.assertEqual(map_username_to_mxid_localpart("tEST_1234"), "test_1234")
  69. self.assertEqual(
  70. map_username_to_mxid_localpart("tEST_1234", case_sensitive=True),
  71. "t_e_s_t__1234",
  72. )
  73. def testSymbols(self):
  74. self.assertEqual(
  75. map_username_to_mxid_localpart("test=$?_1234"), "test=3d=24=3f_1234"
  76. )
  77. def testLeadingUnderscore(self):
  78. self.assertEqual(map_username_to_mxid_localpart("_test_1234"), "=5ftest_1234")
  79. def testNonAscii(self):
  80. # this should work with either a unicode or a bytes
  81. self.assertEqual(map_username_to_mxid_localpart("têst"), "t=c3=aast")
  82. self.assertEqual(
  83. map_username_to_mxid_localpart("têst".encode("utf-8")), "t=c3=aast"
  84. )