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

285 lines
10 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { assert, expect, spy, use } from 'chai'
2019-11-11 12:27:18 +01:00
import spies from 'chai-spies'
2019-02-04 16:26:56 +01:00
2020-01-31 00:15:55 +01:00
import { DDO } from '../../../src/ddo/DDO'
import { Service } from '../../../src/ddo/Service'
import { Ocean } from '../../../src/ocean/Ocean'
2019-06-20 00:20:09 +02:00
import config from '../config'
import TestContractHandler from '../keeper/TestContractHandler'
2019-02-04 16:26:56 +01:00
2020-01-29 14:38:44 +01:00
import * as jsonDDO from '../__fixtures__/ddo.json'
2018-11-01 11:04:59 +01:00
2019-02-04 18:13:54 +01:00
use(spies)
2019-02-04 16:26:56 +01:00
2019-06-20 00:20:09 +02:00
describe('DDO', () => {
2018-11-01 11:04:59 +01:00
const testDDO: DDO = new DDO({
2019-06-20 00:20:09 +02:00
id: `did:op:${'a'.repeat(64)}`,
2018-11-01 11:04:59 +01:00
publicKey: [
{
2019-06-20 00:20:09 +02:00
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
{
2019-06-20 00:20:09 +02:00
id: 'did:op:123456789abcdefghi#keys-2',
type: 'Ed25519VerificationKey2018',
owner: 'did:op:123456789abcdefghi',
publicKeyBase58: 'H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV'
}
2018-11-01 11:04:59 +01:00
],
authentication: [
{
2019-06-20 00:20:09 +02:00
type: 'RsaSignatureAuthentication2018',
publicKey: 'did:op:123456789abcdefghi#keys-1'
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
2019-06-20 00:20:09 +02:00
type: 'ieee2410Authentication2018',
publicKey: 'did:op:123456789abcdefghi#keys-2'
}
2018-11-01 11:04:59 +01:00
],
service: [
{
2019-06-20 00:20:09 +02:00
type: 'OpenIdConnectVersion1.0Service',
serviceEndpoint: 'https://openid.example.com/'
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
2019-06-20 00:20:09 +02:00
type: 'CredentialRepositoryService',
2019-09-09 12:18:54 +02:00
serviceEndpoint: 'https://repository.example.com/service/8377464'
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
{
2019-06-20 00:20:09 +02: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
{
2019-06-20 00:20:09 +02:00
type: 'HubService',
2019-11-15 00:00:10 +01:00
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
{
2019-06-20 00:20:09 +02: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
{
2019-06-20 00:20:09 +02:00
type: 'SocialWebInboxService',
serviceEndpoint: 'https://social.example.com/83hfh37dj',
description: 'My public social inbox',
2018-11-01 11:04:59 +01:00
spamCost: {
2019-06-20 00:20:09 +02:00
amount: '0.50',
currency: 'USD'
}
2019-02-04 18:13:54 +01:00
} as any,
2018-11-01 11:04:59 +01:00
{
2019-06-20 00:20:09 +02: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
{
2019-08-16 14:12:31 +02:00
type: 'consume',
2019-06-20 00:20:09 +02:00
serviceEndpoint:
2019-06-24 13:06:38 +02:00
'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
{
2019-08-16 14:12:31 +02:00
type: 'compute',
2019-06-20 00:20:09 +02:00
serviceEndpoint:
2019-06-24 13:06:38 +02:00
'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
{
2019-08-16 14:12:31 +02:00
type: 'metadata',
2019-08-16 16:12:42 +02:00
index: 0,
2019-11-15 00:00:10 +01:00
serviceEndpoint:
'http://myaquarius.org/api/v1/provider/assets/metadata/{did}',
2019-08-16 16:12:42 +02:00
attributes: {
2019-08-15 13:23:56 +02:00
main: {
2019-06-20 00:20:09 +02:00
name: 'UK Weather information 2011',
type: 'dataset',
dateCreated: '2012-10-10T17:00:000Z',
datePublished: '2012-10-10T17:00:000Z',
author: 'Met Office',
license: 'CC-BY',
2019-11-12 14:21:29 +01:00
price: '10',
2019-02-04 16:26:56 +01:00
files: [
{
2019-04-29 15:24:21 +02:00
index: 0,
2019-06-20 00:20:09 +02:00
checksum: 'efb2c764274b745f5fc37f97c6b0e761',
2019-11-12 13:24:36 +01:00
contentLength: '4535431',
2019-08-16 16:12:42 +02:00
contentType: 'application/json',
2019-11-15 00:00:10 +01:00
resourceId:
'access-log2018-02-13-15-17-29-18386C502CAEA932'
2019-02-04 16:26:56 +01:00
},
{
2019-04-29 15:24:21 +02:00
index: 1,
2019-06-20 00:20:09 +02:00
checksum: '085340abffh21495345af97c6b0e761',
2019-11-12 13:24:36 +01:00
contentLength: '12324',
2019-08-16 16:12:42 +02:00
contentType: 'application/json'
2019-02-04 16:26:56 +01:00
},
{
2019-08-16 16:12:42 +02:00
index: 2,
contentType: ''
2019-06-20 00:20:09 +02:00
}
2019-11-12 14:21:29 +01:00
]
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
curation: {
rating: 0.93,
numVotes: 123,
2019-06-20 00:20:09 +02:00
schema: 'Binary Voting'
2019-02-04 16:26:56 +01:00
},
2018-11-01 11:04:59 +01:00
additionalInformation: {
2019-11-15 00:00:10 +01:00
description:
'Weather information of UK including temperature and humidity',
2019-08-15 13:23:56 +02:00
copyrightHolder: 'Met Office',
2019-11-15 00:00:10 +01:00
workExample:
'423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68',
2019-08-15 13:23:56 +02:00
links: [
{
sample1:
'http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-daily/'
},
{
sample2:
'http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-averages-25km/'
},
{
2019-09-09 12:18:54 +02:00
fieldsDescription: 'http://data.ceda.ac.uk/badc/ukcp09/'
2019-08-15 13:23:56 +02:00
}
],
inLanguage: 'en',
categories: ['Economy', 'Data Science'],
2019-09-09 12:18:54 +02:00
tags: ['weather', 'uk', '2011', 'temperature', 'humidity'],
2019-08-15 13:23:56 +02:00
updateFrequency: 'yearly',
2018-11-01 11:04:59 +01:00
structuredMarkup: [
{
2019-09-09 12:18:54 +02:00
uri: 'http://skos.um.es/unescothes/C01194/jsonld',
2019-06-20 00:20:09 +02:00
mediaType: 'application/ld+json'
},
2018-11-01 11:04:59 +01:00
{
2019-09-09 12:18:54 +02:00
uri: 'http://skos.um.es/unescothes/C01194/turtle',
2019-06-20 00:20:09 +02:00
mediaType: 'text/turtle'
}
]
}
}
}
]
2018-11-01 11:04:59 +01:00
})
let ocean: Ocean
beforeEach(async () => {
2019-03-21 03:17:36 +01:00
await TestContractHandler.prepareContracts()
ocean = await Ocean.getInstance(config)
2019-02-04 16:26:56 +01:00
})
afterEach(() => {
spy.restore()
})
2019-06-20 00:20:09 +02:00
describe('#serialize()', () => {
it('should properly serialize', async () => {
2018-11-01 11:04:59 +01:00
const ddoString = DDO.serialize(testDDO)
assert(ddoString)
2019-06-20 00:20:09 +02:00
assert(ddoString.startsWith('{'))
2018-11-01 11:04:59 +01:00
})
})
2019-06-20 00:20:09 +02:00
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)
})
2019-06-20 00:20:09 +02:00
it('should create an predefined ddo', async () => {
2019-02-15 23:40:55 +01:00
const service: Partial<Service> & any = {
2019-06-20 00:20:09 +02:00
serviceEndpoint: 'http://',
description: 'nice service'
2019-02-04 16:26:56 +01:00
}
const ddo = new DDO({
2019-06-20 00:20:09 +02: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)
})
})
2019-06-20 00:20:09 +02:00
describe('#deserialize()', () => {
it('should properly deserialize from serialized object', async () => {
2018-11-01 11:04:59 +01:00
const ddoString = DDO.serialize(testDDO)
2019-06-20 00:20:09 +02: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)
2019-09-09 12:18:54 +02:00
assert.equal(ddo.publicKey[0].publicKeyPem, testDDO.publicKey[0].publicKeyPem)
2018-11-01 11:04:59 +01:00
})
2019-06-20 00:20:09 +02:00
it('should properly deserialize from json file', async () => {
2018-11-01 11:04:59 +01:00
const ddo: DDO = DDO.deserialize(JSON.stringify(jsonDDO))
assert(ddo)
2019-02-04 16:26:56 +01:00
assert.equal(ddo.id, jsonDDO.id)
2019-09-09 12:18:54 +02:00
assert.equal(ddo.publicKey[0].publicKeyPem, jsonDDO.publicKey[0].publicKeyPem)
2019-02-04 16:26:56 +01:00
})
})
2019-06-20 00:20:09 +02:00
describe('#getChecksum()', () => {
it('should properly generate a the checksum DDO', async () => {
2019-02-04 16:26:56 +01:00
const ddo = new DDO(testDDO)
const checksum = ddo.getChecksum()
2019-11-15 00:00:10 +01:00
assert.equal(
checksum,
'0x15f27a7a3c7b15d2b06dec7347c6b8da168adddd7df51a8ebbbe87b59b80049b'
)
2019-02-04 16:26:56 +01:00
})
})
2019-06-20 00:20:09 +02:00
describe('#generateProof()', () => {
const publicKey = `0x${'a'.repeat(40)}`
const signature = `0x${'a'.repeat(130)}`
2019-02-04 16:26:56 +01:00
2019-06-20 00:20:09 +02:00
it('should properly generate the proof', async () => {
2019-09-09 12:18:54 +02:00
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-06-20 00:20:09 +02:00
type: 'DDOIntegritySignature',
signatureValue: signature
2019-02-04 16:26:56 +01:00
})
expect(signTextSpy).to.have.been.called.with(checksum, publicKey)
})
})
2019-06-20 00:20:09 +02:00
describe('#addProof()', () => {
const publicKey = `0x${'a'.repeat(40)}`
2019-02-04 16:26:56 +01:00
2019-06-20 00:20:09 +02: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-06-20 00:20:09 +02:00
creator: 'test',
type: 'test',
signaturValue: 'test'
2019-02-04 18:13:54 +01:00
} as any
2019-02-04 16:26:56 +01:00
const ddo = new DDO(testDDO)
2019-09-09 12:18:54 +02:00
const generateProofSpy = spy.on(ddo, 'generateProof', () => fakeProof)
2019-11-11 12:27:18 +01:00
await ddo.addProof(ocean, 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
})
})
})