Skip to content

Commit

Permalink
INDY-2340: add dynamic validation of the schema field in the Mapping …
Browse files Browse the repository at this point in the history
…object

Signed-off-by: ashcherbakov <[email protected]>
  • Loading branch information
ashcherbakov committed Mar 18, 2020
1 parent 0100fe4 commit 493cd80
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,30 @@ def do_dynamic_validation_content(self, request):
schema_id = content_as_dict[RS_CRED_DEF_SCHEMA]
mapping_id = content_as_dict[RS_CRED_DEF_MAPPING]

# 1. check that the schema field points to an existing object on the ledger
schema, _, _ = self.get_from_state(schema_id)
if not schema:
raise InvalidClientRequest(request.identifier,
request.reqId,
'Can not find a schema with id={}; please make sure that it has been added to the ledger'.format(
schema_id))

# 2. check that the mapping field points to an existing object on the ledger
mapping, _, _ = self.get_from_state(mapping_id)
if not mapping:
raise InvalidClientRequest(request.identifier,
request.reqId,
'Can not find a mapping with id={}; please make sure that it has been added to the ledger'.format(
mapping_id))

# 3. check that the schema field points to an object of the Schema type
if schema.get(RS_TYPE) != RS_SCHEMA_TYPE_VALUE:
raise InvalidClientRequest(request.identifier,
request.reqId,
"'{}' field must reference a schema with {}={}".format(
RS_CRED_DEF_SCHEMA, RS_TYPE, RS_SCHEMA_TYPE_VALUE))

# 4. check that the mapping fields points to an object of the Mapping type
if mapping.get(RS_TYPE) != RS_MAPPING_TYPE_VALUE:
raise InvalidClientRequest(request.identifier,
request.reqId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from common.serializers.json_serializer import JsonSerializer
from indy_common.authorize.auth_request_validator import WriteRequestValidator

from indy_common.constants import RICH_SCHEMA_MAPPING, RS_MAPPING_SCHEMA, RS_CONTENT
from indy_common.constants import RICH_SCHEMA_MAPPING, RS_MAPPING_SCHEMA, RS_CONTENT, RS_TYPE, RS_SCHEMA_TYPE_VALUE

from indy_node.server.request_handlers.domain_req_handlers.rich_schema.abstract_rich_schema_object_handler import \
AbstractRichSchemaObjectHandler
Expand All @@ -27,4 +28,22 @@ def do_static_validation_content(self, content_as_dict, request):
"{} must be set in {}".format(RS_MAPPING_SCHEMA, RS_CONTENT))

def do_dynamic_validation_content(self, request):
pass
# it has been checked on static validation step that the content is a valid JSON.
# and it has a schema fields
content_as_dict = JsonSerializer.loads(request.operation[RS_CONTENT])

# 1. check that the schema field points to an existing object on the ledger
schema_id = content_as_dict[RS_MAPPING_SCHEMA]
schema, _, _ = self.get_from_state(schema_id)
if not schema:
raise InvalidClientRequest(request.identifier,
request.reqId,
'Can not find a schema with id={}; please make sure that it has been added to the ledger'.format(
schema_id))

# 2. check that the schema field points to an object of the Schema type
if schema.get(RS_TYPE) != RS_SCHEMA_TYPE_VALUE:
raise InvalidClientRequest(request.identifier,
request.reqId,
"'{}' field must reference a schema with {}={}".format(
RS_MAPPING_SCHEMA, RS_TYPE, RS_SCHEMA_TYPE_VALUE))
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def test_dynamic_validation_not_schema_in_schema_field(cred_def_handler, cred_de
content[RS_CRED_DEF_MAPPING] = mapping_req.operation[RS_ID]
cred_def_req.operation[RS_CONTENT] = json.dumps(content)
with pytest.raises(InvalidClientRequest,
match="'schema' field must reference a schema with rsType=sch".format(
RS_CRED_DEF_SCHEMA)):
match="'schema' field must reference a schema with rsType=sch"):
cred_def_handler.dynamic_validation(cred_def_req, 0)

def test_dynamic_validation_not_mapping_in_mapping_field(cred_def_handler, cred_def_req,
Expand All @@ -152,6 +151,5 @@ def test_dynamic_validation_not_mapping_in_mapping_field(cred_def_handler, cred_
cred_def_req.operation[RS_CONTENT] = json.dumps(content)

with pytest.raises(InvalidClientRequest,
match="'mapping' field must reference a mapping with rsType=map".format(
RS_CRED_DEF_MAPPING)):
match="'mapping' field must reference a mapping with rsType=map"):
cred_def_handler.dynamic_validation(cred_def_req, 0)
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_schema_dynamic_validation_passes(mapping_handler, mapping_req,

make_rich_schema_object_exist(rich_schema_handler, rich_schema_req)

content = copy.deepcopy(json.loads(mapping_handler.operation[RS_CONTENT]))
content = copy.deepcopy(json.loads(mapping_req.operation[RS_CONTENT]))
content[RS_MAPPING_SCHEMA] = rich_schema_req.operation[RS_ID]
mapping_req.operation[RS_CONTENT] = json.dumps(content)

Expand All @@ -139,11 +139,29 @@ def test_dynamic_validation_not_existent_schema(mapping_handler, mapping_req,
make_rich_schema_object_exist(rich_schema_handler, rich_schema_req)

schema_id = randomString()
content = copy.deepcopy(json.loads(mapping_handler.operation[RS_CONTENT]))
content = copy.deepcopy(json.loads(mapping_req.operation[RS_CONTENT]))
content[RS_MAPPING_SCHEMA] = schema_id
mapping_req.operation[RS_CONTENT] = json.dumps(content)

with pytest.raises(InvalidClientRequest,
match='Can not find a schema with id={}; please make sure that it has been added to the ledger'.format(
schema_id)):
mapping_handler.dynamic_validation(mapping_req, 0)


def test_dynamic_validation_not_schema_in_schema_field(mapping_handler, mapping_req,
encoding_handler, encoding_req,
rich_schema_handler, rich_schema_req):
add_to_idr(mapping_handler.database_manager.idr_cache, mapping_req.identifier, TRUSTEE)
add_to_idr(mapping_handler.database_manager.idr_cache, mapping_req.endorser, ENDORSER)

make_rich_schema_object_exist(rich_schema_handler, rich_schema_req)
make_rich_schema_object_exist(encoding_handler, encoding_req)

content = copy.deepcopy(json.loads(mapping_req.operation[RS_CONTENT]))
content[RS_MAPPING_SCHEMA] = encoding_req.operation[RS_ID]
mapping_req.operation[RS_CONTENT] = json.dumps(content)

with pytest.raises(InvalidClientRequest,
match="'schema' field must reference a schema with rsType=sch"):
mapping_handler.dynamic_validation(mapping_req, 0)

0 comments on commit 493cd80

Please sign in to comment.