def test_additional_properties_parse_parameter(self): schema = {"?bar": "boolean", "?nested": [{"?baz": "integer"}]} values = [{"x1": "yes"}, {"bar": True, "nested": [{"x1": "yes"}]}] for _ in xrange(3): self._testValidation(V.parse(schema, additional_properties=True), valid=values) self._testValidation(V.parse(schema, additional_properties=False), invalid=values) self._testValidation(V.parse( schema, additional_properties=V.Object.REMOVE), adapted=[(values[0], {}), (values[1], { "bar": True, "nested": [{}] })]) self._testValidation(V.parse(schema, additional_properties="string"), valid=values, invalid=[{ "x1": 42 }, { "bar": True, "nested": [{ "x1": 42 }] }])
def test_ignore_optional_property_errors_parse_parameter(self): schema = { "+foo": "number", "?bar": "boolean", "?nested": [{ "+baz": "string", "?zoo": "number", }] } invalid_required = [ {"foo": "2", "bar": True}, ] invalid_optional = [ {"foo": 3, "bar": "nan"}, {"foo": 3.1, "nested": [{"baz": "x", "zoo": "12"}]}, {"foo": 0, "nested": [{"baz": 1, "zoo": 2}]}, ] adapted = [ {"foo": 3}, {"foo": 3.1, "nested": [{"baz": "x"}]}, {"foo": 0}, ] for _ in xrange(3): self._testValidation(V.parse(schema, ignore_optional_property_errors=False), invalid=invalid_required + invalid_optional) self._testValidation(V.parse(schema, ignore_optional_property_errors=True), invalid=invalid_required, adapted=zip(invalid_optional, adapted))
def test_parsing_required_properties(self): get_schema = lambda: { "foo": V.Nullable("number"), "?nested": [V.Nullable({ "baz": "string" })] } valid = [{"foo": 3, "nested": [None]}] missing_properties = [{}, {"foo": 3, "nested": [{}]}] for _ in xrange(3): with V.parsing(required_properties=False): self._testValidation(get_schema(), valid=valid + missing_properties) with V.parsing(required_properties=True): self._testValidation(get_schema(), valid=valid, invalid=missing_properties) # gotcha: calling parse() with required_properties=True is not # equivalent to the above call because the V.Nullable() calls in # get_schema have already called implicitly parse() without parameters. if V.Object.REQUIRED_PROPERTIES: self._testValidation(V.parse(get_schema(), required_properties=True), invalid=[missing_properties[1]]) else: self._testValidation(V.parse(get_schema(), required_properties=True), valid=[missing_properties[1]])
def test_parsing_required_properties(self): get_schema = lambda: { "foo": V.Nullable("number"), "?nested": [V.Nullable({"baz": "string"})] } valid = [{"foo": 3, "nested": [None]}] missing_properties = [{}, {"foo": 3, "nested": [{}]}] for _ in xrange(3): with V.parsing(required_properties=False): self._testValidation(get_schema(), valid=valid + missing_properties) with V.parsing(required_properties=True): self._testValidation(get_schema(), valid=valid, invalid=missing_properties) # gotcha: calling parse() with required_properties=True is not # equivalent to the above call because the V.Nullable() calls in # get_schema have already called implicitly parse() without parameters. if V.Object.REQUIRED_PROPERTIES: self._testValidation(V.parse(get_schema(), required_properties=True), invalid=[missing_properties[1]]) else: self._testValidation(V.parse(get_schema(), required_properties=True), valid=[missing_properties[1]])
def test_parsing_additional_properties(self): get_schema = lambda: { "?bar": "boolean", "?nested": [V.Nullable({"?baz": "integer"})] } values = [{"x1": "yes"}, {"bar": True, "nested": [{"x1": "yes"}]}] for _ in xrange(3): with V.parsing(additional_properties=True): self._testValidation(get_schema(), valid=values) with V.parsing(additional_properties=False): self._testValidation(get_schema(), invalid=values) # gotcha: calling parse() with additional_properties=False is not # equivalent to the above call because the V.Nullable() calls in # get_schema have already called implicitly parse() without parameters. # The 'additional_properties' parameter effectively is applied at # the top level dict only self._testValidation(V.parse(get_schema(), additional_properties=False), invalid=values[:1], valid=values[1:]) with V.parsing(additional_properties=V.Object.REMOVE): self._testValidation(get_schema(), adapted=[(values[0], {}), (values[1], { "bar": True, "nested": [{}] })]) # same gotcha as above self._testValidation(V.parse( get_schema(), additional_properties=V.Object.REMOVE), adapted=[(values[0], {}), (values[1], values[1])]) with V.parsing(additional_properties="string"): self._testValidation(get_schema(), valid=values, invalid=[{ "x1": 42 }, { "bar": True, "nested": [{ "x1": 42 }] }]) # same gotcha as above self._testValidation(V.parse(get_schema(), additional_properties="string"), invalid=[{ "x1": 42 }], valid=[{ "bar": True, "nested": [{ "x1": 42 }] }])
def test_required_properties_parse_parameter(self): schema = { "foo": "number", "?bar": "boolean", "?nested": [{ "baz": "string" }] } missing_properties = [{}, {"bar": True}, {"foo": 3, "nested": [{}]}] for _ in xrange(3): self._testValidation(V.parse(schema, required_properties=True), invalid=missing_properties) self._testValidation(V.parse(schema, required_properties=False), valid=missing_properties)
def test_where_multi_arg(self): q = Query() q.select("a") q.where("a", "a=1") q.where("b", "b=2") q.where("b", "b=3") q.tables("from t") q.into(False) # no column `col_1` self.assertRaises(valideer.ValidationError, q, valideer.parse({"where": "where"}).validate({"where": "a|b|c"})) self.assertEqual(q(valideer.parse({"where": "where"}).validate({"where": "a|b"})), "select a from t where (a=1 or (b=2 and b=3))")
def test_where_arrangement(self): q = Query() q.select("a") q.where("a", "a > 10") q.where("b", "b > 20") q.where("c", "c > 30") q.tables("from t") q.into(False) # no column `d` self.assertRaises(valideer.ValidationError, q, valideer.parse({"where": "where"}).validate({"where": "(d|b)&c"})) self.assertEqual(q(valideer.parse({"where": "where"}).validate({"where": "(a|b)&c"})), "select a from t where ((a > 10 or b > 20) and c > 30)")
def validated(arguments=None, body=None, extra_arguments=True, extra_body=False): if type(body) in (dict, str): body = parse(body, additional_properties=extra_body) elif body not in (None, False): raise ValueError('body must be type None, False, or dict') if type(arguments) is dict: arguments = parse(arguments, additional_properties=extra_arguments) elif arguments not in (None, False): raise ValueError('arguments must be type None, False, or dict') def wrapper(method): @wraps(method) def validate(self, *args, **kwargs): # ------------------ # Validate Body Data # ------------------ if body: try: _body = json_decode(self.request.body) if self.request.body else {} except: # ex. key1=value2&key2=value2 try: _body = dict([(k, v[0] if len(v) == 1 else v) for k, v in parse_qs(self.request.body, strict_parsing=True).items()]) except: raise HTTPError(400, "body was not able to be decoded") kwargs['body'] = body.validate(_body, adapt=True) elif body is False and self.request.body: raise HTTPError(400, reason='No body arguments allowed') # ------------------- # Validate URL Params # ------------------- if arguments: # include url arguments if self.request.query_arguments: _arguments = dict([(k, v[0] if len(v) == 1 else v) for k, v in self.request.query_arguments.items() if v != [''] and k[0] != '_']) else: _arguments = {} kwargs["arguments"] = arguments.validate(_arguments) elif arguments is False and self.request.query_arguments and not any(map(lambda a: a[0] == '_', self.request.query_arguments)): raise HTTPError(400, reason='No url arguments allowed') return method(self, *args, **kwargs) return validate return wrapper
def test_email(self): schema = valideer.parse({"email": "email"}) for email in ('def post(self, @example.com', '*****@*****.**', '[email protected]'): self.assertEqual(schema.validate(dict(email=email))['email'], email.lower()) for email in ('needsanat', 'nodom@fefe'): self.assertRaises(error, schema.validate, dict(email=email))
def __init__(self, allow_extra): schema = { '+id': int, '+client_name': V.String(max_length=255), '+sort_index': float, 'client_phone': V.Nullable(V.String(max_length=255)), 'location': {'latitude': float, 'longitude': float}, 'contractor': V.Range(V.AdaptTo(int), min_value=1), 'upstream_http_referrer': V.Nullable(V.String(max_length=1023)), '+grecaptcha_response': V.String(min_length=20, max_length=1000), 'last_updated': V.AdaptBy(dateutil.parser.parse), 'skills': V.Nullable( [ { '+subject': str, '+subject_id': int, '+category': str, '+qual_level': str, '+qual_level_id': int, 'qual_level_ranking': V.Nullable(float, default=0), } ], default=[], ), } self.validator = V.parse(schema, additional_properties=allow_extra)
def test_file(self): schema = valideer.parse({"value": "file"}) for handler in ('app/base.py', 'codecov.sh'): assert schema.validate(dict(value=handler)) for invalid in ('not/a/path', '**#^@', '../black'): self.assertRaises(valideer.ValidationError, schema.validate, dict(value=invalid))
def test_branch(self): schema = valideer.parse({"branch": "branch"}) for x in ('master', 'stable', 'something/thisoem', '-aples', '.dev', 'builds,/1@#$%&*'): assert schema.validate(dict(branch=x)) for x in (False, None): self.assertRaises(valideer.ValidationError, schema.validate, dict(branch=x))
def test_date(self): schema = valideer.parse({"date": "date"}) for date in ('jan 15th 2015', 'tomorrow at 10:30', 'last tuesday'): self.assertEqual( schema.validate(dict(date=date))['date'], timestring.Date(date)) self.assertRaises(error, schema.validate, dict(date="never"))
def test_daterange(self): schema = valideer.parse({"daterange": "daterange"}) for daterange in ('2 weeks', 'this year', 'next thursday'): self.assertEqual( schema.validate(dict(daterange=daterange))['daterange'], timestring.Range(daterange)) self.assertRaises(error, schema.validate, dict(daterange="never"))
def __checkSanitarinessOfPayloadMeta(self, dictPayload): # check that bad guys haven't put dangerous content into the input # we can't trap malicious output here because it's trivial to change this program to not do the check try: # dictPayload is a python dictionary # check the content is the right format for the schema # adapt_sent = V.parse({"+snt" : [V.AdaptTo(String)]}) payload_schema = { "+snt" : [V.AdaptTo(String)], "+sz" : [V.AdaptTo(Integer)], "+szu" : [V.AdaptTo(Integer)], "+szc" : [V.AdaptTo(Integer)], "+sze" : [V.AdaptTo(Integer)], "+cks" : [V.AdaptTo(String)], "+dat" : "string", ### should be in uuencode format i.e. alphanumerics } # payload_schema = { # V.parse({"+snt" : [V.AdaptTo(String)]}), # V.parse({"+sz" : [V.AdaptTo(Integer)]}), # V.parse({"+szu" : [V.AdaptTo(Integer)]}), # V.parse({"+szc" : [V.AdaptTo(Integer)]}), # V.parse({"+sze" : [V.AdaptTo(Integer)]}), # V.parse({"+cks" : [V.AdaptTo(String)]}), # "+dat" : "string", ### should be in uuencode format i.e. alphanumerics # } # payload_schema = { # "+snt": "datetime", # "+sz": V.Range("number", min_value=0), # "+szu": V.Range("number", min_value=0), # "+szc": V.Range("number", min_value=0), # "+sze": V.Range("number", min_value=0), # "+cks": "string", # "+dat" : "string", ### should be in uuencode format i.e. alphanumerics # } validator = V.parse(payload_schema) # dictValidatePayload = { # "snt": int(unicodeQos.decode('utf-8')), # "sz": , # "szu": , # "szc": , # "sze": , # "cks": , # "dat" : , ### should be in uuencode format i.e. alphanumerics # # } # (int(unicodeQos.decode('utf-8'))) validator.validate(dictPayload) except V.ValidationError as e: logging.error("ERROR - invalid data format in dictionary to send - %s", e) return False except: logging.error("ERROR - invalid data format in dictionary to send - %s", sys.exc_info()[0]) return False return True
def test_version(self): schema = valideer.parse({"value": "version"}) for version in ('0.0.1', '0.0.10', '0.2.10', '1.2.1'): assert schema.validate(dict(value=version)) self.assertRaises(valideer.ValidationError, schema.validate, dict(value='-124.12.51')) self.assertRaises(valideer.ValidationError, schema.validate, dict(value='_something-odd')) self.assertRaises(valideer.ValidationError, schema.validate, dict(value=None))
def test_timezone(self): schema = valideer.parse({"value": "timezone"}) for key, value in timezone.timezones.items(): self.assertEqual(schema.validate(dict(value=key))['value'], value) for x in ('not', None, 'random', object(), int): self.assertRaises(error, schema.validate, dict(value=x))
def test_id(self): schema = valideer.parse({"value": "id"}) self.assertEqual(schema.validate(dict(value=1))['value'], 1) self.assertEqual(schema.validate(dict(value="50"))['value'], 50) for x in ('not', None, 'random', object(), int, -50, "19.123"): self.assertRaises(error, schema.validate, dict(value=x))
def test_handler(self): schema = valideer.parse({"value": "handler"}) for handler in ('valid-handler', 'someothername', '__hello.world-ok'): assert schema.validate(dict(value=handler)) for invalid in "!@#{$%^&*(),<>?/'\";:[]{}\\|~`+": self.assertRaises(valideer.ValidationError, schema.validate, dict(value='characters'+invalid)) self.assertRaises(valideer.ValidationError, schema.validate, dict(value=None))
def test_day(self): schema = valideer.parse({"day": "day"}) for (d, v) in ((0, "sun"), (1, "mon"), (2, "tue"), (3, "wed"), (4, "thu"), (5, "fri"), (6, "sat")): self.assertEqual(schema.validate(dict(day=d))['day'], d) self.assertEqual(schema.validate(dict(day=v))['day'], d) for day in ('needsanat', 'nodom'): self.assertRaises(error, schema.validate, dict(day=day))
def test_ref(self): schema = valideer.parse({"value": "ref"}) self.assertEqual(schema.validate(dict(value="a"*40))['value'], "a"*40) self.assertEqual(schema.validate(dict(value="apples"))['value'], "apples") for x in (None, object(), int, -50): self.assertRaises(error, schema.validate, dict(value=x))
class BuildMessage(BaseMessage): project = attr.ib() delay = attr.ib() incremental = attr.ib(default=None) _validator = V.parse({ "project": "string", "delay": "number", "?incremental": V.Nullable("boolean") })
def test_additional_properties_parse_parameter(self): schema = { "?bar": "boolean", "?nested": [{ "?baz": "integer" }] } values = [{"x1": "yes"}, {"bar":True, "nested": [{"x1": "yes"}]}] for _ in xrange(3): self._testValidation(V.parse(schema, additional_properties=True), valid=values) self._testValidation(V.parse(schema, additional_properties=False), invalid=values) self._testValidation(V.parse(schema, additional_properties="string"), valid=values, invalid=[{"x1": 42}, {"bar":True, "nested": [{"x1": 42}]}])
def parse(cls, value): return cls(**valideer.parse({ "+a": valideer.Boolean, "+b": valideer.Boolean, "+c": valideer.Boolean, "+d": valideer.AdaptTo(float), "+e": valideer.AdaptTo(int), "+f": valideer.AdaptTo(int), }).validate(value))
class WorkMessage(BaseMessage): project = attr.ib() git = attr.ib() revision = attr.ib() _validator = V.parse({ "project": "string", "git": "string", "revision": "string" })
class LogMessage(BaseMessage): project = attr.ib() job = attr.ib() line = attr.ib() _validator = V.parse({ "project": "string", "job": "string", "line": "string" })
def test_email(self): schema = valideer.parse({"email": "email"}) for email in ('def post(self, @example.com', '*****@*****.**', '[email protected]'): self.assertEqual( schema.validate(dict(email=email))['email'], email.lower()) for email in ('needsanat', 'nodom@fefe'): self.assertRaises(error, schema.validate, dict(email=email))
def test_uuid(self): schema = valideer.parse({"value": "uuid"}) self.assertTrue( schema.validate( dict(value='84fc2f3a-f199-42ba-b24b-fa46455952f4'))) self.assertRaises(error, schema.validate, dict(value='84fc2f3a-**-42ba-b24b-fa46455952f4')) self.assertRaises(error, schema.validate, dict(value='84fc2f3a-42ba-b24b-fa46455952f4')) self.assertRaises(error, schema.validate, dict(value=None))
def test_commit(self): schema = valideer.parse({"value": "commit"}) for commit in ('d5eb3baabe1149158817640f9e27e6b947aef043', 'd5eb3baabe1149158817640f9e27e6b947aef043', '1341:233tmgpeqmgpqm', '2f540b1af5fb5f432be686a77ed5206b589a7b52'): assert schema.validate(dict(value=commit)) self.assertRaises(valideer.ValidationError, schema.validate, dict(value='_something odd')) self.assertRaises(valideer.ValidationError, schema.validate, dict(value=None))
def test_ref(self): schema = valideer.parse({"value": "ref"}) self.assertEqual( schema.validate(dict(value="a" * 40))['value'], "a" * 40) self.assertEqual( schema.validate(dict(value="apples"))['value'], "apples") for x in (None, object(), int, -50): self.assertRaises(error, schema.validate, dict(value=x))
def test_it_should_accept_and_enforce_choices(self): from nonobvious import fields field = fields.Field(key='foo', validator='string', choices=('bar', 'baz')) validator = V.parse(dict([field.validation_spec])) ensure(validator.validate).called_with({'foo': 'bar'}).equals({'foo': 'bar'}) ensure(validator.validate).called_with({'foo': 'baz'}).equals({'foo': 'baz'}) ensure(validator.validate).called_with({'foo': 'boo'}).raises(V.ValidationError)
def leave_groups(self, user, data): try: groups = V.parse([V.AdaptTo(uuid.UUID)]).validate(data) except V.ValidationError as ex: raise ValidationError(str(ex)) self.log.info('leave groups', audit=True, data=data, user=userinfo_for_log(user)) userid = user['userid'] for groupid in groups: self.session.del_group_member(groupid, userid)
def test_handler(self): schema = valideer.parse({"value": "handler"}) for handler in ('valid-handler', 'someothername', '__hello.world-ok'): assert schema.validate(dict(value=handler)) for invalid in "!@#{$%^&*(),<>?/'\";:[]{}\\|~`+": self.assertRaises(valideer.ValidationError, schema.validate, dict(value='characters' + invalid)) self.assertRaises(valideer.ValidationError, schema.validate, dict(value=None))
def validate(self, item): validator = V.parse(self.schema, additional_properties=False) try: adapted = validator.validate(item) except V.ValidationError as ex: raise ValidationError(str(ex)) for key in self.schema: if not key.startswith('+') and key not in adapted: adapted[key] = None return adapted
def get_conf(conf=None): if not conf: with open('conf.json', 'br') as f: conf = json.loads(f.read().decode()) exists = v.Condition(lambda v: Path(v).exists()) strip_slash = v.AdaptBy(lambda v: str(v).rstrip('/')) app_dir = Path(__file__).parent.resolve() base_dir = app_dir.parent log_handlers = ['console_simple', 'console_detail', 'file'] with v.parsing(additional_properties=False): schema = v.parse({ 'debug': v.Nullable(bool, False), '+pg_username': str, '+pg_password': str, '+cookie_secret': str, 'google_id': str, 'google_secret': str, 'readonly': v.Nullable(bool, True), 'enabled': v.Nullable(bool, True), 'log_handlers': ( v.Nullable([v.Enum(log_handlers)], log_handlers[:1]) ), 'log_level': v.Nullable(str, 'DEBUG'), 'log_file': v.Nullable(str, ''), 'path_attachments': v.Nullable(str, str(base_dir / 'attachments')), 'path_theme': v.Nullable(exists, str(base_dir / 'front')), 'imap_body_maxsize': v.Nullable(int, 50 * 1024 * 1024), 'imap_batch_size': v.Nullable(int, 2000), 'imap_debug': v.Nullable(int, 0), 'smtp_debug': v.Nullable(bool, False), 'async_pool': v.Nullable(int, 0), 'ui_ga_id': v.Nullable(str, ''), 'ui_is_public': v.Nullable(bool, False), 'ui_use_names': v.Nullable(bool, True), 'ui_per_page': v.Nullable(int, 100), 'ui_greeting': v.Nullable(str, ''), 'ui_ws_proxy': v.Nullable(bool, False), 'ui_ws_enabled': v.Nullable(bool, True), 'ui_ws_timeout': v.Nullable(int, 1000), 'ui_firebug': v.Nullable(bool, False), 'ui_tiny_thread': v.Nullable(int, 5), 'ui_by_thread': v.Nullable(bool, False), 'from_emails': v.Nullable([str], []), 'host_ws': v.Nullable(str, 'ws://localhost/async/'), 'host_web': v.Nullable(strip_slash, 'http://localhost:8000'), 'search_lang': v.Nullable([str], ['simple', 'english']), }) conf = schema.validate(conf) path = Path(conf['path_attachments']) if not path.exists(): path.mkdir() return conf
def info(env): schema = v.parse({'offset': v.AdaptTo(int)}) args = schema.validate(env.request.args) if args.get('offset'): env.session['tz_offset'] = args['offset'] if env.valid_username or env.valid_token: res = ctx_init(env) else: res = {} return res
class JobCompletionMessage(BaseMessage): project = attr.ib() job = attr.ib() exit_code = attr.ib() run_time = attr.ib() _validator = V.parse({ "project": "string", "job": "string", "exit_code": "number", "run_time": "number" })
def del_members(self, groupid, data, user): validator = V.parse(self.del_member_schema, additional_properties=False) try: adapted = validator.validate(data) except V.ValidationError as ex: raise ValidationError(str(ex)) self.log.info('delete group members', audit=True, groupid=groupid, data=data, user=userinfo_for_log(user)) for member in adapted: userid = self.session.get_userid_by_userid_sec(member) self.session.del_group_member(groupid, userid)
def inner(env): ctx = {'title': 'Reset Password'} if env.request.method == 'POST': schema = v.parse({'+password': str, '+password_confirm': str}) args = schema.validate(env.request.json) if args['password'] == args['password_confirm']: env.set_password(args['password']) return {'username': env.username} ctx['error'] = 'Passwords aren\'t the same' return ctx return ctx
def test_parsing_additional_properties(self): get_schema = lambda: { "?bar": "boolean", "?nested": [V.Nullable({ "?baz": "integer" })] } values = [{"x1": "yes"}, {"bar": True, "nested": [{"x1": "yes"}]}] for _ in xrange(3): with V.parsing(additional_properties=True): self._testValidation(get_schema(), valid=values) with V.parsing(additional_properties=False): self._testValidation(get_schema(), invalid=values) # gotcha: calling parse() with additional_properties=False is not # equivalent to the above call because the V.Nullable() calls in # get_schema have already called implicitly parse() without parameters. # The 'additional_properties' parameter effectively is applied at # the top level dict only self._testValidation(V.parse(get_schema(), additional_properties=False), invalid=values[:1], valid=values[1:]) with V.parsing(additional_properties=V.Object.REMOVE): self._testValidation(get_schema(), adapted=[(values[0], {}), (values[1], {"bar": True, "nested": [{}]})]) # same gotcha as above self._testValidation(V.parse(get_schema(), additional_properties=V.Object.REMOVE), adapted=[(values[0], {}), (values[1], values[1])]) with V.parsing(additional_properties="string"): self._testValidation(get_schema(), valid=values, invalid=[{"x1": 42}, {"bar": True, "nested": [{"x1": 42}]}]) # same gotcha as above self._testValidation(V.parse(get_schema(), additional_properties="string"), invalid=[{"x1": 42}], valid=[{"bar": True, "nested": [{"x1": 42}]}])
def test_float(self): schema = valideer.parse({"value": "float"}) self.assertEqual(schema.validate(dict(value=1))['value'], 1) self.assertEqual(schema.validate(dict(value="1k"))['value'], 1000) self.assertEqual(schema.validate(dict(value="-50"))['value'], -50) self.assertEqual(schema.validate(dict(value="5.2k"))['value'], 5200) self.assertEqual(schema.validate(dict(value="19.123"))['value'], 19.123) self.assertEqual(schema.validate(dict(value=12.91))['value'], 12.91) for x in ('not', None, 'random', object(), int, ): self.assertRaises(error, schema.validate, dict(value=x))
def confirm_groups(self, user, data): try: groups = V.parse([V.AdaptTo(uuid.UUID)]).validate(data) except V.ValidationError as ex: raise ValidationError(str(ex)) self.log.info('confirm groups', audit=True, data=data, user=userinfo_for_log(user)) userid = user['userid'] for groupid in groups: self.session.get_membership_data(groupid, userid) # Raises KeyError if not member for groupid in groups: self.session.set_group_member_status(groupid, userid, 'normal')
def test_it_should_not_accept_naive_times(self): from nonobvious import fields field = fields.Time(key='foo') ensure( V.parse(dict([field.validation_spec])).validate ).called_with( {'foo': dt.datetime.now().time()} # Naive time ).raises( V.ValidationError )
def new_thread(env): schema = v.parse({'+ids': [int], '+action': v.Enum(('new', 'merge'))}) params = schema.validate(env.request.json) action = params['action'] if action == 'new': id = params['ids'][0] syncer.new_thread(env, id) return {'url': env.url_for('thread', {'id': id})} elif action == 'merge': thrid = syncer.merge_threads(env, params['ids']) return {'url': env.url_for('thread', {'id': thrid})}
def test_where_multi_arg(self): q = Query() q.select("a") q.where("a", "a=1") q.where("b", "b=2") q.where("b", "b=3") q.tables("from t") q.into(False) # no column `col_1` self.assertRaises( valideer.ValidationError, q, valideer.parse({ "where": "where" }).validate({"where": "a|b|c"})) self.assertEqual( q(valideer.parse({ "where": "where" }).validate({"where": "a|b"})), "select a from t where (a=1 or (b=2 and b=3))")
def test_where_arrangement(self): q = Query() q.select("a") q.where("a", "a > 10") q.where("b", "b > 20") q.where("c", "c > 30") q.tables("from t") q.into(False) # no column `d` self.assertRaises( valideer.ValidationError, q, valideer.parse({ "where": "where" }).validate({"where": "(d|b)&c"})) self.assertEqual( q( valideer.parse({ "where": "where" }).validate({"where": "(a|b)&c"})), "select a from t where ((a > 10 or b > 20) and c > 30)")
def test_parsing_ignore_optional_property_errors(self): get_schema = lambda: V.Nullable({ "+foo": "number", "?bar": "boolean", "?nested": [{ "+baz": "string", "?zoo": "number", }] }) invalid_required = [ {"foo": "2", "bar": True}, ] invalid_optional = [ {"foo": 3, "bar": "nan"}, {"foo": 3.1, "nested": [{"baz": "x", "zoo": "12"}]}, {"foo": 0, "nested": [{"baz": 1, "zoo": 2}]}, ] adapted = [ {"foo": 3}, {"foo": 3.1, "nested": [{"baz": "x"}]}, {"foo": 0}, ] for _ in xrange(3): with V.parsing(ignore_optional_property_errors=False): self._testValidation(get_schema(), invalid=invalid_required + invalid_optional) with V.parsing(ignore_optional_property_errors=True): self._testValidation(get_schema(), invalid=invalid_required, adapted=zip(invalid_optional, adapted)) # gotcha: calling parse() with ignore_optional_property_errors=True # is not equivalent to the above call because the V.Nullable() calls in # get_schema have already called implicitly parse() without parameters. self._testValidation(V.parse(get_schema(), ignore_optional_property_errors=False), invalid=invalid_required + invalid_optional) self._testValidation(V.parse(get_schema(), ignore_optional_property_errors=True), invalid=invalid_required + invalid_optional)
def testGetNotes(self, apikey): repl = self.api.getNotes(66, apikey) expect = [200, "UTF-8", "text/xml; charset=utf-8"] resp = [repl.status_code, repl.encoding, repl.headers["content-type"]] self.showDifferences(expect, resp, "http skelleton") replFields = self.xmlToDict(repl.content) print("- XML structure"), with valideer.parsing(additional_properties=True): validator = valideer.parse(self.SCHEMA_NOTES) try: validator.validate(replFields) print("OK") except ValidationError as e: print("differs at %s " % (str(e)))