Mercurial > libervia-backend
annotate src/test/test_plugin_xep_0033.py @ 789:0cb423500fbb
test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 07 Jan 2014 09:27:53 +0100 |
parents | dd656d745d6a |
children | 2136be5a44a8 |
rev | line source |
---|---|
787 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) | |
6 # Copyright (C) 2013 Adrien Cossa (souliane@mailoo.org) | |
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 | |
32 | |
33 | |
34 class XEP_0033Test(helpers.SatTestCase): | |
35 | |
36 def setUp(self): | |
37 self.host = helpers.FakeSAT() | |
38 self.plugin = plugin.XEP_0033(self.host) | |
39 | |
40 def test_messageReceived(self): | |
41 self.host.memory.init() | |
42 xml = u""" | |
43 <message type="chat" from="%s" to="%s" id="test_1"> | |
44 <body>test</body> | |
45 <addresses xmlns='http://jabber.org/protocol/address'> | |
46 <address type='to' jid='%s'/> | |
47 <address type='cc' jid='%s'/> | |
48 <address type='bcc' jid='%s'/> | |
49 </addresses> | |
50 </message> | |
51 """ % (Const.TEST_JID_2_STR, self.host.getClientHostJid(Const.TEST_PROFILE), | |
52 Const.TEST_JID_STR, Const.TEST_JID_2_STR, Const.TEST_JID_3_STR) | |
53 stanza = parseXml(xml.encode("utf-8")) | |
54 treatments = defer.Deferred() | |
55 self.plugin.messageReceivedTrigger(stanza, treatments, Const.TEST_PROFILE) | |
56 data = {'extra': {}} | |
57 | |
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
|
58 def cb(data): |
787 | 59 expected = ('to', Const.TEST_JID_STR, 'cc', Const.TEST_JID_2_STR, 'bcc', Const.TEST_JID_3_STR) |
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 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
|
61 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg) |
787 | 62 |
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
|
63 treatments.addCallback(cb) |
787 | 64 treatments.callback(data) |
65 | |
66 def test_sendMessageTrigger(self): | |
67 mess_data = {"to": self.host.getClientHostJid(Const.TEST_PROFILE), | |
68 "type": "chat", | |
69 "message": "content", | |
70 "extra": {} | |
71 } | |
72 addresses = ('to', Const.TEST_JID_STR, 'cc', Const.TEST_JID_2_STR, 'bcc', Const.TEST_JID_3_STR) | |
73 mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % addresses | |
74 original_stanza = u""" | |
75 <message type="chat" from="%s" to="%s" id="test_1"> | |
76 <body>content</body> | |
77 </message> | |
78 """ % (Const.TEST_JID_2_STR, self.host.getClientHostJid(Const.TEST_PROFILE)) | |
79 mess_data['xml'] = parseXml(original_stanza.encode("utf-8")) | |
80 expected = deepcopy(mess_data['xml']) | |
81 addresses_extra = """ | |
82 <addresses xmlns='http://jabber.org/protocol/address'> | |
83 <address type='%s' jid='%s'/> | |
84 <address type='%s' jid='%s'/> | |
85 <address type='%s' jid='%s'/> | |
86 </addresses>""" % addresses | |
87 addresses_element = parseXml(addresses_extra.encode('utf-8')) | |
88 expected.addChild(addresses_element) | |
89 | |
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
|
90 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
|
91 """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger, |
787 | 92 check that the addresses element has been added to the stanza.""" |
93 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) | |
94 | |
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
|
95 def sendMessageErrback(failure, exception_class): |
787 | 96 """If the failure does encapsulate the expected exception, it will be silently |
97 trapped, otherwise it will be re-raised and will make the test fail""" | |
98 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
|
99 assertAddresses(failure.value.mess_data) |
787 | 100 failure.trap(exception_class) |
101 | |
102 def checkSentAndStored(): | |
103 """Check that all the recipients got their messages and that the history has been filled. | |
104 /!\ see the comments in XEP_0033.sendAndStoreMessage""" | |
105 sent = [] | |
106 stored = [] | |
107 cache = set() | |
108 for to_s in [addresses[1], addresses[3], addresses[5]]: | |
109 to_jid = JID(to_s) | |
110 host = JID(to_jid.host) | |
111 if self.host.memory.hasServerFeature(plugin.NS_ADDRESS, host, Const.TEST_PROFILE): | |
112 if host not in cache: | |
113 sent.append(host) | |
114 stored.append(host) | |
115 cache.add(host) | |
116 stored.append(to_jid) | |
117 else: | |
118 sent.append(to_jid) | |
119 stored.append(to_jid) | |
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
|
120 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
|
121 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
|
122 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg) |
787 | 123 |
124 # feature is not supported, abort the message | |
125 self.host.memory.init() | |
126 treatments = defer.Deferred() | |
127 data = deepcopy(mess_data) | |
128 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE) | |
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
|
129 treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, AbortSendMessage)) |
787 | 130 treatments.callback(data) |
131 | |
132 # feature is supported | |
133 self.host.init() | |
134 self.host.memory.init() | |
135 self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.TEST_PROFILE), Const.TEST_PROFILE) | |
136 treatments = defer.Deferred() | |
137 data = deepcopy(mess_data) | |
138 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE) | |
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
|
139 treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, MessageSentAndStored)) |
787 | 140 treatments.callback(data) |
141 checkSentAndStored() | |
142 | |
143 # check that a wrong recipient entity is fixed by the backend | |
144 self.host.init() | |
145 self.host.memory.init() | |
146 self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.TEST_PROFILE), Const.TEST_PROFILE) | |
147 treatments = defer.Deferred() | |
148 data = deepcopy(mess_data) | |
149 data["to"] = Const.TEST_JID | |
150 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE) | |
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 treatments.callback(mess_data) |
153 checkSentAndStored() |