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/ConsumeAsset.test.ts

207 lines
6.2 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { assert } from 'chai'
import * as fs from 'fs'
2019-02-21 18:07:02 +01:00
2019-06-20 00:20:09 +02:00
import { config } from '../config'
import { getMetadata } from '../utils'
2019-02-21 18:07:02 +01:00
2020-01-31 00:15:55 +01:00
import { Ocean, DDO, Account, ConditionState } from '../../../src' // @oceanprotocol/squid
2019-02-21 18:07:02 +01:00
2019-06-20 00:20:09 +02:00
describe('Consume Asset', () => {
2019-02-21 18:07:02 +01:00
let ocean: Ocean
let publisher: Account
let consumer: Account
let metadata = getMetadata()
2019-03-14 16:58:07 +01:00
let ddo: DDO
2019-06-20 00:20:09 +02:00
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
2019-06-24 13:06:38 +02:00
;[publisher, consumer] = await ocean.accounts.list()
if (!ocean.keeper.dispenser) {
metadata = getMetadata(0)
}
2019-03-14 16:58:07 +01:00
})
2019-02-21 18:07:02 +01:00
2019-08-15 13:23:56 +02:00
it('should register an asset', async () => {
2019-03-14 16:58:07 +01:00
ddo = await ocean.assets.create(metadata as any, publisher)
2019-06-20 00:20:09 +02:00
assert.isDefined(ddo, 'Register has not returned a DDO')
assert.match(ddo.id, /^did:op:[a-f0-9]{64}$/, 'DDO id is not valid')
2019-09-09 12:18:54 +02:00
assert.isAtLeast(ddo.authentication.length, 1, 'Default authentication not added')
2019-11-15 00:00:10 +01:00
assert.isDefined(
ddo.findServiceByType('access'),
"DDO access service doesn't exist"
)
2019-02-21 18:07:02 +01:00
})
2019-06-20 00:20:09 +02: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 = parseInt(
(
+metadata.main.price *
10 ** -(await ocean.keeper.token.decimals())
).toString()
)
try {
await consumer.requestTokens(claimedTokens)
} catch {}
2019-02-21 18:07:02 +01:00
2019-11-15 00:00:10 +01:00
assert.equal(
(await consumer.getBalance()).ocn,
initialBalance + claimedTokens,
'OCN Tokens not delivered'
)
2019-02-21 18:07:02 +01:00
})
2019-06-20 00:20:09 +02:00
it('should sign the service agreement', async () => {
2019-08-16 14:12:31 +02:00
const accessService = ddo.findServiceByType('access')
2019-02-21 18:07:02 +01:00
2019-11-15 00:00:10 +01:00
serviceAgreementSignatureResult = await ocean.agreements.prepare(
ddo.id,
accessService.index,
consumer
)
2019-02-25 14:06:48 +01:00
2019-06-20 00:20:09 +02:00
const { agreementId, signature } = serviceAgreementSignatureResult
2019-11-15 00:00:10 +01:00
assert.match(
agreementId,
/^0x[a-f0-9]{64}$/,
'Service agreement ID seems not valid'
)
assert.match(
signature,
/^0x[a-f0-9]{130}$/,
'Service agreement signature seems not valid'
)
2019-02-21 18:07:02 +01:00
})
2019-06-20 00:20:09 +02:00
it('should execute the service agreement', async () => {
2019-08-16 14:12:31 +02:00
const accessService = ddo.findServiceByType('access')
2019-02-21 18:07:02 +01:00
2019-03-14 16:58:07 +01:00
const success = await ocean.agreements.create(
ddo.id,
serviceAgreementSignatureResult.agreementId,
2019-08-16 16:12:42 +02:00
accessService.index,
2019-03-14 16:58:07 +01:00
serviceAgreementSignatureResult.signature,
consumer,
config.brizoAddress,
consumer
2019-03-14 16:58:07 +01:00
)
2019-02-21 18:07:02 +01:00
2019-03-14 16:58:07 +01:00
assert.isTrue(success)
})
2019-06-20 00:20:09 +02:00
it('should get the agreement conditions status not fulfilled', async () => {
// Wait for the agreement event
// await ocean.keeper.agreementStoreManager
// .getAgreementCreatedEvent(serviceAgreementSignatureResult.agreementId).once()
2019-11-15 00:00:10 +01:00
const status = await ocean.agreements.status(
serviceAgreementSignatureResult.agreementId
)
assert.deepEqual(status, {
lockReward: ConditionState.Unfulfilled,
accessSecretStore: ConditionState.Unfulfilled,
2019-06-20 00:20:09 +02:00
escrowReward: ConditionState.Unfulfilled
})
})
2019-06-20 00:20:09 +02:00
it('should lock the payment by the consumer', async () => {
const paid = await ocean.agreements.conditions.lockReward(
serviceAgreementSignatureResult.agreementId,
2019-08-16 16:12:42 +02:00
ddo.findServiceByType('metadata').attributes.main.price,
2019-06-20 00:20:09 +02:00
consumer
)
2019-03-14 16:58:07 +01:00
2019-06-20 00:20:09 +02:00
assert.isTrue(paid, 'The asset has not been paid correctly')
2019-03-14 16:58:07 +01:00
})
2019-02-21 18:07:02 +01:00
// The test will fail because Brizo grants the access faster
2019-06-20 00:20:09 +02:00
it('should grant the access by the publisher', async () => {
try {
2019-06-20 00:20:09 +02:00
const granted = await ocean.agreements.conditions.grantAccess(
serviceAgreementSignatureResult.agreementId,
ddo.id,
consumer.getId(),
publisher
)
2019-03-14 16:58:07 +01:00
2019-06-20 00:20:09 +02:00
assert.isTrue(granted, 'The asset has not been granted correctly')
2019-06-20 00:20:09 +02:00
const accessGranted = await ocean.keeper.conditions.accessSecretStoreCondition.checkPermissions(
consumer.getId(),
ddo.id
)
2019-06-20 00:20:09 +02:00
assert.isTrue(accessGranted, 'Consumer has been granted.')
} catch {}
2019-03-14 16:58:07 +01:00
})
2019-06-20 00:20:09 +02:00
it('should get the agreement conditions status fulfilled', async () => {
2019-11-15 00:00:10 +01:00
const status = await ocean.agreements.status(
serviceAgreementSignatureResult.agreementId
)
assert.deepEqual(status, {
lockReward: ConditionState.Fulfilled,
accessSecretStore: ConditionState.Fulfilled,
2019-06-20 00:20:09 +02:00
escrowReward: ConditionState.Unfulfilled
})
})
2019-06-20 00:20:09 +02:00
it('should consume and store the assets', async () => {
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,
consumer,
2019-06-20 00:20:09 +02:00
folder
2019-03-15 15:23:28 +01:00
)
2019-06-20 00:20:09 +02:00
assert.include(path, folder, 'The storage path is not correct.')
2019-03-15 15:23:28 +01:00
const files = await new Promise<string[]>((resolve) => {
2019-06-24 13:06:38 +02:00
fs.readdir(path, (e, fileList) => {
2019-03-15 15:23:28 +01:00
resolve(fileList)
})
})
2019-11-15 00:00:10 +01:00
assert.deepEqual(
files,
['README.md', 'package.json'],
'Stored files are not correct.'
)
2019-02-21 18:07:02 +01:00
})
2019-08-15 13:23:56 +02:00
it('should consume and store one asset', async () => {
2019-06-20 00:20:09 +02:00
const folder = '/tmp/ocean/squid-js-2'
const path = await ocean.assets.consume(
serviceAgreementSignatureResult.agreementId,
ddo.id,
consumer,
folder,
2019-06-20 00:20:09 +02:00
1
)
2019-06-20 00:20:09 +02:00
assert.include(path, folder, 'The storage path is not correct.')
const files = await new Promise<string[]>((resolve) => {
2019-06-24 13:06:38 +02:00
fs.readdir(path, (e, fileList) => {
resolve(fileList)
})
})
2019-06-20 00:20:09 +02:00
assert.deepEqual(files, ['README.md'], 'Stored files are not correct.')
})
2019-02-21 18:07:02 +01:00
})