def test_passing_stdin(self): """ You can pass a stdin object as an option and it should be available on self.stdin. If no such option is passed, it defaults to sys.stdin. """ sentinel = object() command = createsuperuser.Command() command.execute( stdin=sentinel, stdout=six.StringIO(), interactive=False, verbosity=0, username='******', email='*****@*****.**', ) self.assertIs(command.stdin, sentinel) command = createsuperuser.Command() command.execute( stdout=six.StringIO(), interactive=False, verbosity=0, username='******', email='*****@*****.**', ) self.assertIs(command.stdin, sys.stdin)
def test_passing_stdin(self): """ You can pass a stdin object as an option and it should be available on self.stdin. If no such option is passed, it defaults to sys.stdin. """ sentinel = object() command = createsuperuser.Command() call_command( command, stdin=sentinel, interactive=False, verbosity=0, username="******", email="*****@*****.**", ) self.assertIs(command.stdin, sentinel) command = createsuperuser.Command() call_command( command, interactive=False, verbosity=0, username="******", email="*****@*****.**", ) self.assertIs(command.stdin, sys.stdin)
def handle(self, *args, **options): # Create the first user. if not User.objects.filter(is_superuser=True).exists(): print( "Let's create your first Q user. This user will have superuser privileges in the Q administrative interface." ) call_command('createsuperuser') # Create the first organization. if not Organization.objects.exists(): print("Let's create your Q organization.") name = Organization._meta.get_field("name") get_input = createsuperuser.Command().get_input_data name = get_input(name, "Organization Name: ", "Test Organization") org = Organization.create( name=name, subdomain="main", admin_user=User.objects.filter(is_superuser=True).first())
def handle(self, *args, **options): # Sanity check that the database is available and ready --- make sure the system # modules exist (since we need them before creating an Organization). # Also useful in container deployments to make sure container fully deployed. try: if not Module.objects.filter(app__source__is_system_source=True, app__appname="organization", app__system_app=True, module_name="app").exists(): raise OperationalError() # to trigger below except OperationalError: print("The database is not initialized yet.") sys.exit(1) # Create AppSources that we want. if os.path.exists("/mnt/q-files-host"): # For our docker image. AppSource.objects.get_or_create(slug="host", defaults={ "spec": { "type": "local", "path": "/mnt/q-files-host" } }) # Second, for 0.9.x startpack # We can use forward slashes because we are storing the path in the database # and the path will be applied correctly to the operating OS. qfiles_path = 'q-files/vendors/govready/govready-q-files-startpack/q-files' if os.path.exists(qfiles_path): # For 0.9.x+. AppSource.objects.get_or_create( slug="govready-q-files-startpack", defaults={"spec": { "type": "local", "path": qfiles_path }}) # Load the AppSource's assessments (apps) we want # We will do some hard-coding here temporarily created_appsource = AppSource.objects.get( slug="govready-q-files-startpack") for appname in [ "System-Description-Demo", "PTA-Demo", "rules-of-behavior" ]: print("Adding appname '{}' from AppSource '{}' to catalog.". format(appname, created_appsource)) try: appver = created_appsource.add_app_to_catalog(appname) except Exception as e: raise # Finally, for authoring, create an AppSource to the stub file qfiles_path = 'guidedmodules/stubs/q-files' if os.path.exists(qfiles_path): # For 0.9.x+. AppSource.objects.get_or_create( slug="govready-q-files-stubs", defaults={"spec": { "type": "local", "path": qfiles_path }}) print("Adding AppSource for authoring.") # Create GovReady admin users, if specified in local/environment.json if len(settings.GOVREADY_ADMINS): for admin_user in settings.GOVREADY_ADMINS: username = admin_user["username"] if not User.objects.filter(username=username).exists(): user = User.objects.create(username=username, is_superuser=True, is_staff=True) user.set_password(admin_user["password"]) user.email = admin_user["email"] user.save() print( "Created administrator account: username '{}' with email '{}'." .format(user.username, user.email)) else: print( "\n[WARNING] Skipping create admin account '{}' - username already exists.\n" .format(username)) # Create the first user. if not User.objects.filter(is_superuser=True).exists(): if not options['non_interactive']: print( "Let's create your first Q user. This user will have superuser privileges in the Q administrative interface." ) call_command('createsuperuser') else: # Create an "admin" account with a random pwd and # print it on stdout. user = User.objects.create(username="******", is_superuser=True, is_staff=True) password = User.objects.make_random_password(length=24) user.set_password(password) user.save() print( "Created administrator account (username: {}) with password: {}" .format(user.username, password)) # Get the admin user - it was just created and should be the only admin user. user = User.objects.filter(is_superuser=True).get() # Create the first portfolio portfolio = Portfolio.objects.create(title=user.username) portfolio.assign_owner_permissions(user) print("Created administrator portfolio {}".format(portfolio.title)) else: # One or more superusers already exist print( "\n[WARNING] Superuser(s) already exist, not creating default admin superuser. Did you specify 'govready_admins' in 'local/environment.json'? Are you connecting to a persistent database?\n" ) # Create the first organization. if not Organization.objects.filter(slug="main").exists(): if not options['non_interactive']: print("Let's create your organization.") name = Organization._meta.get_field("name") get_input = createsuperuser.Command().get_input_data name = get_input(name, "Organization name: ", "My Organization") else: name = "The Secure Organization" org = Organization.create(name=name, slug="main", admin_user=user) else: org = Organization.objects.get(slug="main") # Add the user to the org's help squad and reviewers lists. try: user except NameError: print("[WARNING] Admin already added to Help Squad and Reviewers") else: if user not in org.help_squad.all(): org.help_squad.add(user) if user not in org.reviewers.all(): org.reviewers.add(user) # Provide feedback to user print("You can now login into GovReady-Q...")
def handle(self, *args, **options): # Sanity check that the database is ready --- make sure the system # modules exist (since we need them before creating an Organization). try: if not Module.objects.filter(app__source__is_system_source=True, app__appname="organization", app__system_app=True, module_name="app").exists(): raise OperationalError() # to trigger below except OperationalError: print("The database is not initialized yet.") sys.exit(1) # Create AppSources that we want. if os.path.exists("/mnt/apps"): # For our docker image. AppSource.objects.get_or_create( slug="host", defaults={"spec": { "type": "local", "path": "/mnt/apps" }}) AppSource.objects.get_or_create( slug="samples", defaults={ "spec": { "type": "git", "url": "https://github.com/GovReady/govready-sample-apps" } }) # Create the first user. if not User.objects.filter(is_superuser=True).exists(): if not options['non_interactive']: print( "Let's create your first Q user. This user will have superuser privileges in the Q administrative interface." ) call_command('createsuperuser') else: # Create an "admin" account with a random password and # print it on stdout. user = User.objects.create(username="******", is_superuser=True, is_staff=True) password = User.objects.make_random_password(length=24) user.set_password(password) user.save() print( "Created administrator account (username: {}) with password: {}" .format(user.username, password)) # Get the admin user - it was just created and should be the only admin user. user = User.objects.filter(is_superuser=True).get() # Create the first organization. if not Organization.objects.filter(subdomain="main").exists(): if not options['non_interactive']: print("Let's create your Q organization.") name = Organization._meta.get_field("name") get_input = createsuperuser.Command().get_input_data name = get_input(name, "Organization Name: ", "Test Organization") else: name = "The Secure Company" org = Organization.create(name=name, subdomain="main", admin_user=user) else: org = Organization.objects.get(subdomain="main") # Add the user to the org's help squad and reviewers lists. if user not in org.help_squad.all(): org.help_squad.add(user) if user not in org.reviewers.all(): org.reviewers.add(user)
def handle(self, *args, **options): # initialize database user = options['user'] pwd = options['password'] email = options['email'] tester = options['tester'] tester_config = options['config_file'] create_tester_config = options['create_tester_config'] # Create tester config will return immediately without init db if create_tester_config: with open(tester_config, 'wt') as f: f.write( os.linesep.join([ '# Tester configurations (tester is set in settings.py).', '# Update me before run auto-unittests.', '# Unittest will select the configured MQ to perform tests on.' ])) for section, confs in ConfKeys.items(): for k, v in confs.items(): f.write('%s= %s' % (v, os.linesep)) return 'Tester configuration file is created at %s' % tester_config # init db & migrate print('Initialize Database ...') call_command(makemigrations.Command(), 'pyqueuer') call_command(migrate.Command()) # create admin print('Creating admin user "%s" ...' % user) try: if pwd: call_command(createsuperuser.Command(), '--noinput', username=user, email=email) u = User.objects.get(username__exact=user) u.set_password(raw_password=pwd) u.save() else: call_command(createsuperuser.Command(), username=user, email=email) except IntegrityError: sys.stderr.write(' Admin with same name is already existed.' + os.linesep) else: print(' Admin user "%s" created. Email is "%s"' % (user, email)) # create tester if tester: name = pwd = settings.TESTER print('Creating test user "%s"...' % name) try: u = User.objects.create(username=name) u.email = email u.set_password(raw_password=pwd) u.is_active = True u.save() except IntegrityError: sys.stderr.write(' Tester is already existed.' + os.linesep) else: print( ' Tester is created. Username and password are both "%s".' % name) # load tester configurations from tester json # Use "manage.py config --import my_config.ini" to import config only. if tester_config: print('Load config for tester.') options = { "import": tester_config, "config_file": tester_config, "user": settings.TESTER, "password": settings.TESTER } call_command('config', **options) # with open(tester_config, 'rt') as f: # tester_dict = json.load(f) # # user = authenticate(username=name, password=pwd) # ucf = UserConf(user=user) # for k, v in tester_dict['test_user_config'].items(): # ucf.set(k, v) # return 'Tester configuration loaded from json %s' % tester_config return