← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/maas-admin-password into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/maas-admin-password into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~rvb/maas/maas-admin-password/+merge/96547

Create a new createadmin command (createadmin --username username --password).  It's different from createsuperuser: it's silent (and thus --username and --password are required options) and it does not require an email to be set.  The packaging guys need that command.
-- 
https://code.launchpad.net/~rvb/maas/maas-admin-password/+merge/96547
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/maas-admin-password into lp:maas.
=== added file 'src/maasserver/management/commands/createadmin.py'
--- src/maasserver/management/commands/createadmin.py	1970-01-01 00:00:00 +0000
+++ src/maasserver/management/commands/createadmin.py	2012-03-08 11:36:21 +0000
@@ -0,0 +1,39 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""..."""
+
+from __future__ import (
+    print_function,
+    unicode_literals,
+    )
+
+__metaclass__ = type
+__all__ = []
+
+
+from django.contrib.auth.models import User
+from optparse import make_option
+from django.core.management.base import BaseCommand, CommandError
+from django.db import DEFAULT_DB_ALIAS
+
+
+class Command(BaseCommand):
+    option_list = BaseCommand.option_list + (
+        make_option('--username', dest='username', default=None,
+            help="Specifies the username for the admin."),
+        make_option('--password', dest='password', default=None,
+            help="Specifies the password for the admin."),
+     )
+    help = "Used to create an admin with an empty email."
+
+    def handle(self, *args, **options):
+        username = options.get('username', None)
+        if username is None:
+            raise CommandError("You must provide a username with --username.")
+        password = options.get('password', None)
+        if password is None:
+            raise CommandError("You must provide a password with --password.")
+
+        User.objects.db_manager(DEFAULT_DB_ALIAS).create_superuser(
+            username, email='', password=password)

=== modified file 'src/maasserver/tests/test_commands.py'
--- src/maasserver/tests/test_commands.py	2012-03-06 10:24:45 +0000
+++ src/maasserver/tests/test_commands.py	2012-03-08 11:36:21 +0000
@@ -12,10 +12,13 @@
 __all__ = []
 
 import os
+from StringIO import StringIO
 
 from django.conf import settings
+from django.contrib.auth.models import User
 from django.core.management import call_command
 from maasserver.models import FileStorage
+from maasserver.testing.factory import factory
 from maastesting import TestCase
 
 
@@ -33,3 +36,42 @@
         call_command('gc')
         # The test is that we get here without errors.
         pass
+
+    def test_createadmin_requires_username(self):
+        stderr = StringIO()
+        self.assertRaises(
+            SystemExit, call_command, 'createadmin', stderr=stderr)
+        command_output = stderr.getvalue().strip()
+
+        self.assertIn(
+            "Error: You must provide a username with --username.",
+             command_output)
+
+    def test_createadmin_requires_password(self):
+        username = factory.getRandomString()
+        stderr = StringIO()
+        self.assertRaises(
+            SystemExit, call_command, 'createadmin', username=username,
+            stderr=stderr)
+        command_output = stderr.getvalue().strip()
+
+        self.assertIn(
+            "Error: You must provide a password with --password.",
+             command_output)
+
+    def test_createadmin_creates_admin(self):
+        stderr = StringIO()
+        stdout = StringIO()
+        username = factory.getRandomString()
+        password = factory.getRandomString()
+        call_command(
+            'createadmin', username=username, password=password,
+            stderr=stderr, stdout=stdout)
+        users = list(User.objects.filter(username=username))
+
+        self.assertEquals('', stderr.getvalue().strip())
+        self.assertEquals('', stdout.getvalue().strip())
+        self.assertEqual(1, len(users))  # One user with that name.
+        self.assertTrue(users[0].check_password(password))
+        self.assertTrue(users[0].is_superuser)
+        self.assertEqual('', users[0].email)  # His email is empty.


Follow ups