1 | # Copyright (c) 2003-2008 Ralph Meijer |
---|
2 | # See LICENSE for details. |
---|
3 | |
---|
4 | """ |
---|
5 | Tests for L{wokkel.pubsub} |
---|
6 | """ |
---|
7 | |
---|
8 | from twisted.trial import unittest |
---|
9 | from twisted.words.xish import domish |
---|
10 | from twisted.words.protocols.jabber import error |
---|
11 | from twisted.words.protocols.jabber.jid import JID |
---|
12 | |
---|
13 | from wokkel import pubsub |
---|
14 | from wokkel.test.helpers import XmlStreamStub |
---|
15 | |
---|
16 | try: |
---|
17 | from twisted.words.protocols.jabber.xmlstream import toResponse |
---|
18 | except ImportError: |
---|
19 | from wokkel.compat import toResponse |
---|
20 | |
---|
21 | NS_PUBSUB = 'http://jabber.org/protocol/pubsub' |
---|
22 | NS_PUBSUB_ERRORS = 'http://jabber.org/protocol/pubsub#errors' |
---|
23 | |
---|
24 | class PubSubClientTest(unittest.TestCase): |
---|
25 | |
---|
26 | def setUp(self): |
---|
27 | self.stub = XmlStreamStub() |
---|
28 | self.protocol = pubsub.PubSubClient() |
---|
29 | self.protocol.xmlstream = self.stub.xmlstream |
---|
30 | |
---|
31 | def test_unsubscribe(self): |
---|
32 | """ |
---|
33 | Test sending unsubscription request. |
---|
34 | """ |
---|
35 | d = self.protocol.unsubscribe(JID('pubsub.example.org'), 'test', |
---|
36 | JID('user@example.org')) |
---|
37 | |
---|
38 | iq = self.stub.output[-1] |
---|
39 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
40 | self.assertEquals('set', iq.getAttribute('type')) |
---|
41 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
42 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
43 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
44 | 'unsubscribe', NS_PUBSUB)) |
---|
45 | self.assertEquals(1, len(children)) |
---|
46 | child = children[0] |
---|
47 | self.assertEquals('test', child['node']) |
---|
48 | self.assertEquals('user@example.org', child['jid']) |
---|
49 | |
---|
50 | self.stub.send(toResponse(iq, 'result')) |
---|
51 | return d |
---|
52 | |
---|
53 | |
---|
54 | class PubSubServiceTest(unittest.TestCase): |
---|
55 | |
---|
56 | def setUp(self): |
---|
57 | self.output = [] |
---|
58 | |
---|
59 | def send(self, obj): |
---|
60 | self.output.append(obj) |
---|
61 | |
---|
62 | def test_onPublishNoNode(self): |
---|
63 | handler = pubsub.PubSubService() |
---|
64 | handler.parent = self |
---|
65 | iq = domish.Element((None, 'iq')) |
---|
66 | iq['from'] = 'user@example.org' |
---|
67 | iq['to'] = 'pubsub.example.org' |
---|
68 | iq['type'] = 'set' |
---|
69 | iq.addElement((NS_PUBSUB, 'pubsub')) |
---|
70 | iq.pubsub.addElement('publish') |
---|
71 | handler.handleRequest(iq) |
---|
72 | |
---|
73 | e = error.exceptionFromStanza(self.output[-1]) |
---|
74 | self.assertEquals('bad-request', e.condition) |
---|
75 | |
---|
76 | def test_onPublish(self): |
---|
77 | class Handler(pubsub.PubSubService): |
---|
78 | def publish(self, *args, **kwargs): |
---|
79 | self.args = args |
---|
80 | self.kwargs = kwargs |
---|
81 | |
---|
82 | handler = Handler() |
---|
83 | handler.parent = self |
---|
84 | iq = domish.Element((None, 'iq')) |
---|
85 | iq['type'] = 'set' |
---|
86 | iq['from'] = 'user@example.org' |
---|
87 | iq['to'] = 'pubsub.example.org' |
---|
88 | iq.addElement((NS_PUBSUB, 'pubsub')) |
---|
89 | iq.pubsub.addElement('publish') |
---|
90 | iq.pubsub.publish['node'] = 'test' |
---|
91 | handler.handleRequest(iq) |
---|
92 | |
---|
93 | self.assertEqual((JID('user@example.org'), |
---|
94 | JID('pubsub.example.org'), 'test', []), handler.args) |
---|
95 | |
---|
96 | def test_onOptionsGet(self): |
---|
97 | handler = pubsub.PubSubService() |
---|
98 | handler.parent = self |
---|
99 | iq = domish.Element((None, 'iq')) |
---|
100 | iq['from'] = 'user@example.org' |
---|
101 | iq['to'] = 'pubsub.example.org' |
---|
102 | iq['type'] = 'get' |
---|
103 | iq.addElement((NS_PUBSUB, 'pubsub')) |
---|
104 | iq.pubsub.addElement('options') |
---|
105 | handler.handleRequest(iq) |
---|
106 | |
---|
107 | e = error.exceptionFromStanza(self.output[-1]) |
---|
108 | self.assertEquals('feature-not-implemented', e.condition) |
---|
109 | self.assertEquals('unsupported', e.appCondition.name) |
---|
110 | self.assertEquals(NS_PUBSUB_ERRORS, e.appCondition.uri) |
---|