From 8b2768bc1abba6c25b2d8b80bdfe692240b4cd85 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Sun, 5 Apr 2015 14:47:52 +0200 Subject: [PATCH] Fix unit tests. --- modoboa/core/tests.py | 68 +-------------------------------------- modoboa/lib/test_utils.py | 45 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 67 deletions(-) diff --git a/modoboa/core/tests.py b/modoboa/core/tests.py index 13e983c17..ee070a693 100644 --- a/modoboa/core/tests.py +++ b/modoboa/core/tests.py @@ -1,16 +1,12 @@ -import os -import tempfile +"""Tests for core application.""" 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 class ProfileTestCase(ModoTestCase): - fixtures = ['initial_users.json'] def setUp(self): super(ProfileTestCase, self).setUp() @@ -46,65 +42,3 @@ class ProfileTestCase(ModoTestCase): self.assertTrue( self.clt.login(username="user@test.com", password="tutu") ) - - -class ModoboaAdminCommandTestCase(TestCase): - - MAP_FILES = { - "modoboa_admin": [ - "sql-domains.cf", "sql-domain-aliases.cf", "sql-aliases.cf", - "sql-domain-aliases-mailboxes.cf", "sql-catchall-aliases.cf", - "sql-maintain.cf" - ], - "modoboa_postfix_autoreply": [ - "sql-autoreplies-transport.cf", "sql-autoreplies.cf" - ], - "modoboa_admin_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 {0}".format(self.workdir)) - - def _test_maps_generation(self, engine, extensions=None): - if extensions is None: - extensions = [ - "modoboa_admin", "modoboa_postfix_autoreply", - "modoboa_admin_relaydomains" - ] - dburl = "{0}://user:password@localhost/testdb".format(engine) - code, output = exec_cmd( - "modoboa-admin.py postfix_maps --extensions {0}" - " --dburl {1} {2}".format( - " ".join(extensions), dburl, self.workdir - ) - ) - self.assertEqual(code, 0) - for extension in extensions: - for mapfile in self.MAP_FILES[extension]: - path = "{0}/{1}".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") diff --git a/modoboa/lib/test_utils.py b/modoboa/lib/test_utils.py index 98888069e..e731d2522 100644 --- a/modoboa/lib/test_utils.py +++ b/modoboa/lib/test_utils.py @@ -3,6 +3,8 @@ import os import tempfile +from django.test import TestCase + from .sysutils import exec_cmd @@ -41,3 +43,46 @@ class TestRunnerMixin(object): self.assertEqual(code, 0) +class MapFilesTestCase(TestCase): + + """A generic test case to check map files generation.""" + + MAP_FILES = None + + def setUp(self): + self.workdir = tempfile.mkdtemp() + + def tearDown(self): + exec_cmd("rm -rf {0}".format(self.workdir)) + + def _test_maps_generation(self, engine): + dburl = "{0}://user:password@localhost/testdb".format(engine) + code, output = exec_cmd( + "modoboa-admin.py postfix_maps --extensions {0}" + " --dburl {1} {2}".format( + self.extension, dburl, self.workdir + ) + ) + self.assertEqual(code, 0) + + for mapfile in self.MAP_FILES: + path = "{0}/{1}".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")