2019-06-20 00:20:09 +02:00
|
|
|
import { assert } from 'chai'
|
|
|
|
import { config } from '../config'
|
|
|
|
import { getMetadata } from '../utils'
|
|
|
|
import { Ocean, Account } from '../../src' // @oceanprotocol/squid
|
2019-04-15 15:30:02 +02:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('Asset Owners', () => {
|
2019-04-15 15:30:02 +02:00
|
|
|
let ocean: Ocean
|
|
|
|
|
2019-04-16 13:27:37 +02:00
|
|
|
let account1: Account
|
|
|
|
let account2: Account
|
2019-04-15 15:30:02 +02:00
|
|
|
|
2019-06-26 20:57:50 +02:00
|
|
|
let metadata = getMetadata()
|
2019-04-15 15:30:02 +02:00
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
ocean = await Ocean.getInstance(config)
|
|
|
|
|
|
|
|
// Accounts
|
2019-06-24 13:06:38 +02:00
|
|
|
;[account1, account2] = await ocean.accounts.list()
|
2019-06-26 20:57:50 +02:00
|
|
|
|
|
|
|
if (!ocean.keeper.dispenser) {
|
|
|
|
metadata = getMetadata(0)
|
|
|
|
}
|
2019-04-15 15:30:02 +02:00
|
|
|
})
|
|
|
|
|
2019-10-31 11:23:41 +01:00
|
|
|
it('should set the owner of an asset', async () => {
|
2019-04-16 13:27:37 +02:00
|
|
|
const ddo = await ocean.assets.create(metadata as any, account1)
|
2019-04-15 15:30:02 +02:00
|
|
|
const owner = await ocean.assets.owner(ddo.id)
|
|
|
|
|
2019-04-16 13:27:37 +02:00
|
|
|
assert.equal(owner, account1.getId())
|
|
|
|
})
|
|
|
|
|
2019-10-31 11:23:41 +01:00
|
|
|
it('should set the provider of an asset', async () => {
|
2019-08-30 10:50:30 +02:00
|
|
|
const ddo = await ocean.assets.create(metadata as any, account1)
|
|
|
|
|
2019-09-09 12:18:54 +02:00
|
|
|
const isProvider = await ocean.keeper.didRegistry.isDIDProvider(ddo.id, config.brizoAddress)
|
2019-08-30 10:50:30 +02:00
|
|
|
|
|
|
|
assert.isTrue(isProvider)
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should get the assets owned by a user', async () => {
|
2019-09-09 12:18:54 +02:00
|
|
|
const { length: initialLength } = await ocean.assets.ownerAssets(account2.getId())
|
2019-04-16 13:27:37 +02:00
|
|
|
|
|
|
|
await ocean.assets.create(metadata as any, account1)
|
|
|
|
await ocean.assets.create(metadata as any, account1)
|
|
|
|
|
|
|
|
await ocean.assets.create(metadata as any, account2)
|
|
|
|
|
2019-09-09 12:18:54 +02:00
|
|
|
const { length: finalLength } = await ocean.assets.ownerAssets(account2.getId())
|
2019-04-16 13:27:37 +02:00
|
|
|
|
|
|
|
assert.equal(finalLength - initialLength, 1)
|
2019-04-15 15:30:02 +02:00
|
|
|
})
|
2019-04-16 14:03:16 +02:00
|
|
|
|
2019-10-31 11:23:41 +01:00
|
|
|
it('should get the assets that can be consumed by a user', async () => {
|
2019-09-09 12:18:54 +02:00
|
|
|
const { length: initialLength } = await ocean.assets.consumerAssets(account2.getId())
|
2019-04-16 14:03:16 +02:00
|
|
|
|
|
|
|
const ddo = await ocean.assets.create(metadata as any, account1)
|
|
|
|
|
2019-09-09 12:18:54 +02:00
|
|
|
const { length: finalLength1 } = await ocean.assets.consumerAssets(account2.getId())
|
2019-04-16 14:03:16 +02:00
|
|
|
assert.equal(finalLength1 - initialLength, 0)
|
|
|
|
|
|
|
|
// Granting access
|
2019-06-26 20:57:50 +02:00
|
|
|
try {
|
2019-09-09 12:18:54 +02:00
|
|
|
await account2.requestTokens(+metadata.main.price * 10 ** -(await ocean.keeper.token.decimals()))
|
2019-06-26 20:57:50 +02:00
|
|
|
} catch {}
|
|
|
|
|
2019-08-16 17:00:14 +02:00
|
|
|
const { index } = ddo.findServiceByType('access')
|
2019-08-16 16:12:42 +02:00
|
|
|
|
2019-08-16 17:00:14 +02:00
|
|
|
await ocean.assets.order(ddo.id, index, account2)
|
2019-04-16 14:03:16 +02:00
|
|
|
// Access granted
|
|
|
|
|
2019-09-09 12:18:54 +02:00
|
|
|
const { length: finalLength2 } = await ocean.assets.consumerAssets(account2.getId())
|
2019-04-16 14:03:16 +02:00
|
|
|
assert.equal(finalLength2 - initialLength, 1)
|
|
|
|
})
|
2019-04-15 15:30:02 +02:00
|
|
|
})
|