File: test_access.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 (279 lines) | stat: -rw-r--r-- 8,642 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import pytest
from slixmpp import ComponentXMPP
from slixmpp.plugins.xep_0050.adhoc import XEP_0050

import slidge.command.adhoc
import slidge.command.base
from slidge.command import Command, CommandAccess
from slidge.command.adhoc import AdhocProvider
from slidge.command.base import Confirmation
from slidge.util.test import SlixTestPlus


class MockSession:
    def __init__(self, jid):
        self.logged = "logged" in jid.user


@pytest.fixture(autouse=True)
def mock(monkeypatch, MockRE):
    monkeypatch.setattr(
        slidge.command.base, "is_admin", lambda j: j.user.startswith("admin")
    )
    monkeypatch.setattr(Command, "_get_session", lambda s, j: MockSession(j))
    monkeypatch.setattr(XEP_0050, "new_session", lambda _: "session-id")
    monkeypatch.setattr(
        ComponentXMPP,
        "jid_validator",
        MockRE,
        raising=False,
    )
    monkeypatch.setattr(
        ComponentXMPP,
        "get_session_from_stanza",
        lambda s, j: MockSession(j.get_from()),
        raising=False,
    )
    monkeypatch.setattr(
        ComponentXMPP,
        "get_session_from_jid",
        lambda s, j: MockSession(j),
        raising=False,
    )


class Command1(Command):
    NAME = "Command number one"
    NODE = "command1"
    ACCESS = CommandAccess.ADMIN_ONLY


class Command2(Command1):
    NAME = "Command number two"
    NODE = "command2"
    ACCESS = CommandAccess.ADMIN_ONLY


class Command3(Command):
    NAME = "Command number three"
    NODE = "command3"

    CATEGORY = "category"
    ACCESS = CommandAccess.ADMIN_ONLY

    async def run(self, _session, _ifrom):
        return Confirmation(
            prompt="Confirm?", handler=self.finish, success="It worked!"
        )

    async def finish(self, _session, _ifrom):
        pass


class Command4(Command):
    NAME = "Command number four"
    NODE = "command4"

    CATEGORY = "category"
    ACCESS = CommandAccess.ADMIN_ONLY

    async def run(self, _session, _ifrom):
        return Confirmation(
            prompt="Confirm?", handler=self.finish, success="It worked!"
        )

    async def finish(self, _session, _ifrom):
        return "OK"


class TestCommandsDisco(SlixTestPlus):
    def setUp(self):
        self.stream_start(
            mode="component",
            plugins=["xep_0050"],
            jid="slidge.whatever.ass",
            server="whatever.ass",
        )
        self.adhoc = AdhocProvider(self.xmpp)
        self.adhoc.register(Command1(self.xmpp))
        self.adhoc.register(Command2(self.xmpp))
        self.adhoc.register(Command3(self.xmpp))
        self.adhoc.register(Command4(self.xmpp))
        super().setUp()

    def test_disco_admin(self):
        self.recv(  # language=XML
            f"""
            <iq type='get'
                from='admin@whatever.ass/cheogram'
                to='{self.xmpp.boundjid.bare}'>
              <query xmlns='http://jabber.org/protocol/disco#items'
                     node='http://jabber.org/protocol/commands' />
            </iq>
            """
        )
        self.send(  # language=XML
            """
            <iq xmlns="jabber:component:accept"
                type="result"
                from="slidge.whatever.ass"
                to="admin@whatever.ass/cheogram"
                id="1">
              <query xmlns="http://jabber.org/protocol/disco#items"
                     node="http://jabber.org/protocol/commands">
                <item jid="slidge.whatever.ass"
                      node="command1"
                      name="Command number one" />
                <item jid="slidge.whatever.ass"
                      node="command2"
                      name="Command number two" />
                <item jid="slidge.whatever.ass"
                      node="category"
                      name="category" />
              </query>
            </iq>
            """
        )

        self.recv(  # language=XML
            f"""
            <iq type='get'
                from='user@whatever.ass/cheogram'
                to='{self.xmpp.boundjid.bare}'>
              <query xmlns='http://jabber.org/protocol/disco#items'
                     node='http://jabber.org/protocol/commands' />
            </iq>
            """
        )
        self.send(  # language=XML
            """
            <iq xmlns="jabber:component:accept"
                type="result"
                from="slidge.whatever.ass"
                to="user@whatever.ass/cheogram"
                id="2">
              <query xmlns="http://jabber.org/protocol/disco#items"
                     node="http://jabber.org/protocol/commands" />
            </iq>
            """
        )

    def test_non_existing_command(self):
        self.recv(  # language=XML
            f"""
            <iq type='set'
                from='admin@whatever.ass/cheogram'
                to='{self.xmpp.boundjid.bare}'
                id="1">
              <command xmlns='http://jabber.org/protocol/commands'
                       node='non-existing'
                       action='execute' />
            </iq>
            """
        )
        self.send(  # language=XML
            """
            <iq xmlns="jabber:component:accept"
                type="error"
                from="slidge.whatever.ass"
                to="admin@whatever.ass/cheogram"
                id="1">
              <error type="cancel">
                <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
              </error>
            </iq>
            """,
            use_values=False,
        )

    def test_category(self):
        self.recv(  # language=XML
            f"""
            <iq type='set'
                from='admin@whatever.ass/cheogram'
                to='{self.xmpp.boundjid.bare}'
                id="1">
              <command xmlns='http://jabber.org/protocol/commands'
                       node='category'
                       action='execute' />
            </iq>
            """
        )
        self.send(  # language=XML
            """
            <iq type="result"
                from="slidge.whatever.ass"
                to="admin@whatever.ass/cheogram"
                id="1">
              <command xmlns="http://jabber.org/protocol/commands"
                       node="category"
                       sessionid="session-id"
                       status="executing">
                <actions>
                  <next />
                </actions>
                <x xmlns="jabber:x:data"
                   type="form">
                  <title>category</title>
                  <field var="command"
                         type="list-single"
                         label="Command">
                    <option label="Command number three">
                      <value>command3</value>
                    </option>
                    <option label="Command number four">
                      <value>command4</value>
                    </option>
                    <value />
                  </field>
                </x>
              </command>
            </iq>
            """,
            use_values=False,
        )
        self.recv(  # language=XML
            f"""
            <iq type='set'
                from='admin@whatever.ass/cheogram'
                to='{self.xmpp.boundjid.bare}'
                id="2">
              <command xmlns='http://jabber.org/protocol/commands'
                       node='category'
                       sessionid="session-id">
                <x xmlns='jabber:x:data'
                   type='submit'>
                  <field var='command'>
                    <value>command3</value>
                  </field>
                </x>
              </command>
            </iq>
            """
        )
        self.send(  # language=XML
            """
            <iq type="result"
                from="slidge.whatever.ass"
                to="admin@whatever.ass/cheogram"
                id="2">
              <command xmlns="http://jabber.org/protocol/commands"
                       node="category"
                       sessionid="session-id"
                       status="executing">
                <actions>
                  <next />
                </actions>
                <x xmlns="jabber:x:data"
                   type="form">
                  <title>Confirm?</title>
                  <field var="confirm"
                         label="Confirm"
                         type="boolean">
                    <value>1</value>
                  </field>
                </x>
              </command>
            </iq>
            """
        )