source: wokkel/test/test_delay.py @ 209:1f9f603586ad

Last change on this file since 209:1f9f603586ad was 209:1f9f603586ad, checked in by Ralph Meijer <ralphm@…>, 6 years ago

imported patch py3-imports.patch

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