From 1a9f01409840d7164518dccc407ffc40dd6966ac Mon Sep 17 00:00:00 2001 From: Simon Bihel Date: Tue, 9 Jul 2024 11:55:00 +0100 Subject: [PATCH] Modify tests for unmapped types --- .../claims/crates/vc/src/syntax/credential.rs | 53 +++++++++++++++++++ .../crates/vc/src/v1/syntax/credential.rs | 15 ++++-- .../crates/vc/src/v2/syntax/credential.rs | 15 ++++-- 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/crates/claims/crates/vc/src/syntax/credential.rs b/crates/claims/crates/vc/src/syntax/credential.rs index e93ecd5a1..4bf13fc90 100644 --- a/crates/claims/crates/vc/src/syntax/credential.rs +++ b/crates/claims/crates/vc/src/syntax/credential.rs @@ -102,3 +102,56 @@ where json.expand_with(ld, loader).await } } + +#[cfg(test)] +mod tests { + use ssi_json_ld::{json_ld, ContextLoader, Expandable}; + + use super::*; + + #[async_std::test] + async fn reject_undefined_type_v2() { + let input: AnyJsonCredential = serde_json::from_value(serde_json::json!({ + "@context": [ + "https://www.w3.org/ns/credentials/v2", + { "@vocab": null } + ], + "type": [ + "VerifiableCredential", + "ExampleTestCredential" + ], + "issuer": "did:example:issuer", + "credentialSubject": { + "id": "did:example:subject" + } + })) + .unwrap(); + match input.expand(&ContextLoader::default()).await.unwrap_err() { + JsonLdError::Expansion(json_ld::expansion::Error::InvalidTypeValue) => (), + e => panic!("{:?}", e), + } + } + + #[async_std::test] + async fn reject_undefined_type_v1() { + let input: AnyJsonCredential = serde_json::from_value(serde_json::json!({ + "@context": [ + "https://www.w3.org/2018/credentials/v1", + { "@vocab": null } + ], + "type": [ + "VerifiableCredential", + "ExampleTestCredential" + ], + "issuer": "did:example:issuer", + "credentialSubject": { + "id": "did:example:subject" + } + })) + .unwrap(); + match input.expand(&ContextLoader::default()).await.unwrap_err() { + JsonLdError::Expansion(json_ld::expansion::Error::InvalidTypeValue) => (), + e => panic!("{:?}", e), + } + } +} diff --git a/crates/claims/crates/vc/src/v1/syntax/credential.rs b/crates/claims/crates/vc/src/v1/syntax/credential.rs index d4aac42fa..d8e2ce4bd 100644 --- a/crates/claims/crates/vc/src/v1/syntax/credential.rs +++ b/crates/claims/crates/vc/src/v1/syntax/credential.rs @@ -269,11 +269,13 @@ where #[cfg(test)] mod tests { - use ssi_json_ld::{CompactJsonLd, ContextLoader, Expandable}; + use ssi_json_ld::{json_ld, ContextLoader, Expandable}; + + use super::*; #[async_std::test] async fn reject_undefined_type() { - let input = CompactJsonLd(json_syntax::json!({ + let input: JsonCredential = serde_json::from_value(serde_json::json!({ "@context": [ "https://www.w3.org/2018/credentials/v1", { "@vocab": null } @@ -286,8 +288,11 @@ mod tests { "credentialSubject": { "id": "did:example:subject" } - })); - - assert!(input.expand(&ContextLoader::default()).await.is_err()); + })) + .unwrap(); + match input.expand(&ContextLoader::default()).await.unwrap_err() { + JsonLdError::Expansion(json_ld::expansion::Error::InvalidTypeValue) => (), + e => panic!("{:?}", e), + } } } diff --git a/crates/claims/crates/vc/src/v2/syntax/credential.rs b/crates/claims/crates/vc/src/v2/syntax/credential.rs index 04ef85c08..eda771e68 100644 --- a/crates/claims/crates/vc/src/v2/syntax/credential.rs +++ b/crates/claims/crates/vc/src/v2/syntax/credential.rs @@ -247,11 +247,13 @@ where #[cfg(test)] mod tests { - use ssi_json_ld::{CompactJsonLd, ContextLoader, Expandable}; + use ssi_json_ld::{json_ld, ContextLoader, Expandable}; + + use super::*; #[async_std::test] async fn reject_undefined_type() { - let input = CompactJsonLd(json_syntax::json!({ + let input: JsonCredential = serde_json::from_value(serde_json::json!({ "@context": [ "https://www.w3.org/ns/credentials/v2", { "@vocab": null } @@ -264,8 +266,11 @@ mod tests { "credentialSubject": { "id": "did:example:subject" } - })); - - assert!(input.expand(&ContextLoader::default()).await.is_err()); + })) + .unwrap(); + match input.expand(&ContextLoader::default()).await.unwrap_err() { + JsonLdError::Expansion(json_ld::expansion::Error::InvalidTypeValue) => (), + e => panic!("{:?}", e), + } } }