2019-03-14 15:47:31 +01:00
|
|
|
import { assert } from "chai"
|
2019-03-14 21:28:51 +01:00
|
|
|
import * as fs from "fs"
|
2019-03-14 15:47:31 +01:00
|
|
|
|
|
|
|
import { config } from "../config"
|
2019-03-15 15:23:28 +01:00
|
|
|
import { getMetadata } from "../utils"
|
2019-03-14 15:47:31 +01:00
|
|
|
|
2019-03-15 15:23:28 +01:00
|
|
|
import { Ocean, Account, DDO } from "../../src" // @oceanprotocol/squid
|
2019-03-14 15:47:31 +01:00
|
|
|
|
|
|
|
describe("Consume Asset (Brizo)", () => {
|
|
|
|
let ocean: Ocean
|
|
|
|
|
|
|
|
let publisher: Account
|
|
|
|
let consumer: Account
|
|
|
|
|
|
|
|
let ddo: DDO
|
|
|
|
let agreementId: string
|
|
|
|
|
2019-03-15 15:23:28 +01:00
|
|
|
const 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-04-01 12:40:45 +02:00
|
|
|
publisher = (await ocean.accounts.list())[0]
|
|
|
|
consumer = (await ocean.accounts.list())[1]
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should regiester an asset", async () => {
|
2019-03-15 15:23:28 +01:00
|
|
|
ddo = await ocean.assets.create(metadata as any, publisher)
|
2019-03-14 15:47:31 +01:00
|
|
|
|
|
|
|
assert.instanceOf(ddo, DDO)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should order the asset", async () => {
|
|
|
|
const accessService = ddo.findServiceByType("Access")
|
|
|
|
|
|
|
|
await consumer.requestTokens(metadata.base.price)
|
|
|
|
|
2019-04-24 04:08:22 +02:00
|
|
|
const steps = []
|
2019-03-14 15:47:31 +01:00
|
|
|
agreementId = await ocean.assets.order(ddo.id, accessService.serviceDefinitionId, consumer)
|
2019-04-24 04:08:22 +02:00
|
|
|
.next(step => steps.push(step))
|
2019-03-14 15:47:31 +01:00
|
|
|
|
|
|
|
assert.isDefined(agreementId)
|
2019-04-24 04:08:22 +02:00
|
|
|
assert.deepEqual(steps, [0, 1, 2, 3])
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should consume and store the assets", async () => {
|
|
|
|
const accessService = ddo.findServiceByType("Access")
|
|
|
|
|
|
|
|
const folder = "/tmp/ocean/squid-js"
|
|
|
|
const path = await ocean.assets.consume(agreementId, ddo.id, accessService.serviceDefinitionId, consumer, folder)
|
|
|
|
|
|
|
|
assert.include(path, folder, "The storage path is not correct.")
|
|
|
|
|
2019-03-14 22:54:04 +01:00
|
|
|
const files = await new Promise<string[]>((resolve) => {
|
2019-03-14 21:28:51 +01:00
|
|
|
fs.readdir(path, (err, fileList) => {
|
|
|
|
resolve(fileList)
|
|
|
|
})
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
|
2019-04-08 15:04:28 +02:00
|
|
|
assert.deepEqual(files, ["README.md", "package.json"], "Stored files are not correct.")
|
2019-03-14 15:47:31 +01:00
|
|
|
})
|
|
|
|
})
|