Skip to content

Commit

Permalink
Deduplicate oapi ssi schema definitions (#1123)
Browse files Browse the repository at this point in the history
* move schemas to common and validate against used code
  • Loading branch information
gerardsn authored May 31, 2022
1 parent ac22658 commit f438251
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 301 deletions.
99 changes: 99 additions & 0 deletions api/ssi_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions api/ssi_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package ssiTypes

import (
"encoding/json"
"testing"
"time"

ssi "github.com/nuts-foundation/go-did"
vcr "github.com/nuts-foundation/nuts-node/vcr/api/v2"
"github.com/stretchr/testify/assert"
)

func TestSsiTypes_VerifiableCredential(t *testing.T) {
t.Run("required fields only", func(t *testing.T) {
remarshallTest(t, createVerifiableCredential(), VerifiableCredential{})
})

t.Run("all fields", func(t *testing.T) {
vc := createVerifiableCredential()
id := ssi.MustParseURI("did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey#c4199b74-0c0a-4e09-a463-6927553e65f5")
vc.ID = &id
expirationDate := time.Now().Add(time.Hour)
vc.ExpirationDate = &expirationDate

remarshallTest(t, vc, VerifiableCredential{})
})
}

func TestSsiTypes_VerifiablePresentation(t *testing.T) {
t.Run("required fields only", func(t *testing.T) {
remarshallTest(t, createVerifiablePresentation(), VerifiableCredential{})
})

t.Run("all fields", func(t *testing.T) {
id := ssi.MustParseURI("did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey#c4199b74-0c0a-4e09-a463-6927553e65f5")
holder := ssi.MustParseURI("did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey")

vp := createVerifiablePresentation()
vp.ID = &id
vp.Holder = &holder
vp.VerifiableCredential = []vcr.VerifiableCredential{createVerifiableCredential()}
vp.Proof = []interface{}{"because"}

remarshallTest(t, vp, VerifiablePresentation{})
})
}

func createVerifiableCredential() vcr.VerifiableCredential {
return vcr.VerifiableCredential{
Context: []ssi.URI{ssi.MustParseURI("https://www.w3.org/2018/credentials/v1")},
Type: []ssi.URI{
ssi.MustParseURI("NutsOrganizationCredential"),
ssi.MustParseURI("VerifiableCredential"),
},
Issuer: ssi.MustParseURI("did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey"),
IssuanceDate: time.Now(),
CredentialSubject: []interface{}{"subject"},
Proof: []interface{}{"because"},
}
}

func createVerifiablePresentation() vcr.VerifiablePresentation {
return vcr.VerifiablePresentation{
Context: []ssi.URI{
ssi.MustParseURI("https://www.w3.org/2018/credentials/v1"),
ssi.MustParseURI("https://nuts.nl/credentials/v1"),
},
Type: []ssi.URI{ssi.MustParseURI("VerifiablePresentation")},
}
}

func remarshallTest(t *testing.T, source, target any) {
jsonSource, err := json.Marshal(source)
if !assert.NoError(t, err) {
return
}

err = json.Unmarshal(jsonSource, &target)
if !assert.NoError(t, err) {
return
}

jsonTarget, err := json.Marshal(target)
if !assert.NoError(t, err) {
return
}

assert.JSONEq(t, string(jsonSource), string(jsonTarget))
}
29 changes: 7 additions & 22 deletions auth/api/v1/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f438251

Please sign in to comment.