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