Example #1
0
File: runner.py Project: dax/jmc
 def setup_db(self):
     JCLRunner.setup_db(self)
     MailAccount.createTable(ifNotExists=True)
     IMAPAccount.createTable(ifNotExists=True)
     POP3Account.createTable(ifNotExists=True)
     AbstractSMTPAccount.createTable(ifNotExists=True)
     GlobalSMTPAccount.createTable(ifNotExists=True)
     SMTPAccount.createTable(ifNotExists=True)
Example #2
0
File: account.py Project: dax/jmc
 def setUp(self):
     JCLTestCase.setUp(self, tables=[Account, PresenceAccount, User, MailAccount, IMAPAccount])
     self.imap_account = IMAPAccount(
         user=User(jid="*****@*****.**"), name="account1", jid="*****@*****.**", login="******"
     )
     self.imap_account.password = "******"
     self.imap_account.host = "localhost"
     self.imap_account.port = 1143
     self.imap_account.ssl = False
     self.account_class = IMAPAccount
Example #3
0
File: runner.py Project: dax/jmc
 def test__run(self):
     self.runner.pid_file = "/tmp/jmc.pid"
     self.runner.db_url = self.db_url
     def do_nothing():
         return (False, 0)
     self.runner._run(do_nothing)
     model.db_connection_str = self.runner.db_url
     model.db_connect()
     # dropTable should succeed because tables should exist
     Account.dropTable()
     PresenceAccount.dropTable()
     User.dropTable()
     LegacyJID.dropTable()
     MailAccount.dropTable()
     IMAPAccount.dropTable()
     POP3Account.dropTable()
     SMTPAccount.dropTable()
     model.db_disconnect()
     self.assertFalse(os.access("/tmp/jmc.pid", os.F_OK))
Example #4
0
File: account.py Project: dax/jmc
class IMAPAccount_TestCase(InheritableAccount_TestCase):
    def setUp(self):
        JCLTestCase.setUp(self, tables=[Account, PresenceAccount, User, MailAccount, IMAPAccount])
        self.imap_account = IMAPAccount(
            user=User(jid="*****@*****.**"), name="account1", jid="*****@*****.**", login="******"
        )
        self.imap_account.password = "******"
        self.imap_account.host = "localhost"
        self.imap_account.port = 1143
        self.imap_account.ssl = False
        self.account_class = IMAPAccount

    def make_test(self, responses=None, queries=None, core=None):
        def inner():
            self.server = server.DummyServer("localhost", 1143)
            thread.start_new_thread(self.server.serve, ())
            self.server.responses = [
                "* OK [CAPABILITY IMAP4 LOGIN-REFERRALS " + "AUTH=PLAIN]\r\n",
                lambda data: "* CAPABILITY IMAP4 "
                + "LOGIN-REFERRALS AUTH=PLAIN\r\n"
                + data.split()[0]
                + " OK CAPABILITY completed\r\n",
                lambda data: data.split()[0] + " OK LOGIN completed\r\n",
            ]
            if responses:
                self.server.responses += responses
            self.server.queries = ["^[^ ]* CAPABILITY", '^[^ ]* LOGIN login "pass"']
            if queries:
                self.server.queries += queries
            self.server.queries += ["^[^ ]* LOGOUT"]
            if not self.imap_account.connected:
                self.imap_account.connect()
            self.failUnless(self.imap_account.connection, "Cannot establish connection")
            if core:
                model.db_connect()
                core(self)
                model.db_disconnect()
            if self.imap_account.connected:
                self.imap_account.disconnect()
            self.failUnless(self.server.verify_queries())

        return inner

    def test_connection(self):
        test_func = self.make_test()
        test_func()

    def test_get_mail_list_summary(self):
        test_func = self.make_test(
            [
                lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK"
                + " [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*"
                + " OK [PERMANENTFLAGS (\Deleted \Seen\*)\r\n"
                + data.split()[0]
                + " OK [READ-WRITE] SELECT completed\r\n",
                lambda data: "* 1 FETCH ((RFC822.header) {38}\r\n"
                + "Subject: mail subject 1\r\n\r\nbody text\r\n)\r\n"
                + "* 2 FETCH ((RFC822.header) {38}\r\n"
                + "Subject: mail subject 2\r\n\r\nbody text\r\n)\r\n"
                + data.split()[0]
                + " OK FETCH completed\r\n",
            ],
            ["^[^ ]* EXAMINE INBOX", "^[^ ]* FETCH 1:20 RFC822.header"],
            lambda self: self.assertEquals(
                self.imap_account.get_mail_list_summary(), [("1", "mail subject 1"), ("2", "mail subject 2")]
            ),
        )
        test_func()

    def test_get_mail_list_summary_inbox_does_not_exist(self):
        self.__test_select_inbox_does_not_exist(lambda: self.imap_account.get_mail_list_summary(), readonly=True)

    def test_get_mail_list_summary_start_index(self):
        test_func = self.make_test(
            [
                lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK"
                + " [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*"
                + " OK [PERMANENTFLAGS (\Deleted \Seen\*)\r\n"
                + data.split()[0]
                + " OK [READ-WRITE] SELECT completed\r\n",
                lambda data: "* 2 FETCH ((RFC822.header) {38}\r\n"
                + "Subject: mail subject 2\r\n\r\nbody text\r\n)\r\n"
                + "* 3 FETCH ((RFC822.header) {38}\r\n"
                + "Subject: mail subject 3\r\n\r\nbody text\r\n)\r\n"
                + data.split()[0]
                + " OK FETCH completed\r\n",
            ],
            ["^[^ ]* EXAMINE INBOX", "^[^ ]* FETCH 2:20 RFC822.header"],
            lambda self: self.assertEquals(
                self.imap_account.get_mail_list_summary(start_index=2),
                [("2", "mail subject 2"), ("3", "mail subject 3")],
            ),
        )
        test_func()

    def test_get_mail_list_summary_end_index(self):
        test_func = self.make_test(
            [
                lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK"
                + " [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*"
                + " OK [PERMANENTFLAGS (\Deleted \Seen\*)\r\n"
                + data.split()[0]
                + " OK [READ-WRITE] SELECT completed\r\n",
                lambda data: "* 1 FETCH ((RFC822.header) {38}\r\n"
                + "Subject: mail subject 1\r\n\r\nbody text\r\n)\r\n"
                + "* 2 FETCH ((RFC822.header) {38}\r\n"
                + "Subject: mail subject 2\r\n\r\nbody text\r\n)\r\n"
                + data.split()[0]
                + " OK FETCH completed\r\n",
            ],
            ["^[^ ]* EXAMINE INBOX", "^[^ ]* FETCH 1:2 RFC822.header"],
            lambda self: self.assertEquals(
                self.imap_account.get_mail_list_summary(end_index=2), [("1", "mail subject 1"), ("2", "mail subject 2")]
            ),
        )
        test_func()

    def test_get_new_mail_list(self):
        test_func = self.make_test(
            [
                lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK"
                + " [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*"
                + " OK [PERMANENTFLAGS (\Deleted \Seen\*)\r\n"
                + data.split()[0]
                + " OK [READ-WRITE] SELECT completed\r\n",
                lambda data: "* SEARCH 9 10\r\n" + data.split()[0] + " OK SEARCH completed\r\n",
            ],
            ["^[^ ]* SELECT INBOX", "^[^ ]* SEARCH RECENT"],
            lambda self: self.assertEquals(self.imap_account.get_new_mail_list(), ["9", "10"]),
        )
        test_func()

    def __test_select_inbox_does_not_exist(
        self, tested_func, exception_message="Mailbox does not exist", readonly=False
    ):
        def check_func(self):
            try:
                tested_func()
            except Exception, e:
                self.assertEquals(str(e), exception_message)
                return
            self.fail("No exception raised when selecting non existing mailbox")

        test_func = self.make_test(
            [
                lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK"
                + " [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*"
                + " OK [PERMANENTFLAGS (\Deleted \Seen\*)\r\n"
                + data.split()[0]
                + " NO Mailbox does not exist\r\n"
            ],
            ["^[^ ]* " + (readonly and "EXAMINE" or "SELECT") + " INBOX"],
            check_func,
        )
        test_func()