Mercurial > libervia-backend
comparison src/test/test_plugin_xep_0033.py @ 1278:347aee3a3f5c
test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 25 Dec 2014 11:49:13 +0100 |
parents | 7fbc858cd1cd |
children | 069ad98b360d |
comparison
equal
deleted
inserted
replaced
1277:3a3e3014f9f8 | 1278:347aee3a3f5c |
---|---|
23 from constants import Const | 23 from constants import Const |
24 from sat.test import helpers | 24 from sat.test import helpers |
25 from sat.plugins import plugin_xep_0033 as plugin | 25 from sat.plugins import plugin_xep_0033 as plugin |
26 from sat.core.exceptions import CancelError | 26 from sat.core.exceptions import CancelError |
27 from sat.core.log import getLogger | 27 from sat.core.log import getLogger |
28 from copy import deepcopy | |
29 from twisted.internet import defer | 28 from twisted.internet import defer |
30 from wokkel.generic import parseXml | 29 from wokkel.generic import parseXml |
31 from twisted.words.protocols.jabber.jid import JID | 30 from twisted.words.protocols.jabber.jid import JID |
32 from logging import ERROR | 31 from logging import ERROR |
33 | 32 |
69 expected = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC) | 68 expected = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC) |
70 msg = 'Expected: %s\nGot: %s' % (expected, data['extra']['addresses']) | 69 msg = 'Expected: %s\nGot: %s' % (expected, data['extra']['addresses']) |
71 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg) | 70 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg) |
72 | 71 |
73 treatments.addCallback(cb) | 72 treatments.addCallback(cb) |
74 treatments.callback(data) | 73 return treatments.callback(data) |
75 | 74 |
76 def get_mess_data(self): | 75 def _get_mess_data(self): |
77 mess_data = {"to": JID(JID_STR_TO), | 76 mess_data = {"to": JID(JID_STR_TO), |
78 "type": "chat", | 77 "type": "chat", |
79 "message": "content", | 78 "message": "content", |
80 "extra": {} | 79 "extra": {} |
81 } | 80 } |
86 </message> | 85 </message> |
87 """ % (JID_STR_FROM, JID_STR_TO) | 86 """ % (JID_STR_FROM, JID_STR_TO) |
88 mess_data['xml'] = parseXml(original_stanza.encode("utf-8")) | 87 mess_data['xml'] = parseXml(original_stanza.encode("utf-8")) |
89 return mess_data | 88 return mess_data |
90 | 89 |
91 def test_sendMessageTrigger(self): | 90 def _assertAddresses(self, mess_data): |
92 expected = self.get_mess_data()['xml'] | 91 """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger, |
92 check that the addresses element has been added to the stanza.""" | |
93 expected = self._get_mess_data()['xml'] | |
93 addresses_extra = """ | 94 addresses_extra = """ |
94 <addresses xmlns='http://jabber.org/protocol/address'> | 95 <addresses xmlns='http://jabber.org/protocol/address'> |
95 <address type='%s' jid='%s'/> | 96 <address type='%s' jid='%s'/> |
96 <address type='%s' jid='%s'/> | 97 <address type='%s' jid='%s'/> |
97 <address type='%s' jid='%s'/> | 98 <address type='%s' jid='%s'/> |
98 </addresses>""" % ADDRS | 99 </addresses>""" % ADDRS |
99 addresses_element = parseXml(addresses_extra.encode('utf-8')) | 100 addresses_element = parseXml(addresses_extra.encode('utf-8')) |
100 expected.addChild(addresses_element) | 101 expected.addChild(addresses_element) |
102 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) | |
101 | 103 |
102 def assertAddresses(mess_data): | 104 def _checkSentAndStored(self): |
103 """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger, | 105 """Check that all the recipients got their messages and that the history has been filled. |
104 check that the addresses element has been added to the stanza.""" | 106 /!\ see the comments in XEP_0033.sendAndStoreMessage""" |
105 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) | 107 sent = [] |
108 stored = [] | |
109 d_list = [] | |
106 | 110 |
107 def sendMessageErrback(failure, exception_class): | 111 def cb(entities, to_jid, logger, level): |
108 """If the failure does encapsulate the expected exception, it will be silently | 112 if host in entities: |
109 trapped, otherwise it will be re-raised and will make the test fail""" | 113 if host not in sent: # send the message to the entity offering the feature |
110 failure.trap(exception_class) | 114 sent.append(host) |
115 stored.append(host) | |
116 stored.append(to_jid) # store in history for each recipient | |
117 else: # feature not supported, use normal behavior | |
118 sent.append(to_jid) | |
119 stored.append(to_jid) | |
120 logger.setLevel(level) | |
111 | 121 |
112 def checkSentAndStored(): | 122 for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
113 """Check that all the recipients got their messages and that the history has been filled. | 123 to_jid = JID(to_s) |
114 /!\ see the comments in XEP_0033.sendAndStoreMessage""" | 124 host = JID(to_jid.host) |
115 sent = [] | |
116 stored = [] | |
117 d_list = [] | |
118 | |
119 def cb(entities, to_jid, logger, level): | |
120 if host in entities: | |
121 if host not in sent: # send the message to the entity offering the feature | |
122 sent.append(host) | |
123 stored.append(host) | |
124 stored.append(to_jid) # store in history for each recipient | |
125 else: # feature not supported, use normal behavior | |
126 sent.append(to_jid) | |
127 stored.append(to_jid) | |
128 logger.setLevel(level) | |
129 | |
130 for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): | |
131 to_jid = JID(to_s) | |
132 host = JID(to_jid.host) | |
133 logger = getLogger() | |
134 level = logger.getEffectiveLevel() | |
135 logger.setLevel(ERROR) # remove log.warning pollution | |
136 d = self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE) | |
137 d.addCallback(cb, to_jid, logger, level) | |
138 d_list.append(d) | |
139 | |
140 def cb_list(dummy): | |
141 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage" | |
142 sent_recipients = [JID(elt['to']) for elt in self.host.getSentMessages(PROFILE_INDEX)] | |
143 self.assertEqualUnsortedList(sent_recipients, sent, msg) | |
144 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg) | |
145 | |
146 return defer.DeferredList(d_list).addCallback(cb_list) | |
147 | |
148 def trigger(data, exception): | |
149 """Execute self.plugin.sendMessageTrigger with a different logging | |
150 level to not pollute the output, then check that the plugin did its | |
151 job. It should abort sending the message or add the extended | |
152 addressing information to the stanza. | |
153 @param data: the data to be processed by self.plugin.sendMessageTrigger | |
154 @param exception: CancelError | |
155 """ | |
156 logger = getLogger() | 125 logger = getLogger() |
157 level = logger.getEffectiveLevel() | 126 level = logger.getEffectiveLevel() |
158 logger.setLevel(ERROR) # remove log.warning pollution | 127 logger.setLevel(ERROR) # remove log.warning pollution |
159 pre_treatments = defer.Deferred() | 128 d = self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE) |
160 post_treatments = defer.Deferred() | 129 d.addCallback(cb, to_jid, logger, level) |
161 self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE) | 130 d_list.append(d) |
162 post_treatments.callback(data) | |
163 logger.setLevel(level) | |
164 post_treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, exception)) | |
165 return post_treatments | |
166 | 131 |
132 def cb_list(dummy): | |
133 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage" | |
134 sent_recipients = [JID(elt['to']) for elt in self.host.getSentMessages(PROFILE_INDEX)] | |
135 self.assertEqualUnsortedList(sent_recipients, sent, msg) | |
136 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg) | |
137 | |
138 return defer.DeferredList(d_list).addCallback(cb_list) | |
139 | |
140 def _trigger(self, data): | |
141 """Execute self.plugin.sendMessageTrigger with a different logging | |
142 level to not pollute the output, then check that the plugin did its | |
143 job. It should abort sending the message or add the extended | |
144 addressing information to the stanza. | |
145 @param data: the data to be processed by self.plugin.sendMessageTrigger | |
146 """ | |
147 logger = getLogger() | |
148 level = logger.getEffectiveLevel() | |
149 logger.setLevel(ERROR) # remove log.warning pollution | |
150 pre_treatments = defer.Deferred() | |
151 post_treatments = defer.Deferred() | |
152 self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE) | |
153 post_treatments.callback(data) | |
154 logger.setLevel(level) | |
155 post_treatments.addCallbacks(self._assertAddresses, lambda failure: failure.trap(CancelError)) | |
156 return post_treatments | |
157 | |
158 def test_sendMessageTriggerFeatureNotSupported(self): | |
167 # feature is not supported, abort the message | 159 # feature is not supported, abort the message |
168 self.host.memory.reinit() | 160 self.host.memory.reinit() |
169 data = self.get_mess_data() | 161 data = self._get_mess_data() |
170 d = trigger(data, CancelError) | 162 return self._trigger(data) |
171 | 163 |
164 def test_sendMessageTriggerFeatureSupported(self): | |
172 # feature is supported by the main target server | 165 # feature is supported by the main target server |
173 self.host.reinit() | 166 self.host.reinit() |
174 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) | 167 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
175 data = self.get_mess_data() | 168 data = self._get_mess_data() |
176 d.addCallback(lambda dummy: trigger(data, CancelError)) | 169 d = self._trigger(data) |
177 d.addCallback(lambda dummy: checkSentAndStored()) | 170 return d.addCallback(lambda dummy: self._checkSentAndStored()) |
178 | 171 |
172 def test_sendMessageTriggerFeatureFullySupported(self): | |
179 # feature is supported by all target servers | 173 # feature is supported by all target servers |
180 self.host.reinit() | 174 self.host.reinit() |
181 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) | 175 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
182 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): | 176 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
183 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) | 177 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) |
184 data = self.get_mess_data() | 178 data = self._get_mess_data() |
185 d.addCallback(lambda dummy: trigger(data, CancelError)) | 179 d = self._trigger(data) |
186 d.addCallback(lambda dummy: checkSentAndStored()) | 180 return d.addCallback(lambda dummy: self._checkSentAndStored()) |
187 | 181 |
182 def test_sendMessageTriggerFixWrongEntity(self): | |
188 # check that a wrong recipient entity is fixed by the backend | 183 # check that a wrong recipient entity is fixed by the backend |
189 self.host.reinit() | 184 self.host.reinit() |
190 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) | 185 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
191 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): | 186 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
192 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) | 187 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) |
193 data = self.get_mess_data() | 188 data = self._get_mess_data() |
194 data["to"] = JID(JID_STR_X_TO) | 189 data["to"] = JID(JID_STR_X_TO) |
195 d.addCallback(lambda dummy: trigger(data, CancelError)) | 190 d = self._trigger(data) |
196 d.addCallback(lambda dummy: checkSentAndStored()) | 191 return d.addCallback(lambda dummy: self._checkSentAndStored()) |
197 return d |