From f83df1c7070fac030bedafdb304ca62b510aeb0f Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Fri, 4 Jul 2014 10:56:39 +0200 Subject: [PATCH] postfix_maps command silent mode. fix #536 --- modoboa/core/commands/postfix_maps.py | 57 +++++++++++++++++++------ modoboa/core/tests.py | 61 +++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 13 deletions(-) diff --git a/modoboa/core/commands/postfix_maps.py b/modoboa/core/commands/postfix_maps.py index 5ac75ce8a..5ba7c23f3 100644 --- a/modoboa/core/commands/postfix_maps.py +++ b/modoboa/core/commands/postfix_maps.py @@ -10,19 +10,29 @@ from . import Command class MapFilesGenerator(object): - def __init__(self): + def __init__(self, dbinfo=None): + """Constructor. + """ self.template = Template("""user = {{ dbuser }} password = {{ dbpass }} dbname = {{ dbname }} hosts = {{ dbhost }} query = {{ query|safe }} """) - self.tpl_context = { - 'dbhost': raw_input('Database host: '), - 'dbname': raw_input('Database name: '), - 'dbuser': raw_input('Username: '), - 'dbpass': getpass.getpass('Password: ') - } + if dbinfo is None: + self.tpl_context = { + 'dbhost': raw_input('Database host: '), + 'dbname': raw_input('Database name: '), + 'dbuser': raw_input('Username: '), + 'dbpass': getpass.getpass('Password: ') + } + else: + self.tpl_context = { + 'dbhost': dbinfo['HOST'], + 'dbname': dbinfo['NAME'], + 'dbuser': dbinfo['USER'], + 'dbpass': dbinfo['PASSWORD'] + } if not self.tpl_context['dbhost']: self.tpl_context['dbhost'] = '127.0.0.1' @@ -77,13 +87,18 @@ query = {{ query|safe }} class SQLiteMapFilesGenerator(MapFilesGenerator): - def __init__(self): + def __init__(self, dbinfo=None): self.template = Template("""dbpath = {{ dbpath }} query = {{ query|safe }} """) - self.tpl_context = { - 'dbpath': raw_input('Database path: ') - } + if dbinfo is None: + self.tpl_context = { + 'dbpath': raw_input('Database path: ') + } + else: + self.tpl_context = { + 'dbpath': dbinfo['NAME'] + } class MapFile(object): @@ -194,14 +209,30 @@ class PostfixMapsCommand(Command): default='mysql', help='Used database type' ) + self._parser.add_argument( + '--dburl', type=str, nargs=1, default=None, + help="url of Modoboa's database" + ) self._parser.add_argument( '--categories', type=str, nargs='*', default=['std'], help='Map file categories to generate (choices: std, autoreply)' ) def handle(self, parsed_args): + """Command entry point. + """ + dbinfo = None + if parsed_args.dburl: + import dj_database_url + dbinfo = dj_database_url.config(default=parsed_args.dburl[0]) + if 'sqlite' in dbinfo['ENGINE']: + parsed_args.dbtype = 'sqlite' + elif 'psycopg2' in dbinfo['ENGINE']: + parsed_args.dbtype = 'postgres' + else: + parsed_args.dbtype = 'mysql' if parsed_args.dbtype == 'sqlite': - g = SQLiteMapFilesGenerator() + g = SQLiteMapFilesGenerator(dbinfo) else: - g = MapFilesGenerator() + g = MapFilesGenerator(dbinfo) g.render(parsed_args) diff --git a/modoboa/core/tests.py b/modoboa/core/tests.py index 80de9db01..f21c4b34c 100644 --- a/modoboa/core/tests.py +++ b/modoboa/core/tests.py @@ -1,4 +1,10 @@ +import os +import tempfile + from django.core.urlresolvers import reverse +from django.test import TestCase + +from modoboa.lib.sysutils import exec_cmd from modoboa.lib.tests import ModoTestCase from . import factories @@ -40,3 +46,58 @@ class ProfileTestCase(ModoTestCase): self.assertTrue( self.clt.login(username="user@test.com", password="tutu") ) + + +class ModoboaAdminCommandTestCase(TestCase): + MAP_FILES = { + "std": [ + "sql-domains.cf", "sql-domain-aliases.cf", "sql-aliases.cf", + "sql-domain-aliases-mailboxes.cf", "sql-catchall-aliases.cf", + "sql-maintain.cf" + ], + "autoreply": ["sql-autoreplies-transport.cf", "sql-autoreplies.cf"], + "relaydomains": [ + "sql-relaydomains.cf", "sql-relaydomains-transport.cf", + "sql-relaydomain-aliases-transport.cf", + "sql-relay-recipient-verification.cf" + ] + } + + def setUp(self): + self.workdir = tempfile.mkdtemp() + + def tearDown(self): + exec_cmd("rm -rf {}".format(self.workdir)) + + def _test_maps_generation(self, engine, categories=None): + if categories is None: + categories = ["std", "autoreply", "relaydomains"] + dburl = "{}://user:password@localhost/testdb".format(engine) + code, output = exec_cmd( + "modoboa-admin.py postfix_maps --categories {} --dburl {} {}".format( + " ".join(categories), dburl, self.workdir + ) + ) + self.assertEqual(code, 0) + for category in categories: + for mapfile in self.MAP_FILES[category]: + path = "{}/{}".format(self.workdir, mapfile) + self.assertTrue(os.path.exists(path)) + with open(path) as fpo: + content = fpo.read() + if engine != "sqlite": + self.assertIn("user = user", content) + self.assertIn("password = password", content) + self.assertIn("dbname = testdb", content) + self.assertIn("hosts = localhost", content) + else: + self.assertIn("dbpath = testdb", content) + + def test_postgres_maps(self): + self._test_maps_generation("postgres") + + def test_mysql_maps(self): + self._test_maps_generation("mysql") + + def test_sqlite_maps(self): + self._test_maps_generation("sqlite")