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
|
|
|
|
2019-06-20 00:20:09 +02: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
|
|
|
|
|
2019-03-15 15:23:28 +01:00
|
|
|
const 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 () => {
|
2019-04-01 12:40:45 +02:00
|
|
|
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()
|
2019-03-14 16:58:07 +01:00
|
|
|
})
|
2019-02-21 18:07:02 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should regiester a 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')
|
|
|
|
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-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
|
2019-06-20 00:20:09 +02:00
|
|
|
const claimedTokens =
|
|
|
|
+metadata.base.price * 10 ** -(await ocean.keeper.token.decimals())
|
2019-05-23 17:21:25 +02:00
|
|
|
await consumer.requestTokens(claimedTokens)
|
2019-02-21 18:07:02 +01:00
|
|
|
|
2019-06-20 00:20:09 +02: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 () => {
|
|
|
|
const accessService = ddo.findServiceByType('Access')
|
2019-02-21 18:07:02 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
serviceAgreementSignatureResult = await ocean.agreements.prepare(
|
|
|
|
ddo.id,
|
|
|
|
accessService.serviceDefinitionId,
|
|
|
|
consumer
|
|
|
|
)
|
2019-02-25 14:06:48 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
const { agreementId, signature } = serviceAgreementSignatureResult
|
|
|
|
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 () => {
|
|
|
|
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,
|
|
|
|
accessService.serviceDefinitionId,
|
|
|
|
serviceAgreementSignatureResult.signature,
|
|
|
|
consumer,
|
2019-06-20 00:20:09 +02:00
|
|
|
publisher
|
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 () => {
|
|
|
|
const status = await ocean.agreements.status(
|
|
|
|
serviceAgreementSignatureResult.agreementId
|
|
|
|
)
|
2019-04-15 14:28:17 +02:00
|
|
|
|
|
|
|
assert.deepEqual(status, {
|
|
|
|
lockReward: ConditionState.Unfulfilled,
|
|
|
|
accessSecretStore: ConditionState.Unfulfilled,
|
2019-06-20 00:20:09 +02:00
|
|
|
escrowReward: ConditionState.Unfulfilled
|
2019-04-15 14:28:17 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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,
|
|
|
|
ddo.findServiceByType('Metadata').metadata.base.price,
|
|
|
|
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
|
|
|
|
2019-05-21 15:32:01 +02: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 () => {
|
2019-05-21 15:32:01 +02:00
|
|
|
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-04-01 12:40:45 +02:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
const accessGranted = await ocean.keeper.conditions.accessSecretStoreCondition.checkPermissions(
|
|
|
|
consumer.getId(),
|
|
|
|
ddo.id
|
|
|
|
)
|
2019-04-01 12:40:45 +02:00
|
|
|
|
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 () => {
|
|
|
|
const status = await ocean.agreements.status(
|
|
|
|
serviceAgreementSignatureResult.agreementId
|
|
|
|
)
|
2019-04-15 14:28:17 +02:00
|
|
|
|
|
|
|
assert.deepEqual(status, {
|
|
|
|
lockReward: ConditionState.Fulfilled,
|
|
|
|
accessSecretStore: ConditionState.Fulfilled,
|
2019-06-20 00:20:09 +02:00
|
|
|
escrowReward: ConditionState.Unfulfilled
|
2019-04-15 14:28:17 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should consume and store the assets', async () => {
|
|
|
|
const accessService = ddo.findServiceByType('Access')
|
2019-02-21 18:07:02 +01:00
|
|
|
|
2019-06-20 00:20:09 +02: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,
|
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
|
|
|
|
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-15 15:23:28 +01:00
|
|
|
resolve(fileList)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
files,
|
|
|
|
['README.md', 'package.json'],
|
|
|
|
'Stored files are not correct.'
|
|
|
|
)
|
2019-02-21 18:07:02 +01:00
|
|
|
})
|
2019-04-03 17:57:47 +02:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should consume and store one assets', async () => {
|
|
|
|
const accessService = ddo.findServiceByType('Access')
|
2019-04-03 17:57:47 +02:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
const folder = '/tmp/ocean/squid-js-2'
|
2019-04-03 17:57:47 +02:00
|
|
|
const path = await ocean.assets.consume(
|
|
|
|
serviceAgreementSignatureResult.agreementId,
|
|
|
|
ddo.id,
|
|
|
|
accessService.serviceDefinitionId,
|
|
|
|
consumer,
|
|
|
|
folder,
|
2019-06-20 00:20:09 +02:00
|
|
|
1
|
2019-04-03 17:57:47 +02:00
|
|
|
)
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
assert.include(path, folder, 'The storage path is not correct.')
|
2019-04-03 17:57:47 +02: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-04-03 17:57:47 +02:00
|
|
|
resolve(fileList)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
assert.deepEqual(files, ['README.md'], 'Stored files are not correct.')
|
2019-04-03 17:57:47 +02:00
|
|
|
})
|
2019-02-21 18:07:02 +01:00
|
|
|
})
|