source: ralphm-patches/request-stanza.patch @ 47:f6d222b68f1c

Last change on this file since 47:f6d222b68f1c was 47:f6d222b68f1c, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Many new patches, upstreamed affiliations and worked on pubsub item.
Upstreamed pubsub_manage_affiliations

File size: 4.1 KB
RevLine 
[47]1# HG changeset patch
2# Parent cddd993198e15f928c472d299e3351b8802e3608
3Add base class for IQ get/set request stanzas: Request.
4
5diff -r cddd993198e1 wokkel/generic.py
6--- a/wokkel/generic.py Fri Mar 04 09:48:19 2011 +0100
7+++ b/wokkel/generic.py Fri Mar 04 09:49:28 2011 +0100
8@@ -236,6 +236,27 @@
9         self.exception = error.exceptionFromStanza(element)
10 
11 
12+class Request(Stanza):
13+    stanzaKind = 'iq'
14+    stanzaType = 'get'
15+    timeout = None
16+
17+    def __init__(self, stanzaType='get', recipient=None, sender=None):
18+        Stanza.__init__(self, recipient=recipient, sender=sender)
19+        self.stanzaType = stanzaType
20+
21+
22+    def toElement(self):
23+        element = Stanza.toElement(self)
24+
25+        if not self.stanzaID:
26+            element.addUniqueId()
27+            self.stanzaID = element['id']
28+
29+        return element
30+
31+
32+
33 class DeferredXmlStreamFactory(BootstrapMixin, protocol.ClientFactory):
34     protocol = xmlstream.XmlStream
35 
36diff -r cddd993198e1 wokkel/test/test_generic.py
37--- a/wokkel/test/test_generic.py       Fri Mar 04 09:48:19 2011 +0100
38+++ b/wokkel/test/test_generic.py       Fri Mar 04 09:49:28 2011 +0100
39@@ -7,6 +7,7 @@
40 
41 from twisted.trial import unittest
42 from twisted.words.xish import domish
43+from twisted.words.protocols.jabber.jid import JID
44 
45 from wokkel import generic
46 from wokkel.test.helpers import XmlStreamStub
47@@ -86,3 +87,92 @@
48         element = domish.Element(('testns', 'test'))
49         self.pipe.sink.send(element)
50         self.assertEquals([element], called)
51+
52+
53+
54+class RequestTest(unittest.TestCase):
55+    """
56+    Tests for L{generic.Request}.
57+    """
58+
59+    def setUp(self):
60+        self.request = generic.Request()
61+
62+
63+    def test_toElementStanzaKind(self):
64+        """
65+        A request is an iq stanza.
66+        """
67+        element = self.request.toElement()
68+        self.assertIdentical(None, element.uri)
69+        self.assertEquals('iq', element.name)
70+
71+
72+    def test_toElementStanzaType(self):
73+        """
74+        The request has type 'get'.
75+        """
76+        self.assertEquals('get', self.request.stanzaType)
77+        element = self.request.toElement()
78+        self.assertEquals('get', element.getAttribute('type'))
79+
80+
81+    def test_toElementStanzaTypeSet(self):
82+        """
83+        The request has type 'set'.
84+        """
85+        self.request.stanzaType = 'set'
86+        element = self.request.toElement()
87+        self.assertEquals('set', element.getAttribute('type'))
88+
89+
90+    def test_toElementStanzaID(self):
91+        """
92+        A request, when rendered, has an identifier.
93+        """
94+        element = self.request.toElement()
95+        self.assertNotIdentical(None, self.request.stanzaID)
96+        self.assertEquals(self.request.stanzaID, element.getAttribute('id'))
97+
98+
99+    def test_toElementRecipient(self):
100+        """
101+        A request without recipient, has no 'to' attribute.
102+        """
103+        self.request = generic.Request(recipient=JID('other@example.org'))
104+        self.assertEquals(JID('other@example.org'), self.request.recipient)
105+        element = self.request.toElement()
106+        self.assertEquals(u'other@example.org', element.getAttribute('to'))
107+
108+
109+    def test_toElementRecipientNone(self):
110+        """
111+        A request without recipient, has no 'to' attribute.
112+        """
113+        element = self.request.toElement()
114+        self.assertFalse(element.hasAttribute('to'))
115+
116+
117+    def test_toElementSender(self):
118+        """
119+        A request with sender, has a 'from' attribute.
120+        """
121+        self.request = generic.Request(sender=JID('user@example.org'))
122+        self.assertEquals(JID('user@example.org'), self.request.sender)
123+        element = self.request.toElement()
124+        self.assertEquals(u'user@example.org', element.getAttribute('from'))
125+
126+
127+    def test_toElementSenderNone(self):
128+        """
129+        A request without sender, has no 'from' attribute.
130+        """
131+        element = self.request.toElement()
132+        self.assertFalse(element.hasAttribute('from'))
133+
134+
135+    def test_timeoutDefault(self):
136+        """
137+        The default is no timeout.
138+        """
139+        self.assertIdentical(None, self.request.timeout)
Note: See TracBrowser for help on using the repository browser.