source: wokkel/test/test_delay.py @ 104:7de2ae0a6a88

Last change on this file since 104:7de2ae0a6a88 was 104:7de2ae0a6a88, checked in by Ralph Meijer <ralphm@…>, 9 years ago

Add support for the Delayed Delivery information formats.

File size: 6.4 KB
Line 
1# Copyright (c) Ralph Meijer.
2# See LICENSE for details.
3
4"""
5Tests for L{wokkel.delay}.
6"""
7
8from datetime import datetime
9import dateutil.tz
10
11from twisted.trial import unittest
12from twisted.words.protocols.jabber.jid import JID
13
14from wokkel.delay import Delay, DelayMixin
15from wokkel.generic import Stanza, parseXml
16
17class DelayTest(unittest.TestCase):
18    """
19    Tests for L{delay.Delay}.
20    """
21
22    def test_toElement(self):
23        """
24        The DOM structure has the serialized timestamp and sender address.
25        """
26        delay = Delay(stamp=datetime(2002, 9, 10, 23, 8, 25,
27                                  tzinfo=dateutil.tz.tzutc()),
28                      sender=JID(u'user@example.org'))
29        element = delay.toElement()
30
31        self.assertEqual(u'urn:xmpp:delay', element.uri)
32        self.assertEqual(u'delay', element.name)
33        self.assertEqual(u'2002-09-10T23:08:25Z', element.getAttribute('stamp'))
34        self.assertEqual(u'user@example.org', element.getAttribute('from'))
35
36
37    def test_toElementStampMissing(self):
38        """
39        To render to XML, at least a timestamp must be provided.
40        """
41        delay = Delay(stamp=None)
42        self.assertRaises(ValueError, delay.toElement)
43
44
45    def test_toElementStampOffsetNaive(self):
46        """
47        The provided timestamp must be offset aware.
48        """
49        delay = Delay(stamp=datetime(2002, 9, 10, 23, 8, 25))
50        self.assertRaises(ValueError, delay.toElement)
51
52
53    def test_toElementLegacy(self):
54        """
55        The legacy format uses C{CCYYMMDDThh:mm:ss} in the old namespace.
56        """
57        delay = Delay(stamp=datetime(2002, 9, 10, 23, 8, 25,
58                                  tzinfo=dateutil.tz.tzutc()),
59                      sender=JID(u'user@example.org'))
60        element = delay.toElement(legacy=True)
61
62        self.assertEqual(u'jabber:x:delay', element.uri)
63        self.assertEqual(u'x', element.name)
64        self.assertEqual(u'20020910T23:08:25', element.getAttribute('stamp'))
65        self.assertEqual(u'user@example.org', element.getAttribute('from'))
66
67
68    def test_fromElement(self):
69        """
70        The timestamp is parsed with the proper timezone (UTC).
71        """
72        xml = parseXml(u"""
73            <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z"/>
74        """)
75
76        delay = Delay.fromElement(xml)
77        self.assertEqual(datetime(2002, 9, 10, 23, 8, 25,
78                                  tzinfo=dateutil.tz.tzutc()),
79                         delay.stamp)
80        self.assertIdentical(None, delay.sender)
81
82
83    def test_fromElementLegacy(self):
84        """
85        For legacy XEP-0091 support, the timestamp is assumed to be in UTC.
86        """
87        xml = parseXml(u"""
88            <x xmlns="jabber:x:delay" stamp="20020910T23:08:25"/>
89        """)
90
91        delay = Delay.fromElement(xml)
92        self.assertEqual(datetime(2002, 9, 10, 23, 8, 25,
93                                  tzinfo=dateutil.tz.tzutc()),
94                         delay.stamp)
95        self.assertIdentical(None, delay.sender)
96
97
98    def test_fromElementSender(self):
99        """
100        The optional original sender address is parsed as a JID.
101        """
102        xml = parseXml(u"""
103            <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z"
104                                          from="user@example.org"/>
105        """)
106
107        delay = Delay.fromElement(xml)
108        self.assertEqual(JID(u'user@example.org'), delay.sender)
109
110
111    def test_fromElementSenderBad(self):
112        """
113        An invalid original sender address results in C{None}.
114        """
115        xml = parseXml(u"""
116            <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z"
117                                          from="user@@example.org"/>
118        """)
119
120        delay = Delay.fromElement(xml)
121        self.assertIdentical(None, delay.sender)
122
123
124    def test_fromElementMissingStamp(self):
125        """
126        A missing timestamp results in C{None} for the stamp attribute.
127        """
128        xml = parseXml(u"""
129            <delay xmlns="urn:xmpp:delay"/>
130        """)
131
132        delay = Delay.fromElement(xml)
133        self.assertIdentical(None, delay.stamp)
134
135
136    def test_fromElementBadStamp(self):
137        """
138        A malformed timestamp results in C{None} for the stamp attribute.
139        """
140        xml = parseXml(u"""
141            <delay xmlns="urn:xmpp:delay" stamp="foobar"/>
142        """)
143
144        delay = Delay.fromElement(xml)
145        self.assertIdentical(None, delay.stamp)
146
147
148
149class DelayStanza(Stanza, DelayMixin):
150    """
151    Test stanza class that mixes in delayed delivery information parsing.
152    """
153
154
155
156class DelayMixinTest(unittest.TestCase):
157
158    def test_fromParentElement(self):
159        """
160        A child element with delay information is found and parsed.
161        """
162        xml = parseXml(u"""
163            <message>
164              <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z"/>
165            </message>
166        """)
167        stanza = DelayStanza.fromElement(xml)
168        self.assertNotIdentical(None, stanza.delay)
169
170
171    def test_fromParentElementLegacy(self):
172        """
173        A child element with legacy delay information is found and parsed.
174        """
175        xml = parseXml(u"""
176            <message>
177              <x xmlns="jabber:x:delay" stamp="20020910T23:08:25"/>
178            </message>
179        """)
180        stanza = DelayStanza.fromElement(xml)
181        self.assertNotIdentical(None, stanza.delay)
182
183
184    def test_fromParentElementBothLegacyLast(self):
185        """
186        The XEP-0203 format is used over later legacy XEP-0091 format.
187        """
188        xml = parseXml(u"""
189            <message>
190              <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z"/>
191              <x xmlns="jabber:x:delay" stamp="20010910T23:08:25"/>
192            </message>
193        """)
194        stanza = DelayStanza.fromElement(xml)
195        self.assertNotIdentical(None, stanza.delay)
196        self.assertEqual(2002, stanza.delay.stamp.year)
197
198
199    def test_fromParentElementBothLegacyFirst(self):
200        """
201        The XEP-0203 format is used over earlier legacy XEP-0091 format.
202        """
203        xml = parseXml(u"""
204            <message>
205              <x xmlns="jabber:x:delay" stamp="20010910T23:08:25"/>
206              <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z"/>
207            </message>
208        """)
209        stanza = DelayStanza.fromElement(xml)
210        self.assertNotIdentical(None, stanza.delay)
211        self.assertEqual(2002, stanza.delay.stamp.year)
Note: See TracBrowser for help on using the repository browser.