comparison src/test/test_plugin_xep_0033.py @ 1272:7fbc858cd1cd

test: fixes tests for XEP-0033
author souliane <souliane@mailoo.org>
date Fri, 19 Dec 2014 11:36:43 +0100
parents 2308f8405ffb
children 347aee3a3f5c
comparison
equal deleted inserted replaced
1271:2308f8405ffb 1272:7fbc858cd1cd
29 from twisted.internet import defer 29 from twisted.internet import defer
30 from wokkel.generic import parseXml 30 from wokkel.generic import parseXml
31 from twisted.words.protocols.jabber.jid import JID 31 from twisted.words.protocols.jabber.jid import JID
32 from logging import ERROR 32 from logging import ERROR
33 33
34 PROFILE = Const.PROFILE[0] 34 PROFILE_INDEX = 0
35 PROFILE = Const.PROFILE[PROFILE_INDEX]
35 JID_STR_FROM = Const.JID_STR[1] 36 JID_STR_FROM = Const.JID_STR[1]
36 JID_STR_TO = Const.PROFILE_DICT[PROFILE].host 37 JID_STR_TO = Const.PROFILE_DICT[PROFILE].host
37 JID_STR_X_TO = Const.JID_STR[0] 38 JID_STR_X_TO = Const.JID_STR[0]
38 JID_STR_X_CC = Const.JID_STR[1] 39 JID_STR_X_CC = Const.JID_STR[1]
39 JID_STR_X_BCC = Const.JID_STR[2] 40 JID_STR_X_BCC = Const.JID_STR[2]
41
42 ADDRS = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC)
40 43
41 44
42 class XEP_0033Test(helpers.SatTestCase): 45 class XEP_0033Test(helpers.SatTestCase):
43 46
44 def setUp(self): 47 def setUp(self):
68 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg) 71 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg)
69 72
70 treatments.addCallback(cb) 73 treatments.addCallback(cb)
71 treatments.callback(data) 74 treatments.callback(data)
72 75
73 def test_sendMessageTrigger(self): 76 def get_mess_data(self):
74 mess_data = {"to": JID(JID_STR_TO), 77 mess_data = {"to": JID(JID_STR_TO),
75 "type": "chat", 78 "type": "chat",
76 "message": "content", 79 "message": "content",
77 "extra": {} 80 "extra": {}
78 } 81 }
79 addresses = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC) 82 mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % ADDRS
80 mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % addresses
81 original_stanza = u""" 83 original_stanza = u"""
82 <message type="chat" from="%s" to="%s" id="test_1"> 84 <message type="chat" from="%s" to="%s" id="test_1">
83 <body>content</body> 85 <body>content</body>
84 </message> 86 </message>
85 """ % (JID_STR_FROM, JID_STR_TO) 87 """ % (JID_STR_FROM, JID_STR_TO)
86 mess_data['xml'] = parseXml(original_stanza.encode("utf-8")) 88 mess_data['xml'] = parseXml(original_stanza.encode("utf-8"))
87 expected = deepcopy(mess_data['xml']) 89 return mess_data
90
91 def test_sendMessageTrigger(self):
92 expected = self.get_mess_data()['xml']
88 addresses_extra = """ 93 addresses_extra = """
89 <addresses xmlns='http://jabber.org/protocol/address'> 94 <addresses xmlns='http://jabber.org/protocol/address'>
90 <address type='%s' jid='%s'/> 95 <address type='%s' jid='%s'/>
91 <address type='%s' jid='%s'/> 96 <address type='%s' jid='%s'/>
92 <address type='%s' jid='%s'/> 97 <address type='%s' jid='%s'/>
93 </addresses>""" % addresses 98 </addresses>""" % ADDRS
94 addresses_element = parseXml(addresses_extra.encode('utf-8')) 99 addresses_element = parseXml(addresses_extra.encode('utf-8'))
95 expected.addChild(addresses_element) 100 expected.addChild(addresses_element)
96 101
97 def assertAddresses(mess_data): 102 def assertAddresses(mess_data):
98 """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger, 103 """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger,
107 def checkSentAndStored(): 112 def checkSentAndStored():
108 """Check that all the recipients got their messages and that the history has been filled. 113 """Check that all the recipients got their messages and that the history has been filled.
109 /!\ see the comments in XEP_0033.sendAndStoreMessage""" 114 /!\ see the comments in XEP_0033.sendAndStoreMessage"""
110 sent = [] 115 sent = []
111 stored = [] 116 stored = []
112 for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): 117 d_list = []
113 to_jid = JID(to_s) 118
114 host = JID(to_jid.host) 119 def cb(entities, to_jid, logger, level):
115 logger = getLogger()
116 level = logger.getEffectiveLevel()
117 logger.setLevel(ERROR) # remove log.warning pollution
118 entities = yield self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE)
119 if host in entities: 120 if host in entities:
120 if host not in sent: # send the message to the entity offering the feature 121 if host not in sent: # send the message to the entity offering the feature
121 sent.append(host) 122 sent.append(host)
122 stored.append(host) 123 stored.append(host)
123 stored.append(to_jid) # store in history for each recipient 124 stored.append(to_jid) # store in history for each recipient
124 else: # feature not supported, use normal behavior 125 else: # feature not supported, use normal behavior
125 sent.append(to_jid) 126 sent.append(to_jid)
126 stored.append(to_jid) 127 stored.append(to_jid)
127 logger.setLevel(level) 128 logger.setLevel(level)
128 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage" 129
129 self.assertEqualUnsortedList(self.host.sent_messages, sent, msg) 130 for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
130 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg) 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)
131 147
132 def trigger(data, exception): 148 def trigger(data, exception):
133 """Execute self.plugin.sendMessageTrigger with a different logging 149 """Execute self.plugin.sendMessageTrigger with a different logging
134 level to not pollute the output, then check that the plugin did its 150 level to not pollute the output, then check that the plugin did its
135 job. It should abort sending the message or add the extended 151 job. It should abort sending the message or add the extended
144 post_treatments = defer.Deferred() 160 post_treatments = defer.Deferred()
145 self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE) 161 self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE)
146 post_treatments.callback(data) 162 post_treatments.callback(data)
147 logger.setLevel(level) 163 logger.setLevel(level)
148 post_treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, exception)) 164 post_treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, exception))
165 return post_treatments
149 166
150 # feature is not supported, abort the message 167 # feature is not supported, abort the message
151 data = deepcopy(mess_data)
152 trigger(data, CancelError)
153 self.host.memory.reinit() 168 self.host.memory.reinit()
169 data = self.get_mess_data()
170 d = trigger(data, CancelError)
154 171
155 # feature is supported by the main target server 172 # feature is supported by the main target server
156 self.host.reinit() 173 self.host.reinit()
157 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) 174 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
158 data = deepcopy(mess_data) 175 data = self.get_mess_data()
159 trigger(data, CancelError) 176 d.addCallback(lambda dummy: trigger(data, CancelError))
160 checkSentAndStored() 177 d.addCallback(lambda dummy: checkSentAndStored())
161 178
162 # feature is supported by all target servers 179 # feature is supported by all target servers
163 self.host.reinit() 180 self.host.reinit()
164 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) 181 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
165 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): 182 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
166 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) 183 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE)
167 data = deepcopy(mess_data) 184 data = self.get_mess_data()
168 trigger(data, CancelError) 185 d.addCallback(lambda dummy: trigger(data, CancelError))
169 checkSentAndStored() 186 d.addCallback(lambda dummy: checkSentAndStored())
170 187
171 # check that a wrong recipient entity is fixed by the backend 188 # check that a wrong recipient entity is fixed by the backend
172 self.host.reinit() 189 self.host.reinit()
173 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) 190 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
174 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): 191 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
175 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) 192 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE)
176 data = deepcopy(mess_data) 193 data = self.get_mess_data()
177 data["to"] = JID(JID_STR_X_TO) 194 data["to"] = JID(JID_STR_X_TO)
178 trigger(data, CancelError) 195 d.addCallback(lambda dummy: trigger(data, CancelError))
179 checkSentAndStored() 196 d.addCallback(lambda dummy: checkSentAndStored())
197 return d