Skip to content

Commit

Permalink
fix: resource context
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Jul 19, 2022
1 parent 9052d67 commit 3dc673b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
12 changes: 6 additions & 6 deletions services/diddoc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,24 @@ func (ds DIDDocService) MarshallDID(didDoc cheqd.Did) (string, error) {
func (ds DIDDocService) MarshallContentStream(contentStream protoiface.MessageV1, contentType types.ContentType) (string, error) {
var mapContent orderedmap.OrderedMap
var err error
var context types.ContentType
var context []string
if contentType == types.DIDJSONLD || contentType == types.JSONLD {
context = types.DIDSchemaJSONLD
context = []string{types.DIDSchemaJSONLD}
}

switch contentStream := contentStream.(type) {
case *cheqd.VerificationMethod:
mapContent, err = ds.prepareJWKPubkey(contentStream)
case *cheqd.Did:
contentStream.Context = []string{string(context)}
contentStream.Context = context
jsonDid, err := ds.MarshallDID(*contentStream)
if err != nil {
return "", err
}
return string(jsonDid), nil
case *resource.Resource:
dResource := types.DereferencedResource{
Context: []string{string(context)},
Context: context,
CollectionId: contentStream.Header.CollectionId,
Id: contentStream.Header.Id,
Name: contentStream.Header.Name,
Expand All @@ -107,8 +107,8 @@ func (ds DIDDocService) MarshallContentStream(contentStream protoiface.MessageV1
}

// Context changes
if context != "" {
mapContent.Set("@"+didContext, context)
if len(context) != 0 {
mapContent.Set("@"+didContext, context[0])
mapContent.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool {
return a.Key() == "@"+didContext
})
Expand Down
85 changes: 72 additions & 13 deletions tests/pytest/test_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
(TESTNET_RESOURCE, fr"\"contentStream\":(.*?)collectionId(.*?),\"contentMetadata\":(.*?),"
r"\"dereferencingMetadata(.*?)"),
r"\"dereferencingMetadata(.*?)"),
(FAKE_TESTNET_RESOURCE, r"\"contentStream\":null,\"contentMetadata\":\[\],"
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
]
Expand All @@ -40,25 +40,84 @@ def test_resolution(did_url, expected_output):


@pytest.mark.parametrize(
"accept, expected_header, expected_body",
"accept, expected_header, has_context, expected_body",
[
(LDJSON, LDJSON, r"(.*?)didResolutionMetadata(.*?)application/ld\+json"
r"(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"),
(DIDLDJSON, DIDLDJSON, "(.*?)didResolutionMetadata(.*?)application/did\+ld\+json"
"(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"),
("", DIDLDJSON, "(.*?)didResolutionMetadata(.*?)application/did\+ld\+json"
"(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"),
(DIDJSON, DIDJSON, r"(.*?)didResolutionMetadata(.*?)application/did\+json"
r"(.*?)didDocument(.*?)(?!`@context`)(.*?)didDocumentMetadata"),
(HTML + ",application/xhtml+xml", HTML, fr"(.*?)didResolutionMetadata(.*?){HTML}"
fr"(.*?)didDocument(.*?)(?!`@context`)(.*?)didDocumentMetadata"),
(LDJSON, LDJSON, True, r"(.*?)didResolutionMetadata(.*?)application/ld\+json"
r"(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"),
(DIDLDJSON, DIDLDJSON, True, "(.*?)didResolutionMetadata(.*?)application/did\+ld\+json"
"(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"),
("", DIDLDJSON, True, "(.*?)didResolutionMetadata(.*?)application/did\+ld\+json"
"(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"),
(DIDJSON, DIDJSON, False, r"(.*?)didResolutionMetadata(.*?)application/did\+json"
r"(.*?)didDocument(.*?)(?!`@context`)(.*?)didDocumentMetadata"),
(HTML + ",application/xhtml+xml", HTML, False, fr"(.*?)didResolutionMetadata(.*?){HTML}"
fr"(.*?)didDocument(.*?)(?!`@context`)(.*?)didDocumentMetadata"),
]
)
def test_resolution_content_type(accept, expected_header, expected_body):
def test_resolution_content_type(accept, expected_header, expected_body, has_context):
url = RESOLVER_URL + PATH + TESTNET_DID
header = {"Accept": accept} if accept else {}

r = requests.get(url, headers=header)

assert r.headers["Content-Type"] == expected_header
assert re.match(expected_body, r.text)
if has_context:
assert re.findall(r"context", r.text)
else:
assert not re.findall(r"context", r.text)


dereferencing_content_type_test_set = [
(LDJSON, LDJSON, True,
r"(.*?)contentStream(.*?)@context(.*?)contentMetadata"
r"(.*?)dereferencingMetadata(.*?)application/ld\+json"),
(DIDLDJSON, DIDLDJSON, True,
"(.*?)contentStream(.*?)@context(.*?)contentMetadata"
"(.*?)dereferencingMetadata(.*?)application/did\+ld\+json"),
("", DIDLDJSON, True,
"(.*?)contentStream(.*?)@context(.*?)contentMetadata"
"(.*?)dereferencingMetadata(.*?)application/did\+ld\+json"),
(DIDJSON, DIDJSON, False,
r"(.*?)contentStream(.*?)contentMetadata"
r"(.*?)dereferencingMetadata(.*?)application/did\+json"),
(HTML + ",application/xhtml+xml", HTML, False,
fr"(.*?)contentStream(.*?)contentMetadata"
fr"(.*?)dereferencingMetadata(.*?){HTML}"),
]


@pytest.mark.parametrize(
"accept, expected_header, has_context, expected_body",
dereferencing_content_type_test_set
)
def test_dereferencing_content_type_fragment(accept, expected_header, expected_body, has_context):
url = RESOLVER_URL + PATH + TESTNET_RESOURCE
header = {"Accept": accept} if accept else {}

r = requests.get(url, headers=header)

assert r.headers["Content-Type"] == expected_header
assert re.match(expected_body, r.text)
if has_context:
assert re.findall(r"context", r.text)
else:
assert not re.findall(r"context", r.text)


@pytest.mark.parametrize(
"accept, expected_header, has_context, expected_body",
dereferencing_content_type_test_set
)
def test_dereferencing_content_type_resource(accept, expected_header, expected_body, has_context):
url = RESOLVER_URL + PATH + TESTNET_FRAGMENT.replace("#", "%23")
header = {"Accept": accept} if accept else {}

r = requests.get(url, headers=header)

assert r.headers["Content-Type"] == expected_header
assert re.match(expected_body, r.text)
if has_context:
assert re.findall(r"context", r.text)
else:
assert not re.findall(r"context", r.text)

0 comments on commit 3dc673b

Please sign in to comment.