1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/integration/ocean/ConsumeAsset.test.ts

161 lines
5.6 KiB
TypeScript
Raw Normal View History

2019-03-14 21:28:51 +01:00
import { assert } from "chai"
2019-03-15 15:23:28 +01:00
import * as fs from "fs"
2019-02-21 18:07:02 +01:00
import { config } from "../config"
2019-03-15 15:23:28 +01:00
import { getMetadata } from "../utils"
2019-02-21 18:07:02 +01:00
import { Ocean, DDO, Account, ConditionState } from "../../src" // @oceanprotocol/squid
2019-02-21 18:07:02 +01:00
2019-03-14 16:58:07 +01:00
describe("Consume Asset", () => {
2019-02-21 18:07:02 +01:00
let ocean: Ocean
let publisher: Account
let consumer: Account
2019-03-15 15:23:28 +01:00
const metadata = getMetadata()
2019-03-14 16:58:07 +01:00
let ddo: DDO
let serviceAgreementSignatureResult: {agreementId: string, signature: string}
2019-02-21 18:07:02 +01:00
before(async () => {
ocean = await Ocean.getInstance(config)
2019-02-21 18:07:02 +01:00
// Accounts
publisher = (await ocean.accounts.list())[0]
consumer = (await ocean.accounts.list())[1]
2019-03-14 16:58:07 +01:00
})
2019-02-21 18:07:02 +01:00
2019-03-14 16:58:07 +01:00
it("should regiester a asset", async () => {
ddo = await ocean.assets.create(metadata as any, publisher)
assert.isDefined(ddo, "Register has not returned a DDO")
assert.match(ddo.id, /^did:op:[a-f0-9]{64}$/, "DDO id is not valid")
assert.isAtLeast(ddo.authentication.length, 1, "Default authentication not added")
assert.isDefined(ddo.findServiceByType("Access"), "DDO Access service doesn't exist")
2019-02-21 18:07:02 +01:00
})
2019-03-14 16:58:07 +01:00
it("should be able to request tokens for consumer", async () => {
2019-03-14 21:28:51 +01:00
const initialBalance = (await consumer.getBalance()).ocn
const claimedTokens = +metadata.base.price * (10 ** -await ocean.keeper.token.decimals())
await consumer.requestTokens(claimedTokens)
2019-02-21 18:07:02 +01:00
assert.equal((await consumer.getBalance()).ocn, initialBalance + claimedTokens, "OCN Tokens not delivered")
2019-02-21 18:07:02 +01:00
})
2019-03-14 16:58:07 +01:00
it("should sign the service agreement", async () => {
2019-02-21 18:07:02 +01:00
const accessService = ddo.findServiceByType("Access")
2019-03-14 16:58:07 +01:00
serviceAgreementSignatureResult = await ocean.agreements.prepare(ddo.id, accessService.serviceDefinitionId, consumer)
2019-02-25 14:06:48 +01:00
2019-03-14 16:58:07 +01:00
const {agreementId, signature} = serviceAgreementSignatureResult
2019-04-15 17:45:06 +02:00
assert.match(agreementId, /^0x[a-f0-9]{64}$/, "Service agreement ID seems not valid")
2019-03-14 16:58:07 +01:00
assert.match(signature, /^0x[a-f0-9]{130}$/, "Service agreement signature seems not valid")
2019-02-21 18:07:02 +01:00
})
2019-03-14 16:58:07 +01:00
it("should execute the service agreement", async () => {
2019-02-21 18:07:02 +01:00
const accessService = ddo.findServiceByType("Access")
2019-03-14 16:58:07 +01:00
const success = await ocean.agreements.create(
ddo.id,
serviceAgreementSignatureResult.agreementId,
accessService.serviceDefinitionId,
serviceAgreementSignatureResult.signature,
consumer,
publisher,
)
2019-02-21 18:07:02 +01:00
2019-03-14 16:58:07 +01:00
assert.isTrue(success)
})
it("should get the agreement conditions status not fulfilled", async () => {
const status = await ocean.agreements.status(serviceAgreementSignatureResult.agreementId)
assert.deepEqual(status, {
lockReward: ConditionState.Unfulfilled,
accessSecretStore: ConditionState.Unfulfilled,
escrowReward: ConditionState.Unfulfilled,
})
})
2019-03-18 13:04:59 +01:00
it("should lock the payment by the consumer", async () => {
2019-03-14 16:58:07 +01:00
const paid = await ocean.agreements.conditions
.lockReward(
serviceAgreementSignatureResult.agreementId,
ddo.findServiceByType("Metadata").metadata.base.price,
2019-03-15 15:23:28 +01:00
consumer,
2019-03-14 16:58:07 +01:00
)
assert.isTrue(paid, "The asset has not been paid correctly")
})
2019-02-21 18:07:02 +01:00
2019-03-18 13:04:59 +01:00
it("should grant the access by the publisher", async () => {
2019-03-14 16:58:07 +01:00
const granted = await ocean.agreements.conditions
.grantAccess(serviceAgreementSignatureResult.agreementId, ddo.id, consumer.getId(), publisher)
assert.isTrue(granted, "The asset has not been granted correctly")
const accessGranted = await ocean.keeper.conditions
.accessSecretStoreCondition
.checkPermissions(consumer.getId(), ddo.id)
assert.isTrue(accessGranted, "Consumer has been granted.")
2019-03-14 16:58:07 +01:00
})
it("should get the agreement conditions status fulfilled", async () => {
const status = await ocean.agreements.status(serviceAgreementSignatureResult.agreementId)
assert.deepEqual(status, {
lockReward: ConditionState.Fulfilled,
accessSecretStore: ConditionState.Fulfilled,
escrowReward: ConditionState.Unfulfilled,
})
})
2019-03-15 15:23:28 +01:00
it("should consume and store the assets", async () => {
2019-03-14 16:58:07 +01:00
const accessService = ddo.findServiceByType("Access")
2019-02-21 18:07:02 +01:00
const folder = "/tmp/ocean/squid-js-1"
2019-03-15 15:23:28 +01:00
const path = await ocean.assets.consume(
serviceAgreementSignatureResult.agreementId,
ddo.id,
accessService.serviceDefinitionId,
consumer,
folder,
)
assert.include(path, folder, "The storage path is not correct.")
const files = await new Promise<string[]>((resolve) => {
fs.readdir(path, (err, fileList) => {
resolve(fileList)
})
})
assert.deepEqual(files, ["README.md", "package.json"], "Stored files are not correct.")
2019-02-21 18:07:02 +01:00
})
it("should consume and store one assets", async () => {
const accessService = ddo.findServiceByType("Access")
const folder = "/tmp/ocean/squid-js-2"
const path = await ocean.assets.consume(
serviceAgreementSignatureResult.agreementId,
ddo.id,
accessService.serviceDefinitionId,
consumer,
folder,
1,
)
assert.include(path, folder, "The storage path is not correct.")
const files = await new Promise<string[]>((resolve) => {
fs.readdir(path, (err, fileList) => {
resolve(fileList)
})
})
2019-04-09 12:50:49 +02:00
assert.deepEqual(files, ["README.md"], "Stored files are not correct.")
})
2019-02-21 18:07:02 +01:00
})