1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/test/integration/ocean/AssetOwners.test.ts
2020-05-10 01:08:09 -07:00

259 lines
8.6 KiB
TypeScript

import { assert } from 'chai'
import { config } from '../config'
import { getMetadata } from '../utils'
import { Ocean, Account, EditableMetaData } from '../../../src' // @oceanprotocol/squid
import { ServiceComputePrivacy } from '../../../src/ddo/Service'
describe('Asset Owners', () => {
let ocean: Ocean
let account1: Account
let account2: Account
let consumer1: Account
let consumer2: Account
let metadata = getMetadata()
before(async () => {
ocean = await Ocean.getInstance(config)
// Accounts
;[account1, account2] = await ocean.accounts.list()
consumer1 = (await ocean.accounts.list())[3]
consumer2 = (await ocean.accounts.list())[4]
if (!ocean.keeper.dispenser) {
metadata = getMetadata(0)
}
})
it('should set the owner of an asset', async () => {
const ddo = await ocean.assets.create(metadata as any, account1)
const owner = await ocean.assets.owner(ddo.id)
assert.equal(owner, account1.getId())
})
it('should set the provider of an asset', async () => {
const ddo = await ocean.assets.create(metadata as any, account1)
const isProvider = await ocean.keeper.didRegistry.isDIDProvider(
ddo.id,
config.brizoAddress
)
assert.isTrue(isProvider)
})
it('should be added correctly a permission on an asset', async () => {
const ddo = await ocean.assets.create(metadata as any, account1)
assert.isFalse(
await ocean.keeper.didRegistry.getPermission(ddo.id, account2.getId())
)
await ocean.keeper.didRegistry.grantPermission(
ddo.id,
account2.getId(),
account1.getId()
)
assert.isTrue(
await ocean.keeper.didRegistry.getPermission(ddo.id, account2.getId())
)
})
it('should get the assets owned by a user', async () => {
const { length: initialLength } = await ocean.assets.ownerAssets(account2.getId())
await ocean.assets.create(metadata as any, account1)
await ocean.assets.create(metadata as any, account1)
await ocean.assets.create(metadata as any, account2)
const { length: finalLength } = await ocean.assets.ownerAssets(account2.getId())
assert.equal(finalLength - initialLength, 1)
})
it('should get the assets that can be consumed by a user', async () => {
const { length: initialLength } = await ocean.assets.consumerAssets(
account2.getId()
)
const ddo = await ocean.assets.create(metadata as any, account1)
const { length: finalLength1 } = await ocean.assets.consumerAssets(
account2.getId()
)
assert.equal(finalLength1 - initialLength, 0)
// Granting access
try {
await account2.requestTokens(
parseInt(
(
+metadata.main.price *
10 ** -(await ocean.keeper.token.decimals())
).toString()
)
)
} catch {}
await ocean.assets.order(ddo.id, account2)
// Access granted
const { length: finalLength2 } = await ocean.assets.consumerAssets(
account2.getId()
)
assert.equal(finalLength2 - initialLength, 1)
})
it('should be able to transfer ownership', async () => {
const { id } = await ocean.assets.create(metadata as any, account1)
// transfer
await ocean.assets.transferOwnership(id, account2.getId(), account1)
const newOwner = await ocean.keeper.didRegistry.getDIDOwner(id)
assert.equal(newOwner, account2.getId())
// check aquarius
const aquariusOwner = await ocean.assets.owner(id)
assert.equal(aquariusOwner, account2.getId())
})
it('should be able to update metadata', async () => {
const { id } = await ocean.assets.create(metadata as any, account1)
const links = [
{
name: 'Sample1',
url: 'http://www.example.com',
type: 'sample'
},
{
name: 'Sample2',
url: 'http://www.example.net',
type: 'sample'
}
]
const newPrices = [
{
serviceIndex: 0,
price: '31000000000000000000'
},
{
serviceIndex: 2,
price: '31000000000000000000'
}
]
const newMetaData = {
title: 'New title',
description: 'New description',
links: links,
servicePrices: newPrices
}
await ocean.assets.editMetadata(id, newMetaData, account1)
const newDDO = await ocean.assets.resolve(id)
assert.equal(newDDO.service[0].attributes.main.name, 'New title')
assert.equal(
newDDO.service[0].attributes.additionalInformation.description,
'New description'
)
assert.equal(newDDO.service[0].attributes.main.price, '31000000000000000000')
assert.equal(newDDO.service[2].attributes.main.price, '31000000000000000000')
assert.equal(
newDDO.service[0].attributes.additionalInformation.links[0].name,
'Sample1'
)
assert.equal(
newDDO.service[0].attributes.additionalInformation.links[0].url,
'http://www.example.com'
)
assert.equal(
newDDO.service[0].attributes.additionalInformation.links[1].name,
'Sample2'
)
assert.equal(
newDDO.service[0].attributes.additionalInformation.links[1].url,
'http://www.example.net'
)
})
it('should be able to retire metadata', async () => {
const { id } = await ocean.assets.create(metadata as any, account1)
await ocean.assets.retire(id, account1)
const newDDO = await ocean.assets.resolve(id)
assert.equal(newDDO, null)
})
it('should add and remove correctly an address to/from FreeWhiteList on an asset', async () => {
const ddo = await ocean.assets.create(metadata as any, account1)
await ocean.assets.addConsumerToFreeWhiteList(ddo.id, consumer1.getId(), account1)
await ocean.assets.addConsumerToFreeWhiteList(ddo.id, consumer2.getId(), account1)
const list = await ocean.assets.getFreeWhiteList(ddo.id)
assert.notEqual(-1, list.indexOf(consumer1.getId()))
assert.notEqual(-1, list.indexOf(consumer2.getId()))
await ocean.assets.removeConsumerFromFreeWhiteList(
ddo.id,
consumer1.getId(),
account1
)
const remList = await ocean.assets.getFreeWhiteList(ddo.id)
assert.equal(-1, remList.indexOf(consumer1.getId()))
assert.notEqual(-1, remList.indexOf(consumer2.getId()))
})
it('should be able to update computePrivacy', async () => {
const origComputePrivacy = {
allowRawAlgorithm: true,
allowNetworkAccess: true,
trustedAlgorithms: []
}
const newComputePrivacy = {
allowRawAlgorithm: false,
allowNetworkAccess: false,
trustedAlgorithms: ['did:op:123', 'did:op:1234']
}
const computeService = await ocean.compute.createComputeServiceAttributes(
account1,
'0',
'2020-03-10T10:00:00Z',
origComputePrivacy as ServiceComputePrivacy
)
const { id } = await ocean.assets.create(metadata as any, account1, [
computeService
])
const oldDDO = await ocean.assets.resolve(id)
let serviceIndex = null
for (let index = 0; index < oldDDO.service.length; index++) {
if (oldDDO.service[index].type === 'compute') serviceIndex = index
}
await ocean.assets.updateComputePrivacy(
id,
serviceIndex,
newComputePrivacy as ServiceComputePrivacy,
account1
)
const newDDO = await ocean.assets.resolve(id)
assert.equal(
newDDO.service[serviceIndex].attributes.main.privacy.allowRawAlgorithm,
newComputePrivacy.allowRawAlgorithm
)
assert.equal(
newDDO.service[serviceIndex].attributes.main.privacy.allowNetworkAccess,
newComputePrivacy.allowNetworkAccess
)
assert.deepEqual(
newDDO.service[serviceIndex].attributes.main.privacy.trustedAlgorithms,
newComputePrivacy.trustedAlgorithms
)
})
})