Skip to content

Commit

Permalink
Add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Feb 6, 2025
1 parent 548d6da commit 35be15c
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (dr *ResourceDataWithMetadataDereferencingService) SpecificValidation(c ser
}

func (dr *ResourceDataWithMetadataDereferencingService) Query(c services.ResolverContext) error {
result, err := c.ResourceService.DereferenceResourceWithMetadata(dr.GetDid(), dr.ResourceId, dr.GetContentType())
result, err := c.ResourceService.DereferenceResourceDataWithMetadata(dr.GetDid(), dr.ResourceId, dr.GetContentType())
if err != nil {
err.IsDereferencing = dr.IsDereferencing
return err
Expand Down
2 changes: 1 addition & 1 deletion services/resource_dereference_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (rds ResourceService) DereferenceResourceData(did string, resourceId string
return &types.ResourceDereferencing{ContentStream: &result, DereferencingMetadata: dereferenceMetadata}, nil
}

func (rds ResourceService) DereferenceResourceWithMetadata(did string, resourceId string, contentType types.ContentType) (*types.ResourceDereferencing, *types.IdentityError) {
func (rds ResourceService) DereferenceResourceDataWithMetadata(did string, resourceId string, contentType types.ContentType) (*types.ResourceDereferencing, *types.IdentityError) {
dereferenceMetadata := types.NewDereferencingMetadata(did, contentType, "")

resource, err := rds.ledgerService.QueryResource(did, strings.ToLower(resourceId))
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/rest/resource/data/positive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,21 @@ var _ = DescribeTable("Positive: Get resource data", func(testCase utils.Positiv
ExpectedStatusCode: http.StatusOK,
},
),

Entry(
"can get resource with metadata with an existent DID, and supported JSONLD resolution type and W3DIDUrl dereferencing param",
utils.PositiveTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.UUIDStyleTestnetDid,
testconstants.UUIDStyleTestnetDidResourceId,
),
ResolutionType: string(types.JSONLD) + ";profile=" + string(types.W3IDDIDURL),
EncodingType: testconstants.DefaultEncodingType,
ExpectedEncodingType: "gzip",
ExpectedJSONPath: "../../testdata/resource_data_with_metadata/resource.json",
ExpectedStatusCode: http.StatusOK,
},
),
)
189 changes: 189 additions & 0 deletions tests/integration/rest/resource/data_with_metadata/negative_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
//go:build integration

package data_test

import (
"encoding/json"
"fmt"

testconstants "github.com/cheqd/did-resolver/tests/constants"
utils "github.com/cheqd/did-resolver/tests/integration/rest"
"github.com/cheqd/did-resolver/types"
errors "github.com/cheqd/did-resolver/types"
"github.com/go-resty/resty/v2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = DescribeTable("Negative: Get resource data", func(testCase utils.NegativeTestCase) {
client := resty.New()

resp, err := client.R().
SetHeader("Accept", testCase.ResolutionType).
Get(testCase.DidURL)
Expect(err).To(BeNil())

var receivedDidDereferencing utils.DereferencingResult
Expect(json.Unmarshal(resp.Body(), &receivedDidDereferencing)).To(BeNil())
Expect(testCase.ExpectedStatusCode).To(Equal(resp.StatusCode()))

expectedDidDereferencing := testCase.ExpectedResult.(utils.DereferencingResult)
utils.AssertDidDereferencing(expectedDidDereferencing, receivedDidDereferencing)
},

Entry(
"cannot get resource data with not existent DID and a valid resourceId",
utils.NegativeTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.NotExistentMainnetDid,
testconstants.ValidIdentifier,
),
ResolutionType: string(types.JSONLD) + ";profile=" + string(types.W3IDDIDURL),
ExpectedResult: utils.DereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.DIDJSONLD,
ResolutionError: "notFound",
DidProperties: types.DidProperties{
DidString: testconstants.NotExistentMainnetDid,
MethodSpecificId: testconstants.NotExistentIdentifier,
Method: testconstants.ValidMethod,
},
},
ContentStream: nil,
Metadata: types.ResolutionDidDocMetadata{},
},
ExpectedStatusCode: errors.NotFoundHttpCode,
},
),

Entry(
"cannot get resource data with an invalid DID and not existent resourceId",
utils.NegativeTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.InvalidDid,
testconstants.ValidIdentifier,
),
ResolutionType: string(types.JSONLD) + ";profile=" + string(types.W3IDDIDURL),
ExpectedResult: utils.DereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.DIDJSONLD,
ResolutionError: "methodNotSupported",
DidProperties: types.DidProperties{
DidString: testconstants.InvalidDid,
MethodSpecificId: testconstants.InvalidIdentifier,
Method: testconstants.InvalidMethod,
},
},
ContentStream: nil,
Metadata: types.ResolutionDidDocMetadata{},
},
ExpectedStatusCode: errors.MethodNotSupportedHttpCode,
},
),

Entry(
"cannot get resource data with not existent DID and resourceId",
utils.NegativeTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.NotExistentTestnetDid,
testconstants.NotExistentIdentifier,
),
ResolutionType: string(types.JSONLD) + ";profile=" + string(types.W3IDDIDURL),
ExpectedResult: utils.DereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.DIDJSONLD,
ResolutionError: "notFound",
DidProperties: types.DidProperties{
DidString: testconstants.NotExistentTestnetDid,
MethodSpecificId: testconstants.NotExistentIdentifier,
Method: testconstants.ValidMethod,
},
},
ContentStream: nil,
Metadata: types.ResolutionDidDocMetadata{},
},
ExpectedStatusCode: errors.NotFoundHttpCode,
},
),

Entry(
"cannot get resource data with an existent DID and an invalid resourceId",
utils.NegativeTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.IndyStyleMainnetDid,
testconstants.InvalidIdentifier,
),
ResolutionType: string(types.JSONLD) + ";profile=" + string(types.W3IDDIDURL),
ExpectedResult: utils.DereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.DIDJSONLD,
ResolutionError: "invalidDidUrl",
DidProperties: types.DidProperties{},
},
ContentStream: nil,
Metadata: types.ResolutionDidDocMetadata{},
},
ExpectedStatusCode: errors.InvalidDidUrlHttpCode,
},
),

Entry(
"cannot get resource data with an existent old 16 characters Indy style DID and an invalid resourceId",
utils.NegativeTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.OldIndy16CharStyleTestnetDid,
testconstants.InvalidIdentifier,
),
ResolutionType: string(types.JSONLD) + ";profile=" + string(types.W3IDDIDURL),
ExpectedResult: utils.DereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.DIDJSONLD,
ResolutionError: "invalidDidUrl",
DidProperties: types.DidProperties{},
},
ContentStream: nil,
Metadata: types.ResolutionDidDocMetadata{},
},
ExpectedStatusCode: errors.InvalidDidUrlHttpCode,
},
),

Entry(
"cannot get resource data with an existent old 32 characters Indy style DID and an invalid resourceId",
utils.NegativeTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.OldIndy32CharStyleTestnetDid,
testconstants.InvalidIdentifier,
),
ResolutionType: string(types.JSONLD) + ";profile=" + string(types.W3IDDIDURL),
ExpectedResult: utils.DereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.DIDJSONLD,
ResolutionError: "invalidDidUrl",
DidProperties: types.DidProperties{},
},
ContentStream: nil,
Metadata: types.ResolutionDidDocMetadata{},
},
ExpectedStatusCode: errors.InvalidDidUrlHttpCode,
},
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build integration

package data_test

import (
"encoding/json"
"fmt"
"net/http"

testconstants "github.com/cheqd/did-resolver/tests/constants"
utils "github.com/cheqd/did-resolver/tests/integration/rest"
"github.com/cheqd/did-resolver/types"
"github.com/go-resty/resty/v2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = DescribeTable("Positive: Get resource data with metadata", func(testCase utils.PositiveTestCase) {
client := resty.New()

resp, err := client.R().
SetHeader("Accept", testCase.ResolutionType).
SetHeader("Accept-Encoding", testCase.EncodingType).
Get(testCase.DidURL)
Expect(err).To(BeNil())

var receivedResourceDataWithMetadata any
Expect(json.Unmarshal(resp.Body(), &receivedResourceDataWithMetadata)).To(BeNil())
Expect(testCase.ExpectedStatusCode).To(Equal(resp.StatusCode()))

var expectedResourceDataWithMetadata any
Expect(utils.ConvertJsonFileToType(testCase.ExpectedJSONPath, &expectedResourceDataWithMetadata)).To(BeNil())

Expect(testCase.ExpectedEncodingType).To(Equal(resp.Header().Get("Content-Encoding")))
utils.AssetResourceDataWithMetadata(expectedResourceDataWithMetadata, receivedResourceDataWithMetadata)
},

Entry(
"can get resource with metadata with an existent DID, and supported JSONLD resolution type and W3DIDUrl dereferencing param",
utils.PositiveTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.UUIDStyleTestnetDid,
testconstants.UUIDStyleTestnetDidResourceId,
),
ResolutionType: string(types.JSONLD) + ";profile=" + string(types.W3IDDIDURL),
EncodingType: testconstants.DefaultEncodingType,
ExpectedEncodingType: "gzip",
ExpectedJSONPath: "../../testdata/resource_data_with_metadata/resource.json",
ExpectedStatusCode: http.StatusOK,
},
),
)
15 changes: 15 additions & 0 deletions tests/integration/rest/resource/data_with_metadata/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build integration

package data_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestData(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "[Integration Test]: Resource Data with Metadata")
}
6 changes: 3 additions & 3 deletions tests/integration/rest/resource/metadata/positive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ var _ = DescribeTable("Positive: get resource metadata", func(testCase utils.Pos
),

Entry(
"can get DIDDoc version with an existent DID, and supported JSONLD resolution type",
"can get resource metadata with an existent DID, and supported JSONLD resolution type",
utils.PositiveTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s/metadata",
Expand All @@ -124,7 +124,7 @@ var _ = DescribeTable("Positive: get resource metadata", func(testCase utils.Pos
),

Entry(
"can get DIDDoc version with an existent DID, and supported gzip encoding type",
"can get resource metadata version with an existent DID, and supported gzip encoding type",
utils.PositiveTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s/metadata",
Expand All @@ -141,7 +141,7 @@ var _ = DescribeTable("Positive: get resource metadata", func(testCase utils.Pos
),

Entry(
"can get DIDDoc version with an existent DID, and supported gzip encoding type",
"can get resource metadata with an existent DID, and supported gzip encoding type",
utils.PositiveTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s/metadata",
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/rest/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ type DereferencingResult struct {
Metadata types.ResolutionDidDocMetadata `json:"contentMetadata"`
}

type ResourceDereferencingResult struct {
Context string `json:"@context,omitempty"`
DereferencingMetadata types.DereferencingMetadata `json:"dereferencingMetadata"`
ContentStream *any `json:"contentStream"`
Metadata *types.DereferencedResource `json:"contentMetadata"`
}

func AssertDidDereferencing(expected DereferencingResult, received DereferencingResult) {
Expect(expected.Context).To(Equal(received.Context))
Expect(expected.DereferencingMetadata.ContentType).To(Equal(received.DereferencingMetadata.ContentType))
Expand All @@ -52,6 +59,19 @@ func AssertDidResolution(expected types.DidResolution, received types.DidResolut
Expect(expected.Metadata).To(Equal(received.Metadata))
}

func AssertResourceDataWithMetadata(expected ResourceDereferencingResult, received ResourceDereferencingResult) {
Expect(expected.Context).To(Equal(received.Context))
Expect(expected.DereferencingMetadata.ContentType).To(Equal(received.DereferencingMetadata.ContentType))
Expect(expected.DereferencingMetadata.ResolutionError).To(Equal(received.DereferencingMetadata.ResolutionError))
Expect(expected.DereferencingMetadata.DidProperties).To(Equal(received.DereferencingMetadata.DidProperties))
Expect(expected.ContentStream).To(Equal(received.ContentStream))
Expect(expected.Metadata.ResourceType).To(Equal(received.Metadata.ResourceType))
Expect(expected.Metadata.ResourceId).To(Equal(received.Metadata.ResourceId))
Expect(expected.Metadata.Version).To(Equal(received.Metadata.Version))
Expect(expected.Metadata.Name).To(Equal(received.Metadata.Name))
Expect(expected.Metadata.CollectionId).To(Equal(received.Metadata.CollectionId))
}

func ConvertJsonFileToType(path string, v any) error {
file, err := os.Open(path)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"@context": "https://w3id.org/did-resolution/v1",
"dereferencingMetadata": {
"contentType": "application/did+ld+json",
"retrieved": "2023-03-27T03:52:40Z",
"did": {
"didString": "did:cheqd:testnet:c1685ca0-1f5b-439c-8eb8-5c0e85ab7cd0",
"methodSpecificId": "c1685ca0-1f5b-439c-8eb8-5c0e85ab7cd0",
"method": "cheqd"
}
},
"contentStream": "eyAKICAgICJjb250ZW50IjogInRlc3QgZGF0YSIKfQ==",
"contentMetadata": {
"resourceURI": "did:cheqd:testnet:c1685ca0-1f5b-439c-8eb8-5c0e85ab7cd0/resources/9ba3922e-d5f5-4f53-b265-fc0d4e988c77",
"resourceCollectionId": "c1685ca0-1f5b-439c-8eb8-5c0e85ab7cd0",
"resourceId": "9ba3922e-d5f5-4f53-b265-fc0d4e988c77",
"resourceName": "Demo Resource",
"resourceType": "String",
"resourceVersion" : "",
"mediaType": "application/json",
"created": "2023-01-25T12:08:39Z",
"checksum": "e1dbc03b50bdb995961dc8843df6539b79d03bf49787ed6462189ee97d27eaf3",
"previousVersionId": null,
"nextVersionId": null
}
}
1 change: 1 addition & 0 deletions tests/unit/diddoc/common/did_doc_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _ = Describe("Test NewResolutionDIDDocMetadata function", func() {
metadata := &didTypes.Metadata{
VersionId: testconstants.ValidIdentifier,
Deactivated: false,
Created: nil,
}

resources := []*resourceTypes.Metadata{
Expand Down
Loading

0 comments on commit 35be15c

Please sign in to comment.