Skip to content

Commit

Permalink
update tests, fix a bug in Resource dereferencing
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Jul 13, 2022
1 parent 819813c commit df241ce
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
6 changes: 3 additions & 3 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (ls LedgerService) QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool
return *didDocResponse.Did, *didDocResponse.Metadata, true, err
}

func (ls LedgerService) QueryResource(collectionDid string, resourceId string) (resource.Resource, bool, error) {
collectionId, namespace, _, _ := cheqdUtils.TrySplitDID(collectionDid)
func (ls LedgerService) QueryResource(did string, resourceId string) (resource.Resource, bool, error) {
collectionId, namespace, _, _ := cheqdUtils.TrySplitDID(did)
serverAddr, namespaceFound := ls.ledgers[namespace]
if !namespaceFound {
return resource.Resource{}, false, fmt.Errorf("namespace not supported: %s", namespace)
Expand All @@ -94,7 +94,7 @@ func (ls LedgerService) QueryResource(collectionDid string, resourceId string) (
return resource.Resource{}, false, err
}

log.Info().Msgf("Querying did resource: %s, %s", collectionDid, resourceId)
log.Info().Msgf("Querying did resource: %s, %s", did, resourceId)

client := resource.NewQueryClient(conn)
resourceResponse, err := client.Resource(context.Background(), &resource.QueryGetResourceRequest{CollectionId: collectionId, Id: resourceId})
Expand Down
5 changes: 2 additions & 3 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ func (rs RequestService) dereferencePrimary(path string, did string, didUrl stri
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingNotSupported)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}
_, _, collectionId, _ := cheqdUtils.TrySplitDID(did)

resource, isFound, err := rs.ledgerService.QueryResource(collectionId, resourceId)

resource, isFound, err := rs.ledgerService.QueryResource(did, resourceId)
if err != nil {
return types.DidDereferencing{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions services/request_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func TestDereferencing(t *testing.T) {
name: "successful Primary dereferencing (resource)",
ledgerService: NewMockLedgerService(validDIDDoc, validMetadata(), validResource),
dereferencingType: types.DIDJSONLD,
didUrl: validDid + "/resource/" + validResourceId,
didUrl: validDid + "/resources/" + validResourceId,
expectedContentStream: fmt.Sprintf("{\"@context\":[\"%s\"],\"collectionId\":\"%s\",\"id\":\"%s\",\"name\":\"%s\",\"resourceType\":\"%s\",\"mediaType\":\"%s\",\"checksum\":%s,\"data\":%s}",
types.DIDSchemaJSONLD, validResource.Header.CollectionId, validResource.Header.Id, validResource.Header.Name, validResource.Header.ResourceType, validResource.Header.MediaType, validChecksum, validData),
expectedMetadata: cheqd.Metadata{},
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestDereferencing(t *testing.T) {
name: "resource not found",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}),
dereferencingType: types.DIDJSONLD,
didUrl: validDid + "/resource/00000000-0000-0000-0000-000000000000",
didUrl: validDid + "/resources/00000000-0000-0000-0000-000000000000",
expectedMetadata: cheqd.Metadata{},
expectedError: types.DereferencingNotFound,
},
Expand Down
1 change: 1 addition & 0 deletions tests/pytest/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
TESTNET_FRAGMENT = TESTNET_DID + "#key1"
FAKE_TESTNET_DID = "did:cheqd:testnet:zF7rhDBfUt9d1gJPjx7s1JXfUY7oVWkY"
FAKE_TESTNET_FRAGMENT = TESTNET_DID + "#fake_key"
FAKE_TESTNET_RESOURCE = TESTNET_DID + "/resources/76471e8c-0d1c-4b97-9b11-17b65e024334"

MAINNET_DID = "did:cheqd:mainnet:zF7rhDBfUt9d1gJPjx7s1JXfUY7oVWkY"
MAINNET_FRAGMENT = MAINNET_DID + "#key1"
Expand Down
28 changes: 15 additions & 13 deletions tests/pytest/test_resolution.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import pytest
from helpers import run, TESTNET_DID, MAINNET_DID, TESTNET_FRAGMENT, MAINNET_FRAGMENT, \
FAKE_TESTNET_DID, FAKE_MAINNET_DID, FAKE_TESTNET_FRAGMENT, FAKE_MAINNET_FRAGMENT, RESOLVER_URL, PATH
FAKE_TESTNET_DID, FAKE_MAINNET_DID, FAKE_TESTNET_FRAGMENT, FAKE_MAINNET_FRAGMENT, RESOLVER_URL, PATH, \
FAKE_TESTNET_RESOURCE


@pytest.mark.parametrize(
"did_url, expected_output",
[
(TESTNET_DID, fr"didDocument(.*?)\"id\":\"{TESTNET_DID}\"(.*?)didDocumentMetadata"
r"(.*?)didResolutionMetadata"),
(MAINNET_DID, fr"didDocument(.*?)\"id\":\"{MAINNET_DID}\"(.*?)didDocumentMetadata"
r"(.*?)didResolutionMetadata"),
(FAKE_TESTNET_DID, r"didDocument\":null,\"didDocumentMetadata\":\[\],"
r"\"didResolutionMetadata(.*?)\"error\":\"notFound\""),
(FAKE_MAINNET_DID, r"didDocument\":null,\"didDocumentMetadata\":\[\],"
r"\"didResolutionMetadata(.*?)\"error\":\"notFound\""),
("did:wrong_method:MTMxDQKMTMxDQKMT", r"didDocument\":null,\"didDocumentMetadata\":\[\],"
r"\"didResolutionMetadata(.*?)\"error\":\"methodNotSupported\""),
(TESTNET_DID, fr"didResolutionMetadata(.*?)didDocument(.*?)\"id\":\"{TESTNET_DID}\"(.*?)didDocumentMetadata"),
(MAINNET_DID, fr"didResolutionMetadata(.*?)didDocument(.*?)\"id\":\"{MAINNET_DID}\"(.*?)didDocumentMetadata"),
(FAKE_TESTNET_DID, r"\"didResolutionMetadata(.*?)\"error\":\"notFound\"(.*?)"
r"didDocument\":null,\"didDocumentMetadata\":\[\]"),
(FAKE_MAINNET_DID, r"\"didResolutionMetadata(.*?)\"error\":\"notFound\"(.*?)"
r"didDocument\":null,\"didDocumentMetadata\":\[\]"),
("did:wrong_method:MTMxDQKMTMxDQKMT", r"\"didResolutionMetadata(.*?)\"error\":\"methodNotSupported\"(.*?)"
r"didDocument\":null,\"didDocumentMetadata\":\[\]"),
(TESTNET_FRAGMENT, fr"\"contentStream\":(.*?)\"id\":\"{TESTNET_FRAGMENT}\"(.*?)contentMetadata"
r"(.*?)dereferencingMetadata\""),
(MAINNET_FRAGMENT, fr"\"contentStream\":(.*?)\"id\":\"{MAINNET_FRAGMENT}\"(.*?)contentMetadata"
r"(.*?)dereferencingMetadata\""),
(FAKE_TESTNET_FRAGMENT, r"\"contentStream\":null,\"contentMetadata\":\[\],"
r"\"dereferencingMetadata(.*?)\"error\":\"FragmentNotFound\""),
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
(FAKE_MAINNET_FRAGMENT, r"\"contentStream\":null,\"contentMetadata\":\[\],"
r"\"dereferencingMetadata(.*?)\"error\":\"FragmentNotFound\""),
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
(FAKE_TESTNET_RESOURCE, r"\"contentStream\":null,\"contentMetadata\":\[\],"
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
]
)
def test_resolution(did_url, expected_output):
Expand Down
2 changes: 1 addition & 1 deletion utils/did_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"regexp"
)

var ResourcePath, _ = regexp.Compile(`resource\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`)
var ResourcePath, _ = regexp.Compile(`resources\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`)

func GetResourceId(didUrlPath string) (id string) {
match := ResourcePath.FindStringSubmatch(didUrlPath)
Expand Down

0 comments on commit df241ce

Please sign in to comment.