2019-06-20 00:20:09 +02:00
|
|
|
import { assert } from 'chai'
|
|
|
|
import * as fs from 'fs'
|
2019-03-14 15:47:31 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
import { config } from '../config'
|
|
|
|
import { getMetadata } from '../utils'
|
2019-03-14 15:47:31 +01:00
|
|
|
|
2020-01-31 00:15:55 +01:00
|
|
|
import { Ocean, Account, DDO } from '../../../src' // @oceanprotocol/squid
|
2019-03-14 15:47:31 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('Consume Asset (Brizo)', () => {
|
2019-03-14 15:47:31 +01:00
|
|
|
let ocean: Ocean
|
|
|
|
|
|
|
|
let publisher: Account
|
|
|
|
let consumer: Account
|
|
|
|
|
|
|
|
let ddo: DDO
|
|
|
|
let agreementId: string
|
|
|
|
|
2019-06-26 20:57:50 +02:00
|
|
|
let metadata = getMetadata()
|
2019-03-14 15:47:31 +01:00
|
|
|
|
|
|
|
before(async () => {
|
2019-04-01 12:40:45 +02:00
|
|
|
ocean = await Ocean.getInstance(config)
|
2019-03-14 15:47:31 +01:00
|
|
|
|
|
|
|
// Accounts
|
2019-06-24 13:06:38 +02:00
|
|
|
;[publisher, consumer] = await ocean.accounts.list()
|
2019-06-26 20:57:50 +02:00
|
|
|
|
|
|
|
if (!ocean.keeper.dispenser) {
|
|
|
|
metadata = getMetadata(0)
|
|
|
|
}
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
|
2019-05-20 11:33:15 +02:00
|
|
|
after(() => {
|
|
|
|
try {
|
|
|
|
localStorage.clear()
|
2019-06-20 00:20:09 +02:00
|
|
|
} catch {}
|
2019-05-20 11:33:15 +02:00
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should authenticate the accounts', async () => {
|
2019-05-20 11:33:15 +02:00
|
|
|
await publisher.authenticate()
|
|
|
|
await consumer.authenticate()
|
|
|
|
})
|
|
|
|
|
2020-01-22 13:46:10 +01:00
|
|
|
it('should register an asset', async () => {
|
2019-05-07 13:08:26 +02:00
|
|
|
const steps = []
|
2019-11-15 00:00:10 +01:00
|
|
|
ddo = await ocean.assets
|
|
|
|
.create(metadata as any, publisher)
|
|
|
|
.next(step => steps.push(step))
|
2019-03-14 15:47:31 +01:00
|
|
|
|
|
|
|
assert.instanceOf(ddo, DDO)
|
2019-05-07 13:08:26 +02:00
|
|
|
assert.deepEqual(steps, [0, 1, 2, 3, 4, 5, 6, 7])
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should order the asset', async () => {
|
2020-01-27 19:14:12 +01:00
|
|
|
const steps = []
|
2019-03-14 15:47:31 +01:00
|
|
|
|
2019-06-26 20:57:50 +02:00
|
|
|
try {
|
2019-11-15 00:00:10 +01:00
|
|
|
await consumer.requestTokens(
|
|
|
|
+metadata.main.price * 10 ** -(await ocean.keeper.token.decimals())
|
|
|
|
)
|
2019-03-14 15:47:31 +01:00
|
|
|
|
2020-01-27 19:14:12 +01:00
|
|
|
agreementId = await ocean.assets
|
|
|
|
.order(ddo.id, consumer)
|
|
|
|
.next(step => steps.push(step))
|
|
|
|
} catch {}
|
2019-03-14 15:47:31 +01:00
|
|
|
|
|
|
|
assert.isDefined(agreementId)
|
2019-04-30 15:31:24 +02:00
|
|
|
assert.deepEqual(steps, [0, 1, 2, 3])
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should consume and store the assets', async () => {
|
|
|
|
const folder = '/tmp/ocean/squid-js'
|
2020-01-28 15:36:25 +01:00
|
|
|
const path = await ocean.assets.consume(agreementId, ddo.id, consumer, folder)
|
2019-03-14 15:47:31 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
assert.include(path, folder, 'The storage path is not correct.')
|
2019-03-14 15:47:31 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
const files = await new Promise<string[]>(resolve => {
|
2019-06-24 13:06:38 +02:00
|
|
|
fs.readdir(path, (e, fileList) => {
|
2019-03-14 21:28:51 +01:00
|
|
|
resolve(fileList)
|
|
|
|
})
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
files,
|
|
|
|
['README.md', 'package.json'],
|
|
|
|
'Stored files are not correct.'
|
|
|
|
)
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
})
|