-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssetsWithMultipleServices.test.ts
144 lines (120 loc) Β· 4.63 KB
/
AssetsWithMultipleServices.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
// @ts-nocheck
import { assert } from 'chai'
import { decodeJwt, JWTPayload } from 'jose'
import { getMetadata } from '../utils'
import config from '../../test/config'
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 { AssetAttributes } from '../../src/models/AssetAttributes'
import { repeat } from '../utils/utils'
import { AssetPrice } from '../../src/models/AssetPrice'
import { Token } from '../../src/keeper/contracts/Token'
import { ConditionState } from '../../src/types/ContractTypes'
let nevermined: Nevermined
let publisher: NvmAccount
let consumer: NvmAccount
let metadata: MetaData
let createdMetadata: MetaData
let assetPrice1: AssetPrice
let assetPrice2: AssetPrice
let payload: JWTPayload
let ddo: DDO
let token: Token
let balanceBefore: bigint
let balanceAfter: bigint
let service
let agreementId
let neverminedNodeAddress
const totalAmount1 = '100'
const totalAmount2 = '350'
describe('Assets with multiple services', () => {
before(async () => {
nevermined = await Nevermined.getInstance(config)
;({ token } = nevermined.keeper)
// Accounts
;[publisher, consumer] = nevermined.accounts.list()
neverminedNodeAddress = await nevermined.services.node.getProviderAddress()
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(publisher)
await nevermined.services.marketplace.login(clientAssertion)
payload = decodeJwt(config.marketplaceAuthToken)
assetPrice1 = new AssetPrice(publisher.getId(), BigInt(totalAmount1))
assetPrice2 = new AssetPrice(publisher.getId(), BigInt(totalAmount2))
try {
await nevermined.accounts.requestTokens(consumer, BigInt(totalAmount1) * 10n)
} catch (error) {
console.error(error)
}
metadata = getMetadata()
metadata.userId = payload.sub
})
describe('E2E flow for an asset with multiple access services', () => {
it('Register with multiple access services', async () => {
balanceBefore = await token.balanceOf(consumer.getId())
const nonce = Math.random()
createdMetadata = getMetadata(nonce, `Immutable Multiple Services Test ${nonce}`)
createdMetadata.additionalInformation.tags = ['test']
const assetAttributes = AssetAttributes.getInstance({
metadata: createdMetadata,
services: [
{
serviceType: 'access',
price: assetPrice1,
},
{
serviceType: 'access',
price: assetPrice2,
},
],
providers: [neverminedNodeAddress],
})
ddo = await nevermined.assets.create(assetAttributes, publisher)
assert.isDefined(ddo)
console.log(ddo.id)
const accessServices = ddo.getServicesByType('access')
assert.equal(accessServices.length, 2)
assert.equal(accessServices[0].index, 2)
assert.equal(accessServices[1].index, 3)
assert.equal(
accessServices[0].attributes.serviceAgreementTemplate.conditions[0].parameters[3].value[0],
totalAmount1,
)
assert.equal(
accessServices[1].attributes.serviceAgreementTemplate.conditions[0].parameters[3].value[0],
totalAmount2,
)
const metadata = ddo.findServiceByType('metadata')
assert.equal(metadata.attributes.additionalInformation.tags[0], 'test')
})
it('Order one between multiple access services', async () => {
const accessServices = ddo.getServicesByType('access')
service = accessServices[0]
const price = service.attributes.main.price
agreementId = await nevermined.assets.order(ddo.id, service.index, consumer)
const status = await repeat(3, nevermined.agreements.status(agreementId))
assert.deepEqual(status, {
lockPayment: ConditionState.Fulfilled,
access: ConditionState.Unfulfilled,
escrowPayment: ConditionState.Unfulfilled,
})
balanceAfter = await token.balanceOf(consumer.getId())
console.log(`Asset Price = ${price}`)
console.log(`Balance Before: ${balanceBefore.toString()}`)
console.log(`Balance After : ${balanceAfter.toString()}`)
assert.isTrue(balanceBefore - BigInt(price) === balanceAfter)
})
it('Download assets through the Node', async () => {
const folder = '/tmp/nevermined/sdk-js/multiple-services/access'
const path = (await nevermined.assets.access(
agreementId,
ddo.id,
service.index,
consumer,
folder,
-1,
)) as string
assert.include(path, folder, 'The storage path is not correct.')
})
})
})