Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Dec 9, 2023
1 parent ec5d519 commit edc9645
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 45 deletions.
4 changes: 2 additions & 2 deletions vcr/pe/presentation_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
89 changes: 46 additions & 43 deletions vcr/pe/presentation_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"crypto/rand"
"embed"
"encoding/json"
"fmt"
"github.com/nuts-foundation/nuts-node/vcr/credential"
"testing"

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
})
}
}
3 changes: 3 additions & 0 deletions vcr/pe/test/pd_jsonld_jwt.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,6 +17,7 @@
}
},
{
"id": "credentialsubject_organization_name",
"path": [
"$.credentialSubject.organization.name",
"$.credentialSubject[0].organization.name"
Expand Down

0 comments on commit edc9645

Please sign in to comment.