-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsumeAsset.test.ts
216 lines (177 loc) Β· 6.29 KB
/
ConsumeAsset.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// @ts-nocheck
import { assert } from 'chai'
import * as fs from 'fs'
import { decodeJwt } from 'jose'
import config from '../../test/config'
import { getAssetPrice, 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 { AgreementPrepareResult } from '../../src/nevermined/api/AgreementsApi'
import { AssetAttributes } from '../../src/models/AssetAttributes'
import Logger from '../../src/models/Logger'
import { ConditionState } from '../../src/types/ContractTypes'
describe('Consume Asset', () => {
let nevermined: Nevermined
let publisher: NvmAccount
let consumer: NvmAccount
let metadata: MetaData
let ddo: DDO
let serviceAgreementSignatureResult: AgreementPrepareResult
let assetPrice: AssetPrice
let agreementId: string
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 = getAssetPrice(publisher.getId())
metadata = getMetadata()
metadata.userId = payload.sub
})
it('should register an asset', async () => {
ddo = await nevermined.assets.create(
AssetAttributes.getInstance({
metadata,
services: [
{
serviceType: 'access',
price: assetPrice,
},
],
}),
publisher,
)
assert.isDefined(ddo, 'Register has not returned a DDO')
assert.match(ddo.id, /^did:nv:[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")
})
it('should be able to request tokens for consumer', async () => {
const initialBalance = await nevermined.accounts.getNeverminedBalance(consumer)
//(await consumer.getBalance()).nevermined
const claimedTokens = 100n
try {
await nevermined.accounts.requestTokens(consumer, claimedTokens)
} catch (error) {
Logger.error(error)
}
const balanceAfter = await nevermined.accounts.getNeverminedBalance(consumer)
assert.isTrue(balanceAfter > initialBalance)
})
it('should sign the service agreement', async () => {
serviceAgreementSignatureResult = await nevermined.agreements.prepareSignature(
ddo.id,
'access',
consumer,
)
const { agreementIdSeed, signature } = serviceAgreementSignatureResult
assert.match(agreementIdSeed, /^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')
})
it('should execute the service agreement', async () => {
agreementId = await nevermined.agreements.create(
ddo.id,
serviceAgreementSignatureResult.agreementIdSeed,
'access',
nevermined.keeper.templates.accessTemplate.params(consumer),
consumer,
publisher,
)
assert.isDefined(agreementId)
})
it('should get the agreement conditions status not fulfilled', async () => {
const status = await repeat(3, nevermined.agreements.status(agreementId))
assert.deepEqual(status, {
lockPayment: ConditionState.Unfulfilled,
access: ConditionState.Unfulfilled,
escrowPayment: ConditionState.Unfulfilled,
})
})
it('should lock the payment by the consumer', async () => {
const paid = await nevermined.agreements.conditions.lockPayment(
agreementId,
ddo.id,
assetPrice.getAmounts(),
assetPrice.getReceivers(),
consumer,
undefined,
)
assert.isTrue(paid, 'The asset has not been paid correctly')
})
it('should grant the access by the publisher', async () => {
try {
const granted = await nevermined.agreements.conditions.grantAccess(
agreementId,
ddo.id,
consumer.getId(),
publisher,
)
assert.isTrue(granted, 'The asset has not been granted correctly')
const accessGranted = await nevermined.keeper.conditions.accessCondition.checkPermissions(
consumer.getId(),
ddo.id,
)
assert.isTrue(accessGranted, 'Consumer has been granted.')
} catch (error) {
Logger.error(error)
}
})
it('should return true on access granted', async () => {
const accessGranted = await nevermined.agreements.isAccessGranted(
agreementId,
ddo.id,
consumer.getId(),
publisher,
)
assert.deepEqual(accessGranted, true)
})
it('should get the agreement conditions status fulfilled', async () => {
const status = await nevermined.agreements.status(agreementId)
assert.deepEqual(status, {
lockPayment: ConditionState.Fulfilled,
access: ConditionState.Fulfilled,
escrowPayment: ConditionState.Unfulfilled,
})
})
it('should consume and store the assets', async () => {
const folder = '/tmp/nevermined/sdk-js-1'
const path = (await nevermined.assets.access(
agreementId,
ddo.id,
'access',
consumer,
folder,
)) as string
assert.include(path, folder, 'The storage path is not correct.')
const files = await new Promise<string[]>((resolve) => {
fs.readdir(path as string, (_e, fileList) => {
resolve(fileList)
})
})
assert.deepEqual(files, ['README.md', 'ddo-example.json'], 'Stored files are not correct.')
})
it('should consume and store one asset', async () => {
const folder = '/tmp/nevermined/sdk-js-2'
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'], 'Stored files are not correct.')
})
})