From edc9645666468e6af9e6777f1f999541c21e845d Mon Sep 17 00:00:00 2001 From: Rein Krul Date: Sat, 9 Dec 2023 13:22:41 +0100 Subject: [PATCH] test --- vcr/pe/presentation_definition.go | 4 +- vcr/pe/presentation_definition_test.go | 89 +++++++++++++------------- vcr/pe/test/pd_jsonld_jwt.json | 3 + 3 files changed, 51 insertions(+), 45 deletions(-) diff --git a/vcr/pe/presentation_definition.go b/vcr/pe/presentation_definition.go index e6e72bd9d9..d4cd38912f 100644 --- a/vcr/pe/presentation_definition.go +++ b/vcr/pe/presentation_definition.go @@ -77,7 +77,7 @@ func (presentationDefinition PresentationDefinition) Match(vcs []vc.VerifiableCr // to the corresponding value from the Verifiable Credentials that map to the InputDescriptor. // The credentialMap is a map with the InputDescriptor.Id as key and the VerifiableCredential as value. // Constraints that contain no ID are ignored. -func (presentationDefinition PresentationDefinition) ResolveConstraintsFields(credentialMap map[string]vc.VerifiableCredential) (map[string]interface{}, error) { +func (presentationDefinition PresentationDefinition) ResolveConstraintsFields(credentialMap map[string]vc.VerifiableCredential) map[string]interface{} { result := make(map[string]interface{}) for inputDescriptorID, cred := range credentialMap { // Find the input descriptor @@ -96,7 +96,7 @@ func (presentationDefinition PresentationDefinition) ResolveConstraintsFields(cr result[key] = value } } - return result, nil + return result } func (presentationDefinition PresentationDefinition) matchConstraints(vcs []vc.VerifiableCredential) ([]Candidate, error) { diff --git a/vcr/pe/presentation_definition_test.go b/vcr/pe/presentation_definition_test.go index 4172c32ec6..403cff972a 100644 --- a/vcr/pe/presentation_definition_test.go +++ b/vcr/pe/presentation_definition_test.go @@ -24,7 +24,6 @@ import ( "crypto/rand" "embed" "encoding/json" - "fmt" "github.com/nuts-foundation/nuts-node/vcr/credential" "testing" @@ -671,6 +670,52 @@ func Test_matchFilter(t *testing.T) { }) } +func TestPresentationDefinition_ResolveConstraintsFields(t *testing.T) { + jwtCredential := credential.JWTNutsOrganizationCredential(t) + jsonldCredential := credential.JWTNutsOrganizationCredential(t) + definition := definitions().JSONLDorJWT + t.Run("match JWT", func(t *testing.T) { + credentialMap := map[string]vc.VerifiableCredential{ + "organization_credential": jwtCredential, + } + + fieldValues := definition.ResolveConstraintsFields(credentialMap) + + require.Len(t, fieldValues, 2) + assert.Equal(t, "IJbergen", fieldValues["credentialsubject_organization_city"]) + assert.Equal(t, "care", fieldValues["credentialsubject_organization_name"]) + }) + t.Run("match JSON-LD", func(t *testing.T) { + credentialMap := map[string]vc.VerifiableCredential{ + "organization_credential": jsonldCredential, + } + + fieldValues := definition.ResolveConstraintsFields(credentialMap) + + require.Len(t, fieldValues, 2) + assert.Equal(t, "IJbergen", fieldValues["credentialsubject_organization_city"]) + assert.Equal(t, "care", fieldValues["credentialsubject_organization_name"]) + }) + t.Run("input descriptor without constraints", func(t *testing.T) { + format := PresentationDefinitionClaimFormatDesignations(map[string]map[string][]string{"jwt_vc": {"alg": {"ES256"}}}) + definition := PresentationDefinition{ + InputDescriptors: []*InputDescriptor{ + { + Id: "any_credential", + Format: &format, + }, + }, + } + credentialMap := map[string]vc.VerifiableCredential{ + "any_credential": jwtCredential, + } + + fieldValues := definition.ResolveConstraintsFields(credentialMap) + + assert.Empty(t, fieldValues) + }) +} + func credentialToJSONLD(credential vc.VerifiableCredential) vc.VerifiableCredential { bytes, err := credential.MarshalJSON() if err != nil { @@ -683,45 +728,3 @@ func credentialToJSONLD(credential vc.VerifiableCredential) vc.VerifiableCredent } return result } - -func TestPresentationDefinition_ResolveConstraintsFields(t *testing.T) { - type fields struct { - Format *PresentationDefinitionClaimFormatDesignations - Frame *Frame - Id string - InputDescriptors []*InputDescriptor - Name string - Purpose *string - SubmissionRequirements []*SubmissionRequirement - } - type args struct { - credentialMap map[string]vc.VerifiableCredential - } - tests := []struct { - name string - fields fields - args args - want map[string]interface{} - wantErr assert.ErrorAssertionFunc - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - presentationDefinition := PresentationDefinition{ - Format: tt.fields.Format, - Frame: tt.fields.Frame, - Id: tt.fields.Id, - InputDescriptors: tt.fields.InputDescriptors, - Name: tt.fields.Name, - Purpose: tt.fields.Purpose, - SubmissionRequirements: tt.fields.SubmissionRequirements, - } - got, err := presentationDefinition.ResolveConstraintsFields(tt.args.credentialMap) - if !tt.wantErr(t, err, fmt.Sprintf("ResolveConstraintsFields(%v)", tt.args.credentialMap)) { - return - } - assert.Equalf(t, tt.want, got, "ResolveConstraintsFields(%v)", tt.args.credentialMap) - }) - } -} diff --git a/vcr/pe/test/pd_jsonld_jwt.json b/vcr/pe/test/pd_jsonld_jwt.json index 45f77f383a..5c95549874 100644 --- a/vcr/pe/test/pd_jsonld_jwt.json +++ b/vcr/pe/test/pd_jsonld_jwt.json @@ -2,9 +2,11 @@ "id": "Definition requesting NutsOrganizationCredential", "input_descriptors": [ { + "id": "organization_credential", "constraints": { "fields": [ { + "id": "credentialsubject_organization_city", "path": [ "$.credentialSubject.organization.city", "$.credentialSubject[0].organization.city" @@ -15,6 +17,7 @@ } }, { + "id": "credentialsubject_organization_name", "path": [ "$.credentialSubject.organization.name", "$.credentialSubject[0].organization.name"