Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Adding multi db support to tests (#536)
Browse files Browse the repository at this point in the history
* adding multi db support

* remove comment

* updating all tests

* updates

---------

Co-authored-by: Gabe <[email protected]>
  • Loading branch information
nitro-neal and decentralgabe authored Jun 13, 2023
1 parent 32f3ad8 commit c8541a8
Show file tree
Hide file tree
Showing 22 changed files with 6,842 additions and 6,738 deletions.
2,001 changes: 1,004 additions & 997 deletions pkg/server/router/credential_test.go

Large diffs are not rendered by default.

414 changes: 213 additions & 201 deletions pkg/server/router/did_test.go

Large diffs are not rendered by default.

133 changes: 69 additions & 64 deletions pkg/server/router/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/TBD54566975/ssi-sdk/crypto"
"github.com/mr-tron/base58"
"github.com/stretchr/testify/assert"
"github.com/tbd54566975/ssi-service/pkg/testutil"

"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/pkg/service/framework"
Expand All @@ -29,69 +30,73 @@ func TestKeyStoreRouter(t *testing.T) {
assert.Contains(tt, err.Error(), "could not create key store router with service type: test")
})

t.Run("Key Store Service Test", func(tt *testing.T) {
bolt := setupTestDB(tt)
assert.NotEmpty(tt, bolt)

serviceConfig := config.KeyStoreServiceConfig{
BaseServiceConfig: &config.BaseServiceConfig{Name: "keystore"},
MasterKeyPassword: "test-password",
}
keyStoreService, err := keystore.NewKeyStoreService(serviceConfig, bolt)
assert.NoError(tt, err)
assert.NotEmpty(tt, keyStoreService)

// check type and status
assert.Equal(tt, framework.KeyStore, keyStoreService.Type())
assert.Equal(tt, framework.StatusReady, keyStoreService.Status().Status)

// store an invalid key type
err = keyStoreService.StoreKey(context.Background(), keystore.StoreKeyRequest{
ID: "test-kid",
Type: "bad",
Controller: "me",
PrivateKeyBase58: base58.Encode([]byte("bad")),
for _, test := range testutil.TestDatabases {
t.Run(test.Name, func(t *testing.T) {
t.Run("Key Store Service Test", func(tt *testing.T) {
db := test.ServiceStorage(t)
assert.NotEmpty(tt, db)

serviceConfig := config.KeyStoreServiceConfig{
BaseServiceConfig: &config.BaseServiceConfig{Name: "keystore"},
MasterKeyPassword: "test-password",
}
keyStoreService, err := keystore.NewKeyStoreService(serviceConfig, db)
assert.NoError(tt, err)
assert.NotEmpty(tt, keyStoreService)

// check type and status
assert.Equal(tt, framework.KeyStore, keyStoreService.Type())
assert.Equal(tt, framework.StatusReady, keyStoreService.Status().Status)

// store an invalid key type
err = keyStoreService.StoreKey(context.Background(), keystore.StoreKeyRequest{
ID: "test-kid",
Type: "bad",
Controller: "me",
PrivateKeyBase58: base58.Encode([]byte("bad")),
})
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "unsupported key type: bad")

// store a valid key
_, privKey, err := crypto.GenerateKeyByKeyType(crypto.Ed25519)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKey)

keyID := "did:test:me#key-1"
privKeyBytes, err := crypto.PrivKeyToBytes(privKey)
assert.NoError(tt, err)
err = keyStoreService.StoreKey(context.Background(), keystore.StoreKeyRequest{
ID: keyID,
Type: crypto.Ed25519,
Controller: "did:test:me",
PrivateKeyBase58: base58.Encode(privKeyBytes),
})
assert.NoError(tt, err)

// get a key that doesn't exist
gotDetails, err := keyStoreService.GetKeyDetails(context.Background(), keystore.GetKeyDetailsRequest{ID: "bad"})
assert.Error(tt, err)
assert.Empty(tt, gotDetails)
assert.Contains(tt, err.Error(), "could not get key details for key: bad")

// get a key that exists
gotDetails, err = keyStoreService.GetKeyDetails(context.Background(), keystore.GetKeyDetailsRequest{ID: keyID})
assert.NoError(tt, err)
assert.NotEmpty(tt, gotDetails)

// make sure the details match
assert.Equal(tt, keyID, gotDetails.ID)
assert.Equal(tt, crypto.Ed25519, gotDetails.Type)
assert.Equal(tt, "did:test:me", gotDetails.Controller)

// revoked key checks
err = keyStoreService.RevokeKey(context.Background(), keystore.RevokeKeyRequest{ID: keyID})
assert.NoError(tt, err)
gotDetails, err = keyStoreService.GetKeyDetails(context.Background(), keystore.GetKeyDetailsRequest{ID: keyID})
assert.NoError(tt, err)
assert.True(tt, gotDetails.Revoked)
})
})
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "unsupported key type: bad")

// store a valid key
_, privKey, err := crypto.GenerateKeyByKeyType(crypto.Ed25519)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKey)

keyID := "did:test:me#key-1"
privKeyBytes, err := crypto.PrivKeyToBytes(privKey)
assert.NoError(tt, err)
err = keyStoreService.StoreKey(context.Background(), keystore.StoreKeyRequest{
ID: keyID,
Type: crypto.Ed25519,
Controller: "did:test:me",
PrivateKeyBase58: base58.Encode(privKeyBytes),
})
assert.NoError(tt, err)

// get a key that doesn't exist
gotDetails, err := keyStoreService.GetKeyDetails(context.Background(), keystore.GetKeyDetailsRequest{ID: "bad"})
assert.Error(tt, err)
assert.Empty(tt, gotDetails)
assert.Contains(tt, err.Error(), "could not get key details for key: bad")

// get a key that exists
gotDetails, err = keyStoreService.GetKeyDetails(context.Background(), keystore.GetKeyDetailsRequest{ID: keyID})
assert.NoError(tt, err)
assert.NotEmpty(tt, gotDetails)

// make sure the details match
assert.Equal(tt, keyID, gotDetails.ID)
assert.Equal(tt, crypto.Ed25519, gotDetails.Type)
assert.Equal(tt, "did:test:me", gotDetails.Controller)

// revoked key checks
err = keyStoreService.RevokeKey(context.Background(), keystore.RevokeKeyRequest{ID: keyID})
assert.NoError(tt, err)
gotDetails, err = keyStoreService.GetKeyDetails(context.Background(), keystore.GetKeyDetailsRequest{ID: keyID})
assert.NoError(tt, err)
assert.True(tt, gotDetails.Revoked)
})
}
}
Loading

0 comments on commit c8541a8

Please sign in to comment.