def test_init_tester(self): pwd = '234567' t = settings.TESTER email = 'anonymous@localhost' config_file = InitCommand.default_tester_config call_command('init', password=pwd, tester=True) tester = authenticate(username=t, password=t) self.assertIsNotNone(tester) self.assertTrue(tester.is_active) self.assertFalse(tester.is_superuser) self.assertFalse(tester.is_staff) self.assertEqual(tester.email, email) dummy_section = '_' ucf = UserConf(tester) cf = configparser.ConfigParser() with open(config_file, 'r') as f: config_string = '[%s]\n' % dummy_section + f.read() cf.read_string(config_string) for opt in ucf.all(): v = cf.get(dummy_section, opt.name) self.assertEqual(v, opt.value)
def handle(self, *args, **options): name = options['user'] pwd = options['password'] show_list = options['list'] conf = options['conf'] key = options['get'] imp = options['import'] expt = options['export'] config_file = options['config_file'] if not config_file: config_file = '%s_config.ini' % name if not name: name = input(' Please enter your user name:') if not pwd: pwd = getpass.getpass( ' Please enter the password for user "%s" :' % name) user = authenticate(username=name, password=pwd) if user and user.is_active: pass else: # sys.stderr.write('You are not authorized with given user name and password.\r\n') return ' ou are not authorized with given user name and password.' ucf = UserConf(user=user) sb = [] if show_list: # show config list if conf: print(' Ignore all options due to "--list" specified.') for opt in ucf.all(): sb.append('%s=%s' % (opt.name, opt.value)) return os.linesep.join(sb) elif conf: # set configurations return self.set_configs(user=user, tokens=conf) elif key: # get value of an config by given name return ucf.get(key) elif imp: # import from file. try: with open(config_file, 'rt') as f: return self.set_configs(user=user, tokens=f.readlines()) except (FileNotFoundError, PermissionError, FileExistsError): return ' File is not accessible.' elif expt: # export to file try: with open(config_file, 'wt') as f: for opt in ucf.all(): f.write('%s=%s' % (opt.name, opt.value)) f.write(os.linesep) except (FileNotFoundError, PermissionError, FileExistsError): return ' File is not accessible.' else: # sys.stderr.write('Please specify arguments such as --list or --get.\r\n') return ' Please specify arguments such as --list, --get and so on.' return ''