Mercurial > libervia-backend
annotate src/test/test_plugin_xep_0033.py @ 997:b3f383ab39da
test: configuration/use of new logging system
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 21 Apr 2014 20:04:52 +0200 |
parents | 301b342c697a |
children | c37a24922f27 |
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 | |
26 from sat.core.sat_main import AbortSendMessage, MessageSentAndStored | |
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 |
34 | |
35 class XEP_0033Test(helpers.SatTestCase): | |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
924
diff
changeset
|
36 skip = "Must be fixed after disco changes" |
787 | 37 |
38 def setUp(self): | |
39 self.host = helpers.FakeSAT() | |
40 self.plugin = plugin.XEP_0033(self.host) | |
41 | |
42 def test_messageReceived(self): | |
43 self.host.memory.init() | |
44 xml = u""" | |
45 <message type="chat" from="%s" to="%s" id="test_1"> | |
46 <body>test</body> | |
47 <addresses xmlns='http://jabber.org/protocol/address'> | |
48 <address type='to' jid='%s'/> | |
49 <address type='cc' jid='%s'/> | |
50 <address type='bcc' jid='%s'/> | |
51 </addresses> | |
52 </message> | |
792
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
53 """ % (Const.JID_STR[1], self.host.getClientHostJid(Const.PROFILE[0]), |
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
54 Const.JID_STR[0], Const.JID_STR[1], Const.JID_STR[2]) |
787 | 55 stanza = parseXml(xml.encode("utf-8")) |
56 treatments = defer.Deferred() | |
792
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
57 self.plugin.messageReceivedTrigger(stanza, treatments, Const.PROFILE[0]) |
787 | 58 data = {'extra': {}} |
59 | |
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
|
60 def cb(data): |
792
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
61 expected = ('to', Const.JID_STR[0], 'cc', Const.JID_STR[1], 'bcc', Const.JID_STR[2]) |
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
|
62 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
|
63 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg) |
787 | 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 treatments.addCallback(cb) |
787 | 66 treatments.callback(data) |
67 | |
68 def test_sendMessageTrigger(self): | |
792
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
69 mess_data = {"to": self.host.getClientHostJid(Const.PROFILE[0]), |
787 | 70 "type": "chat", |
71 "message": "content", | |
72 "extra": {} | |
73 } | |
792
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
74 addresses = ('to', Const.JID_STR[0], 'cc', Const.JID_STR[1], 'bcc', Const.JID_STR[2]) |
787 | 75 mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % addresses |
76 original_stanza = u""" | |
77 <message type="chat" from="%s" to="%s" id="test_1"> | |
78 <body>content</body> | |
79 </message> | |
792
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
80 """ % (Const.JID_STR[1], self.host.getClientHostJid(Const.PROFILE[0])) |
787 | 81 mess_data['xml'] = parseXml(original_stanza.encode("utf-8")) |
82 expected = deepcopy(mess_data['xml']) | |
83 addresses_extra = """ | |
84 <addresses xmlns='http://jabber.org/protocol/address'> | |
85 <address type='%s' jid='%s'/> | |
86 <address type='%s' jid='%s'/> | |
87 <address type='%s' jid='%s'/> | |
88 </addresses>""" % addresses | |
89 addresses_element = parseXml(addresses_extra.encode('utf-8')) | |
90 expected.addChild(addresses_element) | |
91 | |
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
|
92 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
|
93 """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger, |
787 | 94 check that the addresses element has been added to the stanza.""" |
95 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) | |
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 sendMessageErrback(failure, exception_class): |
787 | 98 """If the failure does encapsulate the expected exception, it will be silently |
99 trapped, otherwise it will be re-raised and will make the test fail""" | |
100 if exception_class == MessageSentAndStored: | |
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
|
101 assertAddresses(failure.value.mess_data) |
787 | 102 failure.trap(exception_class) |
103 | |
104 def checkSentAndStored(): | |
105 """Check that all the recipients got their messages and that the history has been filled. | |
106 /!\ see the comments in XEP_0033.sendAndStoreMessage""" | |
107 sent = [] | |
108 stored = [] | |
109 cache = set() | |
110 for to_s in [addresses[1], addresses[3], addresses[5]]: | |
111 to_jid = JID(to_s) | |
112 host = JID(to_jid.host) | |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
113 logger = getLogger() |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
114 level = logger.getEffectiveLevel() |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
115 logger.setLevel(ERROR) # remove log.warning pollution |
792
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
116 if self.host.memory.hasServerFeature(plugin.NS_ADDRESS, host, Const.PROFILE[0]): |
787 | 117 if host not in cache: |
118 sent.append(host) | |
119 stored.append(host) | |
120 cache.add(host) | |
121 stored.append(to_jid) | |
122 else: | |
123 sent.append(to_jid) | |
124 stored.append(to_jid) | |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg) |
787 | 129 |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
130 def trigger(data, exception): |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
131 """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
|
132 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
|
133 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
|
134 addressing information to the stanza. |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
135 @param data: the data to be processed by self.plugin.sendMessageTrigger |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
136 @param exception: AbortSendMessage or MessageSentAndStored |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
137 """ |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
138 logger = getLogger() |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
139 level = logger.getEffectiveLevel() |
997
b3f383ab39da
test: configuration/use of new logging system
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
140 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
|
141 pre_treatments = defer.Deferred() |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
142 post_treatments = defer.Deferred() |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
143 self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, Const.PROFILE[0]) |
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
144 post_treatments.callback(data) |
793
cb2db0d85029
test: silent info/warning that were polluting the output
souliane <souliane@mailoo.org>
parents:
792
diff
changeset
|
145 logger.setLevel(level) |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
146 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
|
147 |
787 | 148 # feature is not supported, abort the message |
149 self.host.memory.init() | |
150 data = deepcopy(mess_data) | |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
151 trigger(data, AbortSendMessage) |
787 | 152 |
153 # feature is supported | |
154 self.host.init() | |
155 self.host.memory.init() | |
156 data = deepcopy(mess_data) | |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
157 trigger(data, MessageSentAndStored) |
787 | 158 checkSentAndStored() |
159 | |
160 # check that a wrong recipient entity is fixed by the backend | |
161 self.host.init() | |
162 self.host.memory.init() | |
163 data = deepcopy(mess_data) | |
792
2136be5a44a8
test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents:
789
diff
changeset
|
164 data["to"] = Const.JID[0] |
924
861593a5652b
test: fix tests fo plugins XEP-0033 and XEP-0085
souliane <souliane@mailoo.org>
parents:
915
diff
changeset
|
165 trigger(data, MessageSentAndStored) |
787 | 166 checkSentAndStored() |