comparison src/test/test_plugin_xep_0033.py @ 999:c37a24922f27

plugin XEP_0033: fixes the server part and the tests
author souliane <souliane@mailoo.org>
date Fri, 11 Apr 2014 11:02:42 +0200
parents b3f383ab39da
children 7ea0215e7092
comparison
equal deleted inserted replaced
998:f5761534e0f3 999:c37a24922f27
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]
35 JID_STR_FROM = Const.JID_STR[1]
36 JID_STR_TO = Const.PROFILE_DICT[PROFILE].host
37 JID_STR_X_TO = Const.JID_STR[0]
38 JID_STR_X_CC = Const.JID_STR[1]
39 JID_STR_X_BCC = Const.JID_STR[2]
40
34 41
35 class XEP_0033Test(helpers.SatTestCase): 42 class XEP_0033Test(helpers.SatTestCase):
36 skip = "Must be fixed after disco changes"
37 43
38 def setUp(self): 44 def setUp(self):
39 self.host = helpers.FakeSAT() 45 self.host = helpers.FakeSAT()
40 self.plugin = plugin.XEP_0033(self.host) 46 self.plugin = plugin.XEP_0033(self.host)
41 47
48 <address type='to' jid='%s'/> 54 <address type='to' jid='%s'/>
49 <address type='cc' jid='%s'/> 55 <address type='cc' jid='%s'/>
50 <address type='bcc' jid='%s'/> 56 <address type='bcc' jid='%s'/>
51 </addresses> 57 </addresses>
52 </message> 58 </message>
53 """ % (Const.JID_STR[1], self.host.getClientHostJid(Const.PROFILE[0]), 59 """ % (JID_STR_FROM, JID_STR_TO, JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC)
54 Const.JID_STR[0], Const.JID_STR[1], Const.JID_STR[2])
55 stanza = parseXml(xml.encode("utf-8")) 60 stanza = parseXml(xml.encode("utf-8"))
56 treatments = defer.Deferred() 61 treatments = defer.Deferred()
57 self.plugin.messageReceivedTrigger(stanza, treatments, Const.PROFILE[0]) 62 self.plugin.messageReceivedTrigger(stanza, treatments, PROFILE)
58 data = {'extra': {}} 63 data = {'extra': {}}
59 64
60 def cb(data): 65 def cb(data):
61 expected = ('to', Const.JID_STR[0], 'cc', Const.JID_STR[1], 'bcc', Const.JID_STR[2]) 66 expected = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC)
62 msg = 'Expected: %s\nGot: %s' % (expected, data['extra']['addresses']) 67 msg = 'Expected: %s\nGot: %s' % (expected, data['extra']['addresses'])
63 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg) 68 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg)
64 69
65 treatments.addCallback(cb) 70 treatments.addCallback(cb)
66 treatments.callback(data) 71 treatments.callback(data)
67 72
68 def test_sendMessageTrigger(self): 73 def test_sendMessageTrigger(self):
69 mess_data = {"to": self.host.getClientHostJid(Const.PROFILE[0]), 74 mess_data = {"to": JID(JID_STR_TO),
70 "type": "chat", 75 "type": "chat",
71 "message": "content", 76 "message": "content",
72 "extra": {} 77 "extra": {}
73 } 78 }
74 addresses = ('to', Const.JID_STR[0], 'cc', Const.JID_STR[1], 'bcc', Const.JID_STR[2]) 79 addresses = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC)
75 mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % addresses 80 mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % addresses
76 original_stanza = u""" 81 original_stanza = u"""
77 <message type="chat" from="%s" to="%s" id="test_1"> 82 <message type="chat" from="%s" to="%s" id="test_1">
78 <body>content</body> 83 <body>content</body>
79 </message> 84 </message>
80 """ % (Const.JID_STR[1], self.host.getClientHostJid(Const.PROFILE[0])) 85 """ % (JID_STR_FROM, JID_STR_TO)
81 mess_data['xml'] = parseXml(original_stanza.encode("utf-8")) 86 mess_data['xml'] = parseXml(original_stanza.encode("utf-8"))
82 expected = deepcopy(mess_data['xml']) 87 expected = deepcopy(mess_data['xml'])
83 addresses_extra = """ 88 addresses_extra = """
84 <addresses xmlns='http://jabber.org/protocol/address'> 89 <addresses xmlns='http://jabber.org/protocol/address'>
85 <address type='%s' jid='%s'/> 90 <address type='%s' jid='%s'/>
95 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) 100 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8"))
96 101
97 def sendMessageErrback(failure, exception_class): 102 def sendMessageErrback(failure, exception_class):
98 """If the failure does encapsulate the expected exception, it will be silently 103 """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""" 104 trapped, otherwise it will be re-raised and will make the test fail"""
105 failure.trap(exception_class)
100 if exception_class == MessageSentAndStored: 106 if exception_class == MessageSentAndStored:
101 assertAddresses(failure.value.mess_data) 107 assertAddresses(failure.value.mess_data)
102 failure.trap(exception_class)
103 108
104 def checkSentAndStored(): 109 def checkSentAndStored():
105 """Check that all the recipients got their messages and that the history has been filled. 110 """Check that all the recipients got their messages and that the history has been filled.
106 /!\ see the comments in XEP_0033.sendAndStoreMessage""" 111 /!\ see the comments in XEP_0033.sendAndStoreMessage"""
107 sent = [] 112 sent = []
108 stored = [] 113 stored = []
109 cache = set() 114 for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
110 for to_s in [addresses[1], addresses[3], addresses[5]]:
111 to_jid = JID(to_s) 115 to_jid = JID(to_s)
112 host = JID(to_jid.host) 116 host = JID(to_jid.host)
113 logger = getLogger() 117 logger = getLogger()
114 level = logger.getEffectiveLevel() 118 level = logger.getEffectiveLevel()
115 logger.setLevel(ERROR) # remove log.warning pollution 119 logger.setLevel(ERROR) # remove log.warning pollution
116 if self.host.memory.hasServerFeature(plugin.NS_ADDRESS, host, Const.PROFILE[0]): 120 entities = yield self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE)
117 if host not in cache: 121 if host in entities:
122 if host not in sent: # send the message to the entity offering the feature
118 sent.append(host) 123 sent.append(host)
119 stored.append(host) 124 stored.append(host)
120 cache.add(host) 125 stored.append(to_jid) # store in history for each recipient
121 stored.append(to_jid) 126 else: # feature not supported, use normal behavior
122 else:
123 sent.append(to_jid) 127 sent.append(to_jid)
124 stored.append(to_jid) 128 stored.append(to_jid)
125 logger.setLevel(level) 129 logger.setLevel(level)
126 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage" 130 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage"
127 self.assertEqualUnsortedList(self.host.sent_messages, sent, msg) 131 self.assertEqualUnsortedList(self.host.sent_messages, sent, msg)
138 logger = getLogger() 142 logger = getLogger()
139 level = logger.getEffectiveLevel() 143 level = logger.getEffectiveLevel()
140 logger.setLevel(ERROR) # remove log.warning pollution 144 logger.setLevel(ERROR) # remove log.warning pollution
141 pre_treatments = defer.Deferred() 145 pre_treatments = defer.Deferred()
142 post_treatments = defer.Deferred() 146 post_treatments = defer.Deferred()
143 self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, Const.PROFILE[0]) 147 self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE)
144 post_treatments.callback(data) 148 post_treatments.callback(data)
145 logger.setLevel(level) 149 logger.setLevel(level)
146 post_treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, exception)) 150 post_treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, exception))
147 151
148 # feature is not supported, abort the message 152 # feature is not supported, abort the message
149 self.host.memory.init() 153 self.host.memory.init()
150 data = deepcopy(mess_data) 154 data = deepcopy(mess_data)
151 trigger(data, AbortSendMessage) 155 trigger(data, AbortSendMessage)
152 156
153 # feature is supported 157 # feature is supported by the main target server
154 self.host.init() 158 self.host.init()
155 self.host.memory.init() 159 self.host.memory.init()
160 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
161 data = deepcopy(mess_data)
162 trigger(data, MessageSentAndStored)
163 checkSentAndStored()
164
165 # feature is supported by all target servers
166 self.host.init()
167 self.host.memory.init()
168 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
169 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
170 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE)
156 data = deepcopy(mess_data) 171 data = deepcopy(mess_data)
157 trigger(data, MessageSentAndStored) 172 trigger(data, MessageSentAndStored)
158 checkSentAndStored() 173 checkSentAndStored()
159 174
160 # check that a wrong recipient entity is fixed by the backend 175 # check that a wrong recipient entity is fixed by the backend
161 self.host.init() 176 self.host.init()
162 self.host.memory.init() 177 self.host.memory.init()
178 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
179 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
180 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE)
163 data = deepcopy(mess_data) 181 data = deepcopy(mess_data)
164 data["to"] = Const.JID[0] 182 data["to"] = JID(JID_STR_X_TO)
165 trigger(data, MessageSentAndStored) 183 trigger(data, MessageSentAndStored)
166 checkSentAndStored() 184 checkSentAndStored()