File: test_mds.py

package info (click to toggle)
slidge 0.3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,544 kB
  • sloc: python: 21,794; xml: 525; sh: 57; javascript: 27; makefile: 14
file content (198 lines) | stat: -rw-r--r-- 7,242 bytes parent folder | download | duplicates (2)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import unittest.mock

from slixmpp import JID
from slixmpp.plugins.xep_0356.permissions import IqPermission
from test_muc import Base as BaseMUC

from conftest import UUIDFixtureMixin

class MDSMixin(UUIDFixtureMixin):
    def setUp(self):
        super().setUp()
        for domain in "test.com", "montague.lit":
            self.xmpp["xep_0356"].granted_privileges[domain].iq[
                "http://jabber.org/protocol/pubsub"
            ] = IqPermission.BOTH
            self.xmpp["xep_0356"].granted_privileges[domain].iq[
                "http://jabber.org/protocol/pubsub#owner"
            ] = IqPermission.BOTH


class TestMDS(MDSMixin, BaseMUC):
    def test_add_to_whitelist(self):
        task = self.xmpp.loop.create_task(
            self.xmpp._BaseGateway__add_component_to_mds_whitelist(JID("test@test.com"))
        )
        self.send(  # language=XML
            """
            <iq id="1"
                to="test@test.com"
                from="aim.shakespeare.lit"
                type="set">
              <privileged_iq xmlns="urn:xmpp:privilege:2">
                <iq xmlns="jabber:client"
                    type="set"
                    to="test@test.com"
                    from="test@test.com"
                    id="1">
                  <pubsub xmlns="http://jabber.org/protocol/pubsub">
                    <create node="urn:xmpp:mds:displayed:0" />
                  </pubsub>
                </iq>
              </privileged_iq>
            </iq>
            """,
            use_values=False,
        )
        self.recv(  # language=XML
            """
            <iq id="1"
                type="result"
                to="test@test.com"
                from="aim.shakespeare.lit">
              <privilege xmlns="urn:xmpp:privilege:2">
                <forwarded xmlns="urn:xmpp:forward:0">
                  <iq xmlns="jabber:client"
                      id="1"
                      type="result"
                      to="test@localhost" />
                </forwarded>
              </privilege>
            </iq>
            """
        )
        self.send(  # language=XML
            """
            <iq id="2"
                to="test@test.com"
                from="aim.shakespeare.lit"
                type="set">
              <privileged_iq xmlns="urn:xmpp:privilege:2">
                <iq xmlns="jabber:client"
                    type="set"
                    to="test@test.com"
                    from="test@test.com"
                    id="2">
                  <pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
                    <affiliations node="urn:xmpp:mds:displayed:0">
                      <affiliation jid="aim.shakespeare.lit"
                                   affiliation="member" />
                    </affiliations>
                  </pubsub>
                </iq>
              </privileged_iq>
            </iq>
            """,
            use_values=False,
        )
        self.recv(  # language=XML
            """
            <iq id="2"
                type="result"
                to="test@test.com"
                from="aim.shakespeare.lit">
              <privilege xmlns="urn:xmpp:privilege:2">
                <forwarded xmlns="urn:xmpp:forward:0">
                  <iq xmlns="jabber:client"
                      id="2"
                      type="result"
                      to="test@localhost" />
                </forwarded>
              </privilege>
            </iq>
            """
        )
        assert task.done()

    def test_receive_event(self):
        session = self.get_romeo_session()
        # juliet = self.juliet
        muc = self.get_private_muc()

        with unittest.mock.patch("test_muc.Session.on_displayed") as on_displayed:
            self.recv(  # language=XML
                f"""
            <message from='{session.user_jid}'
                     to='{self.xmpp.boundjid.bare}'
                     type='headline'
                     id='new-displayed-pep-event'>
              <event xmlns='http://jabber.org/protocol/pubsub#event'>
                <items node='urn:xmpp:mds:displayed:0'>
                  <item id='{muc.jid}'>
                    <displayed xmlns='urn:xmpp:mds:displayed:0'>
                      <stanza-id xmlns='urn:xmpp:sid:0'
                                 by='what@ev.er'
                                 id='1337' />
                    </displayed>
                  </item>
                </items>
              </event>
            </message>
            """
            )
            on_displayed.assert_awaited_once()
            assert on_displayed.call_args[0][0].jid == muc.jid
            assert on_displayed.call_args[0][1] == "legacy-1337"

    def test_send_mds(self):
        muc = self.get_private_muc()
        participant = self.run_coro(muc.get_user_participant())
        participant.displayed("legacy-msg-id")
        self.send(  # language=XML
            """
            <iq id="4"
                to="romeo@montague.lit"
                from="aim.shakespeare.lit"
                type="set">
              <privileged_iq xmlns="urn:xmpp:privilege:2">
                <iq xmlns="jabber:client"
                    to="romeo@montague.lit"
                    from="romeo@montague.lit"
                    id="4"
                    type="set">
                  <pubsub xmlns="http://jabber.org/protocol/pubsub">
                    <publish node="urn:xmpp:mds:displayed:0">
                      <item id="room-private@aim.shakespeare.lit">
                        <displayed xmlns="urn:xmpp:mds:displayed:0">
                          <stanza-id xmlns="urn:xmpp:sid:0"
                                     id="msg-id"
                                     by="room-private@aim.shakespeare.lit" />
                        </displayed>
                      </item>
                    </publish>
                    <publish-options>
                      <x xmlns="jabber:x:data"
                         type="submit">
                        <field var="FORM_TYPE"
                               type="hidden">
                          <value>http://jabber.org/protocol/pubsub#publish-options</value>
                        </field>
                        <field var="pubsub#persist_items">
                          <value>true</value>
                        </field>
                        <field var="pubsub#max_items">
                          <value>max</value>
                        </field>
                        <field var="pubsub#send_last_published_item">
                          <value>never</value>
                        </field>
                        <field var="pubsub#access_model">
                          <value>whitelist</value>
                        </field>
                      </x>
                    </publish-options>
                  </pubsub>
                </iq>
              </privileged_iq>
            </iq>
            """,
            use_values=False,
        )
        self.recv(  # language=XML
            """
            <iq id="4"
                from="romeo@montague.lit"
                to="aim.shakespeare.lit"
                type="result" />
            """
        )