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

Commit

Permalink
Make storage more friendly. (#220)
Browse files Browse the repository at this point in the history
* modity storage

* modify tests

* modify service storage

* change module name

* update

* fix warnings

* [OSE-176] Operations can be cancelled (#216)

* Initial draft of cancellation

* Linters

* Moving a constant.

* Better homes for many methods and variables.

* PR comments

* Lets make pointers.

* Rename func

* add schema caching (#219)

Co-authored-by: Andres Uribe <[email protected]>

* modity storage

* modify tests

* modify service storage

* change module name

* update

* fix warnings

* fix server test

* Fixes to the merge problems.

* Small fixes

* Lint

Co-authored-by: Andres Uribe <[email protected]>
Co-authored-by: Gabe <[email protected]>
Co-authored-by: Andres Uribe Gonzalez <[email protected]>
  • Loading branch information
4 people authored Dec 15, 2022
1 parent 1f3dd14 commit cfa2be9
Show file tree
Hide file tree
Showing 47 changed files with 694 additions and 1,088 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ vendor/
*.db

# Log files
log/*.log
log/*.log

# VSCode
.vscode
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ type ServicesConfig struct {
// at present, it is assumed that a single storage provider works for all services
// in the future it may make sense to have per-service storage providers (e.g. mysql for one service,
// mongo for another)
StorageProvider string `toml:"storage"`
ServiceEndpoint string `toml:"service_endpoint"`
StorageProvider string `toml:"storage"`
StorageOption interface{} `toml:"storage_option"`
ServiceEndpoint string `toml:"service_endpoint"`

// Embed all service-specific configs here. The order matters: from which should be instantiated first, to last
KeyStoreConfig KeyStoreServiceConfig `toml:"keystore,omitempty"`
Expand Down
28 changes: 4 additions & 24 deletions pkg/server/router/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package router

import (
"fmt"
"os"
"testing"
"time"

Expand All @@ -16,14 +15,9 @@ import (
"github.com/tbd54566975/ssi-service/pkg/service/did"
"github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/pkg/service/schema"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

func TestCredentialRouter(t *testing.T) {
// remove the db file after the test
t.Cleanup(func() {
_ = os.Remove(storage.DBFile)
})

t.Run("Nil Service", func(tt *testing.T) {
credRouter, err := NewCredentialRouter(nil)
Expand All @@ -41,15 +35,8 @@ func TestCredentialRouter(t *testing.T) {

t.Run("Credential Service Test", func(tt *testing.T) {

bolt, err := storage.NewBoltDB()
assert.NoError(tt, err)
assert.NotEmpty(tt, bolt)

// remove the db file after the test
tt.Cleanup(func() {
_ = bolt.Close()
_ = os.Remove(storage.DBFile)
})
bolt := setupTestDB(tt)
assert.NotNil(tt, bolt)

serviceConfig := config.CredentialServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "credential"}}
keyStoreService := testKeyStoreService(tt, bolt)
Expand Down Expand Up @@ -203,15 +190,8 @@ func TestCredentialRouter(t *testing.T) {
})

t.Run("Credential Status List Test", func(tt *testing.T) {
bolt, err := storage.NewBoltDB()
assert.NoError(tt, err)
assert.NotEmpty(tt, bolt)

// remove the db file after the test
tt.Cleanup(func() {
_ = bolt.Close()
_ = os.Remove(storage.DBFile)
})
bolt := setupTestDB(tt)
assert.NotNil(tt, bolt)

serviceConfig := config.CredentialServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "credential"}}
keyStoreService := testKeyStoreService(tt, bolt)
Expand Down
32 changes: 11 additions & 21 deletions pkg/server/router/did_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package router

import (
"os"
"testing"

"github.com/TBD54566975/ssi-sdk/crypto"
Expand All @@ -11,16 +10,9 @@ import (
"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/pkg/service/did"
"github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

func TestDIDRouter(t *testing.T) {

// remove the db file after the test
t.Cleanup(func() {
_ = os.Remove(storage.DBFile)
})

t.Run("Nil Service", func(tt *testing.T) {
didRouter, err := NewDIDRouter(nil)
assert.Error(tt, err)
Expand All @@ -36,9 +28,9 @@ func TestDIDRouter(t *testing.T) {
})

t.Run("DID Service Test", func(tt *testing.T) {
db, err := storage.NewBoltDB()
assert.NoError(tt, err)
assert.NotEmpty(tt, db)

db := setupTestDB(tt)
assert.NotNil(tt, db)

keyStoreService := testKeyStoreService(tt, db)
methods := []string{didsdk.KeyMethod.String()}
Expand Down Expand Up @@ -94,22 +86,20 @@ func TestDIDRouter(t *testing.T) {
assert.Len(tt, getDIDsResponse.DIDs, 2)

knownDIDs := map[string]bool{createDIDResponse.DID.ID: true, createDIDResponse2.DID.ID: true}
for _, did := range getDIDsResponse.DIDs {
if _, ok := knownDIDs[did.ID]; !ok {
for _, gotDID := range getDIDsResponse.DIDs {
if _, ok := knownDIDs[gotDID.ID]; !ok {
tt.Error("got unknown DID")
} else {
delete(knownDIDs, did.ID)
delete(knownDIDs, gotDID.ID)
}
}
assert.Len(tt, knownDIDs, 0)
})

t.Run("DID Web Service Test", func(tt *testing.T) {
_ = os.Remove(storage.DBFile)

db, err := storage.NewBoltDB()
assert.NoError(tt, err)
assert.NotEmpty(tt, db)
db := setupTestDB(tt)
assert.NotNil(tt, db)

keyStoreService := testKeyStoreService(tt, db)
methods := []string{didsdk.KeyMethod.String(), didsdk.WebMethod.String()}
Expand Down Expand Up @@ -166,11 +156,11 @@ func TestDIDRouter(t *testing.T) {
assert.Len(tt, getDIDsResponse.DIDs, 2)

knownDIDs := map[string]bool{createDIDResponse.DID.ID: true, createDIDResponse2.DID.ID: true}
for _, did := range getDIDsResponse.DIDs {
if _, ok := knownDIDs[did.ID]; !ok {
for _, gotDID := range getDIDsResponse.DIDs {
if _, ok := knownDIDs[gotDID.ID]; !ok {
tt.Error("got unknown DID")
} else {
delete(knownDIDs, did.ID)
delete(knownDIDs, gotDID.ID)
}
}
assert.Len(tt, knownDIDs, 0)
Expand Down
12 changes: 2 additions & 10 deletions pkg/server/router/keystore_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package router

import (
"os"
"testing"

"github.com/TBD54566975/ssi-sdk/crypto"
Expand All @@ -11,16 +10,10 @@ import (
"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/pkg/service/keystore"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

func TestKeyStoreRouter(t *testing.T) {

// remove the db file after the test
t.Cleanup(func() {
_ = os.Remove(storage.DBFile)
})

t.Run("Nil Service", func(tt *testing.T) {
keyStoreRouter, err := NewKeyStoreRouter(nil)
assert.Error(tt, err)
Expand All @@ -36,9 +29,8 @@ func TestKeyStoreRouter(t *testing.T) {
})

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

serviceConfig := config.KeyStoreServiceConfig{
BaseServiceConfig: &config.BaseServiceConfig{Name: "keystore"},
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/router/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (mr ManifestRouter) GetManifests(ctx context.Context, w http.ResponseWriter
gotManifests, err := mr.service.GetManifests()

if err != nil {
errMsg := fmt.Sprintf("could not get manifests")
errMsg := "could not get manifests"
logrus.WithError(err).Error(errMsg)
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusBadRequest)
}
Expand Down Expand Up @@ -384,7 +384,7 @@ func (mr ManifestRouter) GetApplications(ctx context.Context, w http.ResponseWri
gotApplications, err := mr.service.GetApplications()

if err != nil {
errMsg := fmt.Sprintf("could not get applications")
errMsg := "could not get applications"
logrus.WithError(err).Error(errMsg)
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusBadRequest)
}
Expand Down Expand Up @@ -479,7 +479,7 @@ func (mr ManifestRouter) GetResponses(ctx context.Context, w http.ResponseWriter
gotResponses, err := mr.service.GetResponses()

if err != nil {
errMsg := fmt.Sprintf("could not get responses")
errMsg := "could not get responses"
logrus.WithError(err).Error(errMsg)
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusBadRequest)
}
Expand Down
12 changes: 2 additions & 10 deletions pkg/server/router/manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package router

import (
"os"
"testing"

"github.com/TBD54566975/ssi-sdk/credential/exchange"
Expand All @@ -19,14 +18,9 @@ import (
"github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/pkg/service/manifest"
"github.com/tbd54566975/ssi-service/pkg/service/schema"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

func TestManifestRouter(t *testing.T) {
// remove the db file after the test
t.Cleanup(func() {
_ = os.Remove(storage.DBFile)
})

t.Run("Nil Service", func(tt *testing.T) {
manifestRouter, err := NewManifestRouter(nil)
Expand All @@ -43,16 +37,14 @@ func TestManifestRouter(t *testing.T) {
})

t.Run("Manifest Service Test", func(tt *testing.T) {
bolt, err := storage.NewBoltDB()
assert.NoError(tt, err)
assert.NotEmpty(tt, bolt)
bolt := setupTestDB(tt)
assert.NotNil(tt, bolt)

keyStoreService := testKeyStoreService(tt, bolt)
didService := testDIDService(tt, bolt, keyStoreService)
schemaService := testSchemaService(tt, bolt, keyStoreService, didService)
credentialService := testCredentialService(tt, bolt, keyStoreService, didService, schemaService)
manifestService := testManifestService(tt, bolt, keyStoreService, didService, credentialService)
assert.NoError(tt, err)
assert.NotEmpty(tt, manifestService)

// check type and status
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/router/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ type GetPresentationDefinitionResponse struct {
// @Success 200 {object} GetPresentationDefinitionResponse
// @Failure 400 {string} string "Bad request"
// @Router /v1/presentation/definition/{id} [get]
func (pr PresentationRouter) GetPresentationDefinition(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (pr PresentationRouter) GetPresentationDefinition(ctx context.Context, w http.ResponseWriter, _ *http.Request) error {
id := framework.GetParam(ctx, IDParam)
if id == nil {
errMsg := "cannot get presentation without ID parameter"
Expand Down Expand Up @@ -289,7 +289,7 @@ type GetSubmissionResponse struct {
// @Success 200 {object} GetSubmissionResponse
// @Failure 400 {string} string "Bad request"
// @Router /v1/presentations/submissions/{id} [get]
func (pr PresentationRouter) GetSubmission(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (pr PresentationRouter) GetSubmission(ctx context.Context, w http.ResponseWriter, _ *http.Request) error {
id := framework.GetParam(ctx, IDParam)
if id == nil {
return framework.NewRequestError(
Expand Down
9 changes: 2 additions & 7 deletions pkg/server/router/presentation_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package router

import (
"os"
"testing"

"github.com/TBD54566975/ssi-sdk/credential/exchange"
Expand All @@ -10,7 +9,6 @@ import (
"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/pkg/service/presentation"
"github.com/tbd54566975/ssi-service/pkg/service/presentation/model"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

func TestPresentationDefinitionRouter(t *testing.T) {
Expand All @@ -30,12 +28,9 @@ func TestPresentationDefinitionRouter(t *testing.T) {
}

func TestPresentationDefinitionService(t *testing.T) {
t.Cleanup(func() {
_ = os.Remove(storage.DBFile)
})

s, err := storage.NewStorage(storage.Bolt)
assert.NoError(t, err)
s := setupTestDB(t)
assert.NotNil(t, s)

keyStoreService := testKeyStoreService(t, s)
didService := testDIDService(t, s, keyStoreService)
Expand Down
19 changes: 19 additions & 0 deletions pkg/server/router/router_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package router

import (
"os"
"testing"

didsdk "github.com/TBD54566975/ssi-sdk/did"
"github.com/stretchr/testify/require"

"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

// generic test config to be used by all tests in this package
Expand All @@ -29,3 +34,17 @@ func (s *testService) Config() config.ServicesConfig {
ManifestConfig: config.ManifestServiceConfig{},
}
}

func setupTestDB(t *testing.T) storage.ServiceStorage {
file, err := os.CreateTemp("", "bolt")
require.NoError(t, err)
name := file.Name()
file.Close()
s, err := storage.NewStorage(storage.Bolt, name)
require.NoError(t, err)
t.Cleanup(func() {
_ = s.Close()
_ = os.Remove(s.URI())
})
return s
}
Loading

0 comments on commit cfa2be9

Please sign in to comment.