Skip to content

Commit

Permalink
add unit test for credential type
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <[email protected]>
  • Loading branch information
Ryanmtate committed Sep 19, 2024
1 parent 4d8ea71 commit e46ec41
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/core/input_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,33 @@ pub enum ConstraintsLimitDisclosure {
Required,
Preferred,
}

#[cfg(test)]
mod tests {
use crate::core::presentation_definition::PresentationDefinition;

use super::*;

use anyhow::Result;

#[test]
fn test_input_descriptor_credential_type() -> Result<()> {
let definition: PresentationDefinition = serde_json::from_str(include_str!(
"../../tests/presentation-definition/iso.org.18013.5.1.mdl.json"
))?;

let credentials = definition
.input_descriptors()
.iter()
.flat_map(|descriptor| descriptor.constraints().fields())
.filter_map(|field| field.credential_type())
.collect::<Vec<CredentialType>>();

assert_eq!(
credentials.first(),
Some(&"iso.org.18013.5.1.mDL".to_string())
);

Ok(())
}
}
67 changes: 67 additions & 0 deletions tests/presentation-definition/iso.org.18013.5.1.mdl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"id": "mDL_presentation_request",
"input_descriptors": [
{
"id": "mDL_credential",
"name": "Mobile Driving License",
"purpose": "To verify your driving privileges and age.",
"constraints": {
"fields": [
{
"path": ["$.type"],
"filter": {
"type": "array",
"contains": {
"const": "iso.org.18013.5.1.mDL"
}
}
},
{
"path": ["$.credentialSubject.family_name", "$.vc.credentialSubject.family_name"],
"purpose": "We need your family name for identification.",
"filter": {
"type": "string"
}
},
{
"path": ["$.credentialSubject.given_name", "$.vc.credentialSubject.given_name"],
"purpose": "We need your given name for identification.",
"filter": {
"type": "string"
}
},
{
"path": ["$.credentialSubject.birth_date", "$.vc.credentialSubject.birth_date"],
"purpose": "We need your birth date to verify your age.",
"filter": {
"type": "string",
"format": "date"
}
},
{
"path": ["$.credentialSubject.driving_privileges", "$.vc.credentialSubject.driving_privileges"],
"purpose": "We need to verify your driving privileges.",
"filter": {
"type": "array"
}
},
{
"path": ["$.credentialSubject.document_number", "$.vc.credentialSubject.document_number"],
"purpose": "We need your license number for record-keeping.",
"filter": {
"type": "string"
}
},
{
"path": ["$.credentialSubject.expiry_date", "$.vc.credentialSubject.expiry_date"],
"purpose": "We need to check if your license is still valid.",
"filter": {
"type": "string",
"format": "date"
}
}
]
}
}
]
}

0 comments on commit e46ec41

Please sign in to comment.