diff --git a/run_tests.py b/run_tests.py index 50278cd..6371eba 100644 --- a/run_tests.py +++ b/run_tests.py @@ -50,14 +50,14 @@ if __name__ == '__main__': feeder_suite = unittest.makeSuite(Feeder_TestCase, "test") sender_suite = unittest.makeSuite(Sender_TestCase, "test") jcl_suite = unittest.TestSuite() -# jcl_suite.addTest(FeederComponent_TestCase('test_handle_get_register_exist_complex')) -# jcl_suite.addTest(FeederComponent_TestCase('test_constructor')) + jcl_suite.addTest(FeederComponent_TestCase('test_handle_get_register_exist')) +# jcl_suite.addTest(FeederComponent_TestCase('test_handle_presence_available_to_account_live_password')) # jcl_suite = unittest.TestSuite((feeder_component_suite)) # jcl_suite = unittest.TestSuite((component_suite)) - jcl_suite = unittest.TestSuite((component_suite, - feeder_component_suite, - feeder_suite, - sender_suite)) +# jcl_suite = unittest.TestSuite((component_suite, +# feeder_component_suite, +# feeder_suite, +# sender_suite)) test_support.run_suite(jcl_suite) diff --git a/src/jcl/jabber/component.py b/src/jcl/jabber/component.py index 1f1bada..c195498 100644 --- a/src/jcl/jabber/component.py +++ b/src/jcl/jabber/component.py @@ -143,19 +143,23 @@ class JCLComponent(Component, object): self.queue.put(e) raise finally: - if self.stream: - # TODO : send unavailble from transport and all account to users - pass -# for jid in self.__storage.keys(()): -# p = Presence(from_jid = unicode(self.jid), to_jid = jid, \ -# stanza_type = "unavailable") -# self.stream.send(p) -# for jid, name in self.__storage.keys(): -# if self.__storage[(jid, name)].status != "offline": -# p = Presence(from_jid = name + "@" + unicode(self.jid),\ -# to_jid = jid, \ -# stanza_type = "unavailable") -# self.stream.send(p) + if self.stream and not self.stream.eof \ + and self.stream.socket is not None: + current_user_jid = None + self.db_connect() + for _account in \ + self.account_class.select(orderBy = "user_jid"): + if current_user_jid != _account.user_jid: + current_user_jid = _account.user_jid + self.stream.send(Presence(\ + from_jid = unicode(self.jid), \ + to_jid = _account.user_jid, \ + stanza_type = "unavailable")) + self.stream.send(Presence(\ + from_jid = self.get_jid(_account), \ + to_jid = _account.user_jid, \ + stanza_type = "unavailable")) + self.db_disconnect() # threads = threading.enumerate() timer_thread.join(JCLComponent.timeout) # for _thread in threads: diff --git a/tests/jcl/jabber/test_component.py b/tests/jcl/jabber/test_component.py index a748957..ee66da4 100644 --- a/tests/jcl/jabber/test_component.py +++ b/tests/jcl/jabber/test_component.py @@ -189,9 +189,56 @@ class JCLComponent_TestCase(unittest.TestCase): NotImplementedError)) def test_run_go_offline(self): - ## TODO : verify offline stanza are sent - pass - + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + self.comp.time_unit = 1 + self.max_tick_count = 1 + self.comp.handle_tick = self.__handle_tick_test_time_handler + account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) + account11 = Account(user_jid = "test1@test.com", \ + name = "account11", \ + jid = "account11@jcl.test.com") + account12 = Account(user_jid = "test1@test.com", \ + name = "account12", \ + jid = "account12@jcl.test.com") + account2 = Account(user_jid = "test2@test.com", \ + name = "account2", \ + jid = "account2@jcl.test.com") + del account.hub.threadConnection + self.comp.run() + self.assertTrue(self.comp.stream.connection_started) + threads = threading.enumerate() + self.assertEquals(len(threads), 1) + self.assertTrue(self.comp.stream.connection_stopped) + if self.comp.queue.qsize(): + raise self.comp.queue.get(0) + self.assertEquals(len(self.comp.stream.sent), 5) + presence = self.comp.stream.sent[0] + self.assertTrue(isinstance(presence, Presence)) + self.assertEquals(presence.get_from(), "jcl.test.com") + self.assertEquals(presence.get_to(), "test1@test.com") + self.assertEquals(presence.get_node().prop("type"), "unavailable") + presence = self.comp.stream.sent[1] + self.assertTrue(isinstance(presence, Presence)) + self.assertEquals(presence.get_from(), "account11@jcl.test.com") + self.assertEquals(presence.get_to(), "test1@test.com") + self.assertEquals(presence.get_node().prop("type"), "unavailable") + presence = self.comp.stream.sent[2] + self.assertTrue(isinstance(presence, Presence)) + self.assertEquals(presence.get_from(), "account12@jcl.test.com") + self.assertEquals(presence.get_to(), "test1@test.com") + self.assertEquals(presence.get_node().prop("type"), "unavailable") + presence = self.comp.stream.sent[3] + self.assertTrue(isinstance(presence, Presence)) + self.assertEquals(presence.get_from(), "jcl.test.com") + self.assertEquals(presence.get_to(), "test2@test.com") + self.assertEquals(presence.get_node().prop("type"), "unavailable") + presence = self.comp.stream.sent[4] + self.assertTrue(isinstance(presence, Presence)) + self.assertEquals(presence.get_from(), "account2@jcl.test.com") + self.assertEquals(presence.get_to(), "test2@test.com") + self.assertEquals(presence.get_node().prop("type"), "unavailable") + def __handle_tick_test_time_handler(self): self.max_tick_count -= 1 if self.max_tick_count == 0: @@ -215,13 +262,13 @@ class JCLComponent_TestCase(unittest.TestCase): def test_authenticated_send_probe(self): account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account11 = Account(user_jid = "test1@test.com", \ - name = "test11", \ + name = "account11", \ jid = "account11@jcl.test.com") account12 = Account(user_jid = "test1@test.com", \ - name = "test12", \ + name = "account12", \ jid = "account12@jcl.test.com") account2 = Account(user_jid = "test2@test.com", \ - name = "test2", \ + name = "account2", \ jid = "account2@jcl.test.com") del account.hub.threadConnection self.comp.stream = stream = MockStream()