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)
def __init__(self, component_name, component_version): JCLRunner.__init__(self, component_name, component_version) self.component_short_name = "JMC" # define new options self.mail_default_encoding = "iso-8859-1" self.smtp_default_login = None self.smtp_default_password = None self.smtp_default_host = None self.smtp_default_port = 0 self.smtp_default_tls = False self.smtp_default_ssl = False self.smtp_default_label = None self.enable_smtp_default_account = False self.options += [("e:", "mail-default-encoding=", "jmc", " ENCODING\t\tDefault encoding of the component", lambda arg: self.set_attr("mail_default_encoding", arg)), ("g:", "smtp-default-login="******"smtp", " LOGIN\t\t\tDefault SMTP login", lambda arg: self.set_attr("smtp_default_login", arg)), ("a:", "smtp-default-password="******"smtp", " PASSWORD\t\tDefault SMTP password", lambda arg: self.set_attr("smtp_default_password", arg)), ("t:", "smtp-default-host=", "smtp", " HOST\t\t\tDefault SMTP host", lambda arg: self.set_attr("smtp_default_host", arg)), ("r:", "smtp-default-port=", "smtp", " PORT\t\t\tDefault SMTP port", lambda arg: self.set_attr("smtp_default_port", int(arg))), ("m:", "smtp-default-tls=", "smtp", " True/False\t\tDefault SMTP TLS connexion", lambda arg: self.set_attr("smtp_default_tls", arg.lower() == "true" \ or arg == "1")), ("a:", "smtp-default-ssl=", "smtp", " True/False\t\tDefault SMTP SSL connexion", lambda arg: self.set_attr("smtp_default_ssl", arg.lower() == "true" \ or arg == "1")), ("n:", "enable-smtp-default-account=", "smtp", " True/False\t\tEnable default SMTP connexion", lambda arg: self.set_attr("enable_smtp_default_account", arg.lower() == "true" \ or arg == "1")), ("b:", "smtp-default-label=", "smtp", "\t\t\tDefault SMTP account label", lambda arg: self.set_attr("smtp_default_label", arg))] # override JCL default self.service_jid = "jmc.localhost" self.db_url = "sqlite:///var/spool/jabber/jmc.db" self.pid_file = "/var/run/jabber/jmc.pid" self.config_file = "jmc.conf" # set socket connection timeout (for IMAP and POP connections) socket.setdefaulttimeout(10)
def main(): import sys reload(sys) sys.setdefaultencoding('utf-8') del sys.setdefaultencoding import jcl from jcl.runner import JCLRunner runner = JCLRunner("JCL", jcl.version) runner.configure() runner.run()
def setUp(self): self.runner = JCLRunner("JCL", jcl.version)
class JCLRunner_TestCase(unittest.TestCase): def setUp(self): self.runner = JCLRunner("JCL", jcl.version) def tearDown(self): self.runner = None sys.argv = [""] def test_configure_default(self): self.runner.configure() self.assertEquals(self.runner.config_file, "jcl.conf") self.assertEquals(self.runner.server, "localhost") self.assertEquals(self.runner.port, 5347) self.assertEquals(self.runner.secret, "secret") self.assertEquals(self.runner.service_jid, "jcl.localhost") self.assertEquals(self.runner.language, "en") self.assertEquals(self.runner.db_url, "sqlite:///var/spool/jabber/jcl.db") self.assertEquals(self.runner.pid_file, "/var/run/jabber/jcl.pid") self.assertFalse(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.CRITICAL) def test_configure_configfile(self): self.runner.config_file = "src/jcl/tests/jcl.conf" self.runner.configure() self.assertEquals(self.runner.server, "test_localhost") self.assertEquals(self.runner.port, 42) self.assertEquals(self.runner.secret, "test_secret") self.assertEquals(self.runner.service_jid, "test_jcl.localhost") self.assertEquals(self.runner.language, "test_en") self.assertEquals(self.runner.db_url, "test_sqlite://root@localhost/var/spool/jabber/test_jcl.db") self.assertEquals(self.runner.pid_file, "/var/run/jabber/test_jcl.pid") self.assertFalse(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.CRITICAL) def test_configure_uncomplete_configfile(self): self.runner.config_file = "src/jcl/tests/uncomplete_jcl.conf" self.runner.configure() self.assertEquals(self.runner.server, "test_localhost") self.assertEquals(self.runner.port, 42) self.assertEquals(self.runner.secret, "test_secret") self.assertEquals(self.runner.service_jid, "test_jcl.localhost") self.assertEquals(self.runner.language, "test_en") self.assertEquals(self.runner.db_url, "test_sqlite://root@localhost/var/spool/jabber/test_jcl.db") # pid_file is not in uncmplete_jcl.conf, must be default value self.assertEquals(self.runner.pid_file, "/var/run/jabber/jcl.pid") self.assertFalse(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.CRITICAL) def test_configure_commandline_shortopt(self): sys.argv = ["", "-c", "src/jcl/test/jcl.conf", "-S", "test2_localhost", "-P", "43", "-s", "test2_secret", "-j", "test2_jcl.localhost", "-l", "test2_en", "-u", "sqlite:///tmp/test_jcl.db", "-p", "/tmp/test_jcl.pid"] self.runner.configure() self.assertEquals(self.runner.server, "test2_localhost") self.assertEquals(self.runner.port, 43) self.assertEquals(self.runner.secret, "test2_secret") self.assertEquals(self.runner.service_jid, "test2_jcl.localhost") self.assertEquals(self.runner.language, "test2_en") self.assertEquals(self.runner.db_url, "sqlite:///tmp/test_jcl.db") self.assertEquals(self.runner.pid_file, "/tmp/test_jcl.pid") self.assertFalse(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.CRITICAL) def test_configure_commandline_longopt(self): sys.argv = ["", "--config-file", "src/jcl/tests/jcl.conf", "--server", "test2_localhost", "--port", "43", "--secret", "test2_secret", "--service-jid", "test2_jcl.localhost", "--language", "test2_en", "--db-url", "sqlite:///tmp/test_jcl.db", "--pid-file", "/tmp/test_jcl.pid"] self.runner.configure() self.assertEquals(self.runner.server, "test2_localhost") self.assertEquals(self.runner.port, 43) self.assertEquals(self.runner.secret, "test2_secret") self.assertEquals(self.runner.service_jid, "test2_jcl.localhost") self.assertEquals(self.runner.language, "test2_en") self.assertEquals(self.runner.db_url, "sqlite:///tmp/test_jcl.db") self.assertEquals(self.runner.pid_file, "/tmp/test_jcl.pid") self.assertFalse(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.CRITICAL) def test_configure_commandline_shortopts_configfile(self): sys.argv = ["", "-c", "src/jcl/tests/jcl.conf"] self.runner.configure() self.assertEquals(self.runner.server, "test_localhost") self.assertEquals(self.runner.port, 42) self.assertEquals(self.runner.secret, "test_secret") self.assertEquals(self.runner.service_jid, "test_jcl.localhost") self.assertEquals(self.runner.language, "test_en") self.assertEquals(self.runner.db_url, "test_sqlite://root@localhost/var/spool/jabber/test_jcl.db") self.assertEquals(self.runner.pid_file, "/var/run/jabber/test_jcl.pid") self.assertFalse(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.CRITICAL) def test_configure_commandline_longopts_configfile(self): sys.argv = ["", "--config-file", "src/jcl/tests/jcl.conf"] self.runner.configure() self.assertEquals(self.runner.server, "test_localhost") self.assertEquals(self.runner.port, 42) self.assertEquals(self.runner.secret, "test_secret") self.assertEquals(self.runner.service_jid, "test_jcl.localhost") self.assertEquals(self.runner.language, "test_en") self.assertEquals(self.runner.db_url, "test_sqlite://root@localhost/var/spool/jabber/test_jcl.db") self.assertEquals(self.runner.pid_file, "/var/run/jabber/test_jcl.pid") self.assertFalse(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.CRITICAL) def test_configure_commandline_short_debug(self): sys.argv = ["", "-d"] old_debug_func = self.runner.logger.debug old_info_func = self.runner.logger.debug try: self.runner.logger.debug = lambda msg: None self.runner.logger.info = lambda msg: None self.runner.configure() self.assertTrue(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.DEBUG) finally: self.runner.logger.debug = old_debug_func self.runner.logger.info = old_info_func def test_configure_commandline_long_debug(self): sys.argv = ["", "--debug"] old_debug_func = self.runner.logger.debug old_info_func = self.runner.logger.debug try: self.runner.logger.debug = lambda msg: None self.runner.logger.info = lambda msg: None self.runner.configure() self.assertTrue(self.runner.debug) self.assertEquals(self.runner.logger.getEffectiveLevel(), logging.DEBUG) finally: self.runner.logger.debug = old_debug_func self.runner.logger.info = old_info_func def test_setup_pidfile(self): try: self.runner.pid_file = "/tmp/jcl.pid" self.runner.setup_pidfile() pidfile = open("/tmp/jcl.pid", "r") pid = int(pidfile.read()) pidfile.close() self.assertEquals(pid, os.getpid()) finally: os.remove("/tmp/jcl.pid") def test_run(self): """Test if run method of JCLComponent is executed""" self.has_run_func = False def run_func(component_self): self.has_run_func = True return (False, 0) self.runner.pid_file = "/tmp/jcl.pid" db_path = tempfile.mktemp("db", "jcltest", DB_DIR) db_url = "sqlite://" + db_path self.runner.db_url = db_url self.runner.config = None old_run_func = JCLComponent.run JCLComponent.run = run_func try: self.runner.run() finally: JCLComponent.run = old_run_func self.assertTrue(self.has_run_func) Account.dropTable() PresenceAccount.dropTable() User.dropTable() LegacyJID.dropTable() model.db_disconnect() os.unlink(db_path) self.assertFalse(os.access("/tmp/jcl.pid", os.F_OK)) def test__run(self): self.runner.pid_file = "/tmp/jcl.pid" db_path = tempfile.mktemp("db", "jcltest", DB_DIR) db_url = "sqlite://" + db_path self.runner.db_url = db_url def do_nothing(): return (False, 0) self.runner._run(do_nothing) model.db_connect() # dropTable should succeed because tables should exist Account.dropTable() PresenceAccount.dropTable() User.dropTable() LegacyJID.dropTable() model.db_disconnect() os.unlink(db_path) self.assertFalse(os.access("/tmp/jcl.pid", os.F_OK)) def test__run_restart(self): self.runner.pid_file = "/tmp/jcl.pid" db_path = tempfile.mktemp("db", "jcltest", DB_DIR) db_url = "sqlite://" + db_path self.runner.db_url = db_url self.i = 0 def restart(self): self.i += 1 yield (True, 0) self.i += 1 yield (False, 0) self.i += 1 restart_generator = restart(self) self.runner._run(lambda : restart_generator.next()) model.db_connect() # dropTable should succeed because tables should exist Account.dropTable() PresenceAccount.dropTable() User.dropTable() LegacyJID.dropTable() model.db_disconnect() os.unlink(db_path) self.assertFalse(os.access("/tmp/jcl.pid", os.F_OK)) self.assertEquals(self.i, 2) def test__get_help(self): self.assertNotEquals(self.runner._get_help(), None)