diff --git a/modoboa/core/management/commands/load_initial_data.py b/modoboa/core/management/commands/load_initial_data.py index cfbd367f4..dff31da7c 100644 --- a/modoboa/core/management/commands/load_initial_data.py +++ b/modoboa/core/management/commands/load_initial_data.py @@ -45,8 +45,10 @@ class Command(BaseCommand): user=admin, content_object=admin, is_owner=True) lc = models.LocalConfig.objects.first() - secret_key = lc.parameters.get_value("secret_key") - if not secret_key: + condition = ( + "core" not in lc._parameters or + "secret_key" not in lc._parameters["core"]) + if condition: lc.parameters.set_value("secret_key", random_key()) lc.save() diff --git a/modoboa/core/tests/test_core.py b/modoboa/core/tests/test_core.py index 9bc675df9..534ab30d2 100644 --- a/modoboa/core/tests/test_core.py +++ b/modoboa/core/tests/test_core.py @@ -42,16 +42,23 @@ class AuthenticationTestCase(ModoTestCase): self.assertTrue(response.url.endswith(reverse("core:dashboard"))) -class ChangeDefaultAdminTestCase(TestCase): +class LoadInitialDataTestCase(TestCase): """Try to change the default username.""" - def test_management_command(self): + def test_change_default_admin(self): """Use dedicated option.""" management.call_command( "load_initial_data", "--admin-username", "modoadmin") self.assertTrue( self.client.login(username="modoadmin", password="password")) + def test_set_secret_key(self): + """Use dedicated option.""" + management.call_command("load_initial_data") + lc = models.LocalConfig.objects.first() + self.assertIn("core", lc._parameters) + self.assertIn("secret_key", lc._parameters["core"]) + class ProfileTestCase(ModoTestCase): """Profile related tests."""