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

310 lines
11 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { assert, expect, spy, use } from 'chai'
import * as spies from 'chai-spies'
import * as Web3 from 'web3'
2019-02-04 16:26:56 +01:00
2019-06-20 00:20:09 +02:00
import { DDO } from '../../src/ddo/DDO'
import { Service } from '../../src/ddo/Service'
import { Ocean } from '../../src/ocean/Ocean'
import config from '../config'
import TestContractHandler from '../keeper/TestContractHandler'
2019-02-04 16:26:56 +01:00
2019-06-20 00:20:09 +02:00
import * as jsonDDO from '../testdata/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',
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',
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-06-20 00:20:09 +02:00
type: 'Consume',
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-06-20 00:20:09 +02:00
type: 'Compute',
// tslint:disable-next-line
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-06-20 00:20:09 +02:00
type: 'Metadata',
serviceEndpoint:
'http://myaquarius.org/api/v1/provider/assets/metadata/{did}',
2018-11-01 11:04:59 +01:00
metadata: {
base: {
2019-06-20 00:20:09 +02:00
name: 'UK Weather information 2011',
type: 'dataset',
description:
'Weather information of UK including temperature and humidity',
dateCreated: '2012-10-10T17:00:000Z',
datePublished: '2012-10-10T17:00:000Z',
author: 'Met Office',
license: 'CC-BY',
copyrightHolder: 'Met Office',
workExample:
'423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68',
2018-11-01 11:04:59 +01:00
links: [
{
2019-06-20 00:20:09 +02:00
sample1:
'http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-daily/'
2018-11-01 11:04:59 +01:00
},
{
2019-06-20 00:20:09 +02:00
sample2:
'http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-averages-25km/'
2018-11-01 11:04:59 +01:00
},
{
2019-06-20 00:20:09 +02:00
fieldsDescription:
'http://data.ceda.ac.uk/badc/ukcp09/'
}
],
inLanguage: 'en',
categories: ['Economy', 'Data Science'],
tags: [
'weather',
'uk',
'2011',
'temperature',
'humidity'
2018-11-01 11:04:59 +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-04-29 14:35:42 +02:00
contentLength: 4535431,
2019-06-20 00:20:09 +02: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',
contentLength: 12324
2019-02-04 16:26:56 +01:00
},
{
2019-06-20 00:20:09 +02:00
index: 2
}
2019-02-04 16:26:56 +01:00
],
2019-06-20 00:20:09 +02:00
checksum: ''
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-06-20 00:20:09 +02:00
updateFrecuency: 'yearly',
2018-11-01 11:04:59 +01:00
structuredMarkup: [
{
2019-06-20 00:20:09 +02:00
uri:
'http://skos.um.es/unescothes/C01194/jsonld',
mediaType: 'application/ld+json'
},
2018-11-01 11:04:59 +01:00
{
2019-06-20 00:20:09 +02:00
uri:
'http://skos.um.es/unescothes/C01194/turtle',
mediaType: 'text/turtle'
}
]
}
}
}
]
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()
})
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-06-20 00:20:09 +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-06-20 00:20:09 +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-06-20 00:20:09 +02: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 () => {
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-06-20 00:20:09 +02: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
})
})
})