Mercurial > libervia-backend
comparison sat/test/test_plugin_xep_0033.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | be6d91572633 |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
42 class XEP_0033Test(helpers.SatTestCase): | 42 class XEP_0033Test(helpers.SatTestCase): |
43 def setUp(self): | 43 def setUp(self): |
44 self.host = helpers.FakeSAT() | 44 self.host = helpers.FakeSAT() |
45 self.plugin = plugin.XEP_0033(self.host) | 45 self.plugin = plugin.XEP_0033(self.host) |
46 | 46 |
47 def test_messageReceived(self): | 47 def test_message_received(self): |
48 self.host.memory.reinit() | 48 self.host.memory.reinit() |
49 xml = """ | 49 xml = """ |
50 <message type="chat" from="%s" to="%s" id="test_1"> | 50 <message type="chat" from="%s" to="%s" id="test_1"> |
51 <body>test</body> | 51 <body>test</body> |
52 <addresses xmlns='http://jabber.org/protocol/address'> | 52 <addresses xmlns='http://jabber.org/protocol/address'> |
62 JID_STR_X_CC, | 62 JID_STR_X_CC, |
63 JID_STR_X_BCC, | 63 JID_STR_X_BCC, |
64 ) | 64 ) |
65 stanza = parseXml(xml.encode("utf-8")) | 65 stanza = parseXml(xml.encode("utf-8")) |
66 treatments = defer.Deferred() | 66 treatments = defer.Deferred() |
67 self.plugin.messageReceivedTrigger( | 67 self.plugin.message_received_trigger( |
68 self.host.getClient(PROFILE), stanza, treatments | 68 self.host.get_client(PROFILE), stanza, treatments |
69 ) | 69 ) |
70 data = {"extra": {}} | 70 data = {"extra": {}} |
71 | 71 |
72 def cb(data): | 72 def cb(data): |
73 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) |
96 JID_STR_TO, | 96 JID_STR_TO, |
97 ) | 97 ) |
98 mess_data["xml"] = parseXml(original_stanza.encode("utf-8")) | 98 mess_data["xml"] = parseXml(original_stanza.encode("utf-8")) |
99 return mess_data | 99 return mess_data |
100 | 100 |
101 def _assertAddresses(self, mess_data): | 101 def _assert_addresses(self, mess_data): |
102 """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, |
103 check that the addresses element has been added to the stanza.""" | 103 check that the addresses element has been added to the stanza.""" |
104 expected = self._get_mess_data()["xml"] | 104 expected = self._get_mess_data()["xml"] |
105 addresses_extra = ( | 105 addresses_extra = ( |
106 """ | 106 """ |
111 </addresses>""" | 111 </addresses>""" |
112 % ADDRS | 112 % ADDRS |
113 ) | 113 ) |
114 addresses_element = parseXml(addresses_extra.encode("utf-8")) | 114 addresses_element = parseXml(addresses_extra.encode("utf-8")) |
115 expected.addChild(addresses_element) | 115 expected.addChild(addresses_element) |
116 self.assertEqualXML( | 116 self.assert_equal_xml( |
117 mess_data["xml"].toXml().encode("utf-8"), expected.toXml().encode("utf-8") | 117 mess_data["xml"].toXml().encode("utf-8"), expected.toXml().encode("utf-8") |
118 ) | 118 ) |
119 | 119 |
120 def _checkSentAndStored(self): | 120 def _check_sent_and_stored(self): |
121 """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. |
122 /!\ see the comments in XEP_0033.sendAndStoreMessage""" | 122 /!\ see the comments in XEP_0033.send_and_store_message""" |
123 sent = [] | 123 sent = [] |
124 stored = [] | 124 stored = [] |
125 d_list = [] | 125 d_list = [] |
126 | 126 |
127 def cb(entities, to_jid): | 127 def cb(entities, to_jid): |
133 stored.append(host) | 133 stored.append(host) |
134 stored.append(to_jid) # store in history for each recipient | 134 stored.append(to_jid) # store in history for each recipient |
135 else: # feature not supported, use normal behavior | 135 else: # feature not supported, use normal behavior |
136 sent.append(to_jid) | 136 sent.append(to_jid) |
137 stored.append(to_jid) | 137 stored.append(to_jid) |
138 helpers.unmuteLogging() | 138 helpers.unmute_logging() |
139 | 139 |
140 for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): | 140 for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
141 to_jid = JID(to_s) | 141 to_jid = JID(to_s) |
142 host = JID(to_jid.host) | 142 host = JID(to_jid.host) |
143 helpers.muteLogging() | 143 helpers.mute_logging() |
144 d = self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile=PROFILE) | 144 d = self.host.find_features_set([plugin.NS_ADDRESS], jid_=host, profile=PROFILE) |
145 d.addCallback(cb, to_jid) | 145 d.addCallback(cb, to_jid) |
146 d_list.append(d) | 146 d_list.append(d) |
147 | 147 |
148 def cb_list(__): | 148 def cb_list(__): |
149 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage" | 149 msg = "/!\ see the comments in XEP_0033.send_and_store_message" |
150 sent_recipients = [ | 150 sent_recipients = [ |
151 JID(elt["to"]) for elt in self.host.getSentMessages(PROFILE_INDEX) | 151 JID(elt["to"]) for elt in self.host.get_sent_messages(PROFILE_INDEX) |
152 ] | 152 ] |
153 self.assertEqualUnsortedList(sent_recipients, sent, msg) | 153 self.assert_equal_unsorted_list(sent_recipients, sent, msg) |
154 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg) | 154 self.assert_equal_unsorted_list(self.host.stored_messages, stored, msg) |
155 | 155 |
156 return defer.DeferredList(d_list).addCallback(cb_list) | 156 return defer.DeferredList(d_list).addCallback(cb_list) |
157 | 157 |
158 def _trigger(self, data): | 158 def _trigger(self, data): |
159 """Execute self.plugin.messageSendTrigger with a different logging | 159 """Execute self.plugin.messageSendTrigger with a different logging |
162 addressing information to the stanza. | 162 addressing information to the stanza. |
163 @param data: the data to be processed by self.plugin.messageSendTrigger | 163 @param data: the data to be processed by self.plugin.messageSendTrigger |
164 """ | 164 """ |
165 pre_treatments = defer.Deferred() | 165 pre_treatments = defer.Deferred() |
166 post_treatments = defer.Deferred() | 166 post_treatments = defer.Deferred() |
167 helpers.muteLogging() | 167 helpers.mute_logging() |
168 self.plugin.messageSendTrigger( | 168 self.plugin.messageSendTrigger( |
169 self.host.getClient[PROFILE], data, pre_treatments, post_treatments | 169 self.host.get_client[PROFILE], data, pre_treatments, post_treatments |
170 ) | 170 ) |
171 post_treatments.callback(data) | 171 post_treatments.callback(data) |
172 helpers.unmuteLogging() | 172 helpers.unmute_logging() |
173 post_treatments.addCallbacks( | 173 post_treatments.addCallbacks( |
174 self._assertAddresses, lambda failure: failure.trap(CancelError) | 174 self._assert_addresses, lambda failure: failure.trap(CancelError) |
175 ) | 175 ) |
176 return post_treatments | 176 return post_treatments |
177 | 177 |
178 def test_messageSendTriggerFeatureNotSupported(self): | 178 def test_message_send_trigger_feature_not_supported(self): |
179 # feature is not supported, abort the message | 179 # feature is not supported, abort the message |
180 self.host.memory.reinit() | 180 self.host.memory.reinit() |
181 data = self._get_mess_data() | 181 data = self._get_mess_data() |
182 return self._trigger(data) | 182 return self._trigger(data) |
183 | 183 |
184 def test_messageSendTriggerFeatureSupported(self): | 184 def test_message_send_trigger_feature_supported(self): |
185 # feature is supported by the main target server | 185 # feature is supported by the main target server |
186 self.host.reinit() | 186 self.host.reinit() |
187 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) | 187 self.host.add_feature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
188 data = self._get_mess_data() | 188 data = self._get_mess_data() |
189 d = self._trigger(data) | 189 d = self._trigger(data) |
190 return d.addCallback(lambda __: self._checkSentAndStored()) | 190 return d.addCallback(lambda __: self._check_sent_and_stored()) |
191 | 191 |
192 def test_messageSendTriggerFeatureFullySupported(self): | 192 def test_message_send_trigger_feature_fully_supported(self): |
193 # feature is supported by all target servers | 193 # feature is supported by all target servers |
194 self.host.reinit() | 194 self.host.reinit() |
195 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) | 195 self.host.add_feature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
196 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): | 196 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
197 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) | 197 self.host.add_feature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) |
198 data = self._get_mess_data() | 198 data = self._get_mess_data() |
199 d = self._trigger(data) | 199 d = self._trigger(data) |
200 return d.addCallback(lambda __: self._checkSentAndStored()) | 200 return d.addCallback(lambda __: self._check_sent_and_stored()) |
201 | 201 |
202 def test_messageSendTriggerFixWrongEntity(self): | 202 def test_message_send_trigger_fix_wrong_entity(self): |
203 # check that a wrong recipient entity is fixed by the backend | 203 # check that a wrong recipient entity is fixed by the backend |
204 self.host.reinit() | 204 self.host.reinit() |
205 self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) | 205 self.host.add_feature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE) |
206 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): | 206 for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): |
207 self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) | 207 self.host.add_feature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE) |
208 data = self._get_mess_data() | 208 data = self._get_mess_data() |
209 data["to"] = JID(JID_STR_X_TO) | 209 data["to"] = JID(JID_STR_X_TO) |
210 d = self._trigger(data) | 210 d = self._trigger(data) |
211 return d.addCallback(lambda __: self._checkSentAndStored()) | 211 return d.addCallback(lambda __: self._check_sent_and_stored()) |