summary refs log tree commit diff
path: root/tests/federation/test_pdu_codec.py
blob: 688182fa5b976d8db3ee783f93c7c6c7798a10a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
from twisted.trial import unittest

from synapse.federation.pdu_codec import (
    PduCodec, encode_event_id, decode_event_id
)
from synapse.federation.units import Pdu
#from synapse.api.events.room import MessageEvent

from synapse.server import HomeServer

from mock import Mock


class PduCodecTestCase(unittest.TestCase):
    def setUp(self):
        self.hs = HomeServer("blargle.net")
        self.event_factory = self.hs.get_event_factory()

        self.codec = PduCodec(self.hs)

    def test_decode_event_id(self):
        self.assertEquals(
            ("foo", "bar.com"),
            decode_event_id("foo@bar.com", "A")
        )

        self.assertEquals(
            ("foo", "bar.com"),
            decode_event_id("foo", "bar.com")
        )

    def test_encode_event_id(self):
        self.assertEquals("A@B", encode_event_id("A", "B"))

    def test_codec_event_id(self):
        event_id = "aa@bb.com"

        self.assertEquals(
            event_id,
            encode_event_id(*decode_event_id(event_id, None))
        )

        pdu_id = ("aa", "bb.com")

        self.assertEquals(
            pdu_id,
            decode_event_id(encode_event_id(*pdu_id), None)
        )

    def test_event_from_pdu(self):
        pdu = Pdu(
            pdu_id="foo",
            context="rooooom",
            pdu_type="m.room.message",
            origin="bar.com",
            ts=12345,
            depth=5,
            prev_pdus=[("alice", "bob.com")],
            is_state=False,
            content={"msgtype": u"test"},
        )

        event = self.codec.event_from_pdu(pdu)

        self.assertEquals("foo@bar.com", event.event_id)
        self.assertEquals(pdu.context, event.room_id)
        self.assertEquals(pdu.is_state, event.is_state)
        self.assertEquals(pdu.depth, event.depth)
        self.assertEquals(["alice@bob.com"], event.prev_events)
        self.assertEquals(pdu.content, event.content)

    def test_pdu_from_event(self):
        event = self.event_factory.create_event(
            etype="m.room.message",
            event_id="gargh_id",
            room_id="rooom",
            user_id="sender",
            content={"msgtype": u"test"},
        )

        pdu = self.codec.pdu_from_event(event)

        self.assertEquals(event.event_id, pdu.pdu_id)
        self.assertEquals(self.hs.hostname, pdu.origin)
        self.assertEquals(event.room_id, pdu.context)
        self.assertEquals(event.content, pdu.content)
        self.assertEquals(event.type, pdu.pdu_type)

        event = self.event_factory.create_event(
            etype="m.room.message",
            event_id="gargh_id@bob.com",
            room_id="rooom",
            user_id="sender",
            content={"msgtype": u"test"},
        )

        pdu = self.codec.pdu_from_event(event)

        self.assertEquals("gargh_id", pdu.pdu_id)
        self.assertEquals("bob.com", pdu.origin)
        self.assertEquals(event.room_id, pdu.context)
        self.assertEquals(event.content, pdu.content)
        self.assertEquals(event.type, pdu.pdu_type)

    def test_event_from_state_pdu(self):
        pdu = Pdu(
            pdu_id="foo",
            context="rooooom",
            pdu_type="m.room.topic",
            origin="bar.com",
            ts=12345,
            depth=5,
            prev_pdus=[("alice", "bob.com")],
            is_state=True,
            content={"topic": u"test"},
            state_key="",
        )

        event = self.codec.event_from_pdu(pdu)

        self.assertEquals("foo@bar.com", event.event_id)
        self.assertEquals(pdu.context, event.room_id)
        self.assertEquals(pdu.is_state, event.is_state)
        self.assertEquals(pdu.depth, event.depth)
        self.assertEquals(["alice@bob.com"], event.prev_events)
        self.assertEquals(pdu.content, event.content)
        self.assertEquals(pdu.state_key, event.state_key)

    def test_pdu_from_state_event(self):
        event = self.event_factory.create_event(
            etype="m.room.topic",
            event_id="gargh_id",
            room_id="rooom",
            user_id="sender",
            content={"topic": u"test"},
        )

        pdu = self.codec.pdu_from_event(event)

        self.assertEquals(event.event_id, pdu.pdu_id)
        self.assertEquals(self.hs.hostname, pdu.origin)
        self.assertEquals(event.room_id, pdu.context)
        self.assertEquals(event.content, pdu.content)
        self.assertEquals(event.type, pdu.pdu_type)
        self.assertEquals(event.state_key, pdu.state_key)