Skip to content

Commit

Permalink
DEV-1074: Add request_service_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Apr 19, 2022
1 parent bf30610 commit cc5c589
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 13 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
method: cheqd
listener: 0.0.0.0:1313
path: "/1.0/identifiers/:did"
ledgerTimeout: 5s #sec
ledgerTimeout: 5s
loglevel: debug

networks:
Expand Down
4 changes: 4 additions & 0 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"google.golang.org/grpc"
)

type LedgerServiceI interface {
QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool, error)
GetNamespaces() []string
}
type LedgerService struct {
ledgers map[string]string // namespace -> url
}
Expand Down
8 changes: 4 additions & 4 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

type RequestService struct {
ledgerService LedgerService
ledgerService LedgerServiceI
didDocService DIDDocService
}

func NewRequestService(ledgerService LedgerService) RequestService {
func NewRequestService(ledgerService LedgerServiceI) RequestService {
return RequestService{
ledgerService: ledgerService,
didDocService: DIDDocService{},
Expand All @@ -25,7 +25,7 @@ func NewRequestService(ledgerService LedgerService) RequestService {

func (rs RequestService) ProcessDIDRequest(did string, resolutionOptions types.ResolutionOption) (string, error) {

didResolution, err := rs.resolve(did, types.ResolutionOption{resolutionOptions.Accept})
didResolution, err := rs.Resolve(did, types.ResolutionOption{resolutionOptions.Accept})
if err != nil {
return "", err
}
Expand Down Expand Up @@ -54,7 +54,7 @@ func (rs RequestService) ProcessDIDRequest(did string, resolutionOptions types.R
}

// https://w3c-ccg.github.io/did-resolution/#resolving
func (rs RequestService) resolve(did string, resolutionOptions types.ResolutionOption) (types.DidResolution, error) {
func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionOption) (types.DidResolution, error) {

didResolutionMetadata := types.NewResolutionMetadata(did, resolutionOptions.Accept, "")

Expand Down
124 changes: 124 additions & 0 deletions services/request_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package services

import (
"fmt"
"testing"

"github.com/cheqd/cheqd-did-resolver/types"
cheqd "github.com/cheqd/cheqd-node/x/cheqd/types"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)

type MockLedgerService struct {
Did cheqd.Did
Metadata cheqd.Metadata
}

func NewMockLedgerService(did cheqd.Did, metadata cheqd.Metadata) MockLedgerService {
return MockLedgerService{
Did: did,
Metadata: metadata,
}
}

func (ls MockLedgerService) QueryDIDDoc(string) (cheqd.Did, cheqd.Metadata, bool, error) {
isFound := true
if ls.Did.Id == "" {
isFound = false
}
return ls.Did, ls.Metadata, isFound, nil
}
func (ls MockLedgerService) GetNamespaces() []string {
return []string{"testnet", "mainnet"}
}

func TestProcessDIDRequest(t *testing.T) {
viper.SetConfigFile("../config.yaml")
viper.ReadInConfig()
validIdentifier := "N22KY2Dyvmuu2Pyy"
validMethod := viper.GetString("method")
validDIDDoc := cheqd.Did{
Id: "did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue",
}
validMetadata := cheqd.Metadata{VersionId: "test_version_id", Deactivated: false}

subtests := []struct {
name string
ledgerService MockLedgerService
resolutionType string
identifier string
method string
expectedDID cheqd.Did
expectedMetadata cheqd.Metadata
expectedError types.ResolutionError
}{
{
name: "successful resolution",
ledgerService: NewMockLedgerService(validDIDDoc, validMetadata),
resolutionType: types.ResolutionDIDJSONLDType,
identifier: validIdentifier,
method: validMethod,
expectedDID: validDIDDoc,
expectedMetadata: validMetadata,
expectedError: "",
},
{
name: "DID not found",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}),
resolutionType: types.ResolutionDIDJSONLDType,
identifier: validIdentifier,
method: validMethod,
expectedDID: cheqd.Did{},
expectedMetadata: cheqd.Metadata{},
expectedError: types.ResolutionNotFound,
},
{
name: "invalid DID",
ledgerService: NewMockLedgerService(validDIDDoc, validMetadata),
resolutionType: types.ResolutionDIDJSONLDType,
identifier: "oooooo0000OOOO_invalid_did",
method: validMethod,
expectedDID: cheqd.Did{},
expectedMetadata: cheqd.Metadata{},
expectedError: types.ResolutionInvalidDID,
},
{
name: "invalid method",
ledgerService: NewMockLedgerService(validDIDDoc, validMetadata),
resolutionType: types.ResolutionDIDJSONLDType,
identifier: validIdentifier,
method: "not_supported_method",
expectedDID: cheqd.Did{},
expectedMetadata: cheqd.Metadata{},
expectedError: types.ResolutionMethodNotSupported,
},
}

for _, subtest := range subtests {
t.Run(subtest.name, func(t *testing.T) {
requestService := NewRequestService(subtest.ledgerService)
id := "did:" + subtest.method + ":testnet:" + subtest.identifier
expectedDIDProperties := types.DidProperties{
DidString: id,
MethodSpecificId: subtest.identifier,
Method: subtest.method,
}
if (subtest.resolutionType == types.ResolutionDIDJSONLDType || subtest.resolutionType == types.ResolutionJSONLDType) && subtest.expectedError == "" {
subtest.expectedDID.Context = []string{types.DIDSchemaJSONLD}
}

resolutionResult, err := requestService.Resolve(id, types.ResolutionOption{Accept: subtest.resolutionType})

fmt.Println(subtest.name + ": resolutionResult:")
fmt.Println(resolutionResult)
require.EqualValues(t, subtest.expectedDID, resolutionResult.Did)
require.EqualValues(t, subtest.expectedMetadata, resolutionResult.Metadata)
require.EqualValues(t, subtest.resolutionType, resolutionResult.ResolutionMetadata.ContentType)
require.EqualValues(t, subtest.expectedError, resolutionResult.ResolutionMetadata.ResolutionError)
require.EqualValues(t, expectedDIDProperties, resolutionResult.ResolutionMetadata.DidProperties)
require.Empty(t, err)
})
}

}
18 changes: 10 additions & 8 deletions types/resolution_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
cheqdUtils "github.com/cheqd/cheqd-node/x/cheqd/utils"
)

type ResolutionError string

const (
ResolutionInvalidDID = "invalidDid"
ResolutionNotFound = "notFound"
ResolutionMethodNotSupported = "methodNotSupported"
ResolutionInvalidDID ResolutionError = "invalidDid"
ResolutionNotFound ResolutionError = "notFound"
ResolutionMethodNotSupported ResolutionError = "methodNotSupported"
)

const (
Expand All @@ -28,10 +30,10 @@ type ResolutionOption struct {
}

type ResolutionMetadata struct {
ContentType string `json:"contentType,omitempty"`
ResolutionError string `json:"error,omitempty"`
Retrieved string `json:"retrieved,omitempty"`
DidProperties DidProperties `json:"did,omitempty"`
ContentType string `json:"contentType,omitempty"`
ResolutionError ResolutionError `json:"error,omitempty"`
Retrieved string `json:"retrieved,omitempty"`
DidProperties DidProperties `json:"did,omitempty"`
}

type DidProperties struct {
Expand All @@ -46,7 +48,7 @@ type DidResolution struct {
ResolutionMetadata ResolutionMetadata `json:"didResolutionMetadata,omitempty"`
}

func NewResolutionMetadata(did string, contentType string, resolutionError string) ResolutionMetadata {
func NewResolutionMetadata(did string, contentType string, resolutionError ResolutionError) ResolutionMetadata {
method, _, id, _ := cheqdUtils.TrySplitDID(did)
return ResolutionMetadata{
ContentType: contentType,
Expand Down

0 comments on commit cc5c589

Please sign in to comment.