-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsumeAssetViaNode.test.ts
139 lines (112 loc) Β· 4.15 KB
/
ConsumeAssetViaNode.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { assert } from 'chai'
import * as fs from 'fs'
import { decodeJwt } from 'jose'
import config from '../../test/config'
import { getMetadata } from '../utils'
import { repeat } from '../utils/utils'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { MetaData } from '../../src/types/DDOTypes'
import { DDO } from '../../src/ddo/DDO'
import { NvmAccount } from '../../src/models/NvmAccount'
import { AssetPrice } from '../../src/models/AssetPrice'
import { AssetAttributes } from '../../src/models/AssetAttributes'
import Logger from '../../src/models/Logger'
import { ConditionState } from '../../src/types/ContractTypes'
import { getChecksumAddress } from '../../src/nevermined/utils/BlockchainViemUtils'
describe('Consume Asset (Nevermined Node)', () => {
let nevermined: Nevermined
let publisher: NvmAccount
let consumer: NvmAccount
let ddo: DDO
let agreementId: string
let metadata: MetaData
let assetPrice: AssetPrice
before(async () => {
nevermined = await Nevermined.getInstance(config)
// Accounts
;[publisher, consumer] = nevermined.accounts.list()
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(publisher)
await nevermined.services.marketplace.login(clientAssertion)
const payload = decodeJwt(config.marketplaceAuthToken)
assetPrice = new AssetPrice(publisher.getId(), 0n)
metadata = getMetadata()
metadata.main.name = `${metadata.main.name} - ${Math.random()}`
metadata.userId = payload.sub
})
after(() => {
try {
localStorage.clear()
} catch (error) {
Logger.error(error)
}
})
it('should fetch the RSA publicKey from the Nevermined Node', async () => {
const rsaPublicKey = await nevermined.services.node.getRsaPublicKey()
assert.isDefined(rsaPublicKey)
})
it('should register an asset', async () => {
const steps: any[] = []
const assetAttributes = AssetAttributes.getInstance({
metadata,
services: [
{
serviceType: 'access',
price: assetPrice,
},
],
providers: [config.neverminedNodeAddress],
})
ddo = await nevermined.assets
.create(assetAttributes, publisher)
.next((step) => steps.push(step))
assert.instanceOf(ddo, DDO)
assert.deepEqual(steps, [0, 1, 2, 3, 4, 5, 6, 9, 10, 12])
const assetProviders = await nevermined.assets.providers.list(ddo.id)
assert.deepEqual(assetProviders, [getChecksumAddress(config.neverminedNodeAddress)])
})
it('should order the asset', async () => {
const steps: any[] = []
agreementId = await nevermined.assets
.order(ddo.id, 'access', consumer)
.next((step) => steps.push(step))
assert.isDefined(agreementId)
assert.deepEqual(steps, [2, 3, 4, 5])
})
it('should get the lockPayment condition fulfilled', async () => {
const status = await repeat(3, nevermined.agreements.status(agreementId))
assert.deepEqual(status, {
lockPayment: ConditionState.Fulfilled,
access: ConditionState.Unfulfilled,
escrowPayment: ConditionState.Unfulfilled,
})
})
it('should be able to download the asset if you are the owner', async () => {
const folder = '/tmp/nevermined/sdk-js'
const path = (await nevermined.assets.download(ddo.id, publisher, folder, -1)) as string
assert.include(path, folder, 'The storage path is not correct.')
const files = await new Promise<string[]>((resolve) => {
fs.readdir(path, (_e, fileList) => {
resolve(fileList)
})
})
assert.deepEqual(files, ['README.md', 'ddo-example.json'], 'Stored files are not correct.')
})
it('should consume and store the assets', async () => {
const folder = '/tmp/nevermined/sdk-js'
const path = (await nevermined.assets.access(
agreementId,
ddo.id,
'access',
consumer,
folder,
-1,
)) as string
assert.include(path, folder, 'The storage path is not correct.')
const files = await new Promise<string[]>((resolve) => {
fs.readdir(path, (_e, fileList) => {
resolve(fileList)
})
})
assert.deepEqual(files, ['README.md', 'ddo-example.json'], 'Stored files are not correct.')
})
})