Skip to content

Commit

Permalink
move schema creation with revoked key test to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-tim committed Jul 8, 2023
1 parent 73c7696 commit 288b4a5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 47 deletions.
5 changes: 0 additions & 5 deletions pkg/server/router/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ func TestCredentialRouter(t *testing.T) {
assert.Empty(tt, createdCred)
assert.Error(tt, err)
assert.ErrorContains(tt, err, "cannot use revoked key")

// create a schema with the revoked key, it fails
_, err = schemaService.CreateSchema(context.Background(), schema.CreateSchemaRequest{Issuer: controllerDID.DID.ID, Name: "schema (revoked key)", Schema: getEmailSchema(), FullyQualifiedVerificationMethodID: keyID})
assert.Error(tt, err)
assert.ErrorContains(tt, err, "cannot use revoked key")
})

t.Run("Credential Status List Test", func(tt *testing.T) {
Expand Down
107 changes: 65 additions & 42 deletions pkg/server/router/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"testing"

credschema "github.com/TBD54566975/ssi-sdk/credential/schema"
"github.com/TBD54566975/ssi-sdk/crypto"
didsdk "github.com/TBD54566975/ssi-sdk/did"
"github.com/stretchr/testify/assert"

"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/service/keystore"
"github.com/tbd54566975/ssi-service/pkg/service/schema"
"github.com/tbd54566975/ssi-service/pkg/testutil"
)
Expand Down Expand Up @@ -58,27 +62,7 @@ func TestSchemaRouter(t *testing.T) {
assert.Contains(tt, err.Error(), "error getting schema")

// create a schema
simpleSchema := map[string]any{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": map[string]any{
"credentialSubject": map[string]any{
"type": "object",
"properties": map[string]any{
"id": map[string]any{
"type": "string",
},
"firstName": map[string]any{
"type": "string",
},
"lastName": map[string]any{
"type": "string",
},
},
"required": []any{"firstName", "lastName"},
},
},
}
simpleSchema := getSimpleSchema()
createdSchema, err := schemaService.CreateSchema(context.Background(), schema.CreateSchemaRequest{Issuer: "me", Name: "simple schema", Schema: simpleSchema})
assert.NoError(tt, err)
assert.NotEmpty(tt, createdSchema)
Expand Down Expand Up @@ -148,33 +132,72 @@ func TestSchemaSigning(t *testing.T) {
assert.Equal(tt, framework.StatusReady, schemaService.Status().Status)

// create a schema and don't sign it
simpleSchema := map[string]any{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": map[string]any{
"credentialSubject": map[string]any{
"type": "object",
"properties": map[string]any{
"id": map[string]any{
"type": "string",
},
"firstName": map[string]any{
"type": "string",
},
"lastName": map[string]any{
"type": "string",
},
},
"required": []any{"firstName", "lastName"},
},
},
}
simpleSchema := getSimpleSchema()
createdSchema, err := schemaService.CreateSchema(context.Background(), schema.CreateSchemaRequest{Issuer: "me", Name: "simple schema", Schema: simpleSchema})
assert.NoError(tt, err)
assert.NotEmpty(tt, createdSchema)
assert.NotEmpty(tt, createdSchema.ID)
assert.Equal(tt, "simple schema", createdSchema.Schema.Name())
})
})

t.Run("Signing schema with revoked key test", func(tt *testing.T) {
db := test.ServiceStorage(t)
assert.NotEmpty(tt, db)

serviceConfig := config.SchemaServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "schema"}}
keyStoreService := testKeyStoreService(tt, db)
didService := testDIDService(tt, db, keyStoreService)
schemaService, err := schema.NewSchemaService(serviceConfig, db, keyStoreService, didService.GetResolver())
assert.NoError(tt, err)
assert.NotEmpty(tt, schemaService)

// Create a DID
controllerDID, err := didService.CreateDIDByMethod(context.Background(), did.CreateDIDRequest{Method: didsdk.KeyMethod, KeyType: crypto.Ed25519})
assert.NoError(tt, err)
assert.NotEmpty(tt, controllerDID)
didID := controllerDID.DID.ID

// Create a key controlled by the DID
keyID := controllerDID.DID.VerificationMethod[0].ID
privateKey := "2dEPd7mA3aiuh2gky8tTPiCkyMwf8tBNUMZwRzeVxVJnJFGTbdLGUBcx51DCNyFWRjTG9bduvyLRStXSCDMFXULY"

err = keyStoreService.StoreKey(context.Background(), keystore.StoreKeyRequest{ID: keyID, Type: crypto.Ed25519, Controller: didID, PrivateKeyBase58: privateKey})
assert.NoError(tt, err)

// Revoke the key
err = keyStoreService.RevokeKey(context.Background(), keystore.RevokeKeyRequest{ID: keyID})
assert.NoError(tt, err)

// create a schema with the revoked key, it fails
_, err = schemaService.CreateSchema(context.Background(), schema.CreateSchemaRequest{Issuer: controllerDID.DID.ID, Name: "schema (revoked key)", Schema: getEmailSchema(), FullyQualifiedVerificationMethodID: keyID})
assert.Error(tt, err)
assert.ErrorContains(tt, err, "cannot use revoked key")
})
}
}

func getSimpleSchema() map[string]any {
simpleSchema := map[string]any{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": map[string]any{
"credentialSubject": map[string]any{
"type": "object",
"properties": map[string]any{
"id": map[string]any{
"type": "string",
},
"firstName": map[string]any{
"type": "string",
},
"lastName": map[string]any{
"type": "string",
},
},
"required": []any{"firstName", "lastName"},
},
},
}
return simpleSchema
}

0 comments on commit 288b4a5

Please sign in to comment.