diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c831e77c..9c26f466 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: - name: Run Golang unit tests working-directory: ./tests/unit/ - run: ginkgo -r --race --randomize-all --randomize-suites --keep-going --trace + run: ginkgo -r --tags unit --race --randomize-all --randomize-suites --keep-going --trace integration-tests: name: "Integration Tests" diff --git a/services/diddoc/diddoc_query.go b/services/diddoc/diddoc_query.go index 0a3a1670..fe3ef3c1 100644 --- a/services/diddoc/diddoc_query.go +++ b/services/diddoc/diddoc_query.go @@ -20,7 +20,7 @@ func (dd *QueryDIDDocRequestService) Setup(c services.ResolverContext) error { func (dd *QueryDIDDocRequestService) SpecificValidation(c services.ResolverContext) error { _, err := url.QueryUnescape(dd.Did) if err != nil { - return types.NewInvalidDIDUrlError(dd.Did, dd.RequestedContentType, err, dd.IsDereferencing) + return types.NewInvalidDidUrlError(dd.Did, dd.RequestedContentType, err, dd.IsDereferencing) } // ToDo make list of supported queries @@ -31,7 +31,7 @@ func (dd *QueryDIDDocRequestService) SpecificValidation(c services.ResolverConte // Validate that versionId is UUID if !utils.IsValidUUID(dd.Version) { - return types.NewInvalidDIDUrlError(dd.Version, dd.RequestedContentType, nil, dd.IsDereferencing) + return types.NewInvalidDidUrlError(dd.Version, dd.RequestedContentType, nil, dd.IsDereferencing) } return nil diff --git a/services/diddoc/diddoc_version.go b/services/diddoc/diddoc_version.go index 81402116..93431a59 100644 --- a/services/diddoc/diddoc_version.go +++ b/services/diddoc/diddoc_version.go @@ -33,7 +33,7 @@ func (dd DIDDocVersionRequestService) Redirect(c services.ResolverContext) error func (dd *DIDDocVersionRequestService) SpecificValidation(c services.ResolverContext) error { if !utils.IsValidUUID(dd.Version) { - return types.NewInvalidDIDUrlError(dd.Version, dd.RequestedContentType, nil, dd.IsDereferencing) + return types.NewInvalidDidUrlError(dd.Version, dd.RequestedContentType, nil, dd.IsDereferencing) } return nil } diff --git a/services/diddoc/diddoc_version_metadata.go b/services/diddoc/diddoc_version_metadata.go index 099cf89b..8c40c3da 100644 --- a/services/diddoc/diddoc_version_metadata.go +++ b/services/diddoc/diddoc_version_metadata.go @@ -33,7 +33,7 @@ func (dd DIDDocVersionMetadataRequestService) Redirect(c services.ResolverContex func (dd *DIDDocVersionMetadataRequestService) SpecificValidation(c services.ResolverContext) error { if !utils.IsValidUUID(dd.Version) { - return types.NewInvalidDIDUrlError(dd.Version, dd.RequestedContentType, nil, dd.IsDereferencing) + return types.NewInvalidDidUrlError(dd.Version, dd.RequestedContentType, nil, dd.IsDereferencing) } return nil } diff --git a/services/diddoc_service.go b/services/diddoc_service.go index 8b2fff0a..b79812d7 100644 --- a/services/diddoc_service.go +++ b/services/diddoc_service.go @@ -1,13 +1,11 @@ package services import ( - "net/url" "strings" didTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/did/v2" "github.com/cheqd/did-resolver/types" - "github.com/rs/zerolog/log" ) type DIDDocService struct { @@ -37,42 +35,6 @@ func (DIDDocService) GetDIDFragment(fragmentId string, didDoc types.DidDoc) type return nil } -func (dds DIDDocService) ProcessDIDRequest(did string, fragmentId string, queries url.Values, flag *string, contentType types.ContentType) (types.ResolutionResultI, *types.IdentityError) { - log.Trace().Msgf("ProcessDIDRequest %s, %s, %s", did, fragmentId, queries) - var result types.ResolutionResultI - var err *types.IdentityError - var isDereferencing bool - - version := "" - if len(queries) > 0 { - version = queries.Get("versionId") - if version == "" { - return nil, types.NewRepresentationNotSupportedError(did, contentType, nil, true) - } - } - - if flag != nil { - return nil, types.NewRepresentationNotSupportedError(did, contentType, nil, true) - } - - if fragmentId != "" { - log.Trace().Msgf("Dereferencing %s, %s, %s", did, fragmentId, queries) - result, err = dds.DereferenceSecondary(did, version, fragmentId, contentType) - isDereferencing = true - } else { - log.Trace().Msgf("Resolving %s", did) - result, err = dds.Resolve(did, version, contentType) - isDereferencing = false - } - - if err != nil { - err.IsDereferencing = isDereferencing - return nil, err - } - - return result, nil -} - func (dds DIDDocService) Resolve(did string, version string, contentType types.ContentType) (*types.DidResolution, *types.IdentityError) { didResolutionMetadata := types.NewResolutionMetadata(did, contentType, "") diff --git a/services/ledger_service.go b/services/ledger_service.go index d05362b0..04aef61b 100644 --- a/services/ledger_service.go +++ b/services/ledger_service.go @@ -43,7 +43,7 @@ func (ls LedgerService) QueryDIDDoc(did string, version string) (*didTypes.DidDo method, namespace, _, _ := types.TrySplitDID(did) serverAddr, namespaceFound := ls.ledgers[method+DELIMITER+namespace] if !namespaceFound { - return nil, types.NewInvalidDIDError(did, types.JSON, nil, false) + return nil, types.NewInvalidDidError(did, types.JSON, nil, false) } conn, err := ls.openGRPCConnection(serverAddr) @@ -78,7 +78,7 @@ func (ls LedgerService) QueryAllDidDocVersionsMetadata(did string) ([]*didTypes. method, namespace, _, _ := types.TrySplitDID(did) serverAddr, namespaceFound := ls.ledgers[method+DELIMITER+namespace] if !namespaceFound { - return nil, types.NewInvalidDIDError(did, types.JSON, nil, false) + return nil, types.NewInvalidDidError(did, types.JSON, nil, false) } conn, err := ls.openGRPCConnection(serverAddr) @@ -103,7 +103,7 @@ func (ls LedgerService) QueryResource(did string, resourceId string) (*resourceT method, namespace, collectionId, _ := types.TrySplitDID(did) serverAddr, namespaceFound := ls.ledgers[method+DELIMITER+namespace] if !namespaceFound { - return nil, types.NewInvalidDIDError(did, types.JSON, nil, true) + return nil, types.NewInvalidDidError(did, types.JSON, nil, true) } conn, err := ls.openGRPCConnection(serverAddr) @@ -130,7 +130,7 @@ func (ls LedgerService) QueryCollectionResources(did string) ([]*resourceTypes.M method, namespace, collectionId, _ := types.TrySplitDID(did) serverAddr, namespaceFound := ls.ledgers[method+DELIMITER+namespace] if !namespaceFound { - return nil, types.NewInvalidDIDError(did, types.JSON, nil, false) + return nil, types.NewInvalidDidError(did, types.JSON, nil, false) } conn, err := ls.openGRPCConnection(serverAddr) diff --git a/services/request_service_base.go b/services/request_service_base.go index 5efd7e67..e861099d 100644 --- a/services/request_service_base.go +++ b/services/request_service_base.go @@ -35,7 +35,7 @@ func (dd *BaseRequestService) BasicPrepare(c ResolverContext) error { // Get DID from request did, err := GetDidParam(c) if err != nil { - return types.NewInvalidDIDUrlError(c.Param("did"), dd.RequestedContentType, err, dd.IsDereferencing) + return types.NewInvalidDidUrlError(c.Param("did"), dd.RequestedContentType, err, dd.IsDereferencing) } // Get Did @@ -53,7 +53,7 @@ func (dd BaseRequestService) BasicValidation(c ResolverContext) error { err := utils.ValidateDID(dd.Did, "", c.LedgerService.GetNamespaces()) if err != nil { - return types.NewInvalidDIDError(dd.Did, dd.RequestedContentType, nil, dd.IsDereferencing) + return types.NewInvalidDidError(dd.Did, dd.RequestedContentType, nil, dd.IsDereferencing) } return nil diff --git a/services/resource/resource_data_dereferencing.go b/services/resource/resource_data_dereferencing.go index 8815d078..d0018068 100644 --- a/services/resource/resource_data_dereferencing.go +++ b/services/resource/resource_data_dereferencing.go @@ -34,7 +34,7 @@ func (dr ResourceDataDereferencingService) Redirect(c services.ResolverContext) func (dr *ResourceDataDereferencingService) SpecificValidation(c services.ResolverContext) error { if !utils.IsValidUUID(dr.ResourceId) { - return types.NewInvalidDIDUrlError(dr.ResourceId, dr.RequestedContentType, nil, dr.IsDereferencing) + return types.NewInvalidDidUrlError(dr.ResourceId, dr.RequestedContentType, nil, dr.IsDereferencing) } return nil } diff --git a/services/resource/resource_metadata_dereferencing.go b/services/resource/resource_metadata_dereferencing.go index d22690ca..70e99000 100644 --- a/services/resource/resource_metadata_dereferencing.go +++ b/services/resource/resource_metadata_dereferencing.go @@ -33,7 +33,7 @@ func (dr ResourceMetadataDereferencingService) Redirect(c services.ResolverConte func (dr *ResourceMetadataDereferencingService) SpecificValidation(c services.ResolverContext) error { if !utils.IsValidUUID(dr.ResourceId) { - return types.NewInvalidDIDUrlError(dr.ResourceId, dr.RequestedContentType, nil, dr.IsDereferencing) + return types.NewInvalidDidUrlError(dr.ResourceId, dr.RequestedContentType, nil, dr.IsDereferencing) } return nil } diff --git a/tests/constants/common.go b/tests/constants/common.go new file mode 100644 index 00000000..ea939cc7 --- /dev/null +++ b/tests/constants/common.go @@ -0,0 +1,66 @@ +package testconstants + +import ( + "crypto/sha256" + "fmt" + + didTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/did/v2" + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" +) + +func generateVerificationMethod() didTypes.VerificationMethod { + return didTypes.VerificationMethod{ + Id: ExistentDid + "#key-1", + VerificationMethodType: "JsonWebKey2020", + Controller: ExistentDid, + VerificationMaterial: ValidPubKeyJWK, + } +} + +func generateService() didTypes.Service { + return didTypes.Service{ + Id: ExistentDid + "#service-1", + ServiceType: "DIDCommMessaging", + ServiceEndpoint: []string{"http://example.com"}, + } +} + +func generateDIDDoc() didTypes.DidDoc { + service := generateService() + verificationMethod := generateVerificationMethod() + + return didTypes.DidDoc{ + Id: ExistentDid, + VerificationMethod: []*didTypes.VerificationMethod{&verificationMethod}, + Service: []*didTypes.Service{&service}, + } +} + +func generateResource() resourceTypes.ResourceWithMetadata { + data := []byte("{\"attr\":[\"name\",\"age\"]}") + checksum := sha256.New().Sum(data) + return resourceTypes.ResourceWithMetadata{ + Resource: &resourceTypes.Resource{ + Data: data, + }, + Metadata: &resourceTypes.Metadata{ + CollectionId: ValidIdentifier, + Id: ExistentResourceId, + Name: "Existing Resource Name", + ResourceType: "string", + MediaType: "application/json", + Checksum: fmt.Sprintf("%x", checksum), + }, + } +} + +func generateMetadata() didTypes.Metadata { + return didTypes.Metadata{VersionId: "test_version_id", Deactivated: false} +} + +func generateChecksum(data []byte) string { + h := sha256.New() + h.Write(data) + + return fmt.Sprintf("%x", h.Sum(nil)) +} diff --git a/tests/constants/constants.go b/tests/constants/constants.go index dbd24c9a..d36e9a75 100644 --- a/tests/constants/constants.go +++ b/tests/constants/constants.go @@ -1,6 +1,12 @@ package testconstants -import "fmt" +import ( + "fmt" + + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" + "github.com/cheqd/did-resolver/types" + "google.golang.org/protobuf/types/known/timestamppb" +) var ( DefaultResolutionType = "*/*" @@ -40,11 +46,11 @@ var ( ) var ( - MainnetDIDWithInvalidMethod = fmt.Sprintf(DIDStructure, InvalidMethod, ValidMainnetNamespace, ValidIdentifier) - TestnetDIDWithInvalidMethod = fmt.Sprintf(DIDStructure, InvalidMethod, ValidTestnetNamespace, ValidIdentifier) + MainnetDidWithInvalidMethod = fmt.Sprintf(DIDStructure, InvalidMethod, ValidMainnetNamespace, ValidIdentifier) + TestnetDidWithInvalidMethod = fmt.Sprintf(DIDStructure, InvalidMethod, ValidTestnetNamespace, ValidIdentifier) - DIDWithInvalidNamespace = fmt.Sprintf(DIDStructure, ValidMethod, InvalidNamespace, ValidIdentifier) - InvalidDID = fmt.Sprintf(DIDStructure, InvalidMethod, InvalidNamespace, InvalidIdentifier) + DidWithInvalidNamespace = fmt.Sprintf(DIDStructure, ValidMethod, InvalidNamespace, ValidIdentifier) + InvalidDid = fmt.Sprintf(DIDStructure, InvalidMethod, InvalidNamespace, InvalidIdentifier) ) var ( @@ -52,6 +58,43 @@ var ( ValidMainnetNamespace = "mainnet" ValidTestnetNamespace = "testnet" ValidIdentifier = "fb53dd05-329b-4614-a3f2-c0a8c7554ee3" + ValidVersionId = "valid_version_id" + ValidPubKeyJWK = "{" + + "\"crv\":\"Ed25519\"," + + "\"kid\":\"_Qq0UL2Fq651Q0Fjd6TvnYE-faHiOpRlPVQcY_-tA4A\"," + + "\"kty\":\"OKP\"," + + "\"x\":\"VCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ\"" + + "}" +) + +var ( + ExistentDid = fmt.Sprintf(DIDStructure, ValidMethod, ValidMainnetNamespace, ValidIdentifier) + ExistentResourceId = "a09abea0-22e0-4b35-8f70-9cc3a6d0b5fd" +) + +var ( + ValidResourceData = []byte("test_checksum") + ValidResourceMetadata = resourceTypes.Metadata{ + CollectionId: ValidIdentifier, + Id: ExistentResourceId, + Name: "Existing Resource Name", + ResourceType: "CL-Schema", + MediaType: "application/json", + Checksum: generateChecksum(ValidResourceData), + } + + ValidMetadataResource = types.DereferencedResource{ + ResourceURI: ExistentDid + types.RESOURCE_PATH + ValidResourceMetadata.Id, + CollectionId: ValidResourceMetadata.CollectionId, + ResourceId: ValidResourceMetadata.Id, + Name: ValidResourceMetadata.Name, + ResourceType: ValidResourceMetadata.ResourceType, + MediaType: ValidResourceMetadata.MediaType, + Created: &EmptyTime, + Checksum: ValidResourceMetadata.Checksum, + PreviousVersionId: nil, + NextVersionId: nil, + } ) var ( @@ -65,6 +108,32 @@ var ( InvalidIdentifier = "invalid_identifier" ) +var ( + EmptyTimestamp = ×tamppb.Timestamp{ + Seconds: 0, + Nanos: 0, + } + EmptyTime = EmptyTimestamp.AsTime() + + NotEmptyTimestamp = ×tamppb.Timestamp{ + Seconds: 123456789, + Nanos: 0, + } + NotEmptyTime = NotEmptyTimestamp.AsTime() +) + +var ( + ValidDIDDoc = generateDIDDoc() + ValidMetadata = generateMetadata() + ValidResource = generateResource() + ValidVerificationMethod = generateVerificationMethod() + ValidService = generateService() + ValidDIDDocResolution = types.NewDidDoc(&ValidDIDDoc) + ValidFragmentMetadata = types.NewResolutionDidDocMetadata(ExistentDid, &ValidMetadata, []*resourceTypes.Metadata{}) + ValidResourceDereferencing = types.DereferencedResourceData(ValidResource.Resource.Data) + ValidDereferencedResourceList = types.NewDereferencedResourceList(ExistentDid, []*resourceTypes.Metadata{ValidResource.Metadata}) +) + var DIDStructure = "did:%s:%s:%s" var HashTag = "\u0023" diff --git a/tests/integration/rest/diddoc/diddoc/negative_test.go b/tests/integration/rest/diddoc/diddoc/negative_test.go index f6c646ce..67b3ec72 100644 --- a/tests/integration/rest/diddoc/diddoc/negative_test.go +++ b/tests/integration/rest/diddoc/diddoc/negative_test.go @@ -134,7 +134,7 @@ var _ = DescribeTable("Negative: Get DIDDoc", func(testCase utils.NegativeTestCa utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s", - testconstants.MainnetDIDWithInvalidMethod, + testconstants.MainnetDidWithInvalidMethod, ), ResolutionType: testconstants.DefaultResolutionType, ExpectedResult: types.DidResolution{ @@ -143,7 +143,7 @@ var _ = DescribeTable("Negative: Get DIDDoc", func(testCase utils.NegativeTestCa ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.MainnetDIDWithInvalidMethod, + DidString: testconstants.MainnetDidWithInvalidMethod, MethodSpecificId: testconstants.ValidIdentifier, Method: testconstants.InvalidMethod, }, @@ -160,7 +160,7 @@ var _ = DescribeTable("Negative: Get DIDDoc", func(testCase utils.NegativeTestCa utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s", - testconstants.TestnetDIDWithInvalidMethod, + testconstants.TestnetDidWithInvalidMethod, ), ResolutionType: testconstants.DefaultResolutionType, ExpectedResult: types.DidResolution{ @@ -169,7 +169,7 @@ var _ = DescribeTable("Negative: Get DIDDoc", func(testCase utils.NegativeTestCa ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.TestnetDIDWithInvalidMethod, + DidString: testconstants.TestnetDidWithInvalidMethod, MethodSpecificId: testconstants.ValidIdentifier, Method: testconstants.InvalidMethod, }, @@ -186,7 +186,7 @@ var _ = DescribeTable("Negative: Get DIDDoc", func(testCase utils.NegativeTestCa utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s", - testconstants.DIDWithInvalidNamespace, + testconstants.DidWithInvalidNamespace, ), ResolutionType: testconstants.DefaultResolutionType, ExpectedResult: types.DidResolution{ @@ -195,7 +195,7 @@ var _ = DescribeTable("Negative: Get DIDDoc", func(testCase utils.NegativeTestCa ContentType: types.DIDJSONLD, ResolutionError: "invalidDid", DidProperties: types.DidProperties{ - DidString: testconstants.DIDWithInvalidNamespace, + DidString: testconstants.DidWithInvalidNamespace, MethodSpecificId: testconstants.ValidIdentifier, Method: testconstants.ValidMethod, }, @@ -212,7 +212,7 @@ var _ = DescribeTable("Negative: Get DIDDoc", func(testCase utils.NegativeTestCa utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s", - testconstants.InvalidDID, + testconstants.InvalidDid, ), ResolutionType: testconstants.DefaultResolutionType, ExpectedResult: types.DidResolution{ @@ -221,7 +221,7 @@ var _ = DescribeTable("Negative: Get DIDDoc", func(testCase utils.NegativeTestCa ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.InvalidDID, + DidString: testconstants.InvalidDid, MethodSpecificId: testconstants.InvalidIdentifier, Method: testconstants.InvalidMethod, }, diff --git a/tests/integration/rest/diddoc/diddoc/suite_test.go b/tests/integration/rest/diddoc/diddoc/suite_test.go index 170cea56..55a8712b 100644 --- a/tests/integration/rest/diddoc/diddoc/suite_test.go +++ b/tests/integration/rest/diddoc/diddoc/suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package diddoc_test import ( @@ -9,5 +11,5 @@ import ( func TestDiddoc(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "DIDDoc Integration Tests") + RunSpecs(t, "[Integration Test]: DIDDoc") } diff --git a/tests/integration/rest/diddoc/fragment/negative_test.go b/tests/integration/rest/diddoc/fragment/negative_test.go index a048d1b0..76386ec0 100644 --- a/tests/integration/rest/diddoc/fragment/negative_test.go +++ b/tests/integration/rest/diddoc/fragment/negative_test.go @@ -107,7 +107,7 @@ var _ = DescribeTable("Negative: Get DID#fragment", func(testCase utils.Negative utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%skey1", - testconstants.InvalidDID+url.PathEscape(testconstants.HashTag), + testconstants.InvalidDid+url.PathEscape(testconstants.HashTag), ), ResolutionType: testconstants.DefaultResolutionType, ExpectedResult: utils.DereferencingResult{ @@ -116,7 +116,7 @@ var _ = DescribeTable("Negative: Get DID#fragment", func(testCase utils.Negative ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.InvalidDID, + DidString: testconstants.InvalidDid, MethodSpecificId: testconstants.InvalidIdentifier, Method: testconstants.InvalidMethod, }, diff --git a/tests/integration/rest/diddoc/fragment/suite_test.go b/tests/integration/rest/diddoc/fragment/suite_test.go index b54cc4de..1ca63fa3 100644 --- a/tests/integration/rest/diddoc/fragment/suite_test.go +++ b/tests/integration/rest/diddoc/fragment/suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package fragment_test import ( @@ -9,5 +11,5 @@ import ( func TestFragment(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "DIDDoc Fragment Integration Tests") + RunSpecs(t, "[Integration Test]: DIDDoc Fragment") } diff --git a/tests/integration/rest/diddoc/metadata/negative_test.go b/tests/integration/rest/diddoc/metadata/negative_test.go index 0fb36f2e..7f9ee6ca 100644 --- a/tests/integration/rest/diddoc/metadata/negative_test.go +++ b/tests/integration/rest/diddoc/metadata/negative_test.go @@ -109,7 +109,7 @@ var _ = DescribeTable("Negative: Get DIDDoc version metadata", func(testCase uti utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s/version/%s/metadata", - testconstants.InvalidDID, + testconstants.InvalidDid, testconstants.ValidIdentifier, ), ResolutionType: testconstants.DefaultResolutionType, @@ -119,7 +119,7 @@ var _ = DescribeTable("Negative: Get DIDDoc version metadata", func(testCase uti ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.InvalidDID, + DidString: testconstants.InvalidDid, MethodSpecificId: testconstants.InvalidIdentifier, Method: testconstants.InvalidMethod, }, diff --git a/tests/integration/rest/diddoc/metadata/suite_test.go b/tests/integration/rest/diddoc/metadata/suite_test.go index db085231..06d5eb68 100644 --- a/tests/integration/rest/diddoc/metadata/suite_test.go +++ b/tests/integration/rest/diddoc/metadata/suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package metadata_test import ( @@ -9,5 +11,5 @@ import ( func TestMetadata(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "DIDDoc Version Metadata Integration Tests") + RunSpecs(t, "[Integration Test]: DIDDoc Version Metadata") } diff --git a/tests/integration/rest/diddoc/version/negative_test.go b/tests/integration/rest/diddoc/version/negative_test.go index 0661d685..b6e0ebbb 100644 --- a/tests/integration/rest/diddoc/version/negative_test.go +++ b/tests/integration/rest/diddoc/version/negative_test.go @@ -109,7 +109,7 @@ var _ = DescribeTable("Negative: Get DIDDoc version", func(testCase utils.Negati utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s/version/%s", - testconstants.InvalidDID, + testconstants.InvalidDid, testconstants.ValidIdentifier, ), ResolutionType: testconstants.DefaultResolutionType, @@ -119,7 +119,7 @@ var _ = DescribeTable("Negative: Get DIDDoc version", func(testCase utils.Negati ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.InvalidDID, + DidString: testconstants.InvalidDid, MethodSpecificId: testconstants.InvalidIdentifier, Method: testconstants.InvalidMethod, }, diff --git a/tests/integration/rest/diddoc/version/suite_test.go b/tests/integration/rest/diddoc/version/suite_test.go index f579c968..db389b15 100644 --- a/tests/integration/rest/diddoc/version/suite_test.go +++ b/tests/integration/rest/diddoc/version/suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package version_test import ( @@ -9,5 +11,5 @@ import ( func TestVersion(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "DIDDoc Version Integration Tests") + RunSpecs(t, "[Integration Test]: DIDDoc Version") } diff --git a/tests/integration/rest/diddoc/versions/negative_test.go b/tests/integration/rest/diddoc/versions/negative_test.go index 34f8935d..cb9cb6e1 100644 --- a/tests/integration/rest/diddoc/versions/negative_test.go +++ b/tests/integration/rest/diddoc/versions/negative_test.go @@ -106,7 +106,7 @@ var _ = DescribeTable("Negative: Get DIDDoc versions", func(testCase utils.Negat utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s/versions", - testconstants.InvalidDID, + testconstants.InvalidDid, ), ResolutionType: testconstants.DefaultResolutionType, ExpectedResult: utils.DereferencingResult{ @@ -115,7 +115,7 @@ var _ = DescribeTable("Negative: Get DIDDoc versions", func(testCase utils.Negat ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.InvalidDID, + DidString: testconstants.InvalidDid, MethodSpecificId: testconstants.InvalidIdentifier, Method: testconstants.InvalidMethod, }, diff --git a/tests/integration/rest/diddoc/versions/suite_test.go b/tests/integration/rest/diddoc/versions/suite_test.go index 86248c8b..9015a311 100644 --- a/tests/integration/rest/diddoc/versions/suite_test.go +++ b/tests/integration/rest/diddoc/versions/suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package versions_test import ( @@ -9,5 +11,5 @@ import ( func TestVersions(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "DIDDoc Versions Integration Tests") + RunSpecs(t, "[Integration Test]: DIDDoc Versions") } diff --git a/tests/integration/rest/resource/collection/negative_test.go b/tests/integration/rest/resource/collection/negative_test.go index d87ee0c6..50faa968 100644 --- a/tests/integration/rest/resource/collection/negative_test.go +++ b/tests/integration/rest/resource/collection/negative_test.go @@ -106,7 +106,7 @@ var _ = DescribeTable("Negative: Get collection of resources", func(testCase uti utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s/metadata", - testconstants.InvalidDID, + testconstants.InvalidDid, ), ResolutionType: testconstants.DefaultResolutionType, ExpectedResult: utils.DereferencingResult{ @@ -115,7 +115,7 @@ var _ = DescribeTable("Negative: Get collection of resources", func(testCase uti ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.InvalidDID, + DidString: testconstants.InvalidDid, MethodSpecificId: testconstants.InvalidIdentifier, Method: testconstants.InvalidMethod, }, diff --git a/tests/integration/rest/resource/collection/suite_test.go b/tests/integration/rest/resource/collection/suite_test.go index ff3466ff..509b16f3 100644 --- a/tests/integration/rest/resource/collection/suite_test.go +++ b/tests/integration/rest/resource/collection/suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package collection_test import ( @@ -9,5 +11,5 @@ import ( func TestCollection(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "Collection of Resources Integration Tests") + RunSpecs(t, "[Integration Test]: Collection of Resources") } diff --git a/tests/integration/rest/resource/data/negative_test.go b/tests/integration/rest/resource/data/negative_test.go index cb6a6527..b15a9a7d 100644 --- a/tests/integration/rest/resource/data/negative_test.go +++ b/tests/integration/rest/resource/data/negative_test.go @@ -109,7 +109,7 @@ var _ = DescribeTable("Negative: Get resource data", func(testCase utils.Negativ utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s/resources/%s", - testconstants.InvalidDID, + testconstants.InvalidDid, testconstants.ValidIdentifier, ), ResolutionType: testconstants.DefaultResolutionType, @@ -119,7 +119,7 @@ var _ = DescribeTable("Negative: Get resource data", func(testCase utils.Negativ ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.InvalidDID, + DidString: testconstants.InvalidDid, MethodSpecificId: testconstants.InvalidIdentifier, Method: testconstants.InvalidMethod, }, diff --git a/tests/integration/rest/resource/data/suite_test.go b/tests/integration/rest/resource/data/suite_test.go index 4a893294..4ed24e50 100644 --- a/tests/integration/rest/resource/data/suite_test.go +++ b/tests/integration/rest/resource/data/suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package data_test import ( @@ -9,5 +11,5 @@ import ( func TestData(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "Resource Data Integration Tests") + RunSpecs(t, "[Integration Test]: Resource Data") } diff --git a/tests/integration/rest/resource/metadata/negative_test.go b/tests/integration/rest/resource/metadata/negative_test.go index ecb8e2c4..d862492d 100644 --- a/tests/integration/rest/resource/metadata/negative_test.go +++ b/tests/integration/rest/resource/metadata/negative_test.go @@ -109,7 +109,7 @@ var _ = DescribeTable("Negative: get resource metadata", func(testCase utils.Neg utils.NegativeTestCase{ DidURL: fmt.Sprintf( "http://localhost:8080/1.0/identifiers/%s/resources/%s/metadata", - testconstants.InvalidDID, + testconstants.InvalidDid, testconstants.ValidIdentifier, ), ResolutionType: testconstants.DefaultResolutionType, @@ -119,7 +119,7 @@ var _ = DescribeTable("Negative: get resource metadata", func(testCase utils.Neg ContentType: types.DIDJSONLD, ResolutionError: "methodNotSupported", DidProperties: types.DidProperties{ - DidString: testconstants.InvalidDID, + DidString: testconstants.InvalidDid, MethodSpecificId: testconstants.InvalidIdentifier, Method: testconstants.InvalidMethod, }, diff --git a/tests/integration/rest/resource/metadata/suite_test.go b/tests/integration/rest/resource/metadata/suite_test.go index 50dd6e60..6e1362dd 100644 --- a/tests/integration/rest/resource/metadata/suite_test.go +++ b/tests/integration/rest/resource/metadata/suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package metadata_test import ( @@ -9,5 +11,5 @@ import ( func TestMetadata(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "Resource Metadata Integration Tests") + RunSpecs(t, "[Integration Test]: Resource Metadata") } diff --git a/tests/integration/rest/test_suite_test.go b/tests/integration/rest/test_suite_test.go index 5b1257b3..14b26fe0 100644 --- a/tests/integration/rest/test_suite_test.go +++ b/tests/integration/rest/test_suite_test.go @@ -1,3 +1,5 @@ +//go:build integration + package rest import ( @@ -9,5 +11,5 @@ import ( func TestTestsGeneral(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "REST API Integration tests") + RunSpecs(t, "[Integration Test]: REST API") } diff --git a/tests/unit/did_doc_metadata_test.go b/tests/unit/did_doc_metadata_test.go deleted file mode 100644 index 90df72fa..00000000 --- a/tests/unit/did_doc_metadata_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package tests - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - didTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/did/v2" - resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" - "github.com/cheqd/did-resolver/types" -) - -var _ = Describe("Test NewResolutionDIDDocMetadata function", func() { - It("can create the structure with resource", func() { - metadata := &didTypes.Metadata{ - VersionId: ValidVersionId, - Deactivated: false, - } - - resources := []*resourceTypes.Metadata{ - &ResourceMetadata, - } - - expectedResult := types.ResolutionDidDocMetadata{ - Created: nil, - Updated: nil, - Deactivated: false, - VersionId: ValidVersionId, - Resources: []types.DereferencedResource{ValidMetadataResource}, - } - - result := types.NewResolutionDidDocMetadata(ValidDid, metadata, resources) - Expect(result).To(Equal(expectedResult)) - }) - - It("can create the structure without resource", func() { - metadata := &didTypes.Metadata{ - Created: NotEmptyTimestamp, - Updated: NotEmptyTimestamp, - VersionId: ValidVersionId, - Deactivated: false, - } - - expectedResult := types.ResolutionDidDocMetadata{ - Created: &NotEmptyTime, - Updated: &NotEmptyTime, - VersionId: ValidVersionId, - Deactivated: false, - } - - result := types.NewResolutionDidDocMetadata(ValidDid, metadata, []*resourceTypes.Metadata{}) - Expect(result).To(Equal(expectedResult)) - }) -}) diff --git a/tests/unit/did_doc_resolve_service_test.go b/tests/unit/did_doc_resolve_service_test.go deleted file mode 100644 index 1894372f..00000000 --- a/tests/unit/did_doc_resolve_service_test.go +++ /dev/null @@ -1,175 +0,0 @@ -package tests - -import ( - "fmt" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" - "github.com/cheqd/did-resolver/services" - "github.com/cheqd/did-resolver/types" -) - -type resolveTestCase struct { - did string - resolutionType types.ContentType - expectedDIDResolution *types.DidResolution - expectedError *types.IdentityError -} - -var _ = DescribeTable("Test Resolve method", func(testCase resolveTestCase) { - diddocService := services.NewDIDDocService("cheqd", mockLedgerService) - - if (testCase.resolutionType == "" || testCase.resolutionType == types.DIDJSONLD) && testCase.expectedError == nil { - testCase.expectedDIDResolution.Did.Context = []string{types.DIDSchemaJSONLD, types.JsonWebKey2020JSONLD} - } else if testCase.expectedDIDResolution.Did != nil { - testCase.expectedDIDResolution.Did.Context = nil - } - - expectedContentType := defineContentType( - testCase.expectedDIDResolution.ResolutionMetadata.ContentType, testCase.resolutionType, - ) - - resolutionResult, err := diddocService.Resolve(testCase.did, "", testCase.resolutionType) - if testCase.expectedError != nil { - Expect(testCase.expectedError.Code).To(Equal(err.Code)) - Expect(testCase.expectedError.Message).To(Equal(err.Message)) - } else { - Expect(err).To(BeNil()) - Expect(testCase.expectedDIDResolution.Did).To(Equal(resolutionResult.Did)) - Expect(testCase.expectedDIDResolution.Metadata).To(Equal(resolutionResult.Metadata)) - Expect(expectedContentType).To(Equal(resolutionResult.ResolutionMetadata.ContentType)) - Expect(testCase.expectedDIDResolution.ResolutionMetadata.DidProperties).To(Equal(resolutionResult.ResolutionMetadata.DidProperties)) - } -}, - - Entry( - "Successful resolution", - resolveTestCase{ - did: ValidDid, - resolutionType: types.DIDJSONLD, - expectedDIDResolution: &types.DidResolution{ - ResolutionMetadata: types.ResolutionMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Did: &validDIDDocResolution, - Metadata: types.NewResolutionDidDocMetadata( - ValidDid, &validMetadata, - []*resourceTypes.Metadata{validResource.Metadata}, - ), - }, - expectedError: nil, - }, - ), - - Entry( - "DID not found", - resolveTestCase{ - did: NotExistDID, - resolutionType: types.DIDJSONLD, - expectedDIDResolution: &types.DidResolution{ - ResolutionMetadata: types.ResolutionMetadata{ - DidProperties: types.DidProperties{ - DidString: NotExistDID, - MethodSpecificId: NotExistIdentifier, - Method: ValidMethod, - }, - }, - Did: nil, - Metadata: types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewNotFoundError(NotExistDID, types.DIDJSONLD, nil, false), - }, - ), - - Entry( - "invalid DID", - resolveTestCase{ - did: InvalidDid, - resolutionType: types.DIDJSONLD, - expectedDIDResolution: &types.DidResolution{ - ResolutionMetadata: types.ResolutionMetadata{ - DidProperties: types.DidProperties{ - DidString: InvalidDid, - MethodSpecificId: InvalidIdentifier, - Method: InvalidMethod, - }, - }, - Did: nil, - Metadata: types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewNotFoundError(InvalidDid, types.DIDJSONLD, nil, false), - }, - ), - - Entry( - "invalid method", - resolveTestCase{ - did: "did:" + InvalidMethod + ":" + ValidNamespace + ":" + ValidIdentifier, - resolutionType: types.DIDJSONLD, - expectedDIDResolution: &types.DidResolution{ - ResolutionMetadata: types.ResolutionMetadata{ - DidProperties: types.DidProperties{ - DidString: "did:" + InvalidMethod + ":" + ValidNamespace + ":" + ValidIdentifier, - MethodSpecificId: ValidIdentifier, - Method: InvalidMethod, - }, - }, - Did: nil, - Metadata: types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", InvalidMethod, ValidNamespace, ValidIdentifier), types.DIDJSONLD, nil, false, - ), - }, - ), - - Entry( - "invalid namespace", - resolveTestCase{ - did: "did:" + ValidMethod + ":" + InvalidNamespace + ":" + ValidIdentifier, - resolutionType: types.DIDJSONLD, - expectedDIDResolution: &types.DidResolution{ - ResolutionMetadata: types.ResolutionMetadata{ - DidProperties: types.DidProperties{ - DidString: "did:" + ValidMethod + ":" + InvalidNamespace + ":" + ValidIdentifier, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Did: nil, - Metadata: types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", ValidMethod, InvalidNamespace, ValidIdentifier), types.DIDJSONLD, nil, false, - ), - }, - ), - - Entry( - "invalid identifier", - resolveTestCase{ - did: "did:" + ValidMethod + ":" + ValidNamespace + ":" + InvalidIdentifier, - resolutionType: types.DIDJSONLD, - expectedDIDResolution: &types.DidResolution{ - ResolutionMetadata: types.ResolutionMetadata{ - DidProperties: types.DidProperties{ - DidString: "did:" + ValidMethod + ":" + ValidNamespace + ":" + InvalidIdentifier, - MethodSpecificId: InvalidIdentifier, - Method: ValidMethod, - }, - }, - Did: nil, - Metadata: types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", ValidMethod, ValidNamespace, InvalidIdentifier), types.DIDJSONLD, nil, false, - ), - }, - ), -) diff --git a/tests/unit/diddoc/common/did_doc_metadata_test.go b/tests/unit/diddoc/common/did_doc_metadata_test.go new file mode 100644 index 00000000..27d1ef7f --- /dev/null +++ b/tests/unit/diddoc/common/did_doc_metadata_test.go @@ -0,0 +1,56 @@ +//go:build unit + +package common + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + didTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/did/v2" + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" + testconstants "github.com/cheqd/did-resolver/tests/constants" + "github.com/cheqd/did-resolver/types" +) + +var _ = Describe("Test NewResolutionDIDDocMetadata function", func() { + It("can create the structure with resource", func() { + metadata := &didTypes.Metadata{ + VersionId: testconstants.ValidIdentifier, + Deactivated: false, + } + + resources := []*resourceTypes.Metadata{ + &testconstants.ValidResourceMetadata, + } + + expectedResult := types.ResolutionDidDocMetadata{ + Created: nil, + Updated: nil, + Deactivated: false, + VersionId: testconstants.ValidIdentifier, + Resources: []types.DereferencedResource{testconstants.ValidMetadataResource}, + } + + result := types.NewResolutionDidDocMetadata(testconstants.ExistentDid, metadata, resources) + Expect(expectedResult).To(Equal(result)) + }) + + It("can create the structure without resource", func() { + metadata := &didTypes.Metadata{ + Created: testconstants.NotEmptyTimestamp, + Updated: testconstants.NotEmptyTimestamp, + VersionId: testconstants.ValidVersionId, + Deactivated: false, + } + + expectedResult := types.ResolutionDidDocMetadata{ + Created: &testconstants.NotEmptyTime, + Updated: &testconstants.NotEmptyTime, + VersionId: testconstants.ValidVersionId, + Deactivated: false, + } + + result := types.NewResolutionDidDocMetadata(testconstants.ExistentDid, metadata, []*resourceTypes.Metadata{}) + Expect(expectedResult).To(Equal(result)) + }) +}) diff --git a/tests/unit/diddoc/common/did_doc_resolve_service_test.go b/tests/unit/diddoc/common/did_doc_resolve_service_test.go new file mode 100644 index 00000000..f454cfbc --- /dev/null +++ b/tests/unit/diddoc/common/did_doc_resolve_service_test.go @@ -0,0 +1,96 @@ +//go:build unit + +package common + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" + "github.com/cheqd/did-resolver/services" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type resolveDidDocTestCase struct { + did string + resolutionType types.ContentType + expectedDIDResolution *types.DidResolution + expectedError *types.IdentityError +} + +var _ = DescribeTable("Test Resolve method", func(testCase resolveDidDocTestCase) { + diddocService := services.NewDIDDocService("cheqd", utils.MockLedger) + + if (testCase.resolutionType == "" || testCase.resolutionType == types.DIDJSONLD) && + (testCase.expectedError == nil) { + testCase.expectedDIDResolution.Did.Context = []string{ + types.DIDSchemaJSONLD, + types.JsonWebKey2020JSONLD, + } + } else if testCase.expectedDIDResolution.Did != nil { + testCase.expectedDIDResolution.Did.Context = nil + } + + expectedContentType := utils.DefineContentType( + testCase.expectedDIDResolution.ResolutionMetadata.ContentType, + testCase.resolutionType, + ) + + resolutionResult, err := diddocService.Resolve(testCase.did, "", testCase.resolutionType) + if testCase.expectedError != nil { + Expect(testCase.expectedError.Code).To(Equal(err.Code)) + Expect(testCase.expectedError.Message).To(Equal(err.Message)) + } else { + Expect(err).To(BeNil()) + Expect(testCase.expectedDIDResolution.Did).To(Equal(resolutionResult.Did)) + Expect(testCase.expectedDIDResolution.Metadata).To(Equal(resolutionResult.Metadata)) + Expect(expectedContentType).To(Equal(resolutionResult.ResolutionMetadata.ContentType)) + Expect(testCase.expectedDIDResolution.ResolutionMetadata.DidProperties).To(Equal(resolutionResult.ResolutionMetadata.DidProperties)) + } +}, + + Entry( + "can successful resolution DIDDoc with an existent DID", + resolveDidDocTestCase{ + did: testconstants.ExistentDid, + resolutionType: types.DIDJSONLD, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Did: &testconstants.ValidDIDDocResolution, + Metadata: types.NewResolutionDidDocMetadata( + testconstants.ExistentDid, &testconstants.ValidMetadata, + []*resourceTypes.Metadata{testconstants.ValidResource.Metadata}, + ), + }, + expectedError: nil, + }, + ), + + Entry( + "cannot resolution DIDDoc with not existent DID", + resolveDidDocTestCase{ + did: testconstants.NotExistentTestnetDid, + resolutionType: types.DIDJSONLD, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.NotExistentTestnetDid, + MethodSpecificId: testconstants.NotExistentIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Did: nil, + Metadata: types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, false), + }, + ), +) diff --git a/tests/unit/diddoc_dereference_service_test.go b/tests/unit/diddoc/common/diddoc_dereference_service_test.go similarity index 50% rename from tests/unit/diddoc_dereference_service_test.go rename to tests/unit/diddoc/common/diddoc_dereference_service_test.go index 8f95e54f..27286425 100644 --- a/tests/unit/diddoc_dereference_service_test.go +++ b/tests/unit/diddoc/common/diddoc_dereference_service_test.go @@ -1,34 +1,33 @@ -package tests +//go:build unit -import ( - "net/url" +package common +import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/cheqd/did-resolver/services" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" "github.com/cheqd/did-resolver/types" ) type dereferencingTestCase struct { did string fragmentId string - queries url.Values dereferencingType types.ContentType expectedDidDereferencing *types.DidDereferencing expectedError *types.IdentityError } -var _ = DescribeTable("Test Dereferencing method", func(testCase dereferencingTestCase) { - diddocService := services.NewDIDDocService("cheqd", mockLedgerService) +var _ = DescribeTable("Test DereferenceSecondary method", func(testCase dereferencingTestCase) { + diddocService := services.NewDIDDocService("cheqd", utils.MockLedger) - expectedContentType := defineContentType( + expectedContentType := utils.DefineContentType( testCase.expectedDidDereferencing.DereferencingMetadata.ContentType, testCase.dereferencingType, ) - result, err := diddocService.ProcessDIDRequest(testCase.did, testCase.fragmentId, testCase.queries, nil, testCase.dereferencingType) - dereferencingResult, _ := result.(*types.DidDereferencing) - + dereferencingResult, err := diddocService.DereferenceSecondary(testCase.did, "", testCase.fragmentId, testCase.dereferencingType) if testCase.expectedError != nil { Expect(testCase.expectedError.Code).To(Equal(err.Code)) Expect(testCase.expectedError.Message).To(Equal(err.Message)) @@ -43,86 +42,65 @@ var _ = DescribeTable("Test Dereferencing method", func(testCase dereferencingTe }, Entry( - "successful Secondary dereferencing (key)", + "can successful dereferencing secondary (verification method) with an existent DID and verificationMethodId", dereferencingTestCase{ - did: ValidDid, - fragmentId: validVerificationMethod.Id, + did: testconstants.ExistentDid, + fragmentId: testconstants.ValidVerificationMethod.Id, dereferencingType: types.DIDJSON, expectedDidDereferencing: &types.DidDereferencing{ DereferencingMetadata: types.DereferencingMetadata{ DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, }, }, - ContentStream: types.NewVerificationMethod(&validVerificationMethod), - Metadata: validFragmentMetadata, + ContentStream: types.NewVerificationMethod(&testconstants.ValidVerificationMethod), + Metadata: testconstants.ValidFragmentMetadata, }, expectedError: nil, }, ), Entry( - "successful Secondary dereferencing (service)", + "can successful dereferencing secondary (service) with an existent DID and serviceId", dereferencingTestCase{ - did: ValidDid, - fragmentId: validService.Id, + did: testconstants.ExistentDid, + fragmentId: testconstants.ValidService.Id, dereferencingType: types.DIDJSON, expectedDidDereferencing: &types.DidDereferencing{ DereferencingMetadata: types.DereferencingMetadata{ DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, }, }, - ContentStream: types.NewService(&validService), - Metadata: validFragmentMetadata, + ContentStream: types.NewService(&testconstants.ValidService), + Metadata: testconstants.ValidFragmentMetadata, }, expectedError: nil, }, ), Entry( - "not supported query", - dereferencingTestCase{ - did: ValidDid, - queries: validQuery, - dereferencingType: types.DIDJSONLD, - expectedDidDereferencing: &types.DidDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: nil, - Metadata: types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewRepresentationNotSupportedError(ValidDid, types.DIDJSONLD, nil, false), - }, - ), - - Entry( - "key not found", + "cannot dereferencing secondary with an existent DID, but not existent fragment", dereferencingTestCase{ - did: ValidDid, - fragmentId: NotExistFragmentId, + did: testconstants.ExistentDid, + fragmentId: testconstants.NotExistentFragment, dereferencingType: types.DIDJSONLD, expectedDidDereferencing: &types.DidDereferencing{ DereferencingMetadata: types.DereferencingMetadata{ DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, }, }, ContentStream: nil, Metadata: types.ResolutionDidDocMetadata{}, }, - expectedError: types.NewNotFoundError(ValidDid, types.DIDJSONLD, nil, false), + expectedError: types.NewNotFoundError(testconstants.ExistentDid, types.DIDJSONLD, nil, false), }, ), ) diff --git a/tests/unit/diddoc/common/diddoc_fragment_service_test.go b/tests/unit/diddoc/common/diddoc_fragment_service_test.go new file mode 100644 index 00000000..a06edd5a --- /dev/null +++ b/tests/unit/diddoc/common/diddoc_fragment_service_test.go @@ -0,0 +1,40 @@ +//go:build unit + +package common + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/cheqd/did-resolver/services" + testconstants "github.com/cheqd/did-resolver/tests/constants" +) + +var _ = Describe("Test GetDIDFragment method", func() { + It("can find a existent fragment in VerificationMethod", func() { + fragmentId := testconstants.ValidDIDDocResolution.VerificationMethod[0].Id + expectedFragment := &testconstants.ValidDIDDocResolution.VerificationMethod[0] + + didDocService := services.DIDDocService{} + + fragment := didDocService.GetDIDFragment(fragmentId, testconstants.ValidDIDDocResolution) + Expect(fragment).To(Equal(expectedFragment)) + }) + + It("can find a existent fragment in Service", func() { + fragmentId := testconstants.ValidDIDDocResolution.Service[0].Id + expectedFragment := &testconstants.ValidDIDDocResolution.Service[0] + + didDocService := services.DIDDocService{} + + fragment := didDocService.GetDIDFragment(fragmentId, testconstants.ValidDIDDocResolution) + Expect(fragment).To(Equal(expectedFragment)) + }) + + It("cannot find a not-existent fragment", func() { + didDocService := services.DIDDocService{} + + fragment := didDocService.GetDIDFragment(testconstants.NotExistentFragment, testconstants.ValidDIDDocResolution) + Expect(fragment).To(BeNil()) + }) +}) diff --git a/tests/unit/serve_test.go b/tests/unit/diddoc/common/serve_test.go similarity index 66% rename from tests/unit/serve_test.go rename to tests/unit/diddoc/common/serve_test.go index 97c36ff1..59b32373 100644 --- a/tests/unit/serve_test.go +++ b/tests/unit/diddoc/common/serve_test.go @@ -1,10 +1,14 @@ -package tests +//go:build unit + +package common import ( "net/http" "net/http/httptest" diddocServices "github.com/cheqd/did-resolver/services/diddoc" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" "github.com/cheqd/did-resolver/types" "github.com/labstack/echo/v4" . "github.com/onsi/ginkgo/v2" @@ -16,19 +20,19 @@ var _ = Describe("Content/Accept encoding checks", func() { var rec *httptest.ResponseRecorder BeforeEach(func() { - request := httptest.NewRequest(http.MethodGet, "/1.0/identifiers/"+ValidDid, nil) - context, rec = setupEmptyContext(request, types.DIDJSON, mockLedgerService) + request := httptest.NewRequest(http.MethodGet, "/1.0/identifiers/"+testconstants.ExistentDid, nil) + context, rec = utils.SetupEmptyContext(request, types.DIDJSON, utils.MockLedger) }) Context("Gzip in Accept-Encoding", func() { It("should return gzip in Content-Encoding", func() { // Setup Accept header to gzip - context.Request().Header.Set("Accept-Encoding", "gzip") + context.Request().Header.Set(echo.HeaderAcceptEncoding, "gzip") err := diddocServices.DidDocEchoHandler(context) Expect(err).To(BeNil()) // Check if Content-Encoding is gzip - Expect(rec.Header().Get("Content-Encoding")).To(Equal("gzip")) + Expect(rec.Header().Get(echo.HeaderContentEncoding)).To(Equal("gzip")) }) }) Context("Gzip not in Accept-Encoding", func() { @@ -37,31 +41,31 @@ var _ = Describe("Content/Accept encoding checks", func() { Expect(err).To(BeNil()) // Check if Content-Encoding is Empty - Expect(rec.Header().Get("Content-Encoding")).To(BeEmpty()) + Expect(rec.Header().Get(echo.HeaderContentEncoding)).To(BeEmpty()) }) }) Context("Not supported compressing", func() { It("should not return gzip in Content-Encoding", func() { // Setup Accept header to gzip - context.Request().Header.Set("Accept-Encoding", "br") + context.Request().Header.Set(echo.HeaderAcceptEncoding, "br") err := diddocServices.DidDocEchoHandler(context) Expect(err).To(BeNil()) // Check if Content-Encoding is empty - Expect(rec.Header().Get("Content-Encoding")).To(BeEmpty()) + Expect(rec.Header().Get(echo.HeaderContentEncoding)).To(BeEmpty()) }) }) Context("* in Accept-Encoding", func() { It("should return gzip in Content-Encoding", func() { // Setup Accept header to all possible variants - context.Request().Header.Set("Accept-Encoding", "*") + context.Request().Header.Set(echo.HeaderAcceptEncoding, "*") err := diddocServices.DidDocEchoHandler(context) Expect(err).To(BeNil()) // Check if Content-Encoding is Empty - Expect(rec.Header().Get("Content-Encoding")).To(Equal("gzip")) + Expect(rec.Header().Get(echo.HeaderContentEncoding)).To(Equal("gzip")) }) }) }) diff --git a/tests/unit/diddoc/common/suite_test.go b/tests/unit/diddoc/common/suite_test.go new file mode 100644 index 00000000..e352b7d9 --- /dev/null +++ b/tests/unit/diddoc/common/suite_test.go @@ -0,0 +1,15 @@ +//go:build unit + +package common_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestCommon(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "[Unit Test]: DIDDoc common methods and functions") +} diff --git a/tests/unit/diddoc/ledger/query_all_did_doc_versions_ledger_service_test.go b/tests/unit/diddoc/ledger/query_all_did_doc_versions_ledger_service_test.go new file mode 100644 index 00000000..fe8ae9b4 --- /dev/null +++ b/tests/unit/diddoc/ledger/query_all_did_doc_versions_ledger_service_test.go @@ -0,0 +1,54 @@ +//go:build unit + +package ledger + +import ( + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +type queryDIDDocVersionsTestCase struct { + did string + expectedDidDocVersions *types.DereferencedDidVersionsList + expectedError *types.IdentityError +} + +var _ = DescribeTable("Test QueryAllDidDocVersionsMetadata method", func(testCase queryDIDDocVersionsTestCase) { + didDocMetadata, err := utils.MockLedger.QueryAllDidDocVersionsMetadata(testCase.did) + didDocVersions := types.NewDereferencedDidVersionsList(didDocMetadata) + if err != nil { + Expect(testCase.expectedError.Code).To(Equal(err.Code)) + Expect(testCase.expectedError.Message).To(Equal(err.Message)) + } else { + Expect(testCase.expectedError).To(BeNil()) + Expect(testCase.expectedDidDocVersions).To(Equal(didDocVersions)) + } +}, + + Entry( + "can get DIDDoc versions with an existent DID", + queryDIDDocVersionsTestCase{ + did: testconstants.ExistentDid, + expectedDidDocVersions: &types.DereferencedDidVersionsList{ + Versions: []types.ResolutionDidDocMetadata{ + { + VersionId: testconstants.ValidMetadata.VersionId, + }, + }, + }, + expectedError: nil, + }, + ), + + Entry( + "cannot get DIDDoc versions with not existent DID", + queryDIDDocVersionsTestCase{ + did: testconstants.NotExistentTestnetDid, + expectedDidDocVersions: nil, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.JSON, nil, true), + }, + ), +) diff --git a/tests/unit/diddoc/ledger/query_did_doc_ledger_service_test.go b/tests/unit/diddoc/ledger/query_did_doc_ledger_service_test.go new file mode 100644 index 00000000..95aac6ab --- /dev/null +++ b/tests/unit/diddoc/ledger/query_did_doc_ledger_service_test.go @@ -0,0 +1,52 @@ +//go:build unit + +package ledger + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + didTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/did/v2" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type queryDIDDocTestCase struct { + did string + expectedDidDocWithMetadata *didTypes.DidDocWithMetadata + expectedError *types.IdentityError +} + +var _ = DescribeTable("Test QueryDIDDoc method", func(testCase queryDIDDocTestCase) { + didDocWithMetadata, err := utils.MockLedger.QueryDIDDoc(testCase.did, "") + if err != nil { + Expect(testCase.expectedError.Code).To(Equal(err.Code)) + Expect(testCase.expectedError.Message).To(Equal(err.Message)) + } else { + Expect(testCase.expectedError).To(BeNil()) + Expect(testCase.expectedDidDocWithMetadata).To(Equal(didDocWithMetadata)) + } +}, + + Entry( + "can get DIDDoc with an existent DID", + queryDIDDocTestCase{ + did: testconstants.ExistentDid, + expectedDidDocWithMetadata: &didTypes.DidDocWithMetadata{ + DidDoc: &testconstants.ValidDIDDoc, + Metadata: &testconstants.ValidMetadata, + }, + expectedError: nil, + }, + ), + + Entry( + "cannot get DIDDoc with not existent DID", + queryDIDDocTestCase{ + did: testconstants.NotExistentTestnetDid, + expectedDidDocWithMetadata: nil, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.JSON, nil, true), + }, + ), +) diff --git a/tests/unit/diddoc/ledger/suite_test.go b/tests/unit/diddoc/ledger/suite_test.go new file mode 100644 index 00000000..cd083975 --- /dev/null +++ b/tests/unit/diddoc/ledger/suite_test.go @@ -0,0 +1,15 @@ +//go:build unit + +package ledger_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestLedger(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "[Unit Test]: DIDDoc Ledger Service") +} diff --git a/tests/unit/diddoc/request/request_service_resolve_did_doc_test.go b/tests/unit/diddoc/request/request_service_resolve_did_doc_test.go new file mode 100644 index 00000000..b9ac9d2e --- /dev/null +++ b/tests/unit/diddoc/request/request_service_resolve_did_doc_test.go @@ -0,0 +1,296 @@ +//go:build unit + +package request + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + + "github.com/labstack/echo/v4" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" + didDocServices "github.com/cheqd/did-resolver/services/diddoc" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type resolveDIDDocTestCase struct { + didURL string + resolutionType types.ContentType + expectedDIDResolution *types.DidResolution + expectedError error +} + +var _ = DescribeTable("Test DIDDocEchoHandler function", func(testCase resolveDIDDocTestCase) { + request := httptest.NewRequest(http.MethodGet, testCase.didURL, nil) + context, rec := utils.SetupEmptyContext(request, testCase.resolutionType, utils.MockLedger) + + if (testCase.resolutionType == "" || testCase.resolutionType == types.DIDJSONLD) && testCase.expectedError == nil { + testCase.expectedDIDResolution.Did.Context = []string{types.DIDSchemaJSONLD, types.JsonWebKey2020JSONLD} + } else if testCase.expectedDIDResolution.Did != nil { + testCase.expectedDIDResolution.Did.Context = nil + } + + expectedContentType := utils.DefineContentType(testCase.expectedDIDResolution.ResolutionMetadata.ContentType, testCase.resolutionType) + + err := didDocServices.DidDocEchoHandler(context) + if testCase.expectedError != nil { + Expect(testCase.expectedError.Error()).To(Equal(err.Error())) + } else { + var resolutionResult types.DidResolution + Expect(err).To(BeNil()) + Expect(json.Unmarshal(rec.Body.Bytes(), &resolutionResult)).To(BeNil()) + Expect(testCase.expectedDIDResolution.Did).To(Equal(resolutionResult.Did)) + Expect(testCase.expectedDIDResolution.Metadata).To(Equal(resolutionResult.Metadata)) + Expect(expectedContentType).To(Equal(resolutionResult.ResolutionMetadata.ContentType)) + Expect(testCase.expectedDIDResolution.ResolutionMetadata.DidProperties).To(Equal(resolutionResult.ResolutionMetadata.DidProperties)) + Expect(expectedContentType).To(Equal(types.ContentType(rec.Header().Get("Content-Type")))) + } +}, + + Entry( + "can get DIDDoc with an existent DID", + resolveDIDDocTestCase{ + didURL: fmt.Sprintf("/1.0/identifiers/%s", testconstants.ExistentDid), + resolutionType: types.DIDJSONLD, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Did: &testconstants.ValidDIDDocResolution, + Metadata: types.NewResolutionDidDocMetadata( + testconstants.ExistentDid, &testconstants.ValidMetadata, + []*resourceTypes.Metadata{testconstants.ValidResource.Metadata}, + ), + }, + expectedError: nil, + }, + ), + + Entry( + "cannot get DIDDoc with not existent DID", + resolveDIDDocTestCase{ + didURL: fmt.Sprintf("/1.0/identifiers/%s", testconstants.NotExistentTestnetDid), + resolutionType: types.DIDJSONLD, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.NotExistentTestnetDid, + MethodSpecificId: testconstants.NotExistentIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Did: nil, + Metadata: types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get DIDDoc with an invalid DID method", + resolveDIDDocTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s", + fmt.Sprintf( + testconstants.DIDStructure, + testconstants.InvalidMethod, + testconstants.ValidTestnetNamespace, + testconstants.ValidIdentifier, + ), + ), + resolutionType: types.DIDJSONLD, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: fmt.Sprintf( + testconstants.DIDStructure, + testconstants.InvalidMethod, + testconstants.ValidTestnetNamespace, + testconstants.ValidIdentifier, + ), + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.InvalidMethod, + }, + }, + Did: nil, + Metadata: types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewMethodNotSupportedError( + fmt.Sprintf( + testconstants.DIDStructure, + testconstants.InvalidMethod, + testconstants.ValidTestnetNamespace, + testconstants.ValidIdentifier, + ), + types.DIDJSONLD, nil, false, + ), + }, + ), + + Entry( + "cannot get DIDDoc with an invalid DID namespace", + resolveDIDDocTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s", + fmt.Sprintf( + testconstants.DIDStructure, + testconstants.ValidMethod, + testconstants.InvalidNamespace, + testconstants.ValidIdentifier, + ), + ), + resolutionType: types.DIDJSONLD, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: fmt.Sprintf( + testconstants.DIDStructure, + testconstants.ValidMethod, + testconstants.InvalidNamespace, + testconstants.ValidIdentifier, + ), + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Did: nil, + Metadata: types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewInvalidDidError( + fmt.Sprintf( + testconstants.DIDStructure, + testconstants.ValidMethod, + testconstants.InvalidNamespace, + testconstants.ValidIdentifier, + ), + types.DIDJSONLD, nil, false, + ), + }, + ), + + Entry( + "cannot get DIDDoc with an invalid DID identifier", + resolveDIDDocTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s", + fmt.Sprintf( + testconstants.DIDStructure, + testconstants.ValidMethod, + testconstants.ValidTestnetNamespace, + testconstants.InvalidIdentifier, + ), + ), + resolutionType: types.DIDJSONLD, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: fmt.Sprintf( + testconstants.DIDStructure, + testconstants.ValidMethod, + testconstants.ValidTestnetNamespace, + testconstants.InvalidIdentifier, + ), + MethodSpecificId: testconstants.InvalidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Did: nil, + Metadata: types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewInvalidDidError( + fmt.Sprintf( + testconstants.DIDStructure, + testconstants.ValidMethod, + testconstants.ValidTestnetNamespace, + testconstants.InvalidIdentifier, + ), + types.DIDJSONLD, nil, false, + ), + }, + ), + + Entry( + "cannot get DIDDoc with an invalid DID", + resolveDIDDocTestCase{ + didURL: fmt.Sprintf("/1.0/identifiers/%s", testconstants.InvalidDid), + resolutionType: types.DIDJSONLD, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.InvalidDid, + MethodSpecificId: testconstants.InvalidIdentifier, + Method: testconstants.InvalidMethod, + }, + }, + Did: nil, + Metadata: types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewMethodNotSupportedError(testconstants.InvalidDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get DIDDoc with an invalid representation", + resolveDIDDocTestCase{ + didURL: fmt.Sprintf("/1.0/identifiers/%s", testconstants.ExistentDid), + resolutionType: types.JSON, + expectedDIDResolution: &types.DidResolution{ + ResolutionMetadata: types.ResolutionMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Did: nil, + Metadata: types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewRepresentationNotSupportedError(testconstants.ExistentDid, types.JSON, nil, false), + }, + ), +) + +var _ = DescribeTable("Test redirect DID", func(testCase utils.RedirectDIDTestCase) { + request := httptest.NewRequest(http.MethodGet, testCase.DidURL, nil) + context, rec := utils.SetupEmptyContext(request, testCase.ResolutionType, utils.MockLedger) + + err := didDocServices.DidDocEchoHandler(context) + if err != nil { + Expect(testCase.ExpectedError.Error()).To(Equal(err.Error())) + } else { + Expect(testCase.ExpectedError).To(BeNil()) + Expect(http.StatusMovedPermanently).To(Equal(rec.Code)) + Expect(testCase.ExpectedDidURLRedirect).To(Equal(rec.Header().Get(echo.HeaderLocation))) + } +}, + + Entry( + "can redirect when it try to get DIDDoc with an old 16 characters Indy style DID", + utils.RedirectDIDTestCase{ + DidURL: fmt.Sprintf("/1.0/identifiers/%s", testconstants.OldIndy16CharStyleTestnetDid), + ResolutionType: types.DIDJSONLD, + ExpectedDidURLRedirect: fmt.Sprintf("/1.0/identifiers/%s", testconstants.MigratedIndy16CharStyleTestnetDid), + ExpectedError: nil, + }, + ), + + Entry( + "can redirect when it try to get DIDDoc with an old 32 characters Indy style DID", + utils.RedirectDIDTestCase{ + DidURL: fmt.Sprintf("/1.0/identifiers/%s", testconstants.OldIndy32CharStyleTestnetDid), + ResolutionType: types.DIDJSONLD, + ExpectedDidURLRedirect: fmt.Sprintf("/1.0/identifiers/%s", testconstants.MigratedIndy32CharStyleTestnetDid), + ExpectedError: nil, + }, + ), +) diff --git a/tests/unit/diddoc/request/suite_test.go b/tests/unit/diddoc/request/suite_test.go new file mode 100644 index 00000000..4b03ac59 --- /dev/null +++ b/tests/unit/diddoc/request/suite_test.go @@ -0,0 +1,15 @@ +//go:build unit + +package request_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestRequest(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "[Unit Test]: DIDDoc Request Service") +} diff --git a/tests/unit/diddoc_fragment_service_test.go b/tests/unit/diddoc_fragment_service_test.go deleted file mode 100644 index 57fcb6ca..00000000 --- a/tests/unit/diddoc_fragment_service_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package tests - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/cheqd/did-resolver/services" - "github.com/cheqd/did-resolver/types" -) - -var _ = Describe("Test GetDIDFragment method", func() { - DIDDoc := types.NewDidDoc(&validDIDDoc) - - It("can find a existent fragment in VerificationMethod", func() { - fragmentId := DIDDoc.VerificationMethod[0].Id - expectedFragment := &DIDDoc.VerificationMethod[0] - - didDocService := services.DIDDocService{} - - fragment := didDocService.GetDIDFragment(fragmentId, DIDDoc) - Expect(fragment).To(Equal(expectedFragment)) - }) - - It("can find a existent fragment in Service", func() { - fragmentId := DIDDoc.Service[0].Id - expectedFragment := &DIDDoc.Service[0] - - didDocService := services.DIDDocService{} - - fragment := didDocService.GetDIDFragment(fragmentId, DIDDoc) - Expect(fragment).To(Equal(expectedFragment)) - }) - - It("cannot find a not-existent fragment", func() { - didDocService := services.DIDDocService{} - - fragment := didDocService.GetDIDFragment(NotExistFragmentId, DIDDoc) - Expect(fragment).To(BeNil()) - }) -}) diff --git a/tests/unit/query_did_doc_ledger_service_test.go b/tests/unit/query_did_doc_ledger_service_test.go deleted file mode 100644 index 1e7076b3..00000000 --- a/tests/unit/query_did_doc_ledger_service_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package tests - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - didTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/did/v2" - resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" - "github.com/cheqd/did-resolver/services" - "github.com/cheqd/did-resolver/types" -) - -type queryDIDDocTestCase struct { - did string - expectedDidDocWithMetadata *didTypes.DidDocWithMetadata - expectedError *types.IdentityError -} - -var _ = DescribeTable("Test QueryDIDDoc method", func(testCase queryDIDDocTestCase) { - didDocWithMetadata, err := mockLedgerService.QueryDIDDoc(testCase.did, "") - if err != nil { - Expect(testCase.expectedError.Code).To(Equal(err.Code)) - Expect(testCase.expectedError.Message).To(Equal(err.Message)) - } else { - Expect(testCase.expectedError).To(BeNil()) - Expect(testCase.expectedDidDocWithMetadata).To(Equal(didDocWithMetadata)) - } -}, - - Entry( - "existent DID", - queryDIDDocTestCase{ - did: ValidDid, - expectedDidDocWithMetadata: &didTypes.DidDocWithMetadata{ - DidDoc: &validDIDDoc, - Metadata: &validMetadata, - }, - expectedError: nil, - }, - ), - - Entry( - "not existent DID", - queryDIDDocTestCase{ - did: NotExistDID, - expectedDidDocWithMetadata: nil, - expectedError: types.NewNotFoundError(NotExistDID, types.JSON, nil, true), - }, - ), - - Entry( - "invalid DID", - queryDIDDocTestCase{ - did: InvalidDid, - expectedDidDocWithMetadata: nil, - expectedError: types.NewNotFoundError(InvalidDid, types.JSON, nil, true), - }, - ), -) - -var _ = Describe("Test QueryResource method", func() { - type testCase struct { - collectionId string - resourceId string - expectedResource *resourceTypes.ResourceWithMetadata - expectedError error - } - - It("cannot get DIDDoc's resource with a invalid collectionId and resourceId", func() { - test := testCase{ - collectionId: InvalidDid, - resourceId: InvalidResourceId, - expectedResource: nil, - expectedError: types.NewInvalidDIDError(InvalidDid, types.JSON, nil, true), - } - - ledgerService := services.NewLedgerService() - resource, err := ledgerService.QueryResource(test.collectionId, test.resourceId) - Expect(test.expectedResource).To(Equal(resource)) - Expect(test.expectedError.Error()).To(Equal(err.Error())) - }) -}) diff --git a/tests/unit/query_resource_ledger_service_test.go b/tests/unit/query_resource_ledger_service_test.go deleted file mode 100644 index 471fa0e4..00000000 --- a/tests/unit/query_resource_ledger_service_test.go +++ /dev/null @@ -1,87 +0,0 @@ -package tests - -import ( - resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" - "github.com/cheqd/did-resolver/types" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -type queryResourceTestCase struct { - collectionId string - resourceId string - expectedResource *resourceTypes.ResourceWithMetadata - expectedError *types.IdentityError -} - -var _ = DescribeTable("Test QueryDIDDoc method", func(testCase queryResourceTestCase) { - resource, err := mockLedgerService.QueryResource(testCase.collectionId, testCase.resourceId) - if err != nil { - Expect(testCase.expectedError.Code).To(Equal(err.Code)) - Expect(testCase.expectedError.Message).To(Equal(err.Message)) - } else { - Expect(testCase.expectedError).To(BeNil()) - Expect(testCase.expectedResource).To(Equal(resource)) - } -}, - - Entry( - "existent collectionId and resourceId", - queryResourceTestCase{ - collectionId: ValidDid, - resourceId: ValidResourceId, - expectedResource: &validResource, - expectedError: nil, - }, - ), - - Entry( - "existent collectionId, but not existent resourceId", - queryResourceTestCase{ - collectionId: ValidDid, - resourceId: NotExistIdentifier, - expectedResource: nil, - expectedError: types.NewNotFoundError(ValidDid, types.JSON, nil, true), - }, - ), - - Entry( - "existent collectionId, but an invalid resourceId", - queryResourceTestCase{ - collectionId: ValidDid, - resourceId: InvalidIdentifier, - expectedResource: nil, - expectedError: types.NewNotFoundError(ValidDid, types.JSON, nil, true), - }, - ), - - Entry( - "not existent collectionId, but existent resourceId", - queryResourceTestCase{ - collectionId: NotExistDID, - resourceId: ValidResourceId, - expectedResource: nil, - expectedError: types.NewNotFoundError(NotExistDID, types.JSON, nil, true), - }, - ), - - Entry( - "an invalid collectionId, but existent resourceId", - queryResourceTestCase{ - collectionId: InvalidDid, - resourceId: ValidResourceId, - expectedResource: nil, - expectedError: types.NewNotFoundError(InvalidDid, types.JSON, nil, true), - }, - ), - - Entry( - "not existent collectionId and resourceId", - queryResourceTestCase{ - collectionId: NotExistDID, - resourceId: NotExistIdentifier, - expectedResource: nil, - expectedError: types.NewNotFoundError(NotExistDID, types.JSON, nil, true), - }, - ), -) diff --git a/tests/unit/request_service_dereference_collection_resources_test.go b/tests/unit/request_service_dereference_collection_resources_test.go deleted file mode 100644 index 12e190bf..00000000 --- a/tests/unit/request_service_dereference_collection_resources_test.go +++ /dev/null @@ -1,97 +0,0 @@ -package tests - -import ( - "encoding/json" - "fmt" - "net/http" - "net/http/httptest" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" - resourceServices "github.com/cheqd/did-resolver/services/resource" - "github.com/cheqd/did-resolver/types" -) - -type resourceCollectionTestCase struct { - didURL string - resolutionType types.ContentType - expectedDereferencingResult *DereferencingResult - expectedError error -} - -var _ = DescribeTable("Test ResourceCollectionEchoHandler function", func(testCase resourceCollectionTestCase) { - request := httptest.NewRequest(http.MethodGet, testCase.didURL, nil) - context, rec := setupEmptyContext(request, testCase.resolutionType, mockLedgerService) - - if (testCase.resolutionType == "" || testCase.resolutionType == types.DIDJSONLD) && testCase.expectedError == nil { - testCase.expectedDereferencingResult.ContentStream.AddContext(types.DIDSchemaJSONLD) - } else if testCase.expectedDereferencingResult.ContentStream != nil { - testCase.expectedDereferencingResult.ContentStream.RemoveContext() - } - - expectedContentType := defineContentType(testCase.expectedDereferencingResult.DereferencingMetadata.ContentType, testCase.resolutionType) - - err := resourceServices.ResourceCollectionEchoHandler(context) - if testCase.expectedError != nil { - Expect(testCase.expectedError.Error(), err.Error()) - } else { - var dereferencingResult DereferencingResult - unmarshalErr := json.Unmarshal(rec.Body.Bytes(), &dereferencingResult) - - Expect(err).To(BeNil()) - Expect(unmarshalErr).To(BeNil()) - Expect(testCase.expectedDereferencingResult.ContentStream).To(Equal(dereferencingResult.ContentStream)) - Expect(testCase.expectedDereferencingResult.Metadata).To(Equal(dereferencingResult.Metadata)) - Expect(expectedContentType).To(Equal(dereferencingResult.DereferencingMetadata.ContentType)) - Expect(testCase.expectedDereferencingResult.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) - Expect(expectedContentType).To(Equal(types.ContentType(rec.Header().Get("Content-Type")))) - } -}, - - Entry( - "successful resolution", - resourceCollectionTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s/metadata", ValidDid), - resolutionType: types.DIDJSONLD, - expectedDereferencingResult: &DereferencingResult{ - DereferencingMetadata: &types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: types.NewDereferencedResourceList( - ValidDid, - []*resourceTypes.Metadata{validResource.Metadata}, - ), - Metadata: &types.ResolutionDidDocMetadata{}, - }, - expectedError: nil, - }, - ), - - Entry( - "DID not found", - resourceCollectionTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s/metadata", NotExistDID), - resolutionType: types.DIDJSONLD, - expectedDereferencingResult: &DereferencingResult{ - DereferencingMetadata: &types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: NotExistDID, - MethodSpecificId: NotExistIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: nil, - Metadata: &types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewNotFoundError(NotExistDID, types.DIDJSONLD, nil, false), - }, - ), - - // TODO: add unit tests for invalid DID case. -) diff --git a/tests/unit/request_service_dereference_resource_data_test.go b/tests/unit/request_service_dereference_resource_data_test.go deleted file mode 100644 index e58ffb02..00000000 --- a/tests/unit/request_service_dereference_resource_data_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package tests - -import ( - "fmt" - "net/http" - "net/http/httptest" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - resourceServices "github.com/cheqd/did-resolver/services/resource" - "github.com/cheqd/did-resolver/types" -) - -type resourceDataTestCase struct { - didURL string - resolutionType types.ContentType - expectedResource types.ContentStreamI - expectedError error -} - -var validResourceDereferencing = types.DereferencedResourceData(validResource.Resource.Data) - -var _ = DescribeTable("Test ResourceDataEchoHandler function", func(testCase resourceDataTestCase) { - request := httptest.NewRequest(http.MethodGet, testCase.didURL, nil) - context, rec := setupEmptyContext(request, testCase.resolutionType, mockLedgerService) - - expectedContentType := types.ContentType(validResource.Metadata.MediaType) - - err := resourceServices.ResourceDataEchoHandler(context) - if testCase.expectedError != nil { - Expect(testCase.expectedError.Error()).To(Equal(err.Error())) - } else { - Expect(err).To(BeNil()) - Expect(testCase.expectedResource.GetBytes(), rec.Body.Bytes()) - Expect(expectedContentType).To(Equal(types.ContentType(rec.Header().Get("Content-Type")))) - } -}, - - Entry( - "successful resolution", - resourceDataTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s/resources/%s", ValidDid, ValidResourceId), - resolutionType: types.DIDJSONLD, - expectedResource: &validResourceDereferencing, - expectedError: nil, - }, - ), - - Entry( - "DID not found", - resourceDataTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s/resources/%s", NotExistDID, ValidResourceId), - resolutionType: types.DIDJSONLD, - expectedResource: nil, - expectedError: types.NewNotFoundError(NotExistDID, types.DIDJSONLD, nil, false), - }, - ), - - Entry( - "invalid representation", - resourceDataTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s/resources/%s", ValidDid, ValidResourceId), - resolutionType: types.JSON, - expectedResource: nil, - expectedError: types.NewRepresentationNotSupportedError(ValidDid, types.DIDJSONLD, nil, true), - }, - ), - - // TODO: add more unit tests. -) diff --git a/tests/unit/request_service_dereference_resource_metadata_test.go b/tests/unit/request_service_dereference_resource_metadata_test.go deleted file mode 100644 index 455c67ab..00000000 --- a/tests/unit/request_service_dereference_resource_metadata_test.go +++ /dev/null @@ -1,122 +0,0 @@ -package tests - -import ( - "encoding/json" - "fmt" - "net/http" - "net/http/httptest" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" - resourceServices "github.com/cheqd/did-resolver/services/resource" - "github.com/cheqd/did-resolver/types" -) - -type DereferencingResult struct { - DereferencingMetadata *types.DereferencingMetadata `json:"dereferencingMetadata"` - ContentStream *types.DereferencedResourceList `json:"contentStream"` - Metadata *types.ResolutionDidDocMetadata `json:"contentMetadata"` -} - -type resourceMetadataTestCase struct { - didURL string - resolutionType types.ContentType - expectedDereferencingResult *DereferencingResult - expectedError error -} - -var _ = DescribeTable("Test ResourceMetadataEchoHandler function", func(testCase resourceMetadataTestCase) { - request := httptest.NewRequest(http.MethodGet, testCase.didURL, nil) - context, rec := setupEmptyContext(request, testCase.resolutionType, mockLedgerService) - - if (testCase.resolutionType == "" || testCase.resolutionType == types.DIDJSONLD) && testCase.expectedError == nil { - testCase.expectedDereferencingResult.ContentStream.AddContext(types.DIDSchemaJSONLD) - } else if testCase.expectedDereferencingResult.ContentStream != nil { - testCase.expectedDereferencingResult.ContentStream.RemoveContext() - } - expectedContentType := defineContentType(testCase.expectedDereferencingResult.DereferencingMetadata.ContentType, testCase.resolutionType) - - err := resourceServices.ResourceMetadataEchoHandler(context) - - if testCase.expectedError != nil { - Expect(testCase.expectedError.Error()).To(Equal(err.Error())) - } else { - var dereferencingResult DereferencingResult - unmarshalErr := json.Unmarshal(rec.Body.Bytes(), &dereferencingResult) - - Expect(err).To(BeNil()) - Expect(unmarshalErr).To(BeNil()) - Expect(testCase.expectedDereferencingResult.ContentStream, dereferencingResult.ContentStream) - Expect(testCase.expectedDereferencingResult.Metadata).To(Equal(dereferencingResult.Metadata)) - Expect(expectedContentType).To(Equal(dereferencingResult.DereferencingMetadata.ContentType)) - Expect(testCase.expectedDereferencingResult.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) - Expect(expectedContentType).To(Equal(types.ContentType(rec.Header().Get("Content-Type")))) - } -}, - Entry( - "successful resolution", - resourceMetadataTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s/resources/%s/metadata", ValidDid, ValidResourceId), - resolutionType: types.DIDJSONLD, - expectedDereferencingResult: &DereferencingResult{ - DereferencingMetadata: &types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: types.NewDereferencedResourceList( - ValidDid, - []*resourceTypes.Metadata{validResource.Metadata}, - ), - Metadata: &types.ResolutionDidDocMetadata{}, - }, - expectedError: nil, - }, - ), - - Entry( - "DID not found", - resourceMetadataTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s/resources/%s/metadata", NotExistDID, ValidResourceId), - resolutionType: types.DIDJSONLD, - expectedDereferencingResult: &DereferencingResult{ - DereferencingMetadata: &types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: NotExistDID, - MethodSpecificId: NotExistIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: nil, - Metadata: &types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewNotFoundError(NotExistDID, types.DIDJSONLD, nil, false), - }, - ), - - Entry( - "invalid representation", - resourceMetadataTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s/resources/%s/metadata", ValidDid, ValidResourceId), - resolutionType: types.JSON, - expectedDereferencingResult: &DereferencingResult{ - DereferencingMetadata: &types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: nil, - Metadata: &types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewRepresentationNotSupportedError(ValidDid, types.DIDJSONLD, nil, true), - }, - ), - - // TODO: add unit tests for invalid DID case. -) diff --git a/tests/unit/request_service_resolve_did_doc_test.go b/tests/unit/request_service_resolve_did_doc_test.go deleted file mode 100644 index 330fbce8..00000000 --- a/tests/unit/request_service_resolve_did_doc_test.go +++ /dev/null @@ -1,96 +0,0 @@ -package tests - -import ( - "encoding/json" - "fmt" - "net/http" - "net/http/httptest" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" - didDocServices "github.com/cheqd/did-resolver/services/diddoc" - "github.com/cheqd/did-resolver/types" -) - -type resolveDIDDocTestCase struct { - didURL string - resolutionType types.ContentType - expectedDIDResolution *types.DidResolution - expectedError error -} - -var _ = DescribeTable("Test DIDDocEchoHandler function", func(testCase resolveDIDDocTestCase) { - request := httptest.NewRequest(http.MethodGet, testCase.didURL, nil) - context, rec := setupEmptyContext(request, testCase.resolutionType, mockLedgerService) - - if (testCase.resolutionType == "" || testCase.resolutionType == types.DIDJSONLD) && testCase.expectedError == nil { - testCase.expectedDIDResolution.Did.Context = []string{types.DIDSchemaJSONLD, types.JsonWebKey2020JSONLD} - } else if testCase.expectedDIDResolution.Did != nil { - testCase.expectedDIDResolution.Did.Context = nil - } - - expectedContentType := defineContentType(testCase.expectedDIDResolution.ResolutionMetadata.ContentType, testCase.resolutionType) - - err := didDocServices.DidDocEchoHandler(context) - if testCase.expectedError != nil { - Expect(testCase.expectedError.Error()).To(Equal(err.Error())) - } else { - var resolutionResult types.DidResolution - unmarshalErr := json.Unmarshal(rec.Body.Bytes(), &resolutionResult) - Expect(unmarshalErr).To(BeNil()) - Expect(err).To(BeNil()) - Expect(testCase.expectedDIDResolution.Did).To(Equal(resolutionResult.Did)) - Expect(testCase.expectedDIDResolution.Metadata).To(Equal(resolutionResult.Metadata)) - Expect(expectedContentType).To(Equal(resolutionResult.ResolutionMetadata.ContentType)) - Expect(testCase.expectedDIDResolution.ResolutionMetadata.DidProperties).To(Equal(resolutionResult.ResolutionMetadata.DidProperties)) - Expect(expectedContentType).To(Equal(types.ContentType(rec.Header().Get("Content-Type")))) - } -}, - - Entry( - "successful resolution", - resolveDIDDocTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s", ValidDid), - resolutionType: types.DIDJSONLD, - expectedDIDResolution: &types.DidResolution{ - ResolutionMetadata: types.ResolutionMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Did: &validDIDDocResolution, - Metadata: types.NewResolutionDidDocMetadata( - ValidDid, &validMetadata, - []*resourceTypes.Metadata{validResource.Metadata}, - ), - }, - expectedError: nil, - }, - ), - - Entry( - "DID not found", - resolveDIDDocTestCase{ - didURL: fmt.Sprintf("/1.0/identifiers/%s", NotExistDID), - resolutionType: types.DIDJSONLD, - expectedDIDResolution: &types.DidResolution{ - ResolutionMetadata: types.ResolutionMetadata{ - DidProperties: types.DidProperties{ - DidString: NotExistDID, - MethodSpecificId: NotExistIdentifier, - Method: ValidMethod, - }, - }, - Did: nil, - Metadata: types.ResolutionDidDocMetadata{}, - }, - expectedError: types.NewNotFoundError(NotExistDID, types.DIDJSONLD, nil, false), - }, - ), - - // TODO: add unit tests for invalid DID case. -) diff --git a/tests/unit/resource/common/resource_dereference_collection_resources_test.go b/tests/unit/resource/common/resource_dereference_collection_resources_test.go new file mode 100644 index 00000000..207d3d00 --- /dev/null +++ b/tests/unit/resource/common/resource_dereference_collection_resources_test.go @@ -0,0 +1,81 @@ +//go:build unit + +package common + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/cheqd/did-resolver/services" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type dereferenceCollectionResourcesTestCase struct { + did string + dereferencingType types.ContentType + expectedResourceDereferencing *types.ResourceDereferencing + expectedError *types.IdentityError +} + +var _ = DescribeTable("Test DereferenceCollectionResources method", func(testCase dereferenceCollectionResourcesTestCase) { + resourceService := services.NewResourceService(testconstants.ValidMethod, utils.MockLedger) + + expectedContentType := utils.DefineContentType( + testCase.expectedResourceDereferencing.DereferencingMetadata.ContentType, + testCase.dereferencingType, + ) + + dereferencingResult, err := resourceService.DereferenceCollectionResources(testCase.did, testCase.dereferencingType) + if err != nil { + Expect(testCase.expectedError.Code).To(Equal(err.Code)) + Expect(testCase.expectedError.Message).To(Equal(err.Message)) + } else { + Expect(testCase.expectedResourceDereferencing.ContentStream, dereferencingResult.ContentStream) + Expect(testCase.expectedResourceDereferencing.Metadata, dereferencingResult.Metadata) + Expect(expectedContentType, dereferencingResult.DereferencingMetadata.ContentType) + Expect(testCase.expectedResourceDereferencing.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) + Expect(dereferencingResult.DereferencingMetadata.ResolutionError).To(BeEmpty()) + } +}, + + Entry( + "can get collection of resources with an existent DID", + dereferenceCollectionResourcesTestCase{ + did: testconstants.ExistentDid, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: testconstants.ValidDereferencedResourceList, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: nil, + }, + ), + + Entry( + "cannot get collection of resources with not existent DID", + dereferenceCollectionResourcesTestCase{ + did: testconstants.NotExistentTestnetDid, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.NotExistentTestnetDid, + MethodSpecificId: testconstants.NotExistentIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, true), + }, + ), +) diff --git a/tests/unit/resource/common/resource_dereference_metadata_service_test.go b/tests/unit/resource/common/resource_dereference_metadata_service_test.go new file mode 100644 index 00000000..ffbf0d29 --- /dev/null +++ b/tests/unit/resource/common/resource_dereference_metadata_service_test.go @@ -0,0 +1,147 @@ +//go:build unit + +package common + +import ( + "strings" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/cheqd/did-resolver/services" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type dereferenceResourceMetadataTestCase struct { + did string + resourceId string + dereferencingType types.ContentType + expectedResourceDereferencing *types.ResourceDereferencing + expectedError *types.IdentityError +} + +var _ = DescribeTable("Test DereferenceResourceMetadata method", func(testCase dereferenceResourceMetadataTestCase) { + resourceService := services.NewResourceService(testconstants.ValidMethod, utils.MockLedger) + + expectedContentType := utils.DefineContentType( + testCase.expectedResourceDereferencing.DereferencingMetadata.ContentType, + testCase.dereferencingType, + ) + + dereferencingResult, err := resourceService.DereferenceResourceMetadata(testCase.did, testCase.resourceId, testCase.dereferencingType) + if err != nil { + Expect(testCase.expectedError.Code).To(Equal(err.Code)) + Expect(testCase.expectedError.Message).To(Equal(err.Message)) + } else { + Expect(testCase.expectedResourceDereferencing.ContentStream).To(Equal(dereferencingResult.ContentStream)) + Expect(testCase.expectedResourceDereferencing.Metadata).To(Equal(dereferencingResult.Metadata)) + Expect(expectedContentType).To(Equal(dereferencingResult.DereferencingMetadata.ContentType)) + Expect(testCase.expectedResourceDereferencing.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) + Expect(dereferencingResult.DereferencingMetadata.ResolutionError).To(BeEmpty()) + } +}, + + Entry( + "can get resource metadata with an existent DID and resourceId", + dereferenceResourceMetadataTestCase{ + did: testconstants.ExistentDid, + resourceId: testconstants.ExistentResourceId, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: testconstants.ValidDereferencedResourceList, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: nil, + }, + ), + + Entry( + "can get resource metadata with an existent DID and upper case resourceId", + dereferenceResourceMetadataTestCase{ + did: testconstants.ExistentDid, + resourceId: strings.ToUpper(testconstants.ExistentResourceId), + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: testconstants.ValidDereferencedResourceList, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: nil, + }, + ), + + Entry( + "cannot get resource metadata with not existent DID", + dereferenceResourceMetadataTestCase{ + did: testconstants.NotExistentTestnetDid, + resourceId: testconstants.NotExistentIdentifier, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.NotExistentTestnetDid, + MethodSpecificId: testconstants.NotExistentIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, true), + }, + ), + + Entry( + "cannot get resource metadata with an existent DID, but not existent resourceId", + dereferenceResourceMetadataTestCase{ + did: testconstants.ExistentDid, + resourceId: testconstants.NotExistentIdentifier, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.ExistentDid, types.DIDJSONLD, nil, true), + }, + ), + + Entry( + "cannot get resource metadata not existent DID and resourceId", + dereferenceResourceMetadataTestCase{ + did: testconstants.ExistentDid, + resourceId: testconstants.NotExistentIdentifier, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.ExistentDid, types.DIDJSONLD, nil, true), + }, + ), +) diff --git a/tests/unit/resource/common/resource_dereference_resource_data_test.go b/tests/unit/resource/common/resource_dereference_resource_data_test.go new file mode 100644 index 00000000..ead3da63 --- /dev/null +++ b/tests/unit/resource/common/resource_dereference_resource_data_test.go @@ -0,0 +1,143 @@ +//go:build unit + +package common + +import ( + "strings" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/cheqd/did-resolver/services" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type dereferenceResourceDataTestCase struct { + did string + resourceId string + dereferencingType types.ContentType + expectedResourceDereferencing *types.ResourceDereferencing + expectedError *types.IdentityError +} + +var _ = DescribeTable("Test DereferenceResourceData method", func(testCase dereferenceResourceDataTestCase) { + resourceService := services.NewResourceService(testconstants.ValidMethod, utils.MockLedger) + + expectedContentType := types.ContentType(testconstants.ValidResource.Metadata.MediaType) + dereferencingResult, err := resourceService.DereferenceResourceData(testCase.did, testCase.resourceId, testCase.dereferencingType) + if err != nil { + Expect(testCase.expectedError.Code).To(Equal(err.Code)) + Expect(testCase.expectedError.Message).To(Equal(err.Message)) + } else { + Expect(testCase.expectedResourceDereferencing.ContentStream.GetBytes()).To(Equal(dereferencingResult.ContentStream.GetBytes())) + Expect(testCase.expectedResourceDereferencing.Metadata).To(Equal(dereferencingResult.Metadata)) + Expect(expectedContentType).To(Equal(dereferencingResult.DereferencingMetadata.ContentType)) + Expect(testCase.expectedResourceDereferencing.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) + Expect(dereferencingResult.DereferencingMetadata.ResolutionError).To(BeEmpty()) + } +}, + + Entry( + "can get resource data with an existent DID and resourceId", + dereferenceResourceDataTestCase{ + did: testconstants.ExistentDid, + resourceId: testconstants.ExistentResourceId, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: &testconstants.ValidResourceDereferencing, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: nil, + }, + ), + + Entry( + "can get resource data with an existent DID and upper case resourceId", + dereferenceResourceDataTestCase{ + did: testconstants.ExistentDid, + resourceId: strings.ToUpper(testconstants.ExistentResourceId), + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: &testconstants.ValidResourceDereferencing, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: nil, + }, + ), + + Entry( + "cannot get resource data with not existent DID and a valid resourceId", + dereferenceResourceDataTestCase{ + did: testconstants.NotExistentTestnetDid, + resourceId: testconstants.ValidIdentifier, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.NotExistentTestnetDid, + MethodSpecificId: testconstants.NotExistentIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, true), + }, + ), + + Entry( + "cannot get resource data with an existent DID, but not existent resourceId", + dereferenceResourceDataTestCase{ + did: testconstants.ExistentDid, + resourceId: testconstants.ValidIdentifier, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.ExistentDid, types.DIDJSONLD, nil, true), + }, + ), + + Entry( + "cannot get resource data with not existent DID and resourceId", + dereferenceResourceDataTestCase{ + did: testconstants.NotExistentTestnetDid, + resourceId: testconstants.NotExistentIdentifier, + dereferencingType: types.DIDJSON, + expectedResourceDereferencing: &types.ResourceDereferencing{ + DereferencingMetadata: types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.NotExistentTestnetDid, + MethodSpecificId: testconstants.NotExistentIdentifier, + Method: testconstants.ValidMethod, + }, + }, + Metadata: types.ResolutionResourceMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, true), + }, + ), +) diff --git a/tests/unit/resource/common/suite_test.go b/tests/unit/resource/common/suite_test.go new file mode 100644 index 00000000..226f9221 --- /dev/null +++ b/tests/unit/resource/common/suite_test.go @@ -0,0 +1,15 @@ +//go:build unit + +package common_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestCommon(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "[Unit Test]: Resource common methods and functions") +} diff --git a/tests/unit/resource/ledger/query_collection_resource_ledger_service_test.go b/tests/unit/resource/ledger/query_collection_resource_ledger_service_test.go new file mode 100644 index 00000000..a6837c57 --- /dev/null +++ b/tests/unit/resource/ledger/query_collection_resource_ledger_service_test.go @@ -0,0 +1,50 @@ +//go:build unit + +package ledger + +import ( + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +type queryCollectionResourcesTestCase struct { + did string + expectedCollection []*resourceTypes.Metadata + expectedError *types.IdentityError +} + +var _ = DescribeTable("Test QueryCollectionResources method", func(testCase queryCollectionResourcesTestCase) { + collection, err := utils.MockLedger.QueryCollectionResources(testCase.did) + if err != nil { + Expect(testCase.expectedError.Code).To(Equal(err.Code)) + Expect(testCase.expectedError.Message).To(Equal(err.Message)) + } else { + Expect(testCase.expectedError).To(BeNil()) + Expect(testCase.expectedCollection).To(Equal(collection)) + } +}, + + Entry( + "can get collection of resources an existent DID", + queryCollectionResourcesTestCase{ + did: testconstants.ExistentDid, + expectedCollection: []*resourceTypes.Metadata{ + testconstants.ValidResource.Metadata, + }, + expectedError: nil, + }, + ), + + Entry( + "can get collection of resources not existent DID", + queryCollectionResourcesTestCase{ + did: testconstants.NotExistentTestnetDid, + expectedCollection: nil, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.JSON, nil, true), + }, + ), +) diff --git a/tests/unit/resource/ledger/query_resource_ledger_service_test.go b/tests/unit/resource/ledger/query_resource_ledger_service_test.go new file mode 100644 index 00000000..94c075e3 --- /dev/null +++ b/tests/unit/resource/ledger/query_resource_ledger_service_test.go @@ -0,0 +1,71 @@ +//go:build unit + +package ledger + +import ( + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +type queryResourceTestCase struct { + collectionId string + resourceId string + expectedResource *resourceTypes.ResourceWithMetadata + expectedError *types.IdentityError +} + +var _ = DescribeTable("Test QueryResource method", func(testCase queryResourceTestCase) { + resource, err := utils.MockLedger.QueryResource(testCase.collectionId, testCase.resourceId) + if err != nil { + Expect(testCase.expectedError.Code).To(Equal(err.Code)) + Expect(testCase.expectedError.Message).To(Equal(err.Message)) + } else { + Expect(testCase.expectedError).To(BeNil()) + Expect(testCase.expectedResource).To(Equal(resource)) + } +}, + + Entry( + "can get resource data with an existent collectionId and resourceId", + queryResourceTestCase{ + collectionId: testconstants.ExistentDid, + resourceId: testconstants.ExistentResourceId, + expectedResource: &testconstants.ValidResource, + expectedError: nil, + }, + ), + + Entry( + "cannot get resource data with an existent collectionId, but not existent resourceId", + queryResourceTestCase{ + collectionId: testconstants.ExistentDid, + resourceId: testconstants.NotExistentIdentifier, + expectedResource: nil, + expectedError: types.NewNotFoundError(testconstants.ExistentDid, types.JSON, nil, true), + }, + ), + + Entry( + "cannot get resource data with not existent collectionId, but existent resourceId", + queryResourceTestCase{ + collectionId: testconstants.NotExistentTestnetDid, + resourceId: testconstants.ExistentResourceId, + expectedResource: nil, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.JSON, nil, true), + }, + ), + + Entry( + "cannot get resource data with not existent collectionId and resourceId", + queryResourceTestCase{ + collectionId: testconstants.NotExistentTestnetDid, + resourceId: testconstants.NotExistentIdentifier, + expectedResource: nil, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.JSON, nil, true), + }, + ), +) diff --git a/tests/unit/resource/ledger/suite_test.go b/tests/unit/resource/ledger/suite_test.go new file mode 100644 index 00000000..9a977f36 --- /dev/null +++ b/tests/unit/resource/ledger/suite_test.go @@ -0,0 +1,15 @@ +//go:build unit + +package ledger_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestLedger(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "[Unit Test]: Resource Ledger Service") +} diff --git a/tests/unit/resource/request/request_service_dereference_collection_resources_test.go b/tests/unit/resource/request/request_service_dereference_collection_resources_test.go new file mode 100644 index 00000000..64a20223 --- /dev/null +++ b/tests/unit/resource/request/request_service_dereference_collection_resources_test.go @@ -0,0 +1,173 @@ +//go:build unit + +package request + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + + "github.com/labstack/echo/v4" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" + resourceServices "github.com/cheqd/did-resolver/services/resource" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type resourceCollectionTestCase struct { + didURL string + resolutionType types.ContentType + expectedDereferencingResult *DereferencingResult + expectedError error +} + +var _ = DescribeTable("Test ResourceCollectionEchoHandler function", func(testCase resourceCollectionTestCase) { + request := httptest.NewRequest(http.MethodGet, testCase.didURL, nil) + context, rec := utils.SetupEmptyContext(request, testCase.resolutionType, utils.MockLedger) + + if (testCase.resolutionType == "" || testCase.resolutionType == types.DIDJSONLD) && testCase.expectedError == nil { + testCase.expectedDereferencingResult.ContentStream.AddContext(types.DIDSchemaJSONLD) + } else if testCase.expectedDereferencingResult.ContentStream != nil { + testCase.expectedDereferencingResult.ContentStream.RemoveContext() + } + + expectedContentType := utils.DefineContentType(testCase.expectedDereferencingResult.DereferencingMetadata.ContentType, testCase.resolutionType) + + err := resourceServices.ResourceCollectionEchoHandler(context) + if testCase.expectedError != nil { + Expect(testCase.expectedError.Error(), err.Error()) + } else { + var dereferencingResult DereferencingResult + Expect(err).To(BeNil()) + Expect(json.Unmarshal(rec.Body.Bytes(), &dereferencingResult)).To(BeNil()) + Expect(testCase.expectedDereferencingResult.ContentStream).To(Equal(dereferencingResult.ContentStream)) + Expect(testCase.expectedDereferencingResult.Metadata).To(Equal(dereferencingResult.Metadata)) + Expect(expectedContentType).To(Equal(dereferencingResult.DereferencingMetadata.ContentType)) + Expect(testCase.expectedDereferencingResult.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) + Expect(expectedContentType).To(Equal(types.ContentType(rec.Header().Get("Content-Type")))) + } +}, + + Entry( + "can get collection of resource with an existent DID", + resourceCollectionTestCase{ + didURL: fmt.Sprintf("/1.0/identifiers/%s/metadata", testconstants.ExistentDid), + resolutionType: types.DIDJSONLD, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: types.NewDereferencedResourceList( + testconstants.ExistentDid, + []*resourceTypes.Metadata{testconstants.ValidResource.Metadata}, + ), + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: nil, + }, + ), + + Entry( + "cannot get collection of resources with not existent DID", + resourceCollectionTestCase{ + didURL: fmt.Sprintf("/1.0/identifiers/%s/metadata", testconstants.NotExistentTestnetDid), + resolutionType: types.DIDJSONLD, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.NotExistentTestnetDid, + MethodSpecificId: testconstants.NotExistentIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: nil, + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get collection of resources with an invalid DID", + resourceCollectionTestCase{ + didURL: fmt.Sprintf("/1.0/identifiers/%s/metadata", testconstants.InvalidDid), + resolutionType: types.DIDJSONLD, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.InvalidDid, + MethodSpecificId: testconstants.InvalidIdentifier, + Method: testconstants.InvalidMethod, + }, + }, + ContentStream: nil, + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewMethodNotSupportedError(testconstants.InvalidDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "invalid representation", + resourceCollectionTestCase{ + didURL: fmt.Sprintf("/1.0/identifiers/%s/metadata", testconstants.ExistentDid), + resolutionType: types.JSON, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: nil, + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewRepresentationNotSupportedError(testconstants.ExistentDid, types.JSON, nil, false), + }, + ), +) + +var _ = DescribeTable("Test redirect DID", func(testCase utils.RedirectDIDTestCase) { + request := httptest.NewRequest(http.MethodGet, testCase.DidURL, nil) + context, rec := utils.SetupEmptyContext(request, testCase.ResolutionType, utils.MockLedger) + + err := resourceServices.ResourceCollectionEchoHandler(context) + if err != nil { + Expect(testCase.ExpectedError.Error()).To(Equal(err.Error())) + } else { + Expect(testCase.ExpectedError).To(BeNil()) + Expect(http.StatusMovedPermanently).To(Equal(rec.Code)) + Expect(testCase.ExpectedDidURLRedirect).To(Equal(rec.Header().Get(echo.HeaderLocation))) + } +}, + + Entry( + "can redirect when it try to get collection of resources with an old 16 characters Indy style DID", + utils.RedirectDIDTestCase{ + DidURL: fmt.Sprintf("/1.0/identifiers/%s/metadata", testconstants.OldIndy16CharStyleTestnetDid), + ResolutionType: types.DIDJSONLD, + ExpectedDidURLRedirect: fmt.Sprintf("/1.0/identifiers/%s/metadata", testconstants.MigratedIndy16CharStyleTestnetDid), + ExpectedError: nil, + }, + ), + + Entry( + "can redirect when it try to get collection of resources with an old 32 characters Indy style DID", + utils.RedirectDIDTestCase{ + DidURL: fmt.Sprintf("/1.0/identifiers/%s/metadata", testconstants.OldIndy32CharStyleTestnetDid), + ResolutionType: types.DIDJSONLD, + ExpectedDidURLRedirect: fmt.Sprintf("/1.0/identifiers/%s/metadata", testconstants.MigratedIndy32CharStyleTestnetDid), + ExpectedError: nil, + }, + ), +) diff --git a/tests/unit/resource/request/request_service_dereference_resource_data_test.go b/tests/unit/resource/request/request_service_dereference_resource_data_test.go new file mode 100644 index 00000000..7a5d2d50 --- /dev/null +++ b/tests/unit/resource/request/request_service_dereference_resource_data_test.go @@ -0,0 +1,177 @@ +//go:build unit + +package request + +import ( + "fmt" + "net/http" + "net/http/httptest" + + "github.com/labstack/echo/v4" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + resourceServices "github.com/cheqd/did-resolver/services/resource" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type resourceDataTestCase struct { + didURL string + resolutionType types.ContentType + expectedResource types.ContentStreamI + expectedError error +} + +var _ = DescribeTable("Test ResourceDataEchoHandler function", func(testCase resourceDataTestCase) { + request := httptest.NewRequest(http.MethodGet, testCase.didURL, nil) + context, rec := utils.SetupEmptyContext(request, testCase.resolutionType, utils.MockLedger) + + expectedContentType := types.ContentType(testconstants.ValidResource.Metadata.MediaType) + + err := resourceServices.ResourceDataEchoHandler(context) + if testCase.expectedError != nil { + Expect(testCase.expectedError.Error()).To(Equal(err.Error())) + } else { + Expect(err).To(BeNil()) + Expect(testCase.expectedResource.GetBytes(), rec.Body.Bytes()) + Expect(expectedContentType).To(Equal(types.ContentType(rec.Header().Get("Content-Type")))) + } +}, + + Entry( + "can get resource data with an existent DID and resourceId", + resourceDataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.ExistentDid, + testconstants.ExistentResourceId, + ), + resolutionType: types.DIDJSONLD, + expectedResource: &testconstants.ValidResourceDereferencing, + expectedError: nil, + }, + ), + + Entry( + "cannot get resource data with not existent DID", + resourceDataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.NotExistentTestnetDid, + testconstants.ExistentResourceId, + ), + resolutionType: types.DIDJSONLD, + expectedResource: nil, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get resource data with an invalid DID", + resourceDataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.InvalidDid, + testconstants.ExistentResourceId, + ), + resolutionType: types.DIDJSONLD, + expectedResource: nil, + expectedError: types.NewMethodNotSupportedError(testconstants.InvalidDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get resource data with an existent DID, but not existent resourceId", + resourceDataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.ExistentDid, + testconstants.NotExistentIdentifier, + ), + resolutionType: types.DIDJSONLD, + expectedResource: nil, + expectedError: types.NewNotFoundError(testconstants.ExistentDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get resource data with an existent DID, but an invalid resourceId", + resourceDataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.ExistentDid, + testconstants.InvalidIdentifier, + ), + resolutionType: types.DIDJSONLD, + expectedResource: nil, + expectedError: types.NewInvalidDidUrlError(testconstants.ExistentDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get resource data with invalid representation", + resourceDataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.ExistentDid, + testconstants.ExistentResourceId, + ), + resolutionType: types.JSON, + expectedResource: nil, + expectedError: types.NewRepresentationNotSupportedError(testconstants.ExistentDid, types.JSON, nil, true), + }, + ), +) + +var _ = DescribeTable("Test redirect DID", func(testCase utils.RedirectDIDTestCase) { + request := httptest.NewRequest(http.MethodGet, testCase.DidURL, nil) + context, rec := utils.SetupEmptyContext(request, testCase.ResolutionType, utils.MockLedger) + + err := resourceServices.ResourceDataEchoHandler(context) + if err != nil { + Expect(testCase.ExpectedError.Error()).To(Equal(err.Error())) + } else { + Expect(testCase.ExpectedError).To(BeNil()) + Expect(http.StatusMovedPermanently).To(Equal(rec.Code)) + Expect(testCase.ExpectedDidURLRedirect).To(Equal(rec.Header().Get(echo.HeaderLocation))) + } +}, + + Entry( + "can redirect when it try to get resource data with an old 16 characters Indy style DID", + utils.RedirectDIDTestCase{ + DidURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.OldIndy16CharStyleTestnetDid, + testconstants.ValidIdentifier, + ), + ResolutionType: types.DIDJSONLD, + ExpectedDidURLRedirect: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.MigratedIndy16CharStyleTestnetDid, + testconstants.ValidIdentifier, + ), + ExpectedError: nil, + }, + ), + + Entry( + "can redirect when it try to get resource data with an old 32 characters Indy style DID", + utils.RedirectDIDTestCase{ + DidURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.OldIndy32CharStyleTestnetDid, + "214b8b61-a861-416b-a7e4-45533af40ada", + ), + ResolutionType: types.DIDJSONLD, + ExpectedDidURLRedirect: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s", + testconstants.MigratedIndy32CharStyleTestnetDid, + "214b8b61-a861-416b-a7e4-45533af40ada", + ), + ExpectedError: nil, + }, + ), +) diff --git a/tests/unit/resource/request/request_service_dereference_resource_metadata_test.go b/tests/unit/resource/request/request_service_dereference_resource_metadata_test.go new file mode 100644 index 00000000..a2490010 --- /dev/null +++ b/tests/unit/resource/request/request_service_dereference_resource_metadata_test.go @@ -0,0 +1,258 @@ +//go:build unit + +package request + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + + "github.com/labstack/echo/v4" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" + resourceServices "github.com/cheqd/did-resolver/services/resource" + testconstants "github.com/cheqd/did-resolver/tests/constants" + utils "github.com/cheqd/did-resolver/tests/unit" + "github.com/cheqd/did-resolver/types" +) + +type DereferencingResult struct { + DereferencingMetadata *types.DereferencingMetadata `json:"dereferencingMetadata"` + ContentStream *types.DereferencedResourceList `json:"contentStream"` + Metadata *types.ResolutionDidDocMetadata `json:"contentMetadata"` +} + +type resourceMetadataTestCase struct { + didURL string + resolutionType types.ContentType + expectedDereferencingResult *DereferencingResult + expectedError error +} + +var _ = DescribeTable("Test ResourceMetadataEchoHandler function", func(testCase resourceMetadataTestCase) { + request := httptest.NewRequest(http.MethodGet, testCase.didURL, nil) + context, rec := utils.SetupEmptyContext(request, testCase.resolutionType, utils.MockLedger) + + if (testCase.resolutionType == "" || testCase.resolutionType == types.DIDJSONLD) && testCase.expectedError == nil { + testCase.expectedDereferencingResult.ContentStream.AddContext(types.DIDSchemaJSONLD) + } else if testCase.expectedDereferencingResult.ContentStream != nil { + testCase.expectedDereferencingResult.ContentStream.RemoveContext() + } + expectedContentType := utils.DefineContentType(testCase.expectedDereferencingResult.DereferencingMetadata.ContentType, testCase.resolutionType) + + err := resourceServices.ResourceMetadataEchoHandler(context) + + if testCase.expectedError != nil { + Expect(testCase.expectedError.Error()).To(Equal(err.Error())) + } else { + var dereferencingResult DereferencingResult + Expect(err).To(BeNil()) + Expect(json.Unmarshal(rec.Body.Bytes(), &dereferencingResult)).To(BeNil()) + Expect(testCase.expectedDereferencingResult.ContentStream, dereferencingResult.ContentStream) + Expect(testCase.expectedDereferencingResult.Metadata).To(Equal(dereferencingResult.Metadata)) + Expect(expectedContentType).To(Equal(dereferencingResult.DereferencingMetadata.ContentType)) + Expect(testCase.expectedDereferencingResult.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) + Expect(expectedContentType).To(Equal(types.ContentType(rec.Header().Get("Content-Type")))) + } +}, + Entry( + "can get resource metadata with an existent DID and resourceId", + resourceMetadataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.ExistentDid, + testconstants.ExistentResourceId, + ), + resolutionType: types.DIDJSONLD, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: types.NewDereferencedResourceList( + testconstants.ExistentDid, + []*resourceTypes.Metadata{testconstants.ValidResource.Metadata}, + ), + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: nil, + }, + ), + + Entry( + "cannot get resource metadata with not existent DID", + resourceMetadataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.NotExistentTestnetDid, + testconstants.ExistentResourceId, + ), + resolutionType: types.DIDJSONLD, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.NotExistentTestnetDid, + MethodSpecificId: testconstants.NotExistentIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: nil, + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.NotExistentTestnetDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get resource metadata with an invalid DID", + resourceMetadataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.InvalidDid, + testconstants.ExistentResourceId, + ), + resolutionType: types.DIDJSONLD, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.InvalidDid, + MethodSpecificId: testconstants.InvalidIdentifier, + Method: testconstants.InvalidMethod, + }, + }, + ContentStream: nil, + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewMethodNotSupportedError(testconstants.InvalidDid, types.DIDJSONLD, nil, false), + }, + ), + + Entry( + "cannot get resource metadata with an existent DID, but not existent resourceId", + resourceMetadataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.ExistentDid, + testconstants.NotExistentIdentifier, + ), + resolutionType: types.DIDJSONLD, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: nil, + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewNotFoundError(testconstants.ExistentDid, types.DIDJSONLD, nil, true), + }, + ), + + Entry( + "cannot get resource metadata with an existent DID, but an invalid resourceId", + resourceMetadataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.ExistentDid, + testconstants.InvalidIdentifier, + ), + resolutionType: types.DIDJSONLD, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: nil, + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewInvalidDidUrlError(testconstants.ExistentDid, types.DIDJSONLD, nil, true), + }, + ), + + Entry( + "cannot get resource metadata with an invalid content type", + resourceMetadataTestCase{ + didURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.ExistentDid, + testconstants.ExistentResourceId, + ), + resolutionType: types.JSON, + expectedDereferencingResult: &DereferencingResult{ + DereferencingMetadata: &types.DereferencingMetadata{ + DidProperties: types.DidProperties{ + DidString: testconstants.ExistentDid, + MethodSpecificId: testconstants.ValidIdentifier, + Method: testconstants.ValidMethod, + }, + }, + ContentStream: nil, + Metadata: &types.ResolutionDidDocMetadata{}, + }, + expectedError: types.NewRepresentationNotSupportedError(testconstants.ExistentDid, types.JSON, nil, false), + }, + ), +) + +var _ = DescribeTable("Test redirect DID", func(testCase utils.RedirectDIDTestCase) { + request := httptest.NewRequest(http.MethodGet, testCase.DidURL, nil) + context, rec := utils.SetupEmptyContext(request, testCase.ResolutionType, utils.MockLedger) + + err := resourceServices.ResourceMetadataEchoHandler(context) + if err != nil { + Expect(testCase.ExpectedError.Error()).To(Equal(err.Error())) + } else { + Expect(testCase.ExpectedError).To(BeNil()) + Expect(http.StatusMovedPermanently).To(Equal(rec.Code)) + Expect(testCase.ExpectedDidURLRedirect).To(Equal(rec.Header().Get(echo.HeaderLocation))) + } +}, + + Entry( + "can redirect when it try to get resource metadata with an old 16 characters Indy style DID", + utils.RedirectDIDTestCase{ + DidURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.OldIndy16CharStyleTestnetDid, + testconstants.ValidIdentifier, + ), + ResolutionType: types.DIDJSONLD, + ExpectedDidURLRedirect: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.MigratedIndy16CharStyleTestnetDid, + testconstants.ValidIdentifier, + ), + ExpectedError: nil, + }, + ), + + Entry( + "can redirect when it try to get resource metadata with an old 32 characters Indy style DID", + utils.RedirectDIDTestCase{ + DidURL: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.OldIndy32CharStyleTestnetDid, + "214b8b61-a861-416b-a7e4-45533af40ada", + ), + ResolutionType: types.DIDJSONLD, + ExpectedDidURLRedirect: fmt.Sprintf( + "/1.0/identifiers/%s/resources/%s/metadata", + testconstants.MigratedIndy32CharStyleTestnetDid, + "214b8b61-a861-416b-a7e4-45533af40ada", + ), + ExpectedError: nil, + }, + ), +) diff --git a/tests/unit/resource/request/suite_test.go b/tests/unit/resource/request/suite_test.go new file mode 100644 index 00000000..e460996a --- /dev/null +++ b/tests/unit/resource/request/suite_test.go @@ -0,0 +1,15 @@ +//go:build unit + +package request_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestRequest(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "[Unit Test]: Resource Request Service") +} diff --git a/tests/unit/resource_dereference_collection_resources_test.go b/tests/unit/resource_dereference_collection_resources_test.go deleted file mode 100644 index 20959170..00000000 --- a/tests/unit/resource_dereference_collection_resources_test.go +++ /dev/null @@ -1,146 +0,0 @@ -package tests - -import ( - "fmt" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/cheqd/did-resolver/services" - "github.com/cheqd/did-resolver/types" -) - -type dereferenceCollectionResourcesTestCase struct { - did string - dereferencingType types.ContentType - expectedResourceDereferencing *types.ResourceDereferencing - expectedError *types.IdentityError -} - -var _ = DescribeTable("Test DereferenceCollectionResources method", func(testCase dereferenceCollectionResourcesTestCase) { - resourceService := services.NewResourceService(ValidMethod, mockLedgerService) - - expectedContentType := defineContentType( - testCase.expectedResourceDereferencing.DereferencingMetadata.ContentType, - testCase.dereferencingType, - ) - - dereferencingResult, err := resourceService.DereferenceCollectionResources(testCase.did, testCase.dereferencingType) - if err != nil { - Expect(testCase.expectedError.Code).To(Equal(err.Code)) - Expect(testCase.expectedError.Message).To(Equal(err.Message)) - - } else { - Expect(testCase.expectedResourceDereferencing.ContentStream, dereferencingResult.ContentStream) - Expect(testCase.expectedResourceDereferencing.Metadata, dereferencingResult.Metadata) - Expect(expectedContentType, dereferencingResult.DereferencingMetadata.ContentType) - Expect(testCase.expectedResourceDereferencing.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) - Expect(dereferencingResult.DereferencingMetadata.ResolutionError).To(BeEmpty()) - } -}, - - Entry( - "successful dereferencing for collection resources", - dereferenceCollectionResourcesTestCase{ - did: ValidDid, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: dereferencedResourceList, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: nil, - }, - ), - - Entry( - "invalid method", - dereferenceCollectionResourcesTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", InvalidMethod, ValidNamespace, ValidIdentifier), - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: InvalidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", InvalidMethod, ValidNamespace, ValidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - Entry( - "invalid namespace", - dereferenceCollectionResourcesTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", ValidMethod, InvalidNamespace, ValidIdentifier), - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", ValidMethod, InvalidNamespace, ValidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - Entry( - "invalid identifier", - dereferenceCollectionResourcesTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", ValidMethod, ValidNamespace, InvalidIdentifier), - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: InvalidDid, - MethodSpecificId: InvalidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", ValidMethod, ValidNamespace, InvalidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - Entry( - "invalid did", - dereferenceCollectionResourcesTestCase{ - did: InvalidDid, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: InvalidDid, - MethodSpecificId: InvalidIdentifier, - Method: InvalidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError(InvalidDid, types.DIDJSONLD, nil, true), - }, - ), -) diff --git a/tests/unit/resource_dereference_metadata_service_test.go b/tests/unit/resource_dereference_metadata_service_test.go deleted file mode 100644 index be0c3bd1..00000000 --- a/tests/unit/resource_dereference_metadata_service_test.go +++ /dev/null @@ -1,198 +0,0 @@ -package tests - -import ( - "fmt" - "strings" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/cheqd/did-resolver/services" - "github.com/cheqd/did-resolver/types" -) - -type dereferenceResourceMetadataTestCase struct { - did string - resourceId string - dereferencingType types.ContentType - expectedResourceDereferencing *types.ResourceDereferencing - expectedError *types.IdentityError -} - -var _ = DescribeTable("Test DereferenceResourceMetadata method", func(testCase dereferenceResourceMetadataTestCase) { - resourceService := services.NewResourceService(ValidMethod, mockLedgerService) - - expectedContentType := defineContentType( - testCase.expectedResourceDereferencing.DereferencingMetadata.ContentType, - testCase.dereferencingType, - ) - - dereferencingResult, err := resourceService.DereferenceResourceMetadata(testCase.did, testCase.resourceId, testCase.dereferencingType) - if err != nil { - Expect(testCase.expectedError.Code).To(Equal(err.Code)) - Expect(testCase.expectedError.Message).To(Equal(err.Message)) - } else { - Expect(testCase.expectedResourceDereferencing.ContentStream).To(Equal(dereferencingResult.ContentStream)) - Expect(testCase.expectedResourceDereferencing.Metadata).To(Equal(dereferencingResult.Metadata)) - Expect(expectedContentType).To(Equal(dereferencingResult.DereferencingMetadata.ContentType)) - Expect(testCase.expectedResourceDereferencing.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) - Expect(dereferencingResult.DereferencingMetadata.ResolutionError).To(BeEmpty()) - } -}, - - Entry( - "successful dereferencing for resource", - dereferenceResourceMetadataTestCase{ - did: ValidDid, - resourceId: ValidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: dereferencedResourceList, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: nil, - }, - ), - - Entry( - "successful dereferencing for resource (upper case UUID)", - dereferenceResourceMetadataTestCase{ - did: ValidDid, - resourceId: strings.ToUpper(ValidResourceId), - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: dereferencedResourceList, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: nil, - }, - ), - - Entry( - "resource not found", - dereferenceResourceMetadataTestCase{ - did: ValidDid, - resourceId: NotExistIdentifier, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError(ValidDid, types.DIDJSONLD, nil, true), - }, - ), - - Entry( - "invalid resource id", - dereferenceResourceMetadataTestCase{ - did: ValidDid, - resourceId: InvalidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError(ValidDid, types.DIDJSONLD, nil, true), - }, - ), - - Entry( - "invalid did method", - dereferenceResourceMetadataTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", InvalidMethod, ValidNamespace, ValidIdentifier), - resourceId: ValidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: InvalidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", InvalidMethod, ValidNamespace, ValidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - Entry( - "invalid did namespace", - dereferenceResourceMetadataTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", ValidMethod, InvalidNamespace, ValidIdentifier), - resourceId: ValidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", ValidMethod, InvalidNamespace, ValidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - Entry( - "invalid did identifier", - dereferenceResourceMetadataTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", ValidMethod, ValidNamespace, InvalidIdentifier), - resourceId: ValidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: InvalidDid, - MethodSpecificId: InvalidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", ValidMethod, ValidNamespace, InvalidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - // TODO: add unit tests for: - // - invalid DID and invalid resourceId - // - not existent DID and existent resourceId - // - not existent DID and not existent resourceId. -) diff --git a/tests/unit/resource_dereference_resource_data_test.go b/tests/unit/resource_dereference_resource_data_test.go deleted file mode 100644 index e7f26574..00000000 --- a/tests/unit/resource_dereference_resource_data_test.go +++ /dev/null @@ -1,194 +0,0 @@ -package tests - -import ( - "fmt" - "strings" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/cheqd/did-resolver/services" - "github.com/cheqd/did-resolver/types" -) - -type dereferenceResourceDataTestCase struct { - did string - resourceId string - dereferencingType types.ContentType - expectedResourceDereferencing *types.ResourceDereferencing - expectedError *types.IdentityError -} - -var _ = DescribeTable("Test DereferenceResourceData method", func(testCase dereferenceResourceDataTestCase) { - resourceService := services.NewResourceService(ValidMethod, mockLedgerService) - - expectedContentType := types.ContentType(validResource.Metadata.MediaType) - dereferencingResult, err := resourceService.DereferenceResourceData(testCase.did, testCase.resourceId, testCase.dereferencingType) - if err != nil { - Expect(testCase.expectedError.Code).To(Equal(err.Code)) - Expect(testCase.expectedError.Message).To(Equal(err.Message)) - } else { - Expect(testCase.expectedResourceDereferencing.ContentStream.GetBytes()).To(Equal(dereferencingResult.ContentStream.GetBytes())) - Expect(testCase.expectedResourceDereferencing.Metadata).To(Equal(dereferencingResult.Metadata)) - Expect(expectedContentType).To(Equal(dereferencingResult.DereferencingMetadata.ContentType)) - Expect(testCase.expectedResourceDereferencing.DereferencingMetadata.DidProperties).To(Equal(dereferencingResult.DereferencingMetadata.DidProperties)) - Expect(dereferencingResult.DereferencingMetadata.ResolutionError).To(BeEmpty()) - } -}, - - Entry( - "successful dereferencing for resource", - dereferenceResourceDataTestCase{ - did: ValidDid, - resourceId: ValidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: &validResourceDereferencing, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: nil, - }, - ), - - Entry( - "successful dereferencing for resource (upper case UUID)", - dereferenceResourceDataTestCase{ - did: ValidDid, - resourceId: strings.ToUpper(ValidResourceId), - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - ContentStream: &validResourceDereferencing, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: nil, - }, - ), - - Entry( - "resource not found", - dereferenceResourceDataTestCase{ - did: ValidDid, - resourceId: NotExistIdentifier, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError(ValidDid, types.DIDJSONLD, nil, true), - }, - ), - - Entry( - "invalid resource id", - dereferenceResourceDataTestCase{ - did: ValidDid, - resourceId: InvalidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError(ValidDid, types.DIDJSONLD, nil, true), - }, - ), - - Entry( - "invalid method", - dereferenceResourceDataTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", InvalidMethod, ValidNamespace, ValidIdentifier), - resourceId: InvalidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: InvalidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", InvalidMethod, ValidNamespace, ValidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - Entry( - "invalid namespace", - dereferenceResourceDataTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", ValidMethod, InvalidNamespace, ValidIdentifier), - resourceId: InvalidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: ValidDid, - MethodSpecificId: ValidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", ValidMethod, InvalidNamespace, ValidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - Entry( - "invalid identifier", - dereferenceResourceDataTestCase{ - did: fmt.Sprintf("did:%s:%s:%s", ValidMethod, ValidNamespace, InvalidIdentifier), - resourceId: InvalidResourceId, - dereferencingType: types.DIDJSON, - expectedResourceDereferencing: &types.ResourceDereferencing{ - DereferencingMetadata: types.DereferencingMetadata{ - DidProperties: types.DidProperties{ - DidString: InvalidDid, - MethodSpecificId: InvalidIdentifier, - Method: ValidMethod, - }, - }, - Metadata: types.ResolutionResourceMetadata{}, - }, - expectedError: types.NewNotFoundError( - fmt.Sprintf("did:%s:%s:%s", ValidMethod, ValidNamespace, InvalidIdentifier), - types.DIDJSONLD, nil, true, - ), - }, - ), - - // TODO: add unit tests for: - // - invalid DID and invalid resourceId - // - not existent DID and existent resourceId - // - not existent DID and not existent resourceId. -) diff --git a/tests/unit/test_suite_test.go b/tests/unit/test_suite_test.go deleted file mode 100644 index d9599adc..00000000 --- a/tests/unit/test_suite_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package tests - -import ( - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -func TestTestsGeneral(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "UNIT TESTS") -} diff --git a/tests/unit/test_utils.go b/tests/unit/test_utils.go index 02a134f9..556b3b9a 100644 --- a/tests/unit/test_utils.go +++ b/tests/unit/test_utils.go @@ -1,163 +1,22 @@ -package tests +//go:build unit + +package unit import ( - "crypto/sha256" - "fmt" "net/http" "net/http/httptest" - "net/url" didTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/did/v2" resourceTypes "github.com/cheqd/cheqd-node/api/v2/cheqd/resource/v2" "github.com/cheqd/did-resolver/services" didDocServices "github.com/cheqd/did-resolver/services/diddoc" resourceServices "github.com/cheqd/did-resolver/services/resource" + testconstants "github.com/cheqd/did-resolver/tests/constants" "github.com/cheqd/did-resolver/types" "github.com/labstack/echo/v4" - "google.golang.org/protobuf/types/known/timestamppb" -) - -var mockLedgerService = NewMockLedgerService(&validDIDDoc, &validMetadata, &validResource) - -const ( - ValidMethod = "cheqd" - ValidNamespace = "mainnet" - ValidIdentifier = "fb53dd05-329b-4614-a3f2-c0a8c7554ee3" - ValidDid = "did:" + ValidMethod + ":" + ValidNamespace + ":" + ValidIdentifier - ValidResourceId = "a09abea0-22e0-4b35-8f70-9cc3a6d0b5fd" - ValidPubKeyJWK = "{" + - "\"crv\":\"Ed25519\"," + - "\"kid\":\"_Qq0UL2Fq651Q0Fjd6TvnYE-faHiOpRlPVQcY_-tA4A\"," + - "\"kty\":\"OKP\"," + - "\"x\":\"VCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ\"" + - "}" - ValidVersionId = "test_version_id" ) -const ( - InvalidMethod = "invalid_method" - InvalidNamespace = "invalid_namespace" - InvalidIdentifier = "invalid_identifier" - InvalidDid = "did:" + InvalidMethod + ":" + InvalidNamespace + ":" + InvalidIdentifier - InvalidResourceId = "invalid_resource_id" -) - -const ( - NotExistIdentifier = "fb53dd05-329b-4614-a3f2-c0a8c7ffffff" - NotExistDID = "did:" + ValidMethod + ":" + ValidNamespace + ":" + NotExistIdentifier - NotExistFragmentId = "not_found_fragment_id" -) - -var ( - EmptyTimestamp = ×tamppb.Timestamp{ - Seconds: 0, - Nanos: 0, - } - EmptyTime = EmptyTimestamp.AsTime() - - NotEmptyTimestamp = ×tamppb.Timestamp{ - Seconds: 123456789, - Nanos: 0, - } - NotEmptyTime = NotEmptyTimestamp.AsTime() -) - -var ( - ResourceData = []byte("test_checksum") - ResourceMetadata = resourceTypes.Metadata{ - CollectionId: ValidIdentifier, - Id: ValidResourceId, - Name: "Existing Resource Name", - ResourceType: "CL-Schema", - MediaType: "application/json", - Checksum: generateChecksum(ResourceData), - } - - ValidMetadataResource = types.DereferencedResource{ - ResourceURI: ValidDid + types.RESOURCE_PATH + ResourceMetadata.Id, - CollectionId: ResourceMetadata.CollectionId, - ResourceId: ResourceMetadata.Id, - Name: ResourceMetadata.Name, - ResourceType: ResourceMetadata.ResourceType, - MediaType: ResourceMetadata.MediaType, - Created: &EmptyTime, - Checksum: ResourceMetadata.Checksum, - PreviousVersionId: nil, - NextVersionId: nil, - } -) - -var ( - validDIDDoc = ValidDIDDoc() - validVerificationMethod = ValidVerificationMethod() - validDIDDocResolution = types.NewDidDoc(&validDIDDoc) - validMetadata = ValidMetadata() - validService = ValidService() - validResource = ValidResource() - validFragmentMetadata = types.NewResolutionDidDocMetadata(ValidDid, &validMetadata, []*resourceTypes.Metadata{}) - validQuery, _ = url.ParseQuery("attr=value") -) - -var dereferencedResourceList = types.NewDereferencedResourceList(ValidDid, []*resourceTypes.Metadata{validResource.Metadata}) - -func ValidVerificationMethod() didTypes.VerificationMethod { - return didTypes.VerificationMethod{ - Id: ValidDid + "#key-1", - VerificationMethodType: "JsonWebKey2020", - Controller: ValidDid, - VerificationMaterial: ValidPubKeyJWK, - } -} - -func ValidService() didTypes.Service { - return didTypes.Service{ - Id: ValidDid + "#service-1", - ServiceType: "DIDCommMessaging", - ServiceEndpoint: []string{"http://example.com"}, - } -} - -func ValidDIDDoc() didTypes.DidDoc { - service := ValidService() - verificationMethod := ValidVerificationMethod() - - return didTypes.DidDoc{ - Id: ValidDid, - VerificationMethod: []*didTypes.VerificationMethod{&verificationMethod}, - Service: []*didTypes.Service{&service}, - } -} - -func ValidResource() resourceTypes.ResourceWithMetadata { - data := []byte("{\"attr\":[\"name\",\"age\"]}") - checksum := sha256.New().Sum(data) - return resourceTypes.ResourceWithMetadata{ - Resource: &resourceTypes.Resource{ - Data: data, - }, - Metadata: &resourceTypes.Metadata{ - CollectionId: ValidIdentifier, - Id: ValidResourceId, - Name: ValidResourceId, - ResourceType: "string", - MediaType: "application/json", - Checksum: fmt.Sprintf("%x", checksum), - }, - } -} - -func ValidMetadata() didTypes.Metadata { - return didTypes.Metadata{VersionId: "test_version_id", Deactivated: false} -} - -func generateChecksum(data []byte) string { - h := sha256.New() - h.Write(data) - - return fmt.Sprintf("%x", h.Sum(nil)) -} - -func defineContentType(expectedContentType types.ContentType, resolutionType types.ContentType) types.ContentType { +func DefineContentType(expectedContentType types.ContentType, resolutionType types.ContentType) types.ContentType { if expectedContentType == "" { return resolutionType } @@ -165,7 +24,7 @@ func defineContentType(expectedContentType types.ContentType, resolutionType typ return expectedContentType } -func setupEmptyContext(request *http.Request, resolutionType types.ContentType, ledgerService services.LedgerServiceI) (echo.Context, *httptest.ResponseRecorder) { +func SetupEmptyContext(request *http.Request, resolutionType types.ContentType, ledgerService services.LedgerServiceI) (echo.Context, *httptest.ResponseRecorder) { e := echo.New() didDocServices.SetRoutes(e) resourceServices.SetRoutes(e) @@ -187,6 +46,15 @@ func setupEmptyContext(request *http.Request, resolutionType types.ContentType, return rc, rec } +type RedirectDIDTestCase struct { + DidURL string + ResolutionType types.ContentType + ExpectedDidURLRedirect string + ExpectedError error +} + +var MockLedger = NewMockLedgerService(&testconstants.ValidDIDDoc, &testconstants.ValidMetadata, &testconstants.ValidResource) + type MockLedgerService struct { Did *didTypes.DidDoc Metadata *didTypes.Metadata diff --git a/types/errors.go b/types/errors.go index df4a1502..1237587e 100644 --- a/types/errors.go +++ b/types/errors.go @@ -57,11 +57,11 @@ func NewIdentityError(code int, message string, isDereferencing bool, did string return &e } -func NewInvalidDIDError(did string, contentType ContentType, err error, isDereferencing bool) *IdentityError { +func NewInvalidDidError(did string, contentType ContentType, err error, isDereferencing bool) *IdentityError { return NewIdentityError(InvalidDidHttpCode, "invalidDid", isDereferencing, did, contentType, err) } -func NewInvalidDIDUrlError(did string, contentType ContentType, err error, isDereferencing bool) *IdentityError { +func NewInvalidDidUrlError(did string, contentType ContentType, err error, isDereferencing bool) *IdentityError { return NewIdentityError(InvalidDidUrlHttpCode, "invalidDidUrl", isDereferencing, did, contentType, err) } diff --git a/utils/error_handler.go b/utils/error_handler.go index e9c504b5..5c644e8d 100644 --- a/utils/error_handler.go +++ b/utils/error_handler.go @@ -34,5 +34,5 @@ func generateIdentityError(err error) *types.IdentityError { if !ok || he.Code != http.StatusNotFound { return types.NewInternalError("", types.JSON, err, false) } - return types.NewInvalidDIDUrlError("", types.JSON, err, true) + return types.NewInvalidDidUrlError("", types.JSON, err, true) }