Skip to content

Commit

Permalink
Do not load remote JSONLD contexts during unit tests (#2533)
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul authored Oct 25, 2023
1 parent 08d3b81 commit 5521f17
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 8 deletions.
2 changes: 1 addition & 1 deletion auth/services/selfsigned/test/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
func Test_GenerateTestData(t *testing.T) {
store := false

contextLoader, _ := jsonld.NewContextLoader(false, jsonld.DefaultContextConfig())
contextLoader := jsonld.NewTestJSONLDManager(t).DocumentLoader()

createdTime := time.Date(2023, 4, 20, 9, 53, 3, 0, time.UTC)
expirationTime := createdTime.Add(4 * 24 * time.Hour)
Expand Down
5 changes: 4 additions & 1 deletion jsonld/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,18 @@ func NewTestJSONLDManager(t *testing.T) JSONLD {
t.Helper()

contextConfig := DefaultContextConfig()
contextConfig.RemoteAllowList = nil
contextConfig.LocalFileMapping["http://example.org/credentials/V1"] = "test_assets/contexts/test.ldjson"
contextConfig.LocalFileMapping["https://www.w3.org/2018/credentials/examples/v1"] = "test_assets/contexts/examples.ldjson"
contextConfig.LocalFileMapping["https://www.w3.org/ns/odrl.jsonld"] = "test_assets/contexts/odrl.ldjson"

loader := NewMappedDocumentLoader(contextConfig.LocalFileMapping,
NewEmbeddedFSDocumentLoader(assets.Assets,
// Handle all embedded file system files
NewEmbeddedFSDocumentLoader(assets.TestAssets,
// Last in the chain is the defaultLoader which can resolve
// local files and remote (via http) context documents
ld.NewDefaultDocumentLoader(nil))))
nil)))

manager := testContextManager{loader: loader}

Expand Down
2 changes: 1 addition & 1 deletion storage/leia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func newStoreInDir(t *testing.T, testDir string, backupConfig LeiaBackupConfigur
backupStorePath := path.Join(testDir, "vcr", "backup-private-credentials.db")
backupStore, err := bbolt.CreateBBoltStore(backupStorePath)
require.NoError(t, err)
leiaStore, err := leia.NewStore(issuerStorePath)
leiaStore, err := leia.NewStore(issuerStorePath, leia.WithDocumentLoader(jsonld.NewTestJSONLDManager(t).DocumentLoader()))
require.NoError(t, err)
store, err := NewKVBackedLeiaStore(leiaStore, backupStore)
require.NoError(t, err)
Expand Down
53 changes: 53 additions & 0 deletions vcr/assets/test_assets/contexts/examples.ldjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"@context": [{
"@version": 1.1
},"https://www.w3.org/ns/odrl.jsonld", {
"ex": "https://example.org/examples#",
"schema": "http://schema.org/",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",

"3rdPartyCorrelation": "ex:3rdPartyCorrelation",
"AllVerifiers": "ex:AllVerifiers",
"Archival": "ex:Archival",
"BachelorDegree": "ex:BachelorDegree",
"Child": "ex:Child",
"CLCredentialDefinition2019": "ex:CLCredentialDefinition2019",
"CLSignature2019": "ex:CLSignature2019",
"IssuerPolicy": "ex:IssuerPolicy",
"HolderPolicy": "ex:HolderPolicy",
"Mother": "ex:Mother",
"RelationshipCredential": "ex:RelationshipCredential",
"UniversityDegreeCredential": "ex:UniversityDegreeCredential",
"AlumniCredential": "ex:AlumniCredential",
"DisputeCredential": "ex:DisputeCredential",
"PrescriptionCredential": "ex:PrescriptionCredential",
"ZkpExampleSchema2018": "ex:ZkpExampleSchema2018",

"issuerData": "ex:issuerData",
"attributes": "ex:attributes",
"signature": "ex:signature",
"signatureCorrectnessProof": "ex:signatureCorrectnessProof",
"primaryProof": "ex:primaryProof",
"nonRevocationProof": "ex:nonRevocationProof",

"alumniOf": {"@id": "schema:alumniOf", "@type": "rdf:HTML"},
"child": {"@id": "ex:child", "@type": "@id"},
"degree": "ex:degree",
"degreeType": "ex:degreeType",
"degreeSchool": "ex:degreeSchool",
"college": "ex:college",
"name": {"@id": "schema:name", "@type": "rdf:HTML"},
"givenName": "schema:givenName",
"familyName": "schema:familyName",
"parent": {"@id": "ex:parent", "@type": "@id"},
"referenceId": "ex:referenceId",
"documentPresence": "ex:documentPresence",
"evidenceDocument": "ex:evidenceDocument",
"spouse": "schema:spouse",
"subjectPresence": "ex:subjectPresence",
"verifier": {"@id": "ex:verifier", "@type": "@id"},
"currentStatus": "ex:currentStatus",
"statusReason": "ex:statusReason",
"prescription": "ex:prescription"
}]
}
200 changes: 200 additions & 0 deletions vcr/assets/test_assets/contexts/odrl.ldjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"@context": {
"odrl": "http://www.w3.org/ns/odrl/2/",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"owl": "http://www.w3.org/2002/07/owl#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"dct": "http://purl.org/dc/terms/",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"vcard": "http://www.w3.org/2006/vcard/ns#",
"foaf": "http://xmlns.com/foaf/0.1/",
"schema": "http://schema.org/",
"cc": "http://creativecommons.org/ns#",

"uid": "@id",
"type": "@type",

"Policy": "odrl:Policy",
"Rule": "odrl:Rule",
"profile": {"@type": "@id", "@id": "odrl:profile"},

"inheritFrom": {"@type": "@id", "@id": "odrl:inheritFrom"},

"ConflictTerm": "odrl:ConflictTerm",
"conflict": {"@type": "@vocab", "@id": "odrl:conflict"},
"perm": "odrl:perm",
"prohibit": "odrl:prohibit",
"invalid": "odrl:invalid",

"Agreement": "odrl:Agreement",
"Assertion": "odrl:Assertion",
"Offer": "odrl:Offer",
"Privacy": "odrl:Privacy",
"Request": "odrl:Request",
"Set": "odrl:Set",
"Ticket": "odrl:Ticket",

"Asset": "odrl:Asset",
"AssetCollection": "odrl:AssetCollection",
"relation": {"@type": "@id", "@id": "odrl:relation"},
"hasPolicy": {"@type": "@id", "@id": "odrl:hasPolicy"},

"target": {"@type": "@id", "@id": "odrl:target"},
"output": {"@type": "@id", "@id": "odrl:output"},

"partOf": {"@type": "@id", "@id": "odrl:partOf"},
"source": {"@type": "@id", "@id": "odrl:source"},

"Party": "odrl:Party",
"PartyCollection": "odrl:PartyCollection",
"function": {"@type": "@vocab", "@id": "odrl:function"},
"PartyScope": "odrl:PartyScope",

"assignee": {"@type": "@id", "@id": "odrl:assignee"},
"assigner": {"@type": "@id", "@id": "odrl:assigner"},
"assigneeOf": {"@type": "@id", "@id": "odrl:assigneeOf"},
"assignerOf": {"@type": "@id", "@id": "odrl:assignerOf"},
"attributedParty": {"@type": "@id", "@id": "odrl:attributedParty"},
"attributingParty": {"@type": "@id", "@id": "odrl:attributingParty"},
"compensatedParty": {"@type": "@id", "@id": "odrl:compensatedParty"},
"compensatingParty": {"@type": "@id", "@id": "odrl:compensatingParty"},
"consentingParty": {"@type": "@id", "@id": "odrl:consentingParty"},
"consentedParty": {"@type": "@id", "@id": "odrl:consentedParty"},
"informedParty": {"@type": "@id", "@id": "odrl:informedParty"},
"informingParty": {"@type": "@id", "@id": "odrl:informingParty"},
"trackingParty": {"@type": "@id", "@id": "odrl:trackingParty"},
"trackedParty": {"@type": "@id", "@id": "odrl:trackedParty"},
"contractingParty": {"@type": "@id", "@id": "odrl:contractingParty"},
"contractedParty": {"@type": "@id", "@id": "odrl:contractedParty"},

"Action": "odrl:Action",
"action": {"@type": "@vocab", "@id": "odrl:action"},
"includedIn": {"@type": "@id", "@id": "odrl:includedIn"},
"implies": {"@type": "@id", "@id": "odrl:implies"},

"Permission": "odrl:Permission",
"permission": {"@type": "@id", "@id": "odrl:permission"},

"Prohibition": "odrl:Prohibition",
"prohibition": {"@type": "@id", "@id": "odrl:prohibition"},

"obligation": {"@type": "@id", "@id": "odrl:obligation"},

"use": "odrl:use",
"grantUse": "odrl:grantUse",
"aggregate": "odrl:aggregate",
"annotate": "odrl:annotate",
"anonymize": "odrl:anonymize",
"archive": "odrl:archive",
"concurrentUse": "odrl:concurrentUse",
"derive": "odrl:derive",
"digitize": "odrl:digitize",
"display": "odrl:display",
"distribute": "odrl:distribute",
"execute": "odrl:execute",
"extract": "odrl:extract",
"give": "odrl:give",
"index": "odrl:index",
"install": "odrl:install",
"modify": "odrl:modify",
"move": "odrl:move",
"play": "odrl:play",
"present": "odrl:present",
"print": "odrl:print",
"read": "odrl:read",
"reproduce": "odrl:reproduce",
"sell": "odrl:sell",
"stream": "odrl:stream",
"textToSpeech": "odrl:textToSpeech",
"transfer": "odrl:transfer",
"transform": "odrl:transform",
"translate": "odrl:translate",

"Duty": "odrl:Duty",
"duty": {"@type": "@id", "@id": "odrl:duty"},
"consequence": {"@type": "@id", "@id": "odrl:consequence"},
"remedy": {"@type": "@id", "@id": "odrl:remedy"},

"acceptTracking": "odrl:acceptTracking",
"attribute": "odrl:attribute",
"compensate": "odrl:compensate",
"delete": "odrl:delete",
"ensureExclusivity": "odrl:ensureExclusivity",
"include": "odrl:include",
"inform": "odrl:inform",
"nextPolicy": "odrl:nextPolicy",
"obtainConsent": "odrl:obtainConsent",
"reviewPolicy": "odrl:reviewPolicy",
"uninstall": "odrl:uninstall",
"watermark": "odrl:watermark",

"Constraint": "odrl:Constraint",
"LogicalConstraint": "odrl:LogicalConstraint",
"constraint": {"@type": "@id", "@id": "odrl:constraint"},
"refinement": {"@type": "@id", "@id": "odrl:refinement"},
"Operator": "odrl:Operator",
"operator": {"@type": "@vocab", "@id": "odrl:operator"},
"RightOperand": "odrl:RightOperand",
"rightOperand": "odrl:rightOperand",
"rightOperandReference":{"@type": "xsd:anyURI", "@id": "odrl:rightOperandReference"},
"LeftOperand": "odrl:LeftOperand",
"leftOperand": {"@type": "@vocab", "@id": "odrl:leftOperand"},
"unit": "odrl:unit",
"dataType": {"@type": "xsd:anyType", "@id": "odrl:datatype"},
"status": "odrl:status",

"absolutePosition": "odrl:absolutePosition",
"absoluteSpatialPosition": "odrl:absoluteSpatialPosition",
"absoluteTemporalPosition":"odrl:absoluteTemporalPosition",
"absoluteSize": "odrl:absoluteSize",
"count": "odrl:count",
"dateTime": "odrl:dateTime",
"delayPeriod": "odrl:delayPeriod",
"deliveryChannel": "odrl:deliveryChannel",
"elapsedTime": "odrl:elapsedTime",
"event": "odrl:event",
"fileFormat": "odrl:fileFormat",
"industry": "odrl:industry:",
"language": "odrl:language",
"media": "odrl:media",
"meteredTime": "odrl:meteredTime",
"payAmount": "odrl:payAmount",
"percentage": "odrl:percentage",
"product": "odrl:product",
"purpose": "odrl:purpose",
"recipient": "odrl:recipient",
"relativePosition": "odrl:relativePosition",
"relativeSpatialPosition": "odrl:relativeSpatialPosition",
"relativeTemporalPosition":"odrl:relativeTemporalPosition",
"relativeSize": "odrl:relativeSize",
"resolution": "odrl:resolution",
"spatial": "odrl:spatial",
"spatialCoordinates": "odrl:spatialCoordinates",
"systemDevice": "odrl:systemDevice",
"timeInterval": "odrl:timeInterval",
"unitOfCount": "odrl:unitOfCount",
"version": "odrl:version",
"virtualLocation": "odrl:virtualLocation",

"eq": "odrl:eq",
"gt": "odrl:gt",
"gteq": "odrl:gteq",
"lt": "odrl:lt",
"lteq": "odrl:lteq",
"neq": "odrl:neg",
"isA": "odrl:isA",
"hasPart": "odrl:hasPart",
"isPartOf": "odrl:isPartOf",
"isAllOf": "odrl:isAllOf",
"isAnyOf": "odrl:isAnyOf",
"isNoneOf": "odrl:isNoneOf",
"or": "odrl:or",
"xone": "odrl:xone",
"and": "odrl:and",
"andSequence": "odrl:andSequence",

"policyUsage": "odrl:policyUsage"

}
}
3 changes: 1 addition & 2 deletions vcr/signature/json_web_signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func TestJsonWebSignature2020_CanonicalizeDocument(t *testing.T) {
})

t.Run("simple document with resolvable context", func(t *testing.T) {
contextLoader, err := jsonld.NewContextLoader(false, jsonld.DefaultContextConfig())
assert.NoError(t, err)
contextLoader := jsonld.NewTestJSONLDManager(t).DocumentLoader()

sig := JSONWebSignature2020{ContextLoader: contextLoader}
doc := map[string]interface{}{
Expand Down
5 changes: 2 additions & 3 deletions vcr/signature/proof/jsonld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func TestLDProof_Verify(t *testing.T) {
signedDocument := SignedDocument{}
require.NoError(t, json.Unmarshal([]byte(vc_0), &signedDocument))

contextLoader, err := jsonld.NewContextLoader(true, jsonld.ContextsConfig{})
require.NoError(t, err)
contextLoader := jsonld.NewTestJSONLDManager(t).DocumentLoader()

t.Run("ok - JSONWebSignature2020 test vector", func(t *testing.T) {
ldProof := LDProof{}
Expand Down Expand Up @@ -169,7 +168,7 @@ func TestLDProof_Sign(t *testing.T) {

kid := "did:nuts:123#abc"
testKey := crypto.NewTestKey(kid)
contextLoader, _ := jsonld.NewContextLoader(false, jsonld.DefaultContextConfig())
contextLoader := jsonld.NewTestJSONLDManager(t).DocumentLoader()

t.Run("sign and verify a document", func(t *testing.T) {
now := time.Now()
Expand Down

0 comments on commit 5521f17

Please sign in to comment.