Mercurial > libervia-backend
annotate src/test/test_plugin_xep_0033.py @ 1267:ea692d51a0ee
plugins XEP-0059, XEP-0060: leave internal wokkel extensions to sat.tmp.wokkel
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 15 Dec 2014 14:03:13 +0100 |
parents | 7ea0215e7092 |
children | 2308f8405ffb |
rev | line source |
---|---|
787 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
811 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) |
6 # Copyright (C) 2013, 2014 Adrien Cossa (souliane@mailoo.org) | |
787 | 7 |
8 # This program is free software: you can redistribute it and/or modify | |
9 # it under the terms of the GNU Affero General Public License as published by | |
10 # the Free Software Foundation, either version 3 of the License, or | |
11 # (at your option) any later version. | |
12 | |
13 # This program is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 # GNU Affero General Public License for more details. | |
17 | |
18 # You should have received a copy of the GNU Affero General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 """ Plugin extended addressing stanzas """ | |
22 | |
23 from constants import Const | |
24 from sat.test import helpers | |
25 from sat.plugins import plugin_xep_0033 as plugin | |
1057
7ea0215e7092
test: fixes the tests for plugin XEP-0033
souliane <souliane@mailoo.org>
parents:
999
diff
changeset
|
26 from sat.core.exceptions import CancelError |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
27 from sat.core.log import getLogger |
787 | 28 from copy import deepcopy |
29 from twisted.internet import defer | |
30 from wokkel.generic import parseXml | |
31 from twisted.words.protocols.jabber.jid import JID | |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
32 from logging import ERROR |
787 | 33 |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
34 PROFILE = Const.PROFILE[0] |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
35 JID_STR_FROM = Const.JID_STR[1] |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
36 JID_STR_TO = Const.PROFILE_DICT[PROFILE].host |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
37 JID_STR_X_TO = Const.JID_STR[0] |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
38 JID_STR_X_CC = Const.JID_STR[1] |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
39 JID_STR_X_BCC = Const.JID_STR[2] |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
40 |
787 | 41 |
42 class XEP_0033Test(helpers.SatTestCase): | |
43 | |
44 def setUp(self): | |
45 self.host = helpers.FakeSAT() | |
46 self.plugin = plugin.XEP_0033(self.host) | |
47 | |
48 def test_messageReceived(self): | |
49 self.host.memory.init() | |
50 xml = u""" | |
51 <message type="chat" from="%s" to="%s" id="test_1"> | |
52 <body>test</body> | |
53 <addresses xmlns='http://jabber.org/protocol/address'> | |
54 <address type='to' jid='%s'/> | |
55 <address type='cc' jid='%s'/> | |
56 <address type='bcc' jid='%s'/> | |
57 </addresses> | |
58 </message> | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
59 """ % (JID_STR_FROM, JID_STR_TO, JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC) |
787 | 60 stanza = parseXml(xml.encode("utf-8")) |
61 treatments = defer.Deferred() | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
62 self.plugin.messageReceivedTrigger(stanza, treatments, PROFILE) |
787 | 63 data = {'extra': {}} |
64 | |
789
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
65 def cb(data): |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
66 expected = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC) |
789
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
67 msg = 'Expected: %s\nGot: %s' % (expected, data['extra']['addresses']) |
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
68 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg) |
787 | 69 |
789
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
70 treatments.addCallback(cb) |
787 | 71 treatments.callback(data) |
72 | |
73 def test_sendMessageTrigger(self): | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
74 mess_data = {"to": JID(JID_STR_TO), |
787 | 75 "type": "chat", |
76 "message": "content", | |
77 "extra": {} | |
78 } | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
79 addresses = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC) |
787 | 80 mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % addresses |
81 original_stanza = u""" | |
82 <message type="chat" from="%s" to="%s" id="test_1"> | |
83 <body>content</body> | |
84 </message> | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
85 """ % (JID_STR_FROM, JID_STR_TO) |
787 | 86 mess_data['xml'] = parseXml(original_stanza.encode("utf-8")) |
87 expected = deepcopy(mess_data['xml']) | |
88 addresses_extra = """ | |
89 <addresses xmlns='http://jabber.org/protocol/address'> | |
90 <address type='%s' jid='%s'/> | |
91 <address type='%s' jid='%s'/> | |
92 <address type='%s' jid='%s'/> | |
93 </addresses>""" % addresses | |
94 addresses_element = parseXml(addresses_extra.encode('utf-8')) | |
95 expected.addChild(addresses_element) | |
96 | |
789
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
97 def assertAddresses(mess_data): |
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
98 """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger, |
787 | 99 check that the addresses element has been added to the stanza.""" |
100 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) | |
101 | |
789
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
102 def sendMessageErrback(failure, exception_class): |
787 | 103 """If the failure does encapsulate the expected exception, it will be silently |
104 trapped, otherwise it will be re-raised and will make the test fail""" | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
105 failure.trap(exception_class) |
787 | 106 |
107 def checkSentAndStored(): | |
108 """Check that all the recipients got their messages and that the history has been filled. | |
109 /!\ see the comments in XEP_0033.sendAndStoreMessage""" | |
110 sent = [] | |
111 stored = [] | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
112 for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
787 | 113 to_jid = JID(to_s) |
114 host = JID(to_jid.host) | |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
115 logger = getLogger() |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
116 level = logger.getEffectiveLevel() |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
117 logger.setLevel(ERROR) # remove log.warning pollution |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
118 entities = yield self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE) |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
119 if host in entities: |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
120 if host not in sent: # send the message to the entity offering the feature |
787 | 121 sent.append(host) |
122 stored.append(host) | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
123 stored.append(to_jid) # store in history for each recipient |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
124 else: # feature not supported, use normal behavior |
787 | 125 sent.append(to_jid) |
126 stored.append(to_jid) | |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
127 logger.setLevel(level) |
789
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
128 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage" |
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
129 self.assertEqualUnsortedList(self.host.sent_messages, sent, msg) |
0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents:
787
diff
changeset
|
130 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg) |
787 | 131 |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
132 def trigger(data, exception): |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
133 """Execute self.plugin.sendMessageTrigger with a different logging |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
134 level to not pollute the output, then check that the plugin did its |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
135 job. It should abort sending the message or add the extended |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
136 addressing information to the stanza. |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
137 @param data: the data to be processed by self.plugin.sendMessageTrigger |
1057
7ea0215e7092
test: fixes the tests for plugin XEP-0033
souliane <souliane@mailoo.org>
parents:
999
diff
changeset
|
138 @param exception: CancelError |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
139 """ |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
140 logger = getLogger() |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
141 level = logger.getEffectiveLevel() |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
142 logger.setLevel(ERROR) # remove log.warning pollution |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
143 pre_treatments = defer.Deferred() |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
144 post_treatments = defer.Deferred() |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
145 self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE) |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
146 post_treatments.callback(data) |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
147 logger.setLevel(level) |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
148 post_treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, exception)) |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
149 |
787 | 150 # feature is not supported, abort the message |
151 self.host.memory.init() | |
152 data = deepcopy(mess_data) | |
1057
7ea0215e7092
test: fixes the tests for plugin XEP-0033
souliane <souliane@mailoo.org>
parents:
999
diff
changeset
|
153 trigger(data, CancelError) |
787 | 154 |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
155 # feature is supported by the main target server |
787 | 156 self.host.init() |
157 self.host.memory.init() | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
158 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
159 data = deepcopy(mess_data) |
1057
7ea0215e7092
test: fixes the tests for plugin XEP-0033
souliane <souliane@mailoo.org>
parents:
999
diff
changeset
|
160 trigger(data, CancelError) |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
161 checkSentAndStored() |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
162 |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
163 # feature is supported by all target servers |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
164 self.host.init() |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
165 self.host.memory.init() |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
166 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
167 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
168 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) |
787 | 169 data = deepcopy(mess_data) |
1057
7ea0215e7092
test: fixes the tests for plugin XEP-0033
souliane <souliane@mailoo.org>
parents:
999
diff
changeset
|
170 trigger(data, CancelError) |
787 | 171 checkSentAndStored() |
172 | |
173 # check that a wrong recipient entity is fixed by the backend | |
174 self.host.init() | |
175 self.host.memory.init() | |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
176 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
177 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
178 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) |
787 | 179 data = deepcopy(mess_data) |
999
c37a24922f27
plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents:
997
diff
changeset
|
180 data["to"] = JID(JID_STR_X_TO) |
1057
7ea0215e7092
test: fixes the tests for plugin XEP-0033
souliane <souliane@mailoo.org>
parents:
999
diff
changeset
|
181 trigger(data, CancelError) |
787 | 182 checkSentAndStored() |