-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
794 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright Gen Digital Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package file | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
profileapi "github.com/trustbloc/vcs/pkg/profile" | ||
) | ||
|
||
func Test_populateCredentialTemplates(t *testing.T) { | ||
t.Run("success with schema", func(t *testing.T) { | ||
ct := &profileapi.CredentialTemplate{ | ||
JSONSchema: jsonSchema, | ||
} | ||
|
||
err := populateJSONSchemaID(ct) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, ct.JSONSchemaID) | ||
|
||
require.Equal(t, "https://trustbloc.com/universitydegree.schema.json", ct.JSONSchemaID) | ||
}) | ||
|
||
t.Run("success no schema", func(t *testing.T) { | ||
ct := &profileapi.CredentialTemplate{} | ||
|
||
err := populateJSONSchemaID(ct) | ||
require.NoError(t, err) | ||
require.Empty(t, ct.JSONSchemaID) | ||
}) | ||
|
||
t.Run("unmarshal error in schema", func(t *testing.T) { | ||
ct := &profileapi.CredentialTemplate{ | ||
JSONSchema: `{`, | ||
} | ||
|
||
err := populateJSONSchemaID(ct) | ||
require.EqualError(t, err, "unmarshal JSON schema: unexpected end of JSON input") | ||
}) | ||
|
||
t.Run("missing `$id` field", func(t *testing.T) { | ||
ct := &profileapi.CredentialTemplate{ | ||
JSONSchema: `{}`, | ||
} | ||
|
||
err := populateJSONSchemaID(ct) | ||
require.EqualError(t, err, "missing $id field in JSON schema") | ||
}) | ||
|
||
t.Run("invalid `$id` field", func(t *testing.T) { | ||
ct := &profileapi.CredentialTemplate{ | ||
JSONSchema: `{"$id":1}`, | ||
} | ||
|
||
err := populateJSONSchemaID(ct) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "expecting field '$id' in JSON schema to be a string type") | ||
}) | ||
} | ||
|
||
const jsonSchema = `{ | ||
"$id": "https://trustbloc.com/universitydegree.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "UniversityDegreeCredential", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
} | ||
} | ||
}` |
Oops, something went wrong.