def test_multipolygon_success(self): schema = {"location": {"type": "multipolygon"}} doc = { "location": { "type": "MultiPolygon", "coordinates": [ [[ [102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0], ]], [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0], ], [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2], ], ], ], } } v = Validator(schema) self.assertTrue(v.validate(doc))
def test_decimal_fail(self): schema = {"decimal": {"type": "decimal"}} doc = {"decimal": "not_a_decimal"} v = Validator(schema, None) self.assertFalse(v.validate(doc)) self.assertTrue("decimal" in v.errors) self.assertTrue("decimal" in v.errors["decimal"])
def test_point_coordinates_fail(self): schema = {"location": {"type": "point"}} doc = {"location": {"type": "Point", "coordinates": [123.0]}} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue("location" in v.errors) self.assertTrue("point" in v.errors["location"])
def test_dbref_fail(self): schema = {"id": {"type": "dbref"}} doc = {"id": "not_an_object_id"} v = Validator(schema, None) self.assertFalse(v.validate(doc)) self.assertTrue("id" in v.errors) self.assertTrue("dbref" in v.errors["id"])
def test_linestring_success(self): schema = {'location': {'type': 'linestring'}} doc = {'location': {"type": "LineString", "coordinates": [[100.0, 0.0], [101.0, 1.0]] }} v = Validator(schema) self.assertTrue(v.validate(doc))
def test_geojson_not_compilant(self): schema = {'location': {'type': 'point'}} doc = {'location': [10.0, 123.0]} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('location' in v.errors) self.assertTrue('point' in v.errors['location'])
def test_geometrycollection_not_compilant(self): schema = {"location": {"type": "geometrycollection"}} doc = {"location": {"type": "GeometryCollection", "coordinates": [10.0, 123.0]}} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue("location" in v.errors) self.assertTrue("geometrycollection" in v.errors["location"])
def test_dbref_fail(self): schema = {'id': {'type': 'dbref'}} doc = {'id': 'not_an_object_id'} v = Validator(schema, None) self.assertFalse(v.validate(doc)) self.assertTrue('id' in v.errors) self.assertTrue('dbref' in v.errors['id'])
def test_geometry_not_compilant(self): schema = {'location': {'type': 'point'}} doc = {'location': {"type": "Point", "geometries": [10.0, 123.0]}} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('location' in v.errors) self.assertTrue('Point' in v.errors['location'])
def test_geometry_not_compilant(self): schema = {"location": {"type": "point"}} doc = {"location": {"type": "Point", "geometries": [10.0, 123.0]}} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue("location" in v.errors) self.assertTrue("point" in v.errors["location"])
def test_point_fail(self): schema = {'location': {'type': 'point'}} doc = {'location': {'type': "Point", 'coordinates': ["asdasd", 123.0]}} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('location' in v.errors) self.assertTrue('Point' in v.errors['location'])
def test_decimal_fail(self): schema = {'decimal': {'type': 'decimal'}} doc = {'decimal': 'not_a_decimal'} v = Validator(schema, None) self.assertFalse(v.validate(doc)) self.assertTrue('decimal' in v.errors) self.assertTrue('decimal' in v.errors['decimal'])
def test_objectid_fail(self): schema = {'id': {'type': 'objectid'}} doc = {'id': 'not_an_object_id'} v = Validator(schema, None) self.assertFalse(v.validate(doc)) self.assertTrue(ERROR_BAD_TYPE % ('id', 'ObjectId') in v.errors)
def test_dbref_success(self): schema = {"id": {"type": "dbref"}} doc = { "id": DBRef("SomeCollection", ObjectId("50656e4538345b39dd0414f0")) } v = Validator(schema, None) self.assertTrue(v.validate(doc))
def new_order_actions(items): waitress = ClientManager(request, is_waitress=True) visitor = ClientManager(request, is_waitress=False) if not waitress.true_waitress and not visitor.true_visitor: abort(HTTPStatus.FORBIDDEN, 'No visitor data was provided') action_log_data = get_action_log_data({'waitress_status': 'S'}, 'restaurant_id') if not action_log_data: abort(HTTPStatus.FORBIDDEN, 'Waitress is closed or suspended') client = waitress if waitress.true_waitress else visitor data = { key: val for key, val in client.get_client_data().items() if key in ('ip', 'mac', 'hall', 'table', 'lang') } data.update(action_log_data) data.update({ 'status': 'O', 'datetime_open': datetime.now().strftime(DATE_HMS_FORMAT) }) schema = ORDER.copy() schema.pop('by_cash') v = Validator(schema) [order.update(data) for order in items if v.validate(data)] log_client = LogManager(request) log_client.save_order_log(status='O', cancel_source=None, order_details=items)
def test_objectid_fail(self): schema = {'id': {'type': 'objectid'}} doc = {'id': 'not_an_object_id'} v = Validator(schema, None) self.assertFalse(v.validate(doc)) self.assertTrue('id' in v.errors) self.assertTrue('ObjectId' in v.errors['id'])
def _validate_document(self, data_schema: dict, document: dict) -> bool: v = Validator(data_schema) result = v.validate(document) # TODO collect validation errors # if not result: # print(v.errors) return result
def test_multipoint_success(self): schema = {'location': {'type': 'multipoint'}} doc = {'location': {"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]] } } v = Validator(schema) self.assertTrue(v.validate(doc))
def test_linestring_fail(self): schema = {'location': {'type': 'linestring'}} doc = {'location': {'type': "LineString", 'coordinates': [[12.0, 123.0], [12, 'eve']]}} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('location' in v.errors) self.assertTrue('LineString' in v.errors['location'])
def test_geometrycollection_not_compilant(self): schema = {'location': {'type': 'geometrycollection'}} doc = {'location': {"type": "GeometryCollection", "coordinates": [10.0, 123.0]}} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('location' in v.errors) self.assertTrue('GeometryCollection' in v.errors['location'])
def admin_apps_do(app_name): apps_path = Path(settings.config.APPS_PATH) action = request.json.get('action') if not action in ['enable', 'disable']: abort(400) app_path = apps_path / app_name # Check if app is under apps_path try: app_path.relative_to(apps_path) except ValueError: abort(404) if not app_path.exists(): abort(404) app_db = app.data.pymongo('app').db['app'] if action == 'disable': app_db.remove(dict(name=app_name)) return Response(status=204) elif action == 'enable': schema = settings.config.DOMAIN['app']['schema'] validator = Validator(schema=schema, resource='app') with (app_path / 'manifest.json').open('r') as ifile: data = ifile.read() manifest = json.loads(data) app_config = manifest['app_config'] logo = app_config.pop('logo') existing = app_db.find_one(dict(uappid=app_config['uappid'])) if existing: app_db.remove(dict(uappid=app_config['uappid'])) if not validator.validate(app_config): print("Validation error: ", validator.errors) abort(422, "Invalid app data") app_item = App(**app_config) app_config['logo'] = logo if app_config.get('logo'): try: logofile = (app_path / app_config.get('logo')).open('rb') app_item.logo = logofile except: pass app_item.prepare_data() app_item.save() return Response(status=201) else: abort(404, 'Invalid action')
def test_polygon_fail(self): schema = {'location': {'type': 'polygon'}} doc = {'location': {'type': "Polygon", 'coordinates': [[[12.0, 23.0], [12.3, 12.5]], ["eve"]]}} v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('location' in v.errors) self.assertTrue('Polygon' in v.errors['location'])
def test_dependencies_with_defaults(self): schema = { 'test_field': { 'dependencies': 'foo' }, 'foo': { 'type': 'string', 'default': 'foo' }, 'bar': { 'type': 'string', 'default': 'bar' } } doc = {'test_field': 'foobar'} # With `dependencies` as a str v = Validator(schema) self.assertTrue(v.validate(doc)) # With `dependencies` as a dict schema['test_field'] = {'dependencies': {'foo': 'foo', 'bar': 'bar'}} v = Validator(schema) self.assertTrue(v.validate(doc)) # With `dependencies` as a list schema['test_field'] = {'dependencies': ['foo', 'bar']} v = Validator(schema) self.assertTrue(v.validate(doc))
def test_dependencies_with_defaults(self): schema = { "test_field": { "dependencies": "foo" }, "foo": { "type": "string", "default": "foo" }, "bar": { "type": "string", "default": "bar" }, } doc = {"test_field": "foobar"} # With `dependencies` as a str v = Validator(schema) self.assertTrue(v.validate(doc)) # With `dependencies` as a dict schema["test_field"] = {"dependencies": {"foo": "foo", "bar": "bar"}} v = Validator(schema) self.assertTrue(v.validate(doc)) # With `dependencies` as a list schema["test_field"] = {"dependencies": ["foo", "bar"]} v = Validator(schema) self.assertTrue(v.validate(doc))
def test_polygon_success(self): schema = {'location': {'type': 'polygon'}} doc = {'location': {"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]] ] } } v = Validator(schema) self.assertTrue(v.validate(doc))
def test_featurecollection_fail(self): schema = {'locations': {'type': 'featurecollection'}} doc = {"locations": {"type": "FeatureCollection", "geometry": {"type": "Point", "coordinates": [100.0, 0.0]} } } v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('locations' in v.errors) self.assertTrue('featurecollection' in v.errors['locations'])
def authenticate(): log = logging.getLogger(__name__) '''Authorize user data''' if request.method == 'POST': data = request.get_json() log.debug(f'Data: {data}') schema = current_app.config['DOMAIN']['users']['schema'] validator = Validator() validator.resource = 'users' if not validator.validate(data, schema=schema): return jsonify({'_error': validator.errors}), 500 # Create fields data['_updated'] = datetime.now() data['_created'] = datetime.now() data['password'] = salt_password(data['password']) data['token'] = generate_token(data['email'], data['password']) data['_etag'] = generate_token(data, data) data['active'] = False data['last_login'] = datetime.now() try: current_app.data.driver.db['users'].insert(data) except Exception as e: return jsonify({'_error': str(e)}), 500 del data['password'] return jsonify(data) auth = request.authorization if auth is None: return jsonify({'error': 'Invalid user/password'}), 401 username = auth.get('username', None) pwd = auth.get('password', None) if username is None or pwd is None: log.error(f'No user or pwd: {username} - {pwd}') return jsonify({'error': 'Invalid user/password'}), 401 users_db = current_app.data.driver.db['users'] u = users_db.find_one({'username': username}) if u is None or not check_password(pwd, u['password']): log.error(f'Invalid u or pwd: {username} @ {pwd}') return jsonify({'error': 'Invalid user/password'}), 401 dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S') try: users_db.update_one({'_id': u['_id']}, {'$set': {'last_login': dt}}) except: log.exception( f'Could not update last_login time for user {u["username"]}') del u['password'] u['last_login'] = dt return jsonify(u)
def test_simple_endpoint_validation(): """ Test that the endpoint schema generated will validate when used in Eve. :return: """ eg = EveGenie(data=simple_test_data) data = OrderedDict(eg) for endpoint in data: v = Validator(data[endpoint]['schema']) assert (v.validate(simple_test_data[endpoint]))
def test_geometrycollection_fail(self): schema = {'locations': {'type': 'geometrycollection'}} doc = {'locations': {'type': "GeometryCollection", "geometries": [{"type": "GeoJSON", "badinput": "lolololololol"}] } } v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('locations' in v.errors) self.assertTrue('GeometryCollection' in v.errors['locations'])
def test_simple_endpoint_validation(): """ Test that the endpoint schema generated will validate when used in Eve. :return: """ eg = EveGenie(data=simple_test_data) data = OrderedDict(eg) for endpoint in data: v = Validator(data[endpoint]['schema']) assert(v.validate(simple_test_data[endpoint]))
def test_geometrycollection_not_compilant(self): schema = {"location": {"type": "geometrycollection"}} doc = { "location": { "type": "GeometryCollection", "coordinates": [10.0, 123.0] } } v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue("location" in v.errors) self.assertTrue("geometrycollection" in v.errors["location"])
def test_featurecollection_success(self): schema = {'locations': {'type': 'featurecollection'}} doc = {"locations": {"type": "FeatureCollection", "features": [ {"type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]} }] } } v = Validator(schema) self.assertTrue(v.validate(doc))
def test_polygon_fail(self): schema = {"location": {"type": "polygon"}} doc = { "location": { "type": "Polygon", "coordinates": [[[12.0, 23.0], [12.3, 12.5]], ["eve"]], } } v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue("location" in v.errors) self.assertTrue("polygon" in v.errors["location"])
def test_geometrycollection_fail(self): schema = {"locations": {"type": "geometrycollection"}} doc = { "locations": { "type": "GeometryCollection", "geometries": [{"type": "GeoJSON", "badinput": "lolololololol"}], } } v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue("locations" in v.errors) self.assertTrue("geometrycollection" in v.errors["locations"])
def test_featurecollection_fail(self): schema = {"locations": {"type": "featurecollection"}} doc = { "locations": { "type": "FeatureCollection", "geometry": {"type": "Point", "coordinates": [100.0, 0.0]}, } } v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue("locations" in v.errors) self.assertTrue("featurecollection" in v.errors["locations"])
def test_linestring_fail(self): schema = {"location": {"type": "linestring"}} doc = { "location": { "type": "LineString", "coordinates": [[12.0, 123.0], [12, "eve"]], } } v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue("location" in v.errors) self.assertTrue("linestring" in v.errors["location"])
def test_feature_success(self): schema = {'locations': {'type': 'feature'}} doc = {"locations": {"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]} } } v = Validator(schema) self.assertTrue(v.validate(doc))
def test_geometrycollection_success(self): schema = {"locations": {"type": "geometrycollection"}} doc = { "locations": { "type": "GeometryCollection", "geometries": [ {"type": "Point", "coordinates": [100.0, 0.0]}, {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]}, ], } } v = Validator(schema) self.assertTrue(v.validate(doc))
def test_multilinestring_success(self): schema = {"location": {"type": "multilinestring"}} doc = { "location": { "type": "MultiLineString", "coordinates": [ [[100.0, 0.0], [101.0, 1.0]], [[102.0, 2.0], [103.0, 3.0]], ], } } v = Validator(schema) self.assertTrue(v.validate(doc))
def test_feature_fail(self): schema = {'locations': {'type': 'feature'}} doc = {"locations": {"type": "Feature", "geometries": [{"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 0.0]]]}] } } v = Validator(schema) self.assertFalse(v.validate(doc)) self.assertTrue('locations' in v.errors) self.assertTrue('feature' in v.errors['locations'])
def test_multipolygon_success(self): schema = {'location': {'type': 'multipolygon'}} doc = {'location': {"type": "MultiPolygon", "coordinates": [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] ] } } v = Validator(schema) self.assertTrue(v.validate(doc))