def test_randoms(): assert len(randchars()) == 12 assert len(randchars(4)) == 4 assert randchars() != randchars() assert len(randnumerics()) == 12 assert len(randnumerics(4)) == 4 assert randnumerics() != randnumerics() assert len(randhash()) == 32 assert randhash() != randhash()
def testing_create(cls, **kwargs): # automatically sets most field types. Any fields passed into the method # will override the automatic behavior # subclasses need to set any necessary key values before calling this method # including primary and foreign keys insp = sainsp(cls) for column in insp.columns: # skip fields already in kwargs, foreign key references, and any # field having a default or server_default configured if (column.key in kwargs or column.foreign_keys or column.server_default or column.default or column.primary_key): continue # If the column is being used for polymorphic inheritance identification, then don't # set the value. if insp.mapper.polymorphic_on is column: continue if isinstance(column.type, sa.types.Enum): kwargs[column.key] = random.choice(column.type.enums) elif isinstance(column.type, sa.types.String): kwargs[column.key] = randchars(min(column.type.length or 25, 25)) elif isinstance(column.type, (sa.types.Integer, sa.types.Numeric)): kwargs[column.key] = 0 elif isinstance(column.type, sa.types.Date): kwargs[column.key] = dt.date.today() elif isinstance(column.type, sa.types.DateTime): kwargs[column.key] = dt.datetime.now() return cls.add(**kwargs)
def testing_create(cls, loginid=None, approved_perms=[], denied_perms=[], reset_required=False, groups=[]): # use the hierarchy to find the Permission in case the app has changed # it from compstack.auth.model.orm import Permission login_id = loginid or randchars() email_address = '*****@*****.**' % login_id password = randchars(15) appr_perm_ids = [] denied_perm_ids = [] # create the permissions for perm in tolist(approved_perms): p = Permission.get_by(name=perm) if p is None: raise ValueError('permission %s does not exist' % perm) appr_perm_ids.append(p.id) for perm in tolist(denied_perms): p = Permission.get_by(name=perm) if p is None: raise ValueError('permission %s does not exist' % perm) denied_perm_ids.append(p.id) u = cls.add( login_id=login_id, email_address=email_address, password=password, reset_required=reset_required, # don't let the update method set reset_required pass_reset_ok=False, approved_permissions=appr_perm_ids, denied_permissions=denied_perm_ids, # not quite sure why these are needed, they should default, but I # ran into an issue when testing that would throw SAValidation # errors up when I leave them out. inactive_flag=False, super_user=False, ) u.groups.extend(tolist(groups)) db.sess.commit() u.text_password = password return u
def reset_password(cls, email_address): u = cls.get_by_email(email_address) if not u or u.inactive: return False u.pass_reset_key = randchars(12) u.pass_reset_ts = datetime.utcnow() return u
class DefaultProfile(object): SECRET_KEY = randchars() # These three just get rid of warnings on the console. KEG_KEYRING_ENABLE = False SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:' SQLALCHEMY_TRACK_MODIFICATIONS = False SITE_NAME = 'Keg Mail Demo' SITE_ABBR = 'KM Demo'
def calc_salt(cls, record_salt=None): record_salt = record_salt or randchars(32, 'all') if isinstance(record_salt, six.text_type): record_salt = record_salt.encode() full_salt = record_salt password_salt = settings.components.auth.password_salt if password_salt: if isinstance(password_salt, six.text_type): password_salt = password_salt.encode() full_salt = password_salt + record_salt return full_salt, record_salt
class Blog(Base, DefaultMixin): __tablename__ = 'blogs' title = sa.Column(sa.Unicode(255), nullable=False) # use this, instead of id, so we can do tests on updating it without # running into problems on the db side by trying to update an identity # or PK column ident = sa.Column(sa.String(12), unique=True, nullable=False, default=lambda: randchars())
class DefaultProfile(object): SECRET_KEY = randchars() # These three just get rid of warnings on the console. KEG_KEYRING_ENABLE = False SQLALCHEMY_DATABASE_URI = 'postgresql://postgres@localhost/postgres' SQLALCHEMY_TRACK_MODIFICATIONS = False SITE_NAME = 'Keg Auth Demo' SITE_ABBR = 'KA Demo' AUTO_EXPAND_MENU = True
def init_rg(self): rg.ident = randchars() rg.environ = self.environ # the request object binds itself to rg.request Request(self.environ) if 'beaker.session' in self.environ: rg.session = self.environ['beaker.session'] log.debug('using beaker sessions') else: rg.session = None # if set, it will be called with an unhandled exception if necessary rg.exception_handler = None
def randfile(fdir, ext=None, length=12, fullpath=False): if not ext: ext = '' else: ext = '.' + ext.lstrip('.') while True: file_name = randchars(length) + ext fpath = path.join(fdir, file_name) if not path.exists(fpath): if fullpath: return fpath else: return file_name
def __init__(self, ident=None, per_page=_None, on_page=_None, qs_prefix='', class_='datagrid', **kwargs): self._ident = ident self.hah = HTMLAttributes(kwargs) self.hah.id = self.ident self.hah.class_ += class_ self.filtered_cols = OrderedDict() self.subtotal_cols = OrderedDict() self.order_by = [] self.qs_prefix = qs_prefix self.user_warnings = [] self.search_value = None self._record_count = None self._records = None self._page_totals = None self._grand_totals = None if self.hide_excel_link is True: warnings.warn( "Hide excel link is deprecated, you should just override allowed_export_targets instead", # noqa DeprecationWarning) if self.allowed_export_targets is None: self.allowed_export_targets = {} # If the grid doesn't define any export targets # lets setup the export targets for xls and xlsx if we have the requirement if xlwt is not None: self.allowed_export_targets['xls'] = XLS if xlsxwriter is not None: self.allowed_export_targets['xlsx'] = XLSX self.set_renderers() self.export_to = None # when session feature is enabled, key is the unique string # used to distinguish grids. Initially set to a random # string, but will be set to the session key in args self.session_key = randchars(12) # at times, different grids may be made to share a session self.foreign_session_loaded = False self.per_page = per_page if per_page is not _None else self.__class__.per_page self.on_page = on_page if on_page is not _None else self.__class__.on_page self.columns = [] self.key_column_map = {} self._init_columns() self.post_init()
def testing_create(cls, **kwargs): kwargs['password'] = kwargs.get('password') or randchars() if 'permissions' in kwargs: perm_cls = registry().permission_cls kwargs['permissions'] = [ perm_cls.get_by_token(perm) if not isinstance(perm, perm_cls) else perm for perm in tolist(kwargs['permissions']) ] user = super(UserMixin, cls).testing_create(**kwargs) user._plaintext_pass = kwargs['password'] return user
def test_session_clear_beaker(self): # make beaker create a session table. Use the alternate profile to have # a database file, instead of in-memory, where it will get wiped before # we can check results wsgiapp = make_wsgi('BeakerSessionTest') ta = TestApp(wsgiapp) ta.get('/beaker-test') sessions_table = sa.Table( 'beaker_cache', sa.MetaData(settings.beaker.url), autoload=True ) sessions_table.delete().execute() for i in range(10): sessions_table.insert().values( namespace=randchars(20), created=dt.datetime.now(), accessed=( dt.datetime.now() - dt.timedelta(seconds=60 * 5 * i) ), data=b'55' ).execute() eq_( db.sess.execute( sa.sql.select([sa.sql.func.count('*')], from_obj=sa.sql.text('beaker_cache')) ).fetchone(), (10, ) ) # re-run the app to clear sessions wsgiapp = make_wsgi('BeakerSessionTest') eq_( db.sess.execute( sa.sql.select([sa.sql.func.count('*')], from_obj=sa.sql.text('beaker_cache')) ).fetchone(), (6, ) ) run_tasks('clear-db') run_tasks('init-db:~test')
def testing_prep(cls): # For now, do the import here so we don't have a hard dependency on WebTest from keg.testing import ContextManager cm = ContextManager.get_for(cls) # if the context manager's app isn't ready, that means this will be the first time the app # is instantiated. That seems like a good indicator that tests are just beginning, so it's # safe to trigger the signal. We don't want the signal to fire every time b/c # testing_prep() can be called more than once per test run. trigger_signal = not cm.is_ready() cm.ensure_current() # set a random secret key so that sessions work in tests. cm.app.config['SECRET_KEY'] = randchars(25) if trigger_signal: signals.testing_run_start.send(cm.app) return cm.app
def add_user(login_id, email, password=None, super_user=False, send_email=True): """ Creates a new user and optionally sends out the welcome email """ from compstack.auth.model.orm import User from compstack.auth.helpers import send_new_user_email u = User.add(login_id=login_id, email_address=email, password=password or randchars(8), super_user=super_user) if send_email: email_sent = send_new_user_email(u) else: email_sent = False return u, email_sent
def testing_create(cls, **kwargs): """Create an object for testing with default data appropriate for the field type * Will automatically set most field types ignoring those passed in via kwargs. * Subclasses that have foreign key relationships should setup those relationships before calling this method. Special kwargs: _numeric_defaults_range: a tuple of (HIGH, LOW) which controls the acceptable defaults of the two number types. Both integer and numeric (float) fields are controlled by this setting. """ NUMERIC_HIGH, NUMERIC_LOW = kwargs.get('_numeric_defaults_range', (-100, 100)) insp = sainsp(cls) skippable = lambda column: (column.key in kwargs # skip fields already in kwargs or column.foreign_keys # skip foreign keys or column.server_default # skip fields with server defaults or column.default # skip fields with defaults or column.primary_key # skip any primary key ) for column in (col for col in insp.columns if not skippable(col)): if isinstance(column.type, sa.types.Enum): kwargs[column.key] = random.choice(column.type.enums) elif isinstance(column.type, sa.types.Integer): kwargs[column.key] = random.randint(NUMERIC_HIGH, NUMERIC_LOW) elif isinstance(column.type, sa.types.Numeric): kwargs[column.key] = random.uniform(NUMERIC_HIGH, NUMERIC_LOW) elif isinstance(column.type, sa.types.Date): kwargs[column.key] = dt.date.today() elif isinstance(column.type, sa.types.DateTime): kwargs[column.key] = dt.datetime.now() elif isinstance(column.type, EmailType): kwargs[column.key] = randemail(min(column.type.length or 50, 50)) elif isinstance(column.type, sa.types.String): kwargs[column.key] = randchars(min(column.type.length or 25, 25)) return cls.add(**kwargs)
def testing_create(cls, **kwargs): kwargs['password'] = kwargs.get('password') or randchars() if 'permissions' in kwargs: perm_cls = registry().permission_cls # ensure all of the tokens exists flask.current_app.auth_manager.validate_permission_set( list( filter(lambda perm: not isinstance(perm, perm_cls), tolist(kwargs['permissions'])))) kwargs['permissions'] = [ perm_cls.testing_create( token=perm) if not isinstance(perm, perm_cls) else perm for perm in tolist(kwargs['permissions']) ] user = super(UserMixin, cls).testing_create(**kwargs) user._plaintext_pass = kwargs['password'] return user
def random_data_for_column(cls, column, numeric_range): if 'randomdata' in column.info: if type(column.info['randomdata']) is str: # assume randomdata the is name of a method on the class callable = getattr(cls, column.info['randomdata']) data = callable() return data return column.info['randomdata']() default_range = (-100, 100) if numeric_range is None else numeric_range if isinstance(column.type, sa.types.Enum): return random.choice(column.type.enums) elif isinstance(column.type, sa.types.Boolean): return random.choice([True, False]) elif isinstance(column.type, sa.types.Integer): return random.randint(*default_range) elif isinstance(column.type, sa.types.Float): return random.uniform(*default_range) elif isinstance(column.type, sa.types.Numeric): if numeric_range is not None or column.type.scale is None: return random.uniform(*default_range) return random_numeric(column) elif isinstance(column.type, sa.types.Date): return dt.date.today() elif isinstance(column.type, sa.types.DateTime): return dt.datetime.utcnow() elif isinstance(column.type, ArrowType): return arrow.utcnow() elif isinstance(column.type, EmailType): return randemail(min(column.type.length or 50, 50)) # elif isinstance(column.type, columns.TimeZoneType): # return random.choice(pytz.common_timezones) elif isinstance(column.type, (sa.types.String, sa.types.Unicode)): return randchars(min(column.type.length or 25, 25)) elif isinstance(column.type, UUID): return uuid.uuid4() raise ValueError( f'No randomization for this column available {column}')
def update(cls, oid=None, **kwargs): from compstack.auth.model.orm import Group if oid is None: u = cls() db.sess.add(u) # when creating a new user, if the password is not set, assign it as # a random string assuming the email will get sent out to the user # and they will change it when they login if not kwargs.get('password', None): kwargs['password'] = randchars(8) else: u = cls.get(oid) # automatically turn on reset_password when the password get set manually # (i.e. when an admin does it), unless told not to (when a user does it # for their own account) if kwargs.get('password') and kwargs.get('pass_reset_ok', True): kwargs['reset_required'] = True for k, v in six.iteritems(kwargs): try: # some values can not be set directly if k not in ('pass_hash', 'pass_salt', 'assigned_groups', 'approved_permissions', 'denied_permissions'): setattr(u, k, v) except AttributeError: pass if 'assigned_groups' in kwargs: u.groups = [ Group.get(gid) for gid in tolist(kwargs['assigned_groups']) ] db.sess.flush() if 'approved_permissions' in kwargs: u.set_permissions(kwargs['approved_permissions'], True) if 'denied_permissions' in kwargs: u.set_permissions(kwargs['denied_permissions'], False) return u
def testing_create(cls, firstname=None, **kwargs): firstname = firstname or randchars() return cls.add(firstname=firstname, **kwargs)
def testing_create(cls, **kwargs): kwargs['firstname'] = kwargs.get('firstname') or randchars() return cls.add(**kwargs)
def testing_create(cls, label=None, active=True): if label is None: label = u'%s %s' % (cls.__name__, randchars(5)) return cls.add(label=label, active_flag=active)
def testing_create(cls, label=None): label = label or randchars() return cls.add(label=label)
def testing_create(cls): blog = Blog(title=randchars()) db.session.add(blog) db.session.commit() return blog
class MyActionColumn(ActionColumn): default_view_link_class = randchars(20) default_edit_link_class = randchars(20) default_delete_link_class = randchars(20)
def testing_create(cls): dud = cls(name=randchars()) db.session.add(dud) db.session.commit() return dud
def testing_create(cls, firstname=None): firstname = firstname or randchars() return cls.add(firstname=firstname)
def __init__(self, ident=None, per_page=_None, on_page=_None, qs_prefix='', class_='datagrid', **kwargs): self._ident = ident self.hah = HTMLAttributes(kwargs) self.hah.id = self.ident self.hah.class_ += class_ self.filtered_cols = OrderedDict() self.subtotal_cols = OrderedDict() self.order_by = [] self.qs_prefix = qs_prefix self.user_warnings = [] self._record_count = None self._records = None self._page_totals = None self._grand_totals = None if self.hide_excel_link is True: warnings.warn( "Hide excel link is deprecated, you should just override allowed_export_targets instead", # noqa DeprecationWarning ) if self.allowed_export_targets is None: self.allowed_export_targets = {} # If the grid doesn't define any export targets # lets setup the export targets for xls and xlsx if we have the requirement if xlwt is not None: self.allowed_export_targets['xls'] = XLS if xlsxwriter is not None: self.allowed_export_targets['xlsx'] = XLSX self.set_renderers() self.export_to = None # when session feature is enabled, key is the unique string # used to distinguish grids. Initially set to a random # string, but will be set to the session key in args self.session_key = randchars(12) # at times, different grids may be made to share a session self.foreign_session_loaded = False self.per_page = per_page if per_page is not _None else self.__class__.per_page self.on_page = on_page if on_page is not _None else self.__class__.on_page self.columns = [] self.key_column_map = {} def subtotal_function_map(v): # subtotals default to the simplest expression (sum). avg is also an option, or you # can assign a string or expression (string using column labels would probably # work best at this stage) if v is True or v == 'sum': return sum_ elif v == 'avg': return avg_ return v for col in self.__cls_cols__: new_col = col.new_instance(self) self.columns.append(new_col) self.key_column_map[new_col.key] = new_col if new_col.filter is not None: self.filtered_cols[new_col.key] = new_col if new_col.has_subtotal is not False and new_col.has_subtotal is not None: self.subtotal_cols[new_col.key] = ( subtotal_function_map(new_col.has_subtotal), new_col ) self.post_init()
def testing_create(cls): return cls.add(name=randchars())