summaryrefslogtreecommitdiff
path: root/tests/twisted/gabble/message.py
blob: 43e941f638e9b6470cbe4114f27022e4348bd796 (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
#!/usr/bin/env python
#
# Copyright (C) 2011 Intel Corp.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA

from gabbleservicetest import call_async, EventPattern, assertEquals, ProxyWrapper
from gabbletest import exec_test, make_result_iq, sync_stream
from gabblecaps_helper import presence_and_disco

from twisted.words.protocols.jabber.client import IQ
from twisted.words.xish.domish import Element

import gabbleconstants as cs
import yconstants as ycs
import ns

client = 'http://telepathy.im/fake'
caps = {'ver': '0.1', 'node': client}
features = [
    ns.JINGLE_015,
    ns.JINGLE_015_AUDIO,
    ns.JINGLE_015_VIDEO,
    ns.GOOGLE_P2P,
    ycs.SERVICE_NS + '#the.target.service'
    ]
identity = ['client/pc/en/Lolclient 0.L0L']

def wrap_channel(bus, conn, path):
    return ProxyWrapper(bus.get_object(conn.bus_name, path),
                        ycs.CHANNEL_IFACE, {})

def setup_tests(q, bus, conn, stream, announce=False):
    bare_jid = "test-yst-message@example.com"
    full_jid = bare_jid + "/HotHotResource"

    if announce:
        presence_and_disco(q, conn, stream, full_jid,
                           True, client, caps,
                           features, identity, {},
                           True, None)

        sync_stream(q, stream)

    handle = conn.RequestHandles(cs.HT_CONTACT, [full_jid])[0]

    return handle, bare_jid, full_jid

def setup_outgoing_tests(q, bus, conn, stream, announce=True):
    handle, _, _ = setup_tests(q, bus, conn, stream, announce)

    # okay we got our contact, let's go
    request_props = {
        cs.CHANNEL_TYPE: ycs.CHANNEL_IFACE,
        cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
        cs.TARGET_HANDLE: handle,
        ycs.REQUEST_TYPE: ycs.REQUEST_TYPE_GET,
        ycs.REQUEST_ATTRIBUTES: {'hi': 'mom'},
        ycs.TARGET_SERVICE: 'the.target.service',
        ycs.INITIATOR_SERVICE: 'the.initiator.service'
        }

    call_async(q, conn.Requests, 'CreateChannel', request_props)

    e, _ = q.expect_many(EventPattern('dbus-return', method='CreateChannel'),
                         EventPattern('dbus-signal', signal='NewChannels'))
    path, props = e.value

    for k, v in request_props.items():
        assertEquals(v, props[k])

    # finally we have our channel
    chan = wrap_channel(bus, conn, path)

    # let's check we can't call Fail()/Reply()
    call_async(q, chan, 'Fail', ycs.ERROR_TYPE_CANCEL, 'lol', 'whut', 'pear')
    q.expect('dbus-error', method='Fail')

    call_async(q, chan, 'Reply', {'lol':'whut'}, '')
    q.expect('dbus-error', method='Reply')

    # okay enough, let's move on.
    call_async(q, chan, 'Request')

    e, _ = q.expect_many(EventPattern('stream-iq'),
                         EventPattern('dbus-return', method='Request'))

    assertEquals('get', e.iq_type)
    assertEquals('message', e.query_name)
    assertEquals('urn:ytstenut:message', e.query_ns)

    # we shouldn't be able to call this again
    call_async(q, chan, 'Request')
    q.expect('dbus-error', method='Request')

    return path, e.stanza

def outgoing_reply(q, bus, conn, stream):
    path, stanza = setup_outgoing_tests(q, bus, conn, stream)

    # reply with nothing
    reply = make_result_iq(stream, stanza)
    stream.send(reply)

    e = q.expect('dbus-signal', signal='Replied', path=path)
    args, xml = e.args
    assertEquals({}, args)
    assertEquals('<?xml version="1.0" encoding="UTF-8"?>\n' \
                 + '<message xmlns="urn:ytstenut:message"/>\n', xml)

def outgoing_fail(q, bus, conn, stream):
    path, stanza = setup_outgoing_tests(q, bus, conn, stream)

    # construct a nice error reply
    reply = IQ(None, 'error')
    reply['id'] = stanza['id']
    reply['from'] = stanza['to']
    error = reply.addElement('error')
    error['type'] = 'cancel'
    error['code'] = '409'
    error.addElement((ns.STANZA, 'conflict'))
    error.addElement((ycs.MESSAGE_NS, 'yodawg'))
    text = error.addElement((ns.STANZA, 'text'),
                            content='imma let you finish')

    stream.send(reply)

    e = q.expect('dbus-signal', signal='Failed', path=path)
    error_type, stanza_error_name, yst_error_name, text = e.args
    assertEquals(ycs.ERROR_TYPE_CANCEL, error_type)
    assertEquals('conflict', stanza_error_name)
    assertEquals('yodawg', yst_error_name)
    assertEquals('imma let you finish', text)

def bad_requests(q, bus, conn, stream):
    handle, _, _ = setup_tests(q, bus, conn, stream)

    props = {
        cs.CHANNEL_TYPE: ycs.CHANNEL_IFACE,
        cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
        }

    def ensure_error(extra={}):
        copy = props.copy()
        copy.update(extra)

        call_async(q, conn.Requests, 'CreateChannel', copy)
        q.expect('dbus-error', method='CreateChannel')

    # bad handle
    ensure_error({cs.TARGET_HANDLE: 42})

    # offline
    ensure_error({cs.TARGET_ID: 'lolbags@dingdong'})

    props.update({cs.TARGET_HANDLE: handle})

    # RequestType
    ensure_error()
    ensure_error({ycs.REQUEST_TYPE: 99})
    props.update({ycs.REQUEST_TYPE: ycs.REQUEST_TYPE_GET})

    # TargetService
    ensure_error()
    ensure_error({ycs.TARGET_SERVICE: 'lol/bags/what\'s this?!!!!'})
    props.update({ycs.TARGET_SERVICE: 'the.target.service'})

    # InitiatorService
    ensure_error()
    ensure_error({ycs.INITIATOR_SERVICE: 'lol/bags/what\'s this?!!!!'})
    props.update({ycs.INITIATOR_SERVICE: 'the.initiator.service'})

    # RequestAttributes: a{ss}, not a{si}
    ensure_error({ycs.REQUEST_ATTRIBUTES: {'lol': 2}})

    # RequestBody
    ensure_error({ycs.REQUEST_BODY: 'no way is this real XML'})

def setup_incoming_tests(q, bus, conn, stream):
    handle, bare_jid, full_jid = setup_tests(q, bus, conn, stream)

    self_handle = conn.GetSelfHandle()
    self_handle_name =  conn.InspectHandles(cs.HT_CONTACT, [self_handle])[0]

    iq = IQ(None, 'get')
    iq['id'] = 'le-loldongs'
    iq['from'] = full_jid
    iq['to'] = self_handle_name
    msg = iq.addElement((ycs.MESSAGE_NS, 'message'))
    msg['from-service'] = 'the.from.service'
    msg['to-service'] = 'the.to.service'
    msg['owl-companions'] = 'the pussy cat'
    msg['destination'] = 'sea'
    msg['seacraft'] = 'beautiful pea green boat'

    lol = msg.addElement((None, 'lol'))
    lol['some'] = 'stuff'
    lol['to'] = 'fill'
    lol['the'] = 'time'
    lol.addElement((None, 'look-into-my-eyes'),
                   content='and tell me how boring writing these tests is')

    stream.send(iq)

    e = q.expect('dbus-signal', signal='NewChannels', predicate=lambda e:
                     e.args[0][0][1][cs.CHANNEL_TYPE] == ycs.CHANNEL_IFACE)
    path, props = e.args[0][0]

    assertEquals(handle, props[cs.INITIATOR_HANDLE])
    assertEquals(bare_jid, props[cs.INITIATOR_ID])
    assertEquals(False, props[cs.REQUESTED])
    assertEquals(handle, props[cs.TARGET_HANDLE])
    assertEquals(cs.HT_CONTACT, props[cs.TARGET_HANDLE_TYPE])
    assertEquals(bare_jid, props[cs.TARGET_ID])

    assertEquals('the.from.service', props[ycs.INITIATOR_SERVICE])
    assertEquals('the.to.service', props[ycs.TARGET_SERVICE])
    assertEquals(ycs.REQUEST_TYPE_GET, props[ycs.REQUEST_TYPE])
    assertEquals({'destination': 'sea',
                  'owl-companions': 'the pussy cat',
                  'seacraft': 'beautiful pea green boat'},
                 props[ycs.REQUEST_ATTRIBUTES])

    assertEquals('<?xml version="1.0" encoding="UTF-8"?>\n' \
                     '<message seacraft="beautiful pea green boat" ' \
                     'from-service="the.from.service" destination="sea" ' \
                     'owl-companions="the pussy cat" to-service="the.to.service" ' \
                     'xmlns="urn:ytstenut:message">' \
                     '<lol to="fill" the="time" some="stuff">' \
                     '<look-into-my-eyes>and tell me how boring ' \
                     'writing these tests is</look-into-my-eyes>' \
                     '</lol></message>\n', props[ycs.REQUEST_BODY])

    # finally we have our channel
    chan = wrap_channel(bus, conn, path)

    # let's check we can't call Request()
    call_async(q, chan, 'Request')
    q.expect('dbus-error', method='Request')

    return chan, bare_jid, full_jid, self_handle_name

def incoming_reply(q, bus, conn, stream):
    chan, bare_jid, full_jid, self_handle_name = \
        setup_incoming_tests(q, bus, conn, stream)

    moar = Element((ycs.MESSAGE_NS, 'message'))
    moar['ninety-nine-problems'] = 'but a sauvignon blanc aint one'
    moar['also'] = 'my mum said hi'
    trollface = moar.addElement('trollface', content='problem?')

    call_async(q, chan, 'Reply',
               {'ninety-nine-problems': 'but a sauvignon blanc aint one',
                'also': 'my mum said hi'},
               moar.toXml())

    _, e = q.expect_many(EventPattern('dbus-return', method='Reply'),
                         EventPattern('stream-message'))

    iq = e.stanza
    assertEquals('le-loldongs', iq['id'])
    assertEquals('result', iq['type'])
    assertEquals(self_handle_name, iq['from'])
    assertEquals(full_jid, iq['to'])
    assertEquals(1, len(iq.children))

    message = iq.children[0]

    assertEquals('message', message.name)
    assertEquals(ycs.MESSAGE_NS, message.uri)
    assertEquals('my mum said hi', message['also'])
    assertEquals('but a sauvignon blanc aint one', message['ninety-nine-problems'])
    assertEquals('the.from.service', message['to-service'])
    assertEquals('the.to.service', message['from-service'])
    assertEquals(1, len(message.children))

    trollface = message.children[0]

    assertEquals('trollface', trollface.name)
    assertEquals(1, len(trollface.children))

    assertEquals('problem?', trollface.children[0])

    # check we can't call anything any more
    call_async(q, chan, 'Fail', ycs.ERROR_TYPE_CANCEL, 'lol', 'whut', 'pear')
    q.expect('dbus-error', method='Fail')

    call_async(q, chan, 'Reply', {'lol':'whut'}, '')
    q.expect('dbus-error', method='Reply')

    call_async(q, chan, 'Request')
    q.expect('dbus-error', method='Request')

def incoming_fail(q, bus, conn, stream):
    chan, bare_jid, full_jid, self_handle_name = \
        setup_incoming_tests(q, bus, conn, stream)

    call_async(q, chan, 'Fail',
               ycs.ERROR_TYPE_AUTH, 'auth', 'omgwtfbbq',
               'I most certainly dont feel like dancing')

    _, e = q.expect_many(EventPattern('dbus-return', method='Fail'),
                         EventPattern('stream-message'))

    iq = e.stanza
    assertEquals('le-loldongs', iq['id'])
    assertEquals('error', iq['type'])
    assertEquals(self_handle_name, iq['from'])
    assertEquals(full_jid, iq['to'])
    assertEquals(2, len(iq.children))

    def check_message(message):
        assertEquals('message', message.name)
        assertEquals(ycs.MESSAGE_NS, message.uri)
        assertEquals('beautiful pea green boat', message['seacraft'])
        assertEquals('sea', message['destination'])
        assertEquals('the pussy cat', message['owl-companions'])
        assertEquals('the.from.service', message['from-service'])
        assertEquals('the.to.service', message['to-service'])
        assertEquals(1, len(message.children))

        lol = message.children[0]

        assertEquals('lol', lol.name)
        assertEquals('fill', lol['to'])
        assertEquals('time', lol['the'])
        assertEquals('stuff', lol['some'])
        assertEquals(1, len(lol.children))

        look = lol.children[0]

        assertEquals('look-into-my-eyes', look.name)
        assertEquals(1, len(look.children))
        assertEquals('and tell me how boring writing these tests is', look.children[0])

    def check_error(error):
        assertEquals('error', error.name)
        assertEquals('auth', error['type'])
        assertEquals(3, len(error.children))

        for c in error.children:
            if c.name == 'auth':
                assertEquals(ns.STANZA, c.uri)
            elif c.name == 'omgwtfbbq':
                assertEquals(ycs.MESSAGE_NS, c.uri)
            elif c.name == 'text':
                assertEquals(ns.STANZA, c.uri)
                assertEquals(1, len(c.children))
                assertEquals('I most certainly dont feel like dancing',
                             c.children[0])
            else:
                raise

    for child in iq.children:
        if child.name == 'message':
            check_message(child)
        elif child.name == 'error':
            check_error(child)
        else:
            raise

    # check we can't call anything any more
    call_async(q, chan, 'Fail', ycs.ERROR_TYPE_CANCEL, 'lol', 'whut', 'pear')
    q.expect('dbus-error', method='Fail')

    call_async(q, chan, 'Reply', {'lol':'whut'}, '')
    q.expect('dbus-error', method='Reply')

    call_async(q, chan, 'Request')
    q.expect('dbus-error', method='Request')

if __name__ == '__main__':
    exec_test(outgoing_reply)
    exec_test(outgoing_fail)
    exec_test(bad_requests)
    exec_test(incoming_reply)
    exec_test(incoming_fail)