From a18eaceb2b0e993016c365b4561a1af6cf543c98 Mon Sep 17 00:00:00 2001 From: Rein Krul Date: Thu, 23 Nov 2023 14:32:11 +0100 Subject: [PATCH] fixtests --- vdr/api/v1/api.go | 4 ++-- vdr/api/v1/api_test.go | 21 ++++++++++++++++++--- vdr/didnuts/ambassador_test.go | 7 ++++--- vdr/didnuts/creator_test.go | 6 +++--- vdr/didnuts/manipulator_test.go | 8 ++++---- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/vdr/api/v1/api.go b/vdr/api/v1/api.go index 2d9fbd68b1..f6942e7c4d 100644 --- a/vdr/api/v1/api.go +++ b/vdr/api/v1/api.go @@ -92,10 +92,10 @@ func (a *Wrapper) AddNewVerificationMethod(ctx context.Context, request AddNewVe } var verificationMethodType ssi.KeyType - if request.Body.Type == nil { + if opts.Type == nil { verificationMethodType = ssi.JsonWebKey2020 } else { - verificationMethodType = ssi.KeyType(*request.Body.Type) + verificationMethodType = ssi.KeyType(*opts.Type) } vm, err := a.DocManipulator.AddVerificationMethod(ctx, *d, opts.ToFlags(didnuts.DefaultCreationOptions().KeyFlags), verificationMethodType) if err != nil { diff --git a/vdr/api/v1/api_test.go b/vdr/api/v1/api_test.go index 867af71f8d..44f7d92d4e 100644 --- a/vdr/api/v1/api_test.go +++ b/vdr/api/v1/api_test.go @@ -18,6 +18,7 @@ package v1 import ( "context" "errors" + ssi "github.com/nuts-foundation/go-did" "github.com/nuts-foundation/nuts-node/audit" "github.com/nuts-foundation/nuts-node/vdr" "github.com/nuts-foundation/nuts-node/vdr/didnuts" @@ -372,7 +373,7 @@ func TestWrapper_AddNewVerificationMethod(t *testing.T) { t.Run("ok - without key usage", func(t *testing.T) { ctx := newMockContext(t) - ctx.docUpdater.EXPECT().AddVerificationMethod(ctx.requestCtx, *did123, didnuts.DefaultCreationOptions().KeyFlags).Return(newMethod, nil) + ctx.docUpdater.EXPECT().AddVerificationMethod(ctx.requestCtx, *did123, didnuts.DefaultCreationOptions().KeyFlags, ssi.JsonWebKey2020).Return(newMethod, nil) response, err := ctx.client.AddNewVerificationMethod(ctx.requestCtx, AddNewVerificationMethodRequestObject{Did: did123.String()}) @@ -383,7 +384,7 @@ func TestWrapper_AddNewVerificationMethod(t *testing.T) { t.Run("ok - with key usage", func(t *testing.T) { ctx := newMockContext(t) expectedKeyUsage := didnuts.DefaultCreationOptions().KeyFlags | management.AuthenticationUsage | management.CapabilityDelegationUsage - ctx.docUpdater.EXPECT().AddVerificationMethod(ctx.requestCtx, *did123, expectedKeyUsage).Return(newMethod, nil) + ctx.docUpdater.EXPECT().AddVerificationMethod(ctx.requestCtx, *did123, expectedKeyUsage, ssi.JsonWebKey2020).Return(newMethod, nil) trueBool := true request := AddNewVerificationMethodJSONRequestBody{ Authentication: &trueBool, @@ -396,6 +397,20 @@ func TestWrapper_AddNewVerificationMethod(t *testing.T) { assert.Equal(t, *newMethod, VerificationMethod(response.(AddNewVerificationMethod200JSONResponse))) }) + t.Run("ok - with key type", func(t *testing.T) { + ctx := newMockContext(t) + ctx.docUpdater.EXPECT().AddVerificationMethod(ctx.requestCtx, *did123, gomock.Any(), ssi.RSAVerificationKey2018).Return(newMethod, nil) + keyType := string(ssi.RSAVerificationKey2018) + request := AddNewVerificationMethodJSONRequestBody{ + Type: &keyType, + } + + response, err := ctx.client.AddNewVerificationMethod(ctx.requestCtx, AddNewVerificationMethodRequestObject{Did: did123.String(), Body: &request}) + + assert.NoError(t, err) + assert.Equal(t, *newMethod, VerificationMethod(response.(AddNewVerificationMethod200JSONResponse))) + }) + t.Run("error - invalid did", func(t *testing.T) { ctx := newMockContext(t) @@ -408,7 +423,7 @@ func TestWrapper_AddNewVerificationMethod(t *testing.T) { t.Run("error - internal error", func(t *testing.T) { ctx := newMockContext(t) - ctx.docUpdater.EXPECT().AddVerificationMethod(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("something went wrong")) + ctx.docUpdater.EXPECT().AddVerificationMethod(gomock.Any(), gomock.Any(), gomock.Any(), ssi.JsonWebKey2020).Return(nil, errors.New("something went wrong")) response, err := ctx.client.AddNewVerificationMethod(ctx.requestCtx, AddNewVerificationMethodRequestObject{Did: did123.String()}) diff --git a/vdr/didnuts/ambassador_test.go b/vdr/didnuts/ambassador_test.go index c6d251aeda..9570581259 100644 --- a/vdr/didnuts/ambassador_test.go +++ b/vdr/didnuts/ambassador_test.go @@ -27,6 +27,7 @@ import ( "encoding/json" "errors" "fmt" + ssi "github.com/nuts-foundation/go-did" "github.com/nuts-foundation/nuts-node/audit" "github.com/nuts-foundation/nuts-node/network" "github.com/nuts-foundation/nuts-node/vdr/management" @@ -53,7 +54,7 @@ type mockKeyCreator struct { } // New creates a new valid key with the correct KID -func (m *mockKeyCreator) New(_ context.Context, fn crypto.KIDNamingFunc) (crypto.Key, error) { +func (m *mockKeyCreator) New(_ context.Context, _ crypto.KeyType, fn crypto.KIDNamingFunc) (crypto.Key, error) { if m.key == nil { privateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) kid, _ := fn(privateKey.Public()) @@ -425,7 +426,7 @@ func TestAmbassador_handleUpdateDIDDocument(t *testing.T) { currentDoc, signingKey, _ := newDidDoc() newDoc := did.Document{Context: []interface{}{did.DIDContextV1URI()}, ID: currentDoc.ID} - newCapInv, _ := CreateNewVerificationMethodForDID(audit.TestContext(), currentDoc.ID, &mockKeyCreator{}) + newCapInv, _ := CreateNewVerificationMethodForDID(audit.TestContext(), currentDoc.ID, ssi.JsonWebKey2020, &mockKeyCreator{}) newDoc.AddCapabilityInvocation(newCapInv) didDocPayload, _ := json.Marshal(newDoc) @@ -460,7 +461,7 @@ func TestAmbassador_handleUpdateDIDDocument(t *testing.T) { currentDoc, signingKey, _ := newDidDoc() newDoc := did.Document{Context: []interface{}{did.DIDContextV1URI()}, ID: currentDoc.ID} - newCapInv, _ := CreateNewVerificationMethodForDID(audit.TestContext(), currentDoc.ID, &mockKeyCreator{}) + newCapInv, _ := CreateNewVerificationMethodForDID(audit.TestContext(), currentDoc.ID, ssi.JsonWebKey2020, &mockKeyCreator{}) newDoc.AddCapabilityInvocation(newCapInv) didDocPayload, _ := json.Marshal(newDoc) diff --git a/vdr/didnuts/creator_test.go b/vdr/didnuts/creator_test.go index 9eb4cec779..71401580da 100644 --- a/vdr/didnuts/creator_test.go +++ b/vdr/didnuts/creator_test.go @@ -112,7 +112,7 @@ func TestCreator_Create(t *testing.T) { keyCreator := nutsCrypto.NewMockKeyCreator(ctrl) creator := Creator{KeyStore: keyCreator} - keyCreator.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, fn nutsCrypto.KIDNamingFunc) (nutsCrypto.Key, error) { + keyCreator.EXPECT().New(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, fn nutsCrypto.KIDNamingFunc) (nutsCrypto.Key, error) { key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) keyName, _ := fn(key.Public()) return nutsCrypto.TestKey{ @@ -161,7 +161,7 @@ func TestCreator_Create(t *testing.T) { ctrl := gomock.NewController(t) mockKeyStore := nutsCrypto.NewMockKeyStore(ctrl) creator := Creator{KeyStore: mockKeyStore} - mockKeyStore.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil, errors.New("b00m!")) + mockKeyStore.EXPECT().New(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("b00m!")) _, _, err := creator.Create(nil, DefaultCreationOptions()) @@ -176,7 +176,7 @@ func TestCreator_Create(t *testing.T) { KeyFlags: management.AssertionMethodUsage, SelfControl: false, } - mockKeyStore.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil, errors.New("b00m!")) + mockKeyStore.EXPECT().New(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("b00m!")) _, _, err := creator.Create(nil, ops) diff --git a/vdr/didnuts/manipulator_test.go b/vdr/didnuts/manipulator_test.go index 362561894d..6e85eb1882 100644 --- a/vdr/didnuts/manipulator_test.go +++ b/vdr/didnuts/manipulator_test.go @@ -115,7 +115,7 @@ func TestManipulator_CreateNewAuthenticationMethodForDID(t *testing.T) { t.Run("ok", func(t *testing.T) { // Prepare a document with an authenticationMethod: document := &did.Document{ID: *id123} - method, err := CreateNewVerificationMethodForDID(audit.TestContext(), document.ID, kc) + method, err := CreateNewVerificationMethodForDID(audit.TestContext(), document.ID, ssi.JsonWebKey2020, kc) require.NoError(t, err) document.AddCapabilityInvocation(method) @@ -141,7 +141,7 @@ func TestManipulator_AddKey(t *testing.T) { updatedDocument = doc }) - key, err := ctx.manipulator.AddVerificationMethod(ctx.audit, *id, management.AuthenticationUsage) + key, err := ctx.manipulator.AddVerificationMethod(ctx.audit, *id, management.AuthenticationUsage, ssi.JsonWebKey2020) require.NoError(t, err) assert.NotNil(t, key) assert.Equal(t, key.Controller, *id, @@ -160,7 +160,7 @@ func TestManipulator_AddKey(t *testing.T) { ctx.mockResolver.EXPECT().Resolve(*id, &resolver.ResolveMetadata{AllowDeactivated: true}).Return(¤tDIDDocument, &resolver.DocumentMetadata{}, nil) ctx.mockUpdater.EXPECT().Update(ctx.audit, *id, gomock.Any()).Return(resolver.ErrNotFound) - key, err := ctx.manipulator.AddVerificationMethod(ctx.audit, *id, 0) + key, err := ctx.manipulator.AddVerificationMethod(ctx.audit, *id, 0, ssi.JsonWebKey2020) assert.ErrorIs(t, err, resolver.ErrNotFound) assert.Nil(t, key) }) @@ -171,7 +171,7 @@ func TestManipulator_AddKey(t *testing.T) { currentDIDDocument := did.Document{ID: *id, Controller: []did.DID{*id}} ctx.mockResolver.EXPECT().Resolve(*id, &resolver.ResolveMetadata{AllowDeactivated: true}).Return(¤tDIDDocument, &resolver.DocumentMetadata{Deactivated: true}, nil) - key, err := ctx.manipulator.AddVerificationMethod(nil, *id, 0) + key, err := ctx.manipulator.AddVerificationMethod(nil, *id, 0, ssi.JsonWebKey2020) assert.ErrorIs(t, err, resolver.ErrDeactivated) assert.Nil(t, key)