summaryrefslogtreecommitdiff
path: root/tests/twisted/muc/only-text-when-needed.py
blob: 2e840d9b7411143ee1740ebb8a1964bbe1c7e13f (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
Test support for creating MUC text channels when necessary, not all
the time.
"""

from servicetest import call_async, EventPattern, assertEquals, \
    sync_dbus, wrap_channel
from gabbletest import exec_test, make_muc_presence, \
    sync_stream, elem
import constants as cs

from mucutil import echo_muc_presence

def request_stream_tube(q, bus, conn, method, jid):
    call_async(q, conn.Requests, method,
            { cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_STREAM_TUBE,
              cs.TARGET_HANDLE_TYPE: cs.HT_ROOM,
              cs.TARGET_ID: jid,
              cs.STREAM_TUBE_SERVICE: 'the.service',
              })

def stream_tube(q, bus, conn, stream, method, jid, presence=True):
    request_stream_tube(q, bus, conn, method, jid)
    if presence:
        send_muc_presence(q, stream, jid)
    e, _ = q.expect_many(EventPattern('dbus-return', method=method),
                         EventPattern('dbus-signal', signal='NewChannel'))

    # sigh
    if method == 'EnsureChannel':
        path = e.value[1]
    else:
        path = e.value[0]

    tube_chan = wrap_channel(bus.get_object(conn.bus_name, path), 'StreamTube')

    return (tube_chan,) + e.value

def request_text_channel(q, bus, conn, method, jid):
    call_async(q, conn.Requests, method,
            { cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT,
              cs.TARGET_HANDLE_TYPE: cs.HT_ROOM,
              cs.TARGET_ID: jid,
              })

def text_channel(q, bus, conn, stream, method, jid, presence=True):
    request_text_channel(q, bus, conn, method, jid)
    if presence:
        send_muc_presence(q, stream, jid)
    e, _ = q.expect_many(EventPattern('dbus-return', method=method),
                         EventPattern('dbus-signal', signal='NewChannel'))

    # sigh
    if method == 'EnsureChannel':
        path = e.value[1]
    else:
        path = e.value[0]

    text_chan = wrap_channel(bus.get_object(conn.bus_name, path), 'Text')

    return (text_chan,) + e.value

def send_muc_presence(q, stream, jid):
    q.expect('stream-presence', to='%s/test' % jid)

    stream.send(make_muc_presence('owner', 'moderator', jid, 'bob'))
    stream.send(make_muc_presence('none', 'participant', jid, 'test'))

def expect_close(q, path, stream=None, jid=None):
    forbid_unavailable = [EventPattern('stream-presence',
                                       presence_type='unavailable',
                                       to='%s/test' % jid)]

    if jid is not None:
        e = q.expect_many(*forbid_unavailable)[0]
        echo_muc_presence(q, stream, e.stanza, 'none', 'participant')
    else:
        q.forbid_events(forbid_unavailable)

    q.expect_many(EventPattern('dbus-signal', signal='ChannelClosed',
                               args=[path]),
                  EventPattern('dbus-signal', signal='Closed',
                               path=path))

    if jid is not None:
        q.unforbid_events(forbid_unavailable)

def assert_on_bus(q, chan):
    call_async(q, chan.Properties, 'GetAll', cs.CHANNEL)
    e = q.expect('dbus-return', method='GetAll')
    props = e.value[0]
    assert 'ChannelType' in props

def assert_not_on_bus(q, chan):
    call_async(q, chan.Properties, 'GetAll', cs.CHANNEL)
    q.expect('dbus-error', method='GetAll',
             name='org.freedesktop.DBus.Error.UnknownMethod')

# tests start here

def tube_no_text(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    # create a stream tube.
    # this will need a MUC channel to be opened, but we want to make
    # sure it doesn't get signalled.
    request_stream_tube(q, bus, conn, 'CreateChannel', jid)

    send_muc_presence(q, stream, jid)

    ret, new_sig = q.expect_many(
        EventPattern('dbus-return', method='CreateChannel'),
        EventPattern('dbus-signal', signal='NewChannel'))

    q.forbid_events([EventPattern('dbus-signal', signal='NewChannel')])

    tube_path, tube_props = ret.value
    assertEquals(cs.CHANNEL_TYPE_STREAM_TUBE, tube_props[cs.CHANNEL_TYPE])

    path, props = new_sig.args

    assertEquals(tube_path, path)
    assertEquals(tube_props, props)

    sync_dbus(bus, q, conn)

    q.unforbid_all()

def tube_then_text(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    # first let's get a stream tube
    stream_tube(q, bus, conn, stream, 'CreateChannel', jid)

    # now let's try and ensure the text channel which should happen
    # immediately
    request_text_channel(q, bus, conn, 'EnsureChannel', jid)

    ret = q.expect('dbus-return', method='EnsureChannel')

    yours, text_path, text_props = ret.value
    assertEquals(True, yours)
    assertEquals(cs.CHANNEL_TYPE_TEXT, text_props[cs.CHANNEL_TYPE])

    new_sig = q.expect('dbus-signal', signal='NewChannel')

    path, props = new_sig.args

    assertEquals(text_path, path)
    assertEquals(text_props, props)

def tube_remains_text_closes(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    text_chan, text_path, _ = text_channel(q, bus, conn, stream, 'CreateChannel', jid)
    tube_chan, tube_path, _ = stream_tube(q, bus, conn, stream, 'CreateChannel', jid,
                                          presence=False)

    # now let's try and close the text channel
    # this should happen sucessfully but the tube channel
    # should stick around
    q.forbid_events([EventPattern('dbus-signal', signal='ChannelClosed',
                                  args=[tube_path])])

    text_chan.Close()
    expect_close(q, text_path)

    sync_dbus(bus, q, conn)

    assert_on_bus(q, tube_chan)
    assert_not_on_bus(q, text_chan)

    q.unforbid_all()

def normally_close_text(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    text_chan, text_path, _ = text_channel(q, bus, conn, stream, 'CreateChannel', jid)

    sync_stream(q, stream)

    text_chan.Close()
    expect_close(q, text_path, stream, jid)

    assert_not_on_bus(q, text_chan)

def text_can_automatically_close(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    tube_chan, tube_path, _ = stream_tube(q, bus, conn, stream, 'CreateChannel', jid)

    sync_dbus(bus, q, conn)

    tube_chan.Close()
    expect_close(q, tube_path, stream, jid)

    assert_not_on_bus(q, tube_chan)

def text_remains_after_tube(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    tube_chan, tube_path, _ = stream_tube(q, bus, conn, stream, 'CreateChannel', jid)
    text_chan, text_path, _ = text_channel(q, bus, conn, stream, 'CreateChannel', jid,
                                           presence=False)

    sync_dbus(bus, q, conn)

    tube_chan.Close()
    expect_close(q, tube_path)

    assert_not_on_bus(q, tube_chan)
    assert_on_bus(q, text_chan)

    call_async(q, text_chan.Properties, 'GetAll', cs.CHANNEL_TYPE_TEXT)
    q.expect('dbus-return', method='GetAll')

    text_chan.Close()
    expect_close(q, text_path, stream, jid)

    assert_not_on_bus(q, tube_chan)
    assert_not_on_bus(q, text_chan)

def recreate_text(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    tube_chan, _, _ = stream_tube(q, bus, conn, stream, 'CreateChannel', jid)
    text_chan, text_path, text_props = text_channel(q, bus, conn, stream,
                                                    'CreateChannel', jid, presence=False)

    text_chan.Close()
    expect_close(q, text_path)

    assert_on_bus(q, tube_chan)
    assert_not_on_bus(q, text_chan)

    # now let's try and create the same text channel and hope we get
    # back the same channel
    q.forbid_events([EventPattern('stream-presence', to='%s/test' % jid)])

    request_text_channel(q, bus, conn, 'CreateChannel', jid)

    ret = q.expect('dbus-return', method='CreateChannel')

    path, props = ret.value
    assertEquals(cs.CHANNEL_TYPE_TEXT, props[cs.CHANNEL_TYPE])

    new_sig = q.expect('dbus-signal', signal='NewChannel')

    assertEquals(path, new_sig.args[0])
    assertEquals(props, new_sig.args[1])

    # the channel should be identical given it's the same MucChannel
    assertEquals(text_path, path)
    assertEquals(text_props, props)

    assert_on_bus(q, tube_chan)
    assert_on_bus(q, text_chan)

    q.unforbid_all()

def test_channels(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    tube_chan, _, _ = stream_tube(q, bus, conn, stream, 'CreateChannel', jid)
    text_chan, text_path, _ = text_channel(q, bus, conn, stream,'CreateChannel',
                                           jid, presence=False)

    text_chan.Close()
    expect_close(q, text_path)

    # the following are basically the same as assert_[not_]on_bus()
    # but they look pretty.

    # methods on the text channel should fail
    call_async(q, text_chan.Properties, 'GetAll', cs.CHANNEL_TYPE_TEXT)
    q.expect('dbus-error', method='GetAll')

    # but methods on the tube should pass
    call_async(q, tube_chan.Properties, 'GetAll', cs.CHANNEL_TYPE_STREAM_TUBE)
    q.expect('dbus-return', method='GetAll')

def test_message(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    tube_chan, tube_path, _ = stream_tube(q, bus, conn, stream, 'CreateChannel', jid)

    bob_jid = '%s/bob' % jid
    bob_handle = conn.get_contact_handle_sync(bob_jid)

    # now let's send a message
    stream.send(
        elem('message', from_=bob_jid, type='groupchat')(
          elem('body')(
            u'oh hey i didnt see you there'
          ),
        )
      )

    # the text channel appears!
    e = q.expect('dbus-signal', signal='NewChannel')
    path, props = e.args
    assertEquals(cs.CHANNEL_TYPE_TEXT, props[cs.CHANNEL_TYPE])
    # make sure we didn't request it
    assertEquals(False, props[cs.REQUESTED])
    assertEquals(bob_handle, props[cs.INITIATOR_HANDLE])

    # and the message is then signalled
    e = q.expect('dbus-signal', signal='MessageReceived', path=path)
    parts = e.args[0]

    header = parts[0]
    assertEquals(bob_handle, header['message-sender'])
    assertEquals(bob_jid, header['message-sender-id'])

    body = parts[1]
    assertEquals('oh hey i didnt see you there', body['content'])

def test_requested_message(q, bus, conn, stream):
    jid = 'test@conf.localhost'

    self_handle = conn.Properties.Get(cs.CONN, 'SelfHandle')

    text_chan, text_path, _ = text_channel(q, bus, conn, stream,'CreateChannel', jid)
    tube_chan, tube_path, _ = stream_tube(q, bus, conn, stream, 'CreateChannel', jid,
                                          presence=False)

    bob_jid = '%s/bob' % jid
    bob_handle = conn.get_contact_handle_sync(bob_jid)

    # make sure it says we requested it
    props = text_chan.Properties.GetAll(cs.CHANNEL)
    assertEquals(True, props['Requested'])
    assertEquals(self_handle, props['InitiatorHandle'])

    text_chan.Close()
    expect_close(q, text_path)

    assert_on_bus(q, tube_chan)
    assert_not_on_bus(q, text_chan)

    # now let's send a message
    stream.send(
        elem('message', from_=bob_jid, type='groupchat')(
          elem('body')(
            u'hello again'
          ),
        )
      )

    e = q.expect('dbus-signal', signal='NewChannel')
    path, props = e.args
    assertEquals(cs.CHANNEL_TYPE_TEXT, props[cs.CHANNEL_TYPE])
    # now make sure we didn't request it
    assertEquals(False, props[cs.REQUESTED])
    assertEquals(bob_handle, props[cs.INITIATOR_HANDLE])

    e = q.expect('dbus-signal', signal='MessageReceived', path=path)
    parts = e.args[0]

    header = parts[0]
    assertEquals(bob_handle, header['message-sender'])
    assertEquals(bob_jid, header['message-sender-id'])

    body = parts[1]
    assertEquals('hello again', body['content'])

    assert_on_bus(q, tube_chan)
    assert_on_bus(q, text_chan)

if __name__ == '__main__':
    # request tube, assert no text appears
    exec_test(tube_no_text)

    # request tube, request text (no presence), assert both appear
    exec_test(tube_then_text)

    # request tube & text, close text, assert tube doesn't close
    exec_test(tube_remains_text_closes)

    # request text, close text, assert unavailable presence
    exec_test(normally_close_text)

    # request tube, close tube, assert unavailable presence
    exec_test(text_can_automatically_close)

    # request tube & text, close tube, assert text doesn't close
    exec_test(text_remains_after_tube)

    # request tube & text, close text, request text (no presence),
    # assert appears as normal
    exec_test(recreate_text)

    # request tube & text, close text, assert GetAll on text fails but
    # works on tube
    exec_test(test_channels)

    # request tube, incoming message, assert text channel appears
    exec_test(test_message)

    # request text & tube, close text, incoming message, assert text
    # reappears with correct props
    exec_test(test_requested_message)