def test_validate_context_pass_context_single_name_value(): input_dict = { "@context": { "favoriteColor": "https://example.com/vocab#favoriteColor" } } ContextHandler._validate_context(input_dict)
def test_static_validation_fail_context_is_list_with_dict_and_bad_uri(): context = [{ "favoriteColor": "https://example.com/vocab#favoriteColor" }, "this is a bad uri"] operation = { META: { "name": "TestContext", "version": 1, "type": CONTEXT_TYPE }, DATA: { CONTEXT_CONTEXT: context }, RS_TYPE: "200" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) with pytest.raises(InvalidClientRequest) as e: ch.static_validation(req) assert "@context URI this is a bad uri badly formed" in str(e.value)
def test_validate_context_fail_no_context_property(): input_dict = { "name": "Thing" } with pytest.raises(Exception) as e: ContextHandler._validate_context(input_dict) assert "Context missing '@context' property" in str(e.value)
def test_validate_context_fail_context_not_dict_or_list(): input_dict = { "@context": "Thing" } with pytest.raises(Exception) as e: ContextHandler._validate_context(input_dict) assert "'@context' value must be list or dict" in str(e.value)
def test_static_validation_pass_context_w3c_example_15(): context = { "@context": { "referenceNumber": "https://example.com/vocab#referenceNumber", "favoriteFood": "https://example.com/vocab#favoriteFood" } } operation = { META: { "name": "TestContext", "version": 1, "type": CONTEXT_TYPE }, DATA: { CONTEXT_CONTEXT: context }, RS_TYPE: "200" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) ch.static_validation(req)
def test_validate_context_pass_context_w3c_example_15(): input_dict = { "@context": { "referenceNumber": "https://example.com/vocab#referenceNumber", "favoriteFood": "https://example.com/vocab#favoriteFood" } } ContextHandler._validate_context(input_dict)
def make_context_exist(context_request, context_handler): identifier, req_id, operation = get_request_data(context_request) context_name = get_write_context_name(context_request) context_version = get_write_context_version(context_request) path = ContextHandler.make_state_path_for_context(identifier, context_name, context_version) context_handler.state.set(path, encode_state_value("value", "seqNo", "txnTime"))
def test_static_validation_fail_invalid_type(): operation = { "meta": { "type": "context", "name": "TestContext", "version": 1 }, "data": W3C_BASE_CONTEXT, "type": "201" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) with pytest.raises(LogicError): ch.static_validation(req)
def test_update_state(context_request, context_handler): seq_no = 1 txn_time = 1560241033 txn = reqToTxn(context_request) append_txn_metadata(txn, seq_no, txn_time) path, value_bytes = ContextHandler.prepare_context_for_state(txn) value = {META: get_txn_context_meta(txn), DATA: get_txn_context_data(txn)} context_handler.update_state(txn, None, context_request) assert context_handler.get_from_state(path) == (value, seq_no, txn_time)
def test_static_validation_pass_context_w3c_examples_v1(): # test for https://www.w3.org/2018/credentials/examples/v1 operation = { META: { "name": "TestContext", "version": 1, "type": CONTEXT_TYPE }, DATA: { CONTEXT_CONTEXT: W3C_EXAMPLE_V1_CONTEXT }, RS_TYPE: "200" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) ch.static_validation(req)
def test_static_validation_pass_context_value_is_dict(): context = {"favoriteColor": "https://example.com/vocab#favoriteColor"} operation = { META: { "name": "TestContext", "version": 1, "type": CONTEXT_TYPE }, DATA: { CONTEXT_CONTEXT: context }, RS_TYPE: "200" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) ch.static_validation(req)
def test_static_validation_pass_context_w3c_base(): # Sample from specification: https://w3c.github.io/vc-data-model/#base-context # Actual file contents from: https://www.w3.org/2018/credentials/v1 operation = { META: { "name": "TestContext", "version": 1, "type": CONTEXT_TYPE }, DATA: { CONTEXT_CONTEXT: W3C_BASE_CONTEXT }, RS_TYPE: "200" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) ch.static_validation(req)
def test_static_validation_pass_context_value_is_list_with_dict_and_uri(): context = [{ "favoriteColor": "https://example.com/vocab#favoriteColor" }, "https://www.w3.org/ns/odrl.jsonld"] operation = { META: { "name": "TestContext", "version": 1, "type": CONTEXT_TYPE }, DATA: { CONTEXT_CONTEXT: context }, RS_TYPE: "200" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) ch.static_validation(req)
def test_static_validation_fail_context_not_uri_or_array_or_object(): context = 52 operation = { META: { "name": "TestContext", "version": 1, "type": CONTEXT_TYPE }, DATA: { CONTEXT_CONTEXT: context }, RS_TYPE: "200" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) with pytest.raises(InvalidClientRequest) as e: ch.static_validation(req) assert "'@context' value must be url, array, or object" in str(e.value)
def test_static_validation_context_fail_bad_uri(): context = "2http:/..@#$" operation = { META: { "name": "TestContext", "version": 1, "type": CONTEXT_TYPE }, DATA: { CONTEXT_CONTEXT: context }, RS_TYPE: "200" } req = Request( "test", 1, operation, "sig", ) ch = ContextHandler(None, None) with pytest.raises(InvalidClientRequest) as e: ch.static_validation(req) assert "@context URI 2http:/..@#$ badly formed" in str(e.value)
def get_context(self, author: str, context_name: str, context_version: str, is_committed=True, with_proof=True) -> (str, int, int, list): assert author is not None assert context_name is not None assert context_version is not None path = ContextHandler.make_state_path_for_context(author, context_name, context_version) try: keys, seq_no, last_update_time, proof = self.lookup(path, is_committed, with_proof=with_proof) return keys, seq_no, last_update_time, proof except KeyError: return None, None, None, None
def register_domain_req_handlers(self): # Read handlers get_nym_handler = GetNymHandler(database_manager=self.node.db_manager) get_attribute_handler = GetAttributeHandler(database_manager=self.node.db_manager) get_schema_handler = GetSchemaHandler(database_manager=self.node.db_manager) get_context_handler = GetContextHandler(database_manager=self.node.db_manager) get_claim_def_handler = GetClaimDefHandler(database_manager=self.node.db_manager) get_revoc_reg_def_handler = GetRevocRegDefHandler(database_manager=self.node.db_manager) get_revoc_reg_handler = GetRevocRegHandler(database_manager=self.node.db_manager) get_revoc_reg_delta_handler = GetRevocRegDeltaHandler(database_manager=self.node.db_manager, get_revocation_strategy=RevocRegDefHandler.get_revocation_strategy) # Write handlers nym_handler = NymHandler(config=self.node.config, database_manager=self.node.db_manager, write_req_validator=self.node.write_req_validator) attrib_handler = AttributeHandler(database_manager=self.node.db_manager, write_req_validator=self.node.write_req_validator) schema_handler = SchemaHandler(database_manager=self.node.db_manager, write_req_validator=self.node.write_req_validator) context_handler = ContextHandler(database_manager=self.node.db_manager, write_req_validator=self.node.write_req_validator) claim_def_handler = ClaimDefHandler(database_manager=self.node.db_manager, write_req_validator=self.node.write_req_validator) revoc_reg_def_handler = RevocRegDefHandler(database_manager=self.node.db_manager, write_req_validator=self.node.write_req_validator) revoc_reg_entry_handler = RevocRegEntryHandler(database_manager=self.node.db_manager, write_req_validator=self.node.write_req_validator, get_revocation_strategy=RevocRegDefHandler.get_revocation_strategy) # Register write handlers self.node.write_manager.register_req_handler(nym_handler) self.node.write_manager.register_req_handler(attrib_handler) self.node.write_manager.register_req_handler(schema_handler) self.node.write_manager.register_req_handler(context_handler) self.node.write_manager.register_req_handler(claim_def_handler) self.node.write_manager.register_req_handler(revoc_reg_def_handler) self.node.write_manager.register_req_handler(revoc_reg_entry_handler) # Additional handler for idCache self.register_idr_cache_nym_handler() # Register read handlers self.node.read_manager.register_req_handler(get_nym_handler) self.node.read_manager.register_req_handler(get_attribute_handler) self.node.read_manager.register_req_handler(get_schema_handler) self.node.read_manager.register_req_handler(get_context_handler) self.node.read_manager.register_req_handler(get_claim_def_handler) self.node.read_manager.register_req_handler(get_revoc_reg_def_handler) self.node.read_manager.register_req_handler(get_revoc_reg_handler) self.node.read_manager.register_req_handler(get_revoc_reg_delta_handler)
def context_handler(db_manager, write_auth_req_validator): return ContextHandler(db_manager, write_auth_req_validator)
def test_validate_context_fail_on_empty(): with pytest.raises(Exception) as e: ContextHandler._validate_context({}) assert "Context missing '@context' property" in str(e.value)
def test_validate_context_pass_context_w3c_examples_v1(): # test for https://www.w3.org/2018/credentials/examples/v1 ContextHandler._validate_context(w3c_example_v1)
def test_validate_context_pass_context_w3c_base(): # pasted directly out of the reference file, without any format changes # change true to True to correct for python # Sample from specification: https://w3c.github.io/vc-data-model/#base-context # Actual file contents from: https://www.w3.org/2018/credentials/v1 ContextHandler._validate_context(w3c_base)