Skip to content

Commit

Permalink
fixtests
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Nov 23, 2023
1 parent 3503be0 commit a18eace
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions vdr/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 18 additions & 3 deletions vdr/api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()})

Expand All @@ -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,
Expand All @@ -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)

Expand All @@ -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()})

Expand Down
7 changes: 4 additions & 3 deletions vdr/didnuts/ambassador_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions vdr/didnuts/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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())

Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions vdr/didnuts/manipulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand All @@ -160,7 +160,7 @@ func TestManipulator_AddKey(t *testing.T) {
ctx.mockResolver.EXPECT().Resolve(*id, &resolver.ResolveMetadata{AllowDeactivated: true}).Return(&currentDIDDocument, &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)
})
Expand All @@ -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(&currentDIDDocument, &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)
Expand Down

0 comments on commit a18eace

Please sign in to comment.