test_pdu_codec.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 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.federation.pdu_codec import (
  17. PduCodec, encode_event_id, decode_event_id
  18. )
  19. from synapse.federation.units import Pdu
  20. #from synapse.api.events.room import MessageEvent
  21. from synapse.server import HomeServer
  22. from mock import Mock
  23. class PduCodecTestCase(unittest.TestCase):
  24. def setUp(self):
  25. self.hs = HomeServer("blargle.net")
  26. self.event_factory = self.hs.get_event_factory()
  27. self.codec = PduCodec(self.hs)
  28. def test_decode_event_id(self):
  29. self.assertEquals(
  30. ("foo", "bar.com"),
  31. decode_event_id("foo@bar.com", "A")
  32. )
  33. self.assertEquals(
  34. ("foo", "bar.com"),
  35. decode_event_id("foo", "bar.com")
  36. )
  37. def test_encode_event_id(self):
  38. self.assertEquals("A@B", encode_event_id("A", "B"))
  39. def test_codec_event_id(self):
  40. event_id = "aa@bb.com"
  41. self.assertEquals(
  42. event_id,
  43. encode_event_id(*decode_event_id(event_id, None))
  44. )
  45. pdu_id = ("aa", "bb.com")
  46. self.assertEquals(
  47. pdu_id,
  48. decode_event_id(encode_event_id(*pdu_id), None)
  49. )
  50. def test_event_from_pdu(self):
  51. pdu = Pdu(
  52. pdu_id="foo",
  53. context="rooooom",
  54. pdu_type="m.room.message",
  55. origin="bar.com",
  56. origin_server_ts=12345,
  57. depth=5,
  58. prev_pdus=[("alice", "bob.com")],
  59. is_state=False,
  60. content={"msgtype": u"test"},
  61. )
  62. event = self.codec.event_from_pdu(pdu)
  63. self.assertEquals("foo@bar.com", event.event_id)
  64. self.assertEquals(pdu.context, event.room_id)
  65. self.assertEquals(pdu.is_state, event.is_state)
  66. self.assertEquals(pdu.depth, event.depth)
  67. self.assertEquals(["alice@bob.com"], event.prev_events)
  68. self.assertEquals(pdu.content, event.content)
  69. def test_pdu_from_event(self):
  70. event = self.event_factory.create_event(
  71. etype="m.room.message",
  72. event_id="gargh_id",
  73. room_id="rooom",
  74. user_id="sender",
  75. content={"msgtype": u"test"},
  76. )
  77. pdu = self.codec.pdu_from_event(event)
  78. self.assertEquals(event.event_id, pdu.pdu_id)
  79. self.assertEquals(self.hs.hostname, pdu.origin)
  80. self.assertEquals(event.room_id, pdu.context)
  81. self.assertEquals(event.content, pdu.content)
  82. self.assertEquals(event.type, pdu.pdu_type)
  83. event = self.event_factory.create_event(
  84. etype="m.room.message",
  85. event_id="gargh_id@bob.com",
  86. room_id="rooom",
  87. user_id="sender",
  88. content={"msgtype": u"test"},
  89. )
  90. pdu = self.codec.pdu_from_event(event)
  91. self.assertEquals("gargh_id", pdu.pdu_id)
  92. self.assertEquals("bob.com", pdu.origin)
  93. self.assertEquals(event.room_id, pdu.context)
  94. self.assertEquals(event.content, pdu.content)
  95. self.assertEquals(event.type, pdu.pdu_type)
  96. def test_event_from_state_pdu(self):
  97. pdu = Pdu(
  98. pdu_id="foo",
  99. context="rooooom",
  100. pdu_type="m.room.topic",
  101. origin="bar.com",
  102. origin_server_ts=12345,
  103. depth=5,
  104. prev_pdus=[("alice", "bob.com")],
  105. is_state=True,
  106. content={"topic": u"test"},
  107. state_key="",
  108. )
  109. event = self.codec.event_from_pdu(pdu)
  110. self.assertEquals("foo@bar.com", event.event_id)
  111. self.assertEquals(pdu.context, event.room_id)
  112. self.assertEquals(pdu.is_state, event.is_state)
  113. self.assertEquals(pdu.depth, event.depth)
  114. self.assertEquals(["alice@bob.com"], event.prev_events)
  115. self.assertEquals(pdu.content, event.content)
  116. self.assertEquals(pdu.state_key, event.state_key)
  117. def test_pdu_from_state_event(self):
  118. event = self.event_factory.create_event(
  119. etype="m.room.topic",
  120. event_id="gargh_id",
  121. room_id="rooom",
  122. user_id="sender",
  123. content={"topic": u"test"},
  124. )
  125. pdu = self.codec.pdu_from_event(event)
  126. self.assertEquals(event.event_id, pdu.pdu_id)
  127. self.assertEquals(self.hs.hostname, pdu.origin)
  128. self.assertEquals(event.room_id, pdu.context)
  129. self.assertEquals(event.content, pdu.content)
  130. self.assertEquals(event.type, pdu.pdu_type)
  131. self.assertEquals(event.state_key, pdu.state_key)