1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/test/ddo/DDO.test.ts

294 lines
11 KiB
TypeScript
Raw Normal View History

2019-02-04 18:13:54 +01:00
import { assert, expect, spy, use } from "chai"
2019-02-04 16:26:56 +01:00
import * as spies from "chai-spies"
import * as Web3 from "web3"
2019-02-04 16:26:56 +01:00
import { DDO } from "../../src/ddo/DDO"
import { Service } from "../../src/ddo/Service"
import { Ocean } from "../../src/ocean/Ocean"
2019-02-04 18:13:54 +01:00
import config from "../config"
2019-03-21 03:17:36 +01:00
import TestContractHandler from "../keeper/TestContractHandler"
2019-02-04 16:26:56 +01:00
2018-11-01 11:04:59 +01:00
import * as jsonDDO from "../testdata/ddo.json"
2019-02-04 18:13:54 +01:00
use(spies)
2019-02-04 16:26:56 +01:00
2018-11-01 11:04:59 +01:00
describe("DDO", () => {
const testDDO: DDO = new DDO({
2019-02-04 16:26:56 +01:00
id: `did:op:${"a".repeat(64)}`,
2018-11-01 11:04:59 +01:00
publicKey: [
{
id: "did:op:123456789abcdefghi#keys-1",
type: "RsaVerificationKey2018",
owner: "did:op:123456789abcdefghi",
publicKeyPem: "-----BEGIN PUBLIC KEY...END PUBLIC KEY-----\r\n",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
id: "did:op:123456789abcdefghi#keys-2",
type: "Ed25519VerificationKey2018",
owner: "did:op:123456789abcdefghi",
publicKeyBase58: "H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
],
authentication: [
{
type: "RsaSignatureAuthentication2018",
publicKey: "did:op:123456789abcdefghi#keys-1",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
type: "ieee2410Authentication2018",
publicKey: "did:op:123456789abcdefghi#keys-2",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
],
service: [
{
type: "OpenIdConnectVersion1.0Service",
serviceEndpoint: "https://openid.example.com/",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
type: "CredentialRepositoryService",
serviceEndpoint: "https://repository.example.com/service/8377464",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
type: "XdiService",
serviceEndpoint: "https://xdi.example.com/8377464",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
type: "HubService",
serviceEndpoint: "https://hub.example.com/.identity/did:op:0123456789abcdef/",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
type: "MessagingService",
serviceEndpoint: "https://example.com/messages/8377464",
2019-02-04 16:26:56 +01:00
},
2019-02-04 18:13:54 +01:00
{
2018-11-01 11:04:59 +01:00
type: "SocialWebInboxService",
serviceEndpoint: "https://social.example.com/83hfh37dj",
description: "My public social inbox",
spamCost: {
amount: "0.50",
currency: "USD",
},
2019-02-04 18:13:54 +01:00
} as any,
2018-11-01 11:04:59 +01:00
{
id: "did:op:123456789abcdefghi;bops",
type: "BopsService",
serviceEndpoint: "https://bops.example.com/enterprise/",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
type: "Consume",
// tslint:disable-next-line
2018-11-01 11:04:59 +01:00
serviceEndpoint: "http://mybrizo.org/api/v1/brizo/services/consume?pubKey=${pubKey}&serviceId={serviceId}&url={url}",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
type: "Compute",
// tslint:disable-next-line
2018-11-01 11:04:59 +01:00
serviceEndpoint: "http://mybrizo.org/api/v1/brizo/services/compute?pubKey=${pubKey}&serviceId={serviceId}&algo={algo}&container={container}",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
type: "Metadata",
serviceEndpoint: "http://myaquarius.org/api/v1/provider/assets/metadata/{did}",
metadata: {
base: {
name: "UK Weather information 2011",
type: "dataset",
description: "Weather information of UK including temperature and humidity",
size: "3.1gb",
dateCreated: "2012-10-10T17:00:000Z",
author: "Met Office",
license: "CC-BY",
copyrightHolder: "Met Office",
encoding: "UTF-8",
compression: "zip",
contentType: "text/csv",
workExample: "423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68",
contentUrls: [
"https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip",
"https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip",
2018-11-01 11:04:59 +01:00
],
links: [
{
// tslint:disable-next-line
2018-11-01 11:04:59 +01:00
sample1: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-daily/"
},
{
// tslint:disable-next-line
2018-11-01 11:04:59 +01:00
sample2: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-averages-25km/"
},
{
fieldsDescription: "http://data.ceda.ac.uk/badc/ukcp09/",
},
2018-11-01 11:04:59 +01:00
],
inLanguage: "en",
tags: "weather, uk, 2011, temperature, humidity",
price: 10,
2019-02-04 16:26:56 +01:00
files: [
{
url: "234ab87234acbd09543085340abffh21983ddhiiee982143827423421",
checksum: "efb2c764274b745f5fc37f97c6b0e761",
contentLength: "4535431",
2019-02-04 18:13:54 +01:00
resourceId: "access-log2018-02-13-15-17-29-18386C502CAEA932",
2019-02-04 16:26:56 +01:00
},
{
url: "234ab87234acbd6894237582309543085340abffh21983ddhiiee982143827423421",
checksum: "085340abffh21495345af97c6b0e761",
2019-02-04 18:13:54 +01:00
contentLength: "12324",
2019-02-04 16:26:56 +01:00
},
{
2019-02-04 18:13:54 +01:00
url: "80684089027358963495379879a543085340abffh21983ddhiiee982143827abcc2",
},
2019-02-04 16:26:56 +01:00
],
2019-02-04 18:13:54 +01:00
checksum: "",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
curation: {
rating: 0.93,
numVotes: 123,
schema: "Binary Votting",
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
additionalInformation: {
updateFrecuency: "yearly",
structuredMarkup: [
{
uri: "http://skos.um.es/unescothes/C01194/jsonld",
mediaType: "application/ld+json",
},
2018-11-01 11:04:59 +01:00
{
uri: "http://skos.um.es/unescothes/C01194/turtle",
mediaType: "text/turtle",
},
],
2019-02-04 16:26:56 +01:00
},
},
},
],
2018-11-01 11:04:59 +01:00
})
let web3: Web3
let ocean: Ocean
beforeEach(async () => {
2019-03-21 03:17:36 +01:00
await TestContractHandler.prepareContracts()
ocean = await Ocean.getInstance(config)
web3 = (ocean as any).web3
2019-02-04 16:26:56 +01:00
})
afterEach(() => {
spy.restore()
})
2018-11-01 11:04:59 +01:00
describe("#serialize()", () => {
it("should properly serialize", async () => {
const ddoString = DDO.serialize(testDDO)
assert(ddoString)
assert(ddoString.startsWith("{"))
})
})
describe("#constructor()", () => {
it("should create an empty ddo", async () => {
const ddo = new DDO()
assert(ddo)
assert(ddo.service.length === 0)
assert(ddo.authentication.length === 0)
assert(ddo.publicKey.length === 0)
})
it("should create an predefined ddo", async () => {
2019-02-15 23:40:55 +01:00
const service: Partial<Service> & any = {
serviceEndpoint: "http://",
description: "nice service",
2019-02-04 16:26:56 +01:00
}
const ddo = new DDO({
2019-02-04 18:13:54 +01:00
service: [service as any],
})
assert(ddo)
assert(ddo.service.length === 1)
2019-02-15 23:40:55 +01:00
assert((ddo.service[0] as any).description === service.description)
assert(ddo.authentication.length === 0)
assert(ddo.publicKey.length === 0)
})
})
2018-11-01 11:04:59 +01:00
describe("#deserialize()", () => {
it("should properly deserialize from serialized object", async () => {
const ddoString = DDO.serialize(testDDO)
2019-02-04 18:13:54 +01:00
assert.typeOf(ddoString, "string")
2018-11-01 11:04:59 +01:00
const ddo: DDO = DDO.deserialize(ddoString)
2019-02-04 16:26:56 +01:00
assert.instanceOf(ddo, DDO)
2018-11-01 11:04:59 +01:00
2019-02-04 16:26:56 +01:00
assert.equal(ddo.id, testDDO.id)
assert.equal(ddo.publicKey[0].publicKeyPem, testDDO.publicKey[0].publicKeyPem)
2018-11-01 11:04:59 +01:00
})
it("should properly deserialize from json file", async () => {
const ddo: DDO = DDO.deserialize(JSON.stringify(jsonDDO))
assert(ddo)
2019-02-04 16:26:56 +01:00
assert.equal(ddo.id, jsonDDO.id)
assert.equal(ddo.publicKey[0].publicKeyPem, jsonDDO.publicKey[0].publicKeyPem)
})
})
describe("#getChecksum()", () => {
it("should properly generate a the checksum DDO", async () => {
const ddo = new DDO(testDDO)
const checksum = ddo.getChecksum()
assert.equal(checksum, "15f27a7a3c7b15d2b06dec7347c6b8da")
2019-02-04 16:26:56 +01:00
})
})
describe("#generateProof()", () => {
2019-02-04 18:13:54 +01:00
const publicKey = `0x${"a".repeat(40)}`
const signature = `0x${"a".repeat(130)}`
2019-02-04 16:26:56 +01:00
it("should properly generate the proof", async () => {
const signTextSpy = spy.on(ocean.utils.signature, "signText", () => signature)
2019-02-04 16:26:56 +01:00
const ddo = new DDO(testDDO)
const checksum = ddo.getChecksum()
const proof = await ddo.generateProof(ocean, publicKey)
2019-02-04 16:26:56 +01:00
2019-02-04 18:13:54 +01:00
assert.include(proof as any, {
2019-02-04 16:26:56 +01:00
creator: publicKey,
2019-02-04 18:13:54 +01:00
type: "DDOIntegritySignature",
2019-02-04 16:26:56 +01:00
signatureValue: signature,
})
expect(signTextSpy).to.have.been.called.with(checksum, publicKey)
})
})
describe("#addProof()", () => {
2019-02-04 18:13:54 +01:00
const publicKey = `0x${"a".repeat(40)}`
2019-02-04 16:26:56 +01:00
it("should properly add the proof on the DDO", async () => {
2019-02-04 18:13:54 +01:00
const fakeProof = {
2019-02-04 16:26:56 +01:00
creation: Date.now(),
2019-02-04 18:13:54 +01:00
creator: "test",
type: "test",
signaturValue: "test",
} as any
2019-02-04 16:26:56 +01:00
const ddo = new DDO(testDDO)
2019-02-04 18:13:54 +01:00
const generateProofSpy = spy.on(ddo, "generateProof", () => fakeProof)
await ddo.addProof(web3, publicKey)
2019-02-04 16:26:56 +01:00
assert.equal(ddo.proof, fakeProof)
expect(generateProofSpy).to.have.been.called.with(publicKey)
2018-11-01 11:04:59 +01:00
})
})
})