Changeset 100

Show
Ignore:
Timestamp:
07/08/07 20:42:31 (2 years ago)
Author:
David Rousselie <dax@happycoders.org>
Message:

Some refactoring

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/jmc/jabber/__init__.py

    r99 r100  
    1212    """Define filter for email address in JID""" 
    1313 
    14     def __init__(self): 
    15         Handler.__init__(self
     14    def __init__(self, component): 
     15        Handler.__init__(self, component
    1616        self.dest_jid_regexp = re.compile(".*%.*") 
    1717 
  • src/jmc/jabber/component.py

    r99 r100  
    3131from pyxmpp.jid import JID 
    3232 
     33import jcl.jabber as jabber 
    3334from jcl.model.account import Account, PresenceAccount, LegacyJID 
     35from jcl.jabber.disco import RootDiscoGetInfoHandler 
    3436from jcl.jabber.component import Handler, AccountManager 
    3537from jcl.jabber.feeder import FeederComponent, Feeder, MessageSender, \ 
    3638    HeadlineSender, FeederHandler 
    3739 
     40from jmc.jabber.disco import MailRootDiscoGetInfoHandler 
    3841from jmc.jabber.message import SendMailMessageHandler, \ 
    3942     RootSendMailMessageHandler 
     
    5255                 server, 
    5356                 port, 
    54                  db_connection_str, 
    5557                 lang=Lang()): 
    5658        """Use FeederComponent behavior and setup feeder and sender 
     
    6264                                 server, 
    6365                                 port, 
    64                                  db_connection_str, 
    6566                                 lang=lang) 
    6667        self.handler = MailFeederHandler(MailFeeder(self), MailSender(self)) 
    67         self.account_manager = MailAccountManager(self) 
    6868        self.account_manager.account_classes = (IMAPAccount, 
    6969                                                POP3Account, 
    7070                                                SMTPAccount) 
    71         self.msg_handlers += [SendMailMessageHandler(), 
    72                               RootSendMailMessageHandler()] 
    73         self.subscribe_handlers += [MailSubscribeHandler()] 
    74         self.unsubscribe_handlers += [MailUnsubscribeHandler()] 
    75         self.available_handlers += [MailPresenceHandler()] 
    76         self.unavailable_handlers += [MailPresenceHandler()] 
    77  
    78 class MailAccountManager(AccountManager): 
    79     """JMC specific account behavior""" 
    80  
    81     def root_disco_get_info(self, node, name, category, type): 
    82         """Add jabber:iq:gateway support""" 
    83         disco_info = AccountManager.root_disco_get_info(self, node, name, 
    84                                                         category, type) 
    85         disco_info.add_feature("jabber:iq:gateway") 
    86         disco_info.add_identity(name, "headline", "newmail") 
    87         return disco_info 
     71        self.msg_handlers += [[SendMailMessageHandler(self), 
     72                               RootSendMailMessageHandler(self)]] 
     73        self.presence_subscribe_handlers += [[MailSubscribeHandler(self)]] 
     74        self.presence_unsubscribe_handlers += [[MailUnsubscribeHandler(self)]] 
     75        self.presence_available_handlers += [[MailPresenceHandler(self)]] 
     76        self.presence_unavailable_handlers += [[MailPresenceHandler(self)]] 
     77        jabber.replace_handlers(self.disco_get_info_handlers, 
     78                                RootDiscoGetInfoHandler, 
     79                                MailRootDiscoGetInfoHandler(self)) 
    8880 
    8981class MailFeeder(Feeder): 
  • src/jmc/jabber/message.py

    r99 r100  
    3232 
    3333class SendMailMessageHandler(MailHandler): 
    34     def __init__(self): 
    35         MailHandler.__init__(self
     34    def __init__(self, component): 
     35        MailHandler.__init__(self, component
    3636        self.__logger = logging.getLogger(\ 
    3737            "jmc.jabber.component.SendMailMessageHandler") 
     
    5858    """Handle message sent to root JID""" 
    5959 
    60     def __init__(self): 
    61         SendMailMessageHandler.__init__(self
     60    def __init__(self, component): 
     61        SendMailMessageHandler.__init__(self, component
    6262        self.to_regexp = re.compile("^\s*(to|TO|To)\s*:\s*(?P<to_email>.*)") 
    6363        self.__logger = logging.getLogger(\ 
  • src/jmc/jabber/presence.py

    r99 r100  
    3232class MailPresenceHandler(DefaultPresenceHandler): 
    3333    """Define filter for legacy JIDs presence handling""" 
    34     def __init__(self): 
    35         Handler.__init__(self) 
     34 
     35    def __init__(self, component): 
     36        Handler.__init__(self, component) 
    3637        self.dest_jid_regexp = re.compile(".*%.*") 
    3738 
     
    4546 
    4647class MailSubscribeHandler(DefaultSubscribeHandler, MailHandler): 
    47     """Use DefaultSubscribeHandler handle method and MailHandler filter. 
     48    """ 
     49    Use DefaultSubscribeHandler handle method and MailHandler filter. 
    4850    Filter email address in JID. Accept and add to LegacyJID table. 
    4951    """ 
    5052 
    51     def __init__(self): 
    52         DefaultSubscribeHandler.__init__(self
    53         MailHandler.__init__(self
     53    def __init__(self, component): 
     54        DefaultSubscribeHandler.__init__(self, component
     55        MailHandler.__init__(self, component
    5456 
    5557    def filter(self, stanza, lang_class): 
     
    6668 
    6769class MailUnsubscribeHandler(DefaultUnsubscribeHandler, MailHandler): 
    68     """Use DefaultUnsubscribeHandler handle method and MailHandler filter. 
     70    """ 
     71    Use DefaultUnsubscribeHandler handle method and MailHandler filter. 
    6972    """ 
    7073 
    71     def __init__(self): 
    72         DefaultUnsubscribeHandler.__init__(self
    73         MailHandler.__init__(self
     74    def __init__(self, component): 
     75        DefaultUnsubscribeHandler.__init__(self, component
     76        MailHandler.__init__(self, component
    7477 
    7578    def filter(self, stanza, lang_class): 
  • src/jmc/jabber/tests/__init__.py

    r68 r100  
    44import unittest 
    55 
    6 from jmc.jabber.tests import component 
     6from jmc.jabber.tests import component, disco 
    77 
    88def suite(): 
    99    suite = unittest.TestSuite() 
    1010    suite.addTest(component.suite()) 
     11    suite.addTest(disco.suite()) 
    1112    return suite 
    1213 
  • src/jmc/jabber/tests/component.py

    r99 r100  
    3232from pyxmpp.message import Message 
    3333 
     34import jcl.model as model 
    3435from jcl.model import account 
    3536from jcl.model.account import Account, PresenceAccount, LegacyJID 
    36 from jcl.jabber.tests.component import DefaultSubscribeHandler_TestCase, \ 
     37from jcl.jabber.tests.presence import DefaultSubscribeHandler_TestCase, \ 
    3738    DefaultUnsubscribeHandler_TestCase 
    3839from jcl.jabber.tests.feeder import FeederMock, SenderMock 
     
    4647     MailUnsubscribeHandler, MailPresenceHandler 
    4748from jmc.jabber.component import MailComponent, MailFeederHandler, \ 
    48      MailAccountManager, MailSender 
     49     MailSender 
    4950from jmc.lang import Lang 
    5051 
     
    5657 
    5758class MockStream(object): 
    58     def __init__(self, \ 
    59                  jid = "", 
    60                  secret = "", 
    61                  server = "", 
    62                  port = "", 
    63                  keepalive = True): 
     59    def __init__(self, 
     60                 jid="", 
     61                 secret="", 
     62                 server="", 
     63                 port="", 
     64                 keepalive=True): 
    6465        self.sent = [] 
    6566        self.connection_started = False 
     
    7475        if not iq_type in ["query"]: 
    7576            raise Exception("IQ type unknown: " + iq_type) 
    76         if not ns in ["jabber:iq:version", \ 
    77                       "jabber:iq:register", \ 
    78                       "http://jabber.org/protocol/disco#items", \ 
     77        if not ns in ["jabber:iq:version", 
     78                      "jabber:iq:register", 
     79                      "http://jabber.org/protocol/disco#items", 
    7980                      "http://jabber.org/protocol/disco#info"]: 
    8081            raise Exception("Unknown namespace: " + ns) 
     
    8586 
    8687    def set_presence_handler(self, status, handler): 
    87         if not status in ["available", \ 
    88                           "unavailable", \ 
    89                           "probe", \ 
    90                           "subscribe", \ 
    91                           "subscribed", \ 
    92                           "unsubscribe", \ 
     88        if not status in ["available", 
     89                          "unavailable", 
     90                          "probe", 
     91                          "subscribe", 
     92                          "subscribed", 
     93                          "unsubscribe", 
    9394                          "unsubscribed"]: 
    9495            raise Exception("Status unknown: " + status) 
     
    163164                                 "password", 
    164165                                 "localhost", 
    165                                  "5347", 
    166                                  'sqlite://' + DB_URL) 
     166                                 "5347") 
    167167        self.comp.stream = MockStream() 
    168168        self.comp.stream_class = MockStream 
    169         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
    170         Account.createTable(ifNotExists = True) 
    171         PresenceAccount.createTable(ifNotExists = True) 
    172         MailAccount.createTable(ifNotExists = True) 
    173         IMAPAccount.createTable(ifNotExists = True) 
    174         POP3Account.createTable(ifNotExists = True) 
    175         SMTPAccount.createTable(ifNotExists = True) 
    176         MockIMAPAccount.createTable(ifNotExists = True) 
    177         MockPOP3Account.createTable(ifNotExists = True) 
    178         del account.hub.threadConnection 
     169        model.db_connection_str = 'sqlite://' + DB_URL 
     170        model.db_connect() 
     171        Account.createTable(ifNotExists=True) 
     172        PresenceAccount.createTable(ifNotExists=True) 
     173        MailAccount.createTable(ifNotExists=True) 
     174        IMAPAccount.createTable(ifNotExists=True) 
     175        POP3Account.createTable(ifNotExists=True) 
     176        SMTPAccount.createTable(ifNotExists=True) 
     177        MockIMAPAccount.createTable(ifNotExists=True) 
     178        MockPOP3Account.createTable(ifNotExists=True) 
     179        model.db_disconnect() 
    179180 
    180181    def tearDown(self): 
    181         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
    182         MockPOP3Account.dropTable(ifExists = True) 
    183         MockIMAPAccount.dropTable(ifExists = True) 
    184         SMTPAccount.dropTable(ifExists = True) 
    185         POP3Account.dropTable(ifExists = True) 
    186         IMAPAccount.dropTable(ifExists = True) 
    187         MailAccount.dropTable(ifExists = True) 
    188         PresenceAccount.dropTable(ifExists = True) 
    189         Account.dropTable(ifExists = True) 
     182        model.db_connect(
     183        MockPOP3Account.dropTable(ifExists=True) 
     184        MockIMAPAccount.dropTable(ifExists=True) 
     185        SMTPAccount.dropTable(ifExists=True) 
     186        POP3Account.dropTable(ifExists=True) 
     187        IMAPAccount.dropTable(ifExists=True) 
     188        MailAccount.dropTable(ifExists=True) 
     189        PresenceAccount.dropTable(ifExists=True) 
     190        Account.dropTable(ifExists=True) 
    190191        del TheURIOpener.cachedURIs['sqlite://' + DB_URL] 
    191         account.hub.threadConnection.close() 
    192         del account.hub.threadConnection 
     192        model.hub.threadConnection.close() 
     193        model.db_disconnect() 
    193194        if os.path.exists(DB_PATH): 
    194195            os.unlink(DB_PATH) 
     
    198199    ########################################################################### 
    199200    def test_feed_live_email_init_no_password(self): 
    200         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
    201         account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    202                                     name = "account11", \ 
    203                                     jid = "account11@jmc.test.com") 
     201        model.db_connect(
     202        account11 = MockIMAPAccount(user_jid="test1@test.com", 
     203                                    name="account11", 
     204                                    jid="account11@jmc.test.com") 
    204205        account11.status = account.ONLINE 
    205206        self.assertTrue(account11.first_check) 
     
    220221        self.assertFalse(account11.has_connected) 
    221222        self.assertFalse(account11.marked_all_as_read) 
    222         del account.hub.threadConnection 
     223        model.db_disconnect() 
    223224 
    224225    def test_feed_live_email_init_no_password2(self): 
    225         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     226        model.db_connect(
    226227        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    227228                                    name = "account11", \ 
     
    242243        self.assertFalse(account11.marked_all_as_read) 
    243244        self.assertEquals(len(self.comp.stream.sent), 0) 
    244         del account.hub.threadConnection 
     245        model.db_disconnect() 
    245246 
    246247    def test_feed_interval_no_check(self): 
    247         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     248        model.db_connect(
    248249        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    249250                                    name = "account11", \ 
     
    256257        self.assertEquals(result, []) 
    257258        self.assertEquals(account11.lastcheck, 1) 
    258         del account.hub.threadConnection 
     259        model.db_disconnect() 
    259260 
    260261    def test_feed_interval_check(self): 
    261         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     262        model.db_connect(
    262263        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    263264                                    name = "account11", \ 
     
    270271        self.assertEquals(result, []) 
    271272        self.assertEquals(account11.lastcheck, 0) 
    272         del account.hub.threadConnection 
     273        model.db_disconnect() 
    273274 
    274275    def test_feed_no_password(self): 
    275         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     276        model.db_connect(
    276277        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    277278                                    name = "account11", \ 
     
    294295        self.assertFalse(account11.connected) 
    295296        self.assertFalse(account11.has_connected) 
    296         del account.hub.threadConnection 
     297        model.db_disconnect() 
    297298 
    298299    def test_feed_unknown_action(self): 
    299         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     300        model.db_connect(
    300301        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    301302                                    name = "account11", \ 
     
    318319        self.assertFalse(account11.connected) 
    319320        self.assertTrue(account11.has_connected) 
    320         del account.hub.threadConnection 
     321        model.db_disconnect() 
    321322 
    322323    def test_feed_retrieve_no_mail(self): 
    323         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     324        model.db_connect(
    324325        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    325326                                    name = "account11", \ 
     
    339340        self.assertTrue(account11.has_connected) 
    340341        self.assertEquals(len(self.comp.stream.sent), 0) 
    341         del account.hub.threadConnection 
     342        model.db_disconnect() 
    342343 
    343344    def test_feed_retrieve_mail(self): 
     
    345346            return [("body1", "from1@test.com"), 
    346347                    ("body2", "from2@test.com")][index] 
    347         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     348        model.db_connect(
    348349        account11 = MockIMAPAccount(user_jid="test1@test.com", 
    349350                                    name="account11", 
     
    376377                          % ("from2@test.com")) 
    377378        self.assertEquals(result[1][2], "body2") 
    378         del account.hub.threadConnection 
     379        model.db_disconnect() 
    379380 
    380381    def test_feed_digest_no_mail(self): 
    381         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     382        model.db_connect(
    382383        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    383384                                    name = "account11", \ 
     
    397398        self.assertTrue(account11.has_connected) 
    398399        self.assertEquals(len(self.comp.stream.sent), 0) 
    399         del account.hub.threadConnection 
     400        model.db_disconnect() 
    400401 
    401402    def test_feed_digest_mail(self): 
     
    403404            return [("body1", "from1@test.com"), \ 
    404405                    ("body2", "from2@test.com")][index] 
    405         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     406        model.db_connect(
    406407        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    407408                                    name = "account11", \ 
     
    427428        self.assertEquals(result[0][2], \ 
    428429                          "body1\n----------------------------------\nbody2\n----------------------------------\n") 
    429         del account.hub.threadConnection 
     430        model.db_disconnect() 
    430431 
    431432    ########################################################################### 
     
    433434    ########################################################################### 
    434435    def test_initialize_live_email(self): 
    435         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     436        model.db_connect(
    436437        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    437438                                    name = "account11", \ 
     
    450451        self.assertTrue(account11.has_connected) 
    451452        self.assertTrue(account11.marked_all_as_read) 
    452         del account.hub.threadConnection 
     453        model.db_disconnect() 
    453454 
    454455    def test_initialize_live_email_connection_error(self): 
    455456        def raiser(): 
    456457            raise Exception 
    457         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     458        model.db_connect(
    458459        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    459460                                    name = "account11", \ 
     
    477478        self.assertFalse(account11.has_connected) 
    478479        self.assertFalse(account11.marked_all_as_read) 
    479         del account.hub.threadConnection 
     480        model.db_disconnect() 
    480481 
    481482    def test_initialize_live_email_mark_as_read_error(self): 
    482483        def raiser(): 
    483484            raise Exception 
    484         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     485        model.db_connect(
    485486        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    486487                                    name = "account11", \ 
     
    504505        self.assertTrue(account11.has_connected) 
    505506        self.assertFalse(account11.marked_all_as_read) 
    506         del account.hub.threadConnection 
     507        model.db_disconnect() 
    507508 
    508509    def test_initialize_live_email_disconnection_error(self): 
    509510        def raiser(): 
    510511            raise Exception 
    511         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     512        model.db_connect(
    512513        account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    513514                                    name = "account11", \ 
     
    532533        self.assertTrue(account11.has_connected) 
    533534        self.assertTrue(account11.marked_all_as_read) 
    534         del account.hub.threadConnection 
     535        model.db_disconnect() 
    535536 
    536537class SendMailMessageHandler_TestCase(unittest.TestCase): 
    537538    def setUp(self): 
    538         self.handler = SendMailMessageHandler(
     539        self.handler = SendMailMessageHandler(None
    539540 
    540541    def test_handle(self): 
     
    557558                          Lang.en.send_mail_ok_body % ("user@test.com")) 
    558559 
    559 class MailAccountManager_TestCase(unittest.TestCase): 
    560     def test_root_disco_get_info(self): 
    561         mam = MailAccountManager(None) 
    562         disco_info = mam.root_disco_get_info(None, "JMC", "gateway", "smtp") 
    563         self.assertTrue(disco_info.has_feature("jabber:iq:gateway")) 
    564         self.assertEquals(len(disco_info.get_identities()), 2) 
    565         self.assertTrue(disco_info.identity_is("gateway", "smtp")) 
    566         self.assertTrue(disco_info.identity_is("headline", "newmail")) 
    567  
    568560class RootSendMailMessageHandler_TestCase(unittest.TestCase): 
    569561    def setUp(self): 
    570         self.handler = RootSendMailMessageHandler(
     562        self.handler = RootSendMailMessageHandler(None
    571563        if os.path.exists(DB_PATH): 
    572564            os.unlink(DB_PATH) 
    573         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
    574         Account.createTable(ifNotExists = True) 
    575         SMTPAccount.createTable(ifNotExists = True) 
    576         del account.hub.threadConnection 
     565        model.db_connection_str = 'sqlite://' + DB_URL 
     566        model.db_connect() 
     567        Account.createTable(ifNotExists=True) 
     568        SMTPAccount.createTable(ifNotExists=True) 
     569        model.db_disconnect() 
    577570 
    578571    def tearDown(self): 
    579         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
    580         SMTPAccount.dropTable(ifExists = True) 
    581         Account.dropTable(ifExists = True) 
     572        model.db_connect(
     573        SMTPAccount.dropTable(ifExists=True) 
     574        Account.dropTable(ifExists=True) 
    582575        del TheURIOpener.cachedURIs['sqlite://' + DB_URL] 
    583         account.hub.threadConnection.close() 
    584         del account.hub.threadConnection 
     576        model.hub.threadConnection.close() 
     577        model.db_disconnect() 
    585578        if os.path.exists(DB_PATH): 
    586579            os.unlink(DB_PATH) 
    587580 
    588581    def test_filter(self): 
    589         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     582        model.db_connect(
    590583        account11 = SMTPAccount(user_jid="user1@test.com", 
    591584                                name="account11", 
     
    600593        accounts = self.handler.filter(message, None) 
    601594        self.assertEquals(accounts.count(), 1) 
    602         del account.hub.threadConnection 
     595        model.db_disconnect() 
    603596 
    604597    def test_filter_no_default_account(self): 
    605         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     598        model.db_connect(
    606599        account11 = SMTPAccount(user_jid="user1@test.com", 
    607600                                name="account11", 
     
    616609        self.assertEquals(accounts.count(), 2) 
    617610        self.assertEquals(accounts[0].name, "account11") 
    618         del account.hub.threadConnection 
     611        model.db_disconnect() 
    619612 
    620613    def test_filter_wrong_dest(self): 
    621         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     614        model.db_connect(
    622615        account11 = SMTPAccount(user_jid="user1@test.com", 
    623616                                name="account11", 
     
    631624        accounts = self.handler.filter(message, None) 
    632625        self.assertEquals(accounts.count(), 2) 
    633         del account.hub.threadConnection 
     626        model.db_disconnect() 
    634627 
    635628    def test_filter_wrong_user(self): 
    636         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     629        model.db_connect(
    637630        account11 = SMTPAccount(user_jid="user1@test.com", 
    638631                                name="account11", 
     
    646639        accounts = self.handler.filter(message, None) 
    647640        self.assertEquals(accounts.count(), 0) 
    648         del account.hub.threadConnection 
     641        model.db_disconnect() 
    649642 
    650643    def test_handle_email_found_in_header(self): 
     
    689682        if os.path.exists(DB_PATH): 
    690683            os.unlink(DB_PATH) 
    691         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
     684        model.db_connection_str = 'sqlite://' + DB_URL 
     685        model.db_connect() 
    692686        Account.createTable(ifNotExists=True) 
    693687        PresenceAccount.createTable(ifNotExists=True) 
     
    695689        IMAPAccount.createTable(ifNotExists=True) 
    696690        POP3Account.createTable(ifNotExists=True) 
    697         del account.hub.threadConnection 
     691        model.db_disconnect() 
    698692 
    699693    def tearDown(self): 
    700         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     694        model.db_connect(
    701695        POP3Account.dropTable(ifExists=True) 
    702696        IMAPAccount.dropTable(ifExists=True) 
     
    705699        Account.dropTable(ifExists=True) 
    706700        del TheURIOpener.cachedURIs['sqlite://' + DB_URL] 
    707         account.hub.threadConnection.close() 
    708         del account.hub.threadConnection 
     701        model.hub.threadConnection.close() 
     702        model.db_disconnect() 
    709703        if os.path.exists(DB_PATH): 
    710704            os.unlink(DB_PATH) 
     
    712706    def test_create_message(self): 
    713707        mail_sender = MailSender() 
    714         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     708        model.db_connect(
    715709        account11 = IMAPAccount(user_jid="test1@test.com", 
    716710                                name="account11", 
     
    722716                                                         "message body")) 
    723717        self.assertEquals(message.get_to(), account11.user_jid) 
    724         del account.hub.threadConnection 
     718        model.db_disconnect() 
    725719        self.assertEquals(message.get_subject(), "subject") 
    726720        self.assertEquals(message.get_body(), "message body") 
     
    735729    def test_create_message_digest(self): 
    736730        mail_sender = MailSender() 
    737         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     731        model.db_connect(
    738732        account11 = IMAPAccount(user_jid="test1@test.com", 
    739733                                name="account11", 
     
    745739                                                         "message body")) 
    746740        self.assertEquals(message.get_to(), account11.user_jid) 
    747         del account.hub.threadConnection 
     741        model.db_disconnect() 
    748742        self.assertEquals(message.get_subject(), "subject") 
    749743        self.assertEquals(message.get_body(), "message body") 
     
    752746class MailHandler_TestCase(unittest.TestCase): 
    753747    def setUp(self): 
    754         self.handler = MailHandler(
     748        self.handler = MailHandler(None
    755749        if os.path.exists(DB_PATH): 
    756750            os.unlink(DB_PATH) 
    757         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
    758         Account.createTable(ifNotExists = True) 
    759         SMTPAccount.createTable(ifNotExists = True) 
    760         del account.hub.threadConnection 
     751        model.db_connection_str = 'sqlite://' + DB_URL 
     752        model.db_connect() 
     753        Account.createTable(ifNotExists=True) 
     754        SMTPAccount.createTable(ifNotExists=True) 
     755        model.db_disconnect() 
    761756 
    762757    def tearDown(self): 
    763         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
    764         SMTPAccount.dropTable(ifExists = True) 
    765         Account.dropTable(ifExists = True) 
     758        model.db_connect(
     759        SMTPAccount.dropTable(ifExists=True) 
     760        Account.dropTable(ifExists=True) 
    766761        del TheURIOpener.cachedURIs['sqlite://' + DB_URL] 
    767         account.hub.threadConnection.close() 
    768         del account.hub.threadConnection 
     762        model.hub.threadConnection.close() 
     763        model.db_disconnect() 
    769764        if os.path.exists(DB_PATH): 
    770765            os.unlink(DB_PATH) 
    771766 
    772767    def test_filter(self): 
    773         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     768        model.db_connect(
    774769        account11 = SMTPAccount(user_jid="user1@test.com", 
    775770                                name="account11", 
     
    786781        self.assertEquals(accounts.count(), 1) 
    787782        self.assertEquals(accounts[0].name, "account11") 
    788         del account.hub.threadConnection 
     783        model.db_disconnect() 
    789784 
    790785    def test_filter_root(self): 
    791         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     786        model.db_connect(
    792787        account11 = SMTPAccount(user_jid="user1@test.com", 
    793788                                name="account11", 
     
    802797        accounts = self.handler.filter(message, None) 
    803798        self.assertEquals(accounts, None) 
    804         del account.hub.threadConnection 
     799        model.db_disconnect() 
    805800 
    806801    def test_filter_no_default(self): 
    807         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     802        model.db_connect(
    808803        account11 = SMTPAccount(user_jid = "user1@test.com", \ 
    809804                                   name = "account11", \ 
     
    819814        self.assertEquals(accounts.count(), 2) 
    820815        self.assertEquals(accounts[0].name, "account11") 
    821         del account.hub.threadConnection 
     816        model.db_disconnect() 
    822817 
    823818    def test_filter_wrong_dest(self): 
    824         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     819        model.db_connect(
    825820        account11 = SMTPAccount(user_jid = "user1@test.com", \ 
    826821                                   name = "account11", \ 
     
    834829        accounts = self.handler.filter(message, None) 
    835830        self.assertEquals(accounts, None) 
    836         del account.hub.threadConnection 
     831        model.db_disconnect() 
    837832 
    838833    def test_filter_wrong_account(self): 
    839         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     834        model.db_connect(
    840835        account11 = SMTPAccount(user_jid="user1@test.com", 
    841836                                name="account11", 
     
    853848           return 
    854849        finally: 
    855            del account.hub.threadConnection 
     850           model.db_disconnect() 
    856851        self.fail("No exception 'NoAccountError' catched") 
    857852 
    858853class MailPresenceHandler_TestCase(unittest.TestCase): 
    859854    def setUp(self): 
    860         self.handler = MailPresenceHandler(
     855        self.handler = MailPresenceHandler(None
    861856 
    862857    def test_filter(self): 
     
    884879    def setUp(self): 
    885880        MailHandler_TestCase.setUp(self) 
    886         self.handler = MailSubscribeHandler(
    887         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     881        self.handler = MailSubscribeHandler(None
     882        model.db_connect(
    888883        LegacyJID.createTable(ifNotExists=True) 
    889         del account.hub.threadConnection 
     884        model.db_disconnect() 
    890885 
    891886    def test_handle(self): 
    892         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     887        model.db_connect(
    893888        account11 = SMTPAccount(user_jid="user1@test.com", 
    894889                                name="account11", 
     
    900895        legacy_jids = LegacyJID.select() 
    901896        self.assertEquals(legacy_jids.count(), 1) 
    902         del account.hub.threadConnection 
     897        model.db_disconnect() 
    903898 
    904899class MailUnsubscribeHandler_TestCase(DefaultUnsubscribeHandler_TestCase, MailHandler_TestCase): 
    905900    def setUp(self): 
    906901        MailHandler_TestCase.setUp(self) 
    907         self.handler = MailUnsubscribeHandler(
    908         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     902        self.handler = MailUnsubscribeHandler(None
     903        model.db_connect(
    909904        LegacyJID.createTable(ifNotExists=True) 
    910         del account.hub.threadConnection 
     905        model.db_disconnect() 
    911906 
    912907    def test_handle(self): 
    913         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     908        model.db_connect(
    914909        account11 = SMTPAccount(user_jid="user1@test.com", 
    915910                                name="account11", 
     
    945940           LegacyJID.q.jid == "u111%test.com@jcl.test.com") 
    946941        self.assertEquals(removed_legacy_jid.count(), 0) 
    947         del account.hub.threadConnection 
     942        model.db_disconnect() 
    948943 
    949944class MailFeederHandler_TestCase(unittest.TestCase): 
     
    952947        if os.path.exists(DB_PATH): 
    953948            os.unlink(DB_PATH) 
    954         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
     949        model.db_connection_str = 'sqlite://' + DB_URL 
     950        model.db_connect() 
    955951        Account.createTable(ifNotExists=True) 
    956952        PresenceAccount.createTable(ifNotExists=True) 
     
    959955        POP3Account.createTable(ifNotExists=True) 
    960956        SMTPAccount.createTable(ifNotExists=True) 
    961         del account.hub.threadConnection 
     957        model.db_disconnect() 
    962958 
    963959    def tearDown(self): 
    964960        self.handler = None 
    965         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     961        model.db_connect(
    966962        SMTPAccount.dropTable(ifExists=True) 
    967963        IMAPAccount.dropTable(ifExists=True) 
     
    971967        Account.dropTable(ifExists=True) 
    972968        del TheURIOpener.cachedURIs['sqlite://' + DB_URL] 
    973         account.hub.threadConnection.close() 
    974         del account.hub.threadConnection 
     969        model.hub.threadConnection.close() 
     970        model.db_disconnect() 
    975971        if os.path.exists(DB_PATH): 
    976972            os.unlink(DB_PATH) 
    977973 
    978974    def test_filter(self): 
    979         account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL
     975        model.db_connect(
    980976        account11 = SMTPAccount(user_jid="user1@test.com", 
    981977                                name="account11", 
     
    992988        self.assertEquals(accounts[0].name, "account12") 
    993989        self.assertEquals(accounts[1].name, "account13") 
    994         del account.hub.threadConnection 
     990        model.db_disconnect() 
    995991 
    996992def suite(): 
     
    998994    suite.addTest(unittest.makeSuite(MailComponent_TestCase, 'test')) 
    999995    suite.addTest(unittest.makeSuite(SendMailMessageHandler_TestCase, 'test')) 
    1000     suite.addTest(unittest.makeSuite(MailAccountManager_TestCase, 'test')) 
    1001996    suite.addTest(unittest.makeSuite(RootSendMailMessageHandler_TestCase, 'test')) 
    1002997    suite.addTest(unittest.makeSuite(MailSender_TestCase, 'test')) 
  • src/jmc/model/tests/account.py

    r93 r100  
    3030from sqlobject.dbconnection import TheURIOpener 
    3131 
     32import jcl.model as model 
    3233from jcl.model import account 
    3334from jcl.model.account import Account, PresenceAccount 
     
    5051            os.unlink(DB_PATH) 
    5152        self.db_url = DB_URL 
    52         account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url) 
    53         Account.createTable(ifNotExists = True) 
    54         PresenceAccount.createTable(ifNotExists = True) 
    55         MailAccount.createTable(ifNotExists = True) 
    56         self.account = MailAccount(user_jid = "user1@test.com", \ 
    57                                    name = "account1", \ 
    58                                    jid = "account1@jmc.test.com") 
    59         del account.hub.threadConnection 
     53        model.db_connection_str = 'sqlite://' + self.db_url 
     54        model.db_connect() 
     55        Account.createTable(ifNotExists=True) 
     56        PresenceAccount.createTable(ifNotExists=True) 
     57        MailAccount.createTable(ifNotExists=True) 
     58        self.account = MailAccount(user_jid="user1@test.com", 
     59                                   name="account1", 
     60                                   jid="account1@jmc.test.com") 
     61        model.db_disconnect() 
    6062        self.account_class = MailAccount 
    6163 
    6264    def tearDown(self): 
    63         account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url
    64         MailAccount.dropTable(ifExists = True) 
    65         PresenceAccount.dropTable(ifExists = True) 
     65        model.db_connect(
     66        MailAccount.dropTable(ifExists=True) 
     67        PresenceAccount.dropTable(ifExists=True) 
    6668        Account.dropTable(ifExists = True) 
    6769        del TheURIOpener.cachedURIs['sqlite://' + self.db_url] 
    68         account.hub.threadConnection.close() 
    69         del account.hub.threadConnection 
     70        model.hub.threadConnection.close() 
     71        model.db_disconnect() 
    7072        if os.path.exists(DB_PATH): 
    7173            os.unlink(DB_PATH) 
     
    7476        def inner(self): 
    7577            encoded, multipart, header = email_type 
    76             email = email_generator.generate(encoded, \ 
    77                                              multipart, \ 
     78            email = email_generator.generate(encoded, 
     79                                             multipart, 
    7880