class TestSchema(StoredObject): _id = StringField(primary=True) # Simple fields intfield = IntegerField(list=False, validate=True) floatfield = FloatField(list=False, validate=True) boolfield = BooleanField(list=False, validate=True) datetimefield = DateTimeField(list=False, validate=True) stringfield = StringField(list=False, validate=True) regexfield = StringField(list=False, validate=RegexValidator('^foo$')) urlfield = StringField(list=False, validate=URLValidator()) int_min_field = IntegerField(validate=MinValueValidator(3)) int_max_field = IntegerField(validate=MaxValueValidator(15)) string_min_field = StringField(validate=MinLengthValidator(3)) string_max_field = StringField(validate=MaxLengthValidator(15)) # List fields # int_list = IntegerField(list=True, validate=MinValueValidator(3)) # float_list = FloatField(list=True, validate=True) # bool_list = BooleanField(list=True, validate=True) # datetime_list = DateTimeField(list=True, default=[]) # string_list = StringField(list=True, validate=True) _meta = {'optimistic': True}
class Foo(StoredObject): _id = IntegerField() test_field_max = IntegerField(list=True, list_validate=[ MaxLengthValidator(5), ]) test_field_min = IntegerField(list=True, list_validate=[ MinLengthValidator(3), ])
class TwoFactorUserSettings(AddonUserSettingsBase): totp_secret = StringField() # hexadecimal totp_drift = IntegerField() is_confirmed = BooleanField(default=False) @property def totp_secret_b32(self): return b32encode(unhexlify(self.totp_secret)) @property def otpauth_url(self): return 'otpauth://totp/OSF:{}?secret={}'.format( self.owner.username, self.totp_secret_b32) def to_json(self, user): rv = super(TwoFactorUserSettings, self).to_json(user) rv.update({ 'is_confirmed': self.is_confirmed, 'secret': self.totp_secret_b32, 'drift': self.totp_drift, 'otpauth_url': self.otpauth_url, }) return rv ################### # Utility methods # ################### def verify_code(self, code): accepted, drift = accept_totp(key=self.totp_secret, response=code, drift=self.totp_drift) if accepted: self.totp_drift = drift return True return False ############# # Callbacks # ############# def on_add(self): # TODO(hrybacki, sloria): push status message shouldn't need a session push_status_message('Please <a href="#TfaVerify">activate your' ' device</a> before continuing.') super(TwoFactorUserSettings, self).on_add() self.totp_secret = _generate_seed() self.totp_drift = 0 self.is_confirmed = False def on_delete(self): if self.is_confirmed: push_status_message('Successfully deauthorized two-factor' ' authentication. Please delete the' ' verification code on your device.') super(TwoFactorUserSettings, self).on_delete() self.totp_secret = None self.totp_drift = 0 self.is_confirmed = False
class Foo(StoredObject): _id = IntegerField() test_field_max = StringField(list=False, validate=[ MaxLengthValidator(5), ]) test_field_min = StringField(list=False, validate=[ MinLengthValidator(5), ])
class TwoFactorUserSettings(AddonUserSettingsBase): totp_secret = StringField() # hexadecimal totp_drift = IntegerField() is_confirmed = BooleanField(default=False) @property def totp_secret_b32(self): return b32encode(unhexlify(self.totp_secret)) @property def otpauth_url(self): return 'otpauth://totp/OSF:{}?secret={}'.format( self.owner.username, self.totp_secret_b32) def to_json(self, user): rv = super(TwoFactorUserSettings, self).to_json(user) rv.update({ 'is_enabled': True, 'is_confirmed': self.is_confirmed, 'secret': self.totp_secret_b32, 'drift': self.totp_drift, }) return rv ################### # Utility methods # ################### def verify_code(self, code): accepted, drift = accept_totp(key=self.totp_secret, response=code, drift=self.totp_drift) if accepted: self.totp_drift = drift return True return False ############# # Callbacks # ############# def on_add(self): super(TwoFactorUserSettings, self).on_add() self.totp_secret = _generate_seed() self.totp_drift = 0 self.is_confirmed = False def on_delete(self): super(TwoFactorUserSettings, self).on_delete() self.totp_secret = None self.totp_drift = 0 self.is_confirmed = False
def test_min_and_max_value_list(self): ''' Assert that a list with MinLengthValidator(x) and MaxLengthValidator(y) cannot be saved with a length less than x or a length greater than y ''' list_of_list_for_min_max = [ ['whoa'], [ 2, 4, 0, ], [ 17, ], [ 5, 5, 5, 5, ], [ 5, 5, 5, 5, 6, 7, 8, 9, 11, 12, ], ] Schema = create_schema( 'min_max_value_schema', field=IntegerField( list=True, validate=[MinValueValidator(5), MaxValueValidator(15)], list_validate=[MinLengthValidator(5), MaxLengthValidator(7)], )) test_row = Schema() for test_list in list_of_list_for_min_max: test_row.field = test_list self.assertRaises(Exception, test_row.save)
def test_validate_min_value_list(self): ''' Assert that a list with MinLengthValidator(n) cannot be saved with a value less than n. ''' list_of_list_for_min = [ ['whoa'], [ 2, 4, 0, ], [ 2, 3, 4, 5, ], [ 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, ], ] Schema = create_schema( 'min_value_schema', field=IntegerField( list=True, validate=MinValueValidator(5), list_validate=[MinLengthValidator(5), MaxLengthValidator(7)])) test_row = Schema() for test_list in list_of_list_for_min: test_row.field = test_list self.assertRaises(Exception, test_row.save)
def test_validate_integer_list(self): ''' Assert that the field cannot be saved with lists containing any type other than integer ''' test_lists = [[1.5, 2.5, 3.5], [ 'hey', 'hey', ], [ datetime.datetime.now(), datetime.datetime.now(), ]] Schema = create_schema('integer_list_schema', field=IntegerField( list=True, validate=True, )) test_row = Schema() for test_list in test_lists: test_row.field = test_list self.assertRaises(Exception, test_row.save)
class Foo(StoredObject): _id = IntegerField() url_field = StringField(list=False, validate=[URLValidator()])
class Foo(StoredObject): _id = IntegerField(primary=True)
class Foo(StoredObject): _id = IntegerField() my_bar = ForeignField('Bar', list=True, backref='my_foo')
class Bar(StoredObject): _id = IntegerField()
class Foo(StoredObject): _id = IntegerField() field = DateTimeField(list=False, validate=True)
class Foo(StoredObject): _id = IntegerField() field = StringField(list=True, validate=True)
class Foo(StoredObject): _id = IntegerField() field = BooleanField(list=False, validate=True)
class Foo(StoredObject): _id = IntegerField() test_field = StringField(list=True, validate=MaxLengthValidator(3), list_validate=MinLengthValidator(3))
class Foo(StoredObject): _id = IntegerField() int_field = IntegerField(list=False, validate=[ MaxValueValidator(5), ])
class Foo(TestObject): _id = IntegerField() my_bar = ForeignField('Bar', backref='my_foo')
class Bar(TestObject): _id = IntegerField()
class Foo(StoredObject): _id = IntegerField() float_field = FloatField(list=False, validate=[ MaxValueValidator(5.), ])