Skip to content

Commit

Permalink
test: Add additional unit tests for resolver scenarios [DEV-2360] (#134)
Browse files Browse the repository at this point in the history
* Add integration test data payloads.

* Add integration test constants.

* Create test runner for running integration tests.

* Add positive integration tests for getting DIDDoc.

* Add negative integration tests for getting DIDDoc.

* Update go.mod and go.sum files.

* Move DID document payloads to another folder.

* Add did#fragment query payloads for integration
tests.

* Rename testCase member variable:
- from "expectedDIDResolution" to "expectedDidResolution";
- from "receivedDIDResolution" to "receivedDidResolution";
- from "expectedDIDResolution" to "expectedDidResolution".

* Add positive and integration tests for testing API
that gets DID#fragment.

* Fix golangci-lint mistakes.

* Add build tag for building integration tests.

* Fix golangci-lint mistakes.

* Add DIDDoc version payloads for using in
integration tests.

* Add DIDDoc version metadata payloads for using in
integration tests.

* Add integration tests for testing resourceMetadata

* Add integration tests for testing collection of
resources API.

* Add integration tests for testing resolver resource data
request.

* Refactor integration tests.

* Add integration tests for testing get DIDDoc
versions API.

* Add integration tests for testing get DIDDoc
version API.

* Add integration tests for testing get DIDDoc
version metadata API.

* Add integration tests for testing how is working
redirect when we try to get different API with old Indy style DID.

* Refactor and update integration tests.

* Remove an old integration tests.

* Update integration tests.

* Update integration tests for GitHub actions.

* Update GitHub action test.yml file.

* Add integration tests report file to .gitignore.

* Update test.yml

* DIDDoc version should return in resolution format.

* Uncomment integration tests.

* Add integration tests for testing accept header.

* Refactor integration tests.

* Add integration tests for testing accept-encoding
header.

* Rename folder name from "unit-tests" to "unit".

* Update test.yml file.

* Restructure integration tests folder structure.

* Remove an unused ProcessDIDRequest method.

* Move test from did_doc_service and
resource_dereferencing_service to particular handlers.

* Use constant variables instead of a header strings

* Add unit tests for testing how works a redirect
old 16/32 characters Indy style DIDs.

* Re structure unit tests.

* Add integration build flag for test_suite_test.go.

* Refactor unit tests.

* Add more unit tests for testing ledger services.

* Add HTTP binding constants for using tests.

* Correct the names of the integration test cases.

* Add constant variables for using integration tests
instead of magic strings.

* Fix type mistakes.

* Fix golangci-lint mistakes.

* Update integration tests.

* Update unit test runner command in GitHub actions.
  • Loading branch information
abdulla-ashurov authored Apr 11, 2023
1 parent fa2de8a commit bd4ea50
Show file tree
Hide file tree
Showing 66 changed files with 2,050 additions and 1,675 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions services/diddoc/diddoc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion services/diddoc/diddoc_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion services/diddoc/diddoc_version_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
38 changes: 0 additions & 38 deletions services/diddoc_service.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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, "")

Expand Down
8 changes: 4 additions & 4 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions services/request_service_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion services/resource/resource_data_dereferencing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion services/resource/resource_metadata_dereferencing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
66 changes: 66 additions & 0 deletions tests/constants/common.go
Original file line number Diff line number Diff line change
@@ -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))
}
79 changes: 74 additions & 5 deletions tests/constants/constants.go
Original file line number Diff line number Diff line change
@@ -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 = "*/*"
Expand Down Expand Up @@ -40,18 +46,55 @@ 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 (
ValidMethod = "cheqd"
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 (
Expand All @@ -65,6 +108,32 @@ var (
InvalidIdentifier = "invalid_identifier"
)

var (
EmptyTimestamp = &timestamppb.Timestamp{
Seconds: 0,
Nanos: 0,
}
EmptyTime = EmptyTimestamp.AsTime()

NotEmptyTimestamp = &timestamppb.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"
Loading

0 comments on commit bd4ea50

Please sign in to comment.