comparison sat/test/test_plugin_xep_0033.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 378188abe941
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
34 JID_STR_TO = Const.PROFILE_DICT[PROFILE].host 34 JID_STR_TO = Const.PROFILE_DICT[PROFILE].host
35 JID_STR_X_TO = Const.JID_STR[0] 35 JID_STR_X_TO = Const.JID_STR[0]
36 JID_STR_X_CC = Const.JID_STR[1] 36 JID_STR_X_CC = Const.JID_STR[1]
37 JID_STR_X_BCC = Const.JID_STR[2] 37 JID_STR_X_BCC = Const.JID_STR[2]
38 38
39 ADDRS = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC) 39 ADDRS = ("to", JID_STR_X_TO, "cc", JID_STR_X_CC, "bcc", JID_STR_X_BCC)
40 40
41 41
42 class XEP_0033Test(helpers.SatTestCase): 42 class XEP_0033Test(helpers.SatTestCase):
43
44 def setUp(self): 43 def setUp(self):
45 self.host = helpers.FakeSAT() 44 self.host = helpers.FakeSAT()
46 self.plugin = plugin.XEP_0033(self.host) 45 self.plugin = plugin.XEP_0033(self.host)
47 46
48 def test_messageReceived(self): 47 def test_messageReceived(self):
54 <address type='to' jid='%s'/> 53 <address type='to' jid='%s'/>
55 <address type='cc' jid='%s'/> 54 <address type='cc' jid='%s'/>
56 <address type='bcc' jid='%s'/> 55 <address type='bcc' jid='%s'/>
57 </addresses> 56 </addresses>
58 </message> 57 </message>
59 """ % (JID_STR_FROM, JID_STR_TO, JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC) 58 """ % (
59 JID_STR_FROM,
60 JID_STR_TO,
61 JID_STR_X_TO,
62 JID_STR_X_CC,
63 JID_STR_X_BCC,
64 )
60 stanza = parseXml(xml.encode("utf-8")) 65 stanza = parseXml(xml.encode("utf-8"))
61 treatments = defer.Deferred() 66 treatments = defer.Deferred()
62 self.plugin.messageReceivedTrigger(self.host.getClient(PROFILE), stanza, treatments) 67 self.plugin.messageReceivedTrigger(
63 data = {'extra': {}} 68 self.host.getClient(PROFILE), stanza, treatments
69 )
70 data = {"extra": {}}
64 71
65 def cb(data): 72 def cb(data):
66 expected = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC) 73 expected = ("to", JID_STR_X_TO, "cc", JID_STR_X_CC, "bcc", JID_STR_X_BCC)
67 msg = 'Expected: %s\nGot: %s' % (expected, data['extra']['addresses']) 74 msg = "Expected: %s\nGot: %s" % (expected, data["extra"]["addresses"])
68 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg) 75 self.assertEqual(
76 data["extra"]["addresses"], "%s:%s\n%s:%s\n%s:%s\n" % expected, msg
77 )
69 78
70 treatments.addCallback(cb) 79 treatments.addCallback(cb)
71 return treatments.callback(data) 80 return treatments.callback(data)
72 81
73 def _get_mess_data(self): 82 def _get_mess_data(self):
74 mess_data = {"to": JID(JID_STR_TO), 83 mess_data = {
75 "type": "chat", 84 "to": JID(JID_STR_TO),
76 "message": "content", 85 "type": "chat",
77 "extra": {} 86 "message": "content",
78 } 87 "extra": {},
79 mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % ADDRS 88 }
89 mess_data["extra"]["address"] = "%s:%s\n%s:%s\n%s:%s\n" % ADDRS
80 original_stanza = u""" 90 original_stanza = u"""
81 <message type="chat" from="%s" to="%s" id="test_1"> 91 <message type="chat" from="%s" to="%s" id="test_1">
82 <body>content</body> 92 <body>content</body>
83 </message> 93 </message>
84 """ % (JID_STR_FROM, JID_STR_TO) 94 """ % (
85 mess_data['xml'] = parseXml(original_stanza.encode("utf-8")) 95 JID_STR_FROM,
96 JID_STR_TO,
97 )
98 mess_data["xml"] = parseXml(original_stanza.encode("utf-8"))
86 return mess_data 99 return mess_data
87 100
88 def _assertAddresses(self, mess_data): 101 def _assertAddresses(self, mess_data):
89 """The mess_data that we got here has been modified by self.plugin.messageSendTrigger, 102 """The mess_data that we got here has been modified by self.plugin.messageSendTrigger,
90 check that the addresses element has been added to the stanza.""" 103 check that the addresses element has been added to the stanza."""
91 expected = self._get_mess_data()['xml'] 104 expected = self._get_mess_data()["xml"]
92 addresses_extra = """ 105 addresses_extra = (
106 """
93 <addresses xmlns='http://jabber.org/protocol/address'> 107 <addresses xmlns='http://jabber.org/protocol/address'>
94 <address type='%s' jid='%s'/> 108 <address type='%s' jid='%s'/>
95 <address type='%s' jid='%s'/> 109 <address type='%s' jid='%s'/>
96 <address type='%s' jid='%s'/> 110 <address type='%s' jid='%s'/>
97 </addresses>""" % ADDRS 111 </addresses>"""
98 addresses_element = parseXml(addresses_extra.encode('utf-8')) 112 % ADDRS
113 )
114 addresses_element = parseXml(addresses_extra.encode("utf-8"))
99 expected.addChild(addresses_element) 115 expected.addChild(addresses_element)
100 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) 116 self.assertEqualXML(
117 mess_data["xml"].toXml().encode("utf-8"), expected.toXml().encode("utf-8")
118 )
101 119
102 def _checkSentAndStored(self): 120 def _checkSentAndStored(self):
103 """Check that all the recipients got their messages and that the history has been filled. 121 """Check that all the recipients got their messages and that the history has been filled.
104 /!\ see the comments in XEP_0033.sendAndStoreMessage""" 122 /!\ see the comments in XEP_0033.sendAndStoreMessage"""
105 sent = [] 123 sent = []
106 stored = [] 124 stored = []
107 d_list = [] 125 d_list = []
108 126
109 def cb(entities, to_jid): 127 def cb(entities, to_jid):
110 if host in entities: 128 if host in entities:
111 if host not in sent: # send the message to the entity offering the feature 129 if (
130 host not in sent
131 ): # send the message to the entity offering the feature
112 sent.append(host) 132 sent.append(host)
113 stored.append(host) 133 stored.append(host)
114 stored.append(to_jid) # store in history for each recipient 134 stored.append(to_jid) # store in history for each recipient
115 else: # feature not supported, use normal behavior 135 else: # feature not supported, use normal behavior
116 sent.append(to_jid) 136 sent.append(to_jid)
125 d.addCallback(cb, to_jid) 145 d.addCallback(cb, to_jid)
126 d_list.append(d) 146 d_list.append(d)
127 147
128 def cb_list(dummy): 148 def cb_list(dummy):
129 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage" 149 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage"
130 sent_recipients = [JID(elt['to']) for elt in self.host.getSentMessages(PROFILE_INDEX)] 150 sent_recipients = [
151 JID(elt["to"]) for elt in self.host.getSentMessages(PROFILE_INDEX)
152 ]
131 self.assertEqualUnsortedList(sent_recipients, sent, msg) 153 self.assertEqualUnsortedList(sent_recipients, sent, msg)
132 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg) 154 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg)
133 155
134 return defer.DeferredList(d_list).addCallback(cb_list) 156 return defer.DeferredList(d_list).addCallback(cb_list)
135 157
141 @param data: the data to be processed by self.plugin.messageSendTrigger 163 @param data: the data to be processed by self.plugin.messageSendTrigger
142 """ 164 """
143 pre_treatments = defer.Deferred() 165 pre_treatments = defer.Deferred()
144 post_treatments = defer.Deferred() 166 post_treatments = defer.Deferred()
145 helpers.muteLogging() 167 helpers.muteLogging()
146 self.plugin.messageSendTrigger(self.host.getClient[PROFILE], data, pre_treatments, post_treatments) 168 self.plugin.messageSendTrigger(
169 self.host.getClient[PROFILE], data, pre_treatments, post_treatments
170 )
147 post_treatments.callback(data) 171 post_treatments.callback(data)
148 helpers.unmuteLogging() 172 helpers.unmuteLogging()
149 post_treatments.addCallbacks(self._assertAddresses, lambda failure: failure.trap(CancelError)) 173 post_treatments.addCallbacks(
174 self._assertAddresses, lambda failure: failure.trap(CancelError)
175 )
150 return post_treatments 176 return post_treatments
151 177
152 def test_messageSendTriggerFeatureNotSupported(self): 178 def test_messageSendTriggerFeatureNotSupported(self):
153 # feature is not supported, abort the message 179 # feature is not supported, abort the message
154 self.host.memory.reinit() 180 self.host.memory.reinit()