1
0

test_types.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 (
  16. RoomAlias,
  17. UserID,
  18. get_domain_from_id,
  19. get_localpart_from_id,
  20. map_username_to_mxid_localpart,
  21. )
  22. from tests import unittest
  23. class IsMineIDTests(unittest.HomeserverTestCase):
  24. def test_is_mine_id(self) -> None:
  25. self.assertTrue(self.hs.is_mine_id("@user:test"))
  26. self.assertTrue(self.hs.is_mine_id("#room:test"))
  27. self.assertTrue(self.hs.is_mine_id("invalid:test"))
  28. self.assertFalse(self.hs.is_mine_id("@user:test\0"))
  29. self.assertFalse(self.hs.is_mine_id("@user"))
  30. def test_two_colons(self) -> None:
  31. """Test handling of IDs containing more than one colon."""
  32. # The domain starts after the first colon.
  33. # These functions must interpret things consistently.
  34. self.assertFalse(self.hs.is_mine_id("@user:test:test"))
  35. self.assertEqual("user", get_localpart_from_id("@user:test:test"))
  36. self.assertEqual("test:test", get_domain_from_id("@user:test:test"))
  37. class UserIDTestCase(unittest.HomeserverTestCase):
  38. def test_parse(self) -> None:
  39. user = UserID.from_string("@1234abcd:test")
  40. self.assertEqual("1234abcd", user.localpart)
  41. self.assertEqual("test", user.domain)
  42. self.assertEqual(True, self.hs.is_mine(user))
  43. def test_parse_rejects_empty_id(self) -> None:
  44. with self.assertRaises(SynapseError):
  45. UserID.from_string("")
  46. def test_parse_rejects_missing_sigil(self) -> None:
  47. with self.assertRaises(SynapseError):
  48. UserID.from_string("alice:example.com")
  49. def test_parse_rejects_missing_separator(self) -> None:
  50. with self.assertRaises(SynapseError):
  51. UserID.from_string("@alice.example.com")
  52. def test_validation_rejects_missing_domain(self) -> None:
  53. self.assertFalse(UserID.is_valid("@alice:"))
  54. def test_build(self) -> None:
  55. user = UserID("5678efgh", "my.domain")
  56. self.assertEqual(user.to_string(), "@5678efgh:my.domain")
  57. def test_compare(self) -> None:
  58. userA = UserID.from_string("@userA:my.domain")
  59. userAagain = UserID.from_string("@userA:my.domain")
  60. userB = UserID.from_string("@userB:my.domain")
  61. self.assertTrue(userA == userAagain)
  62. self.assertTrue(userA != userB)
  63. class RoomAliasTestCase(unittest.HomeserverTestCase):
  64. def test_parse(self) -> None:
  65. room = RoomAlias.from_string("#channel:test")
  66. self.assertEqual("channel", room.localpart)
  67. self.assertEqual("test", room.domain)
  68. self.assertEqual(True, self.hs.is_mine(room))
  69. def test_build(self) -> None:
  70. room = RoomAlias("channel", "my.domain")
  71. self.assertEqual(room.to_string(), "#channel:my.domain")
  72. def test_validate(self) -> None:
  73. id_string = "#test:domain,test"
  74. self.assertFalse(RoomAlias.is_valid(id_string))
  75. class MapUsernameTestCase(unittest.TestCase):
  76. def test_pass_througuh(self) -> None:
  77. self.assertEqual(map_username_to_mxid_localpart("test1234"), "test1234")
  78. def test_upper_case(self) -> None:
  79. self.assertEqual(map_username_to_mxid_localpart("tEST_1234"), "test_1234")
  80. self.assertEqual(
  81. map_username_to_mxid_localpart("tEST_1234", case_sensitive=True),
  82. "t_e_s_t__1234",
  83. )
  84. def test_symbols(self) -> None:
  85. self.assertEqual(
  86. map_username_to_mxid_localpart("test=$?_1234"), "test=3d=24=3f_1234"
  87. )
  88. def test_leading_underscore(self) -> None:
  89. self.assertEqual(map_username_to_mxid_localpart("_test_1234"), "=5ftest_1234")
  90. def test_non_ascii(self) -> None:
  91. # this should work with either a unicode or a bytes
  92. self.assertEqual(map_username_to_mxid_localpart("têst"), "t=c3=aast")
  93. self.assertEqual(map_username_to_mxid_localpart("têst".encode()), "t=c3=aast")