diff --git a/modoboa/core/commands/deploy.py b/modoboa/core/commands/deploy.py index 61ef81c03..28c55936a 100644 --- a/modoboa/core/commands/deploy.py +++ b/modoboa/core/commands/deploy.py @@ -82,6 +82,10 @@ class DeployCommand(Command): '--dont-install-extensions', action='store_true', default=False, help='Do not install extensions using pip' ) + self._parser.add_argument( + '--admin-username', default='admin', + help="Username of the initial super administrator" + ) def _exec_django_command(self, name, cwd, *args): """Run a django command for the freshly created project @@ -249,7 +253,8 @@ class DeployCommand(Command): "migrate", parsed_args.name, '--noinput' ) self._exec_django_command( - "load_initial_data", parsed_args.name + "load_initial_data", parsed_args.name, + "--admin-username", parsed_args.admin_username ) if parsed_args.collectstatic: self._exec_django_command( diff --git a/modoboa/core/management/commands/load_initial_data.py b/modoboa/core/management/commands/load_initial_data.py index 88f47117a..093343324 100644 --- a/modoboa/core/management/commands/load_initial_data.py +++ b/modoboa/core/management/commands/load_initial_data.py @@ -24,10 +24,17 @@ class Command(BaseCommand): help = "Load Modoboa initial data" + def add_arguments(self, parser): + """Add extra arguments to command.""" + parser.add_argument( + "--admin-username", default="admin", + help="Username of the initial super administrator." + ) + def handle(self, *args, **options): """Command entry point.""" if not User.objects.filter(is_superuser=True).count(): - admin = User(username="admin", is_superuser=True) + admin = User(username=options["admin_username"], is_superuser=True) admin.set_password("password") admin.save() ObjectAccess.objects.create( diff --git a/modoboa/core/tests.py b/modoboa/core/tests.py index 58093eb23..273716ed8 100644 --- a/modoboa/core/tests.py +++ b/modoboa/core/tests.py @@ -4,7 +4,7 @@ import httmock from django.core import management from django.core.urlresolvers import reverse -from django.test import override_settings +from django.test import override_settings, TestCase from modoboa.lib import exceptions from modoboa.lib import parameters @@ -44,6 +44,17 @@ class AuthenticationTestCase(ModoTestCase): self.assertTrue(response.url.endswith(reverse("core:dashboard"))) +class ChangeDefaultAdminTestCase(TestCase): + """Try to change the default username.""" + + def test_management_command(self): + """Use dedicated option.""" + management.call_command( + "load_initial_data", "--admin-username", "modoadmin") + self.assertTrue( + self.client.login(username="modoadmin", password="password")) + + class DashboardTestCase(ModoTestCase): """Dashboard tests.""" diff --git a/tests.py b/tests.py index 21fc99b58..69a5e184f 100644 --- a/tests.py +++ b/tests.py @@ -24,7 +24,7 @@ class DeployTest(unittest.TestCase): self.dbhost, self.projname) cmd = ( "modoboa-admin.py deploy --collectstatic " - "--dburl %s --domain %s %s" + "--dburl %s --domain %s --admin-username admin %s" % (dburl, 'localhost', self.projname) ) code, output = exec_cmd(cmd, cwd=self.workdir)