-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNVMAppAPI.test.ts
273 lines (232 loc) Β· 9.57 KB
/
NVMAppAPI.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// @ts-nocheck
import chai, { assert } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { NativeTokenAddress } from '../../src/constants/AssetConstants'
import { NvmAppMetadata } from '../../src/ddo/NvmAppMetadata'
import { Web3Error } from '../../src/errors/NeverminedErrors'
import { AssetPrice } from '../../src/models/AssetPrice'
import { NvmAccount } from '../../src/models/NvmAccount'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { NVMAppEnvironments, NvmApp } from '../../src/nevermined/NvmApp'
import { SubscriptionCreditsNFTApi } from '../../src/nevermined/api/nfts/SubscriptionCreditsNFTApi'
import { ResourceAuthentication } from '../../src/types/DDOTypes'
import config from '../../test/config'
import TestContractHandler from '../../test/keeper/TestContractHandler'
chai.use(chaiAsPromised)
describe('NVM App API', () => {
describe('LOCAL: As NVM App integrator I want to initialize the api in different ways', () => {
let nvmApp: NvmApp
let signerAddress: string
let defaultSigner: NvmAccount
let publisher: NvmAccount
let subscriptionNFTAddress: string
let subscriptionDid: string
let agentDid: string
let datasetDid: string
let agreementId
let subscriptionPrice: AssetPrice
let subscriptionPriceWithFees: AssetPrice
// Agent/Service test configuration
const SERVICE_ENDPOINT = process.env.SERVICE_ENDPOINT || 'http://127.0.0.1:3000'
const OPEN_PATH = process.env.OPEN_PATH || '/openapi.json'
const OPEN_ENDPOINT = process.env.OPEN_ENDPOINT || `${SERVICE_ENDPOINT}${OPEN_PATH}`
const AUTHORIZATION_TYPE = (process.env.AUTHORIZATION_TYPE ||
'bearer') as ResourceAuthentication['type']
const AUTHORIZATION_TOKEN = process.env.AUTHORIZATION_TOKEN || 'new_authorization_token'
const AUTHORIZATION_USER = process.env.AUTHORIZATION_USER || 'user'
const AUTHORIZATION_PASSWORD = process.env.AUTHORIZATION_PASSWORD || 'password'
before(async () => {
// TestContractHandler.setConfig(config)
const nevermined = await Nevermined.getInstance(config)
;[, publisher] = nevermined.accounts.list()
const contractABI = await TestContractHandler.getABIArtifact(
`NFT1155SubscriptionUpgradeable.geth-localnet`,
'./artifacts/',
)
const subscriptionNFT = await SubscriptionCreditsNFTApi.deployInstance(
config,
contractABI,
publisher,
[
publisher.getId(),
nevermined.keeper.didRegistry.address,
'App Subscription NFT',
'CRED',
'',
nevermined.keeper.nvmConfig.address,
],
)
subscriptionNFTAddress = subscriptionNFT.address
console.debug(`Deployed ERC-1155 Subscription NFT on address: ${subscriptionNFTAddress}`)
await nevermined.contracts.loadNft1155Api(subscriptionNFT)
const neverminedNodeAddress = await nevermined.services.node.getProviderAddress()
await subscriptionNFT.grantOperatorRole(
nevermined.keeper.conditions.transferNftCondition.address,
publisher,
)
console.debug(`Granting operator role to Nevermined Node Address: ${neverminedNodeAddress}`)
await subscriptionNFT.grantOperatorRole(neverminedNodeAddress, publisher)
assert.equal(nevermined.nfts1155.getContract.address, subscriptionNFTAddress)
subscriptionPrice = new AssetPrice(publisher.getId(), 1000n).setTokenAddress(
NativeTokenAddress,
)
})
it('I want to search content from the app', async () => {
nvmApp = await NvmApp.getInstance(NVMAppEnvironments.Local)
const results = await nvmApp.search.query({})
console.log(JSON.stringify(results.totalResults))
assert.isDefined(results)
})
it('Get the default configuration used', async () => {
const appConfig = nvmApp.config
assert.isDefined(appConfig)
})
it('Overwrite the default config with some parameters', async () => {
assert.notEqual(nvmApp.config.artifactsFolder, './artifacts')
nvmApp = await NvmApp.getInstance(NVMAppEnvironments.Local, {
...config,
nftContractTimeAddress: subscriptionNFTAddress,
nftContractCreditsAddress: subscriptionNFTAddress,
})
assert.equal(nvmApp.config.artifactsFolder, './artifacts')
})
it('I want to connect my account', async () => {
assert.isFalse(nvmApp.isWeb3Connected())
assert.throws(() => nvmApp.sdk.accounts.list(), Web3Error)
defaultSigner = config.accounts[0]
signerAddress = defaultSigner.getAddress()
console.log(`Account address: ${signerAddress}`)
await nvmApp.connect(defaultSigner)
nvmApp.assetProviders.push(publisher.getId())
assert.isTrue(nvmApp.isWeb3Connected())
assert.doesNotThrow(() => nvmApp.sdk.accounts.list(), Web3Error)
})
it('I can calculate and include network fees', async () => {
subscriptionPriceWithFees = nvmApp.addNetworkFee(subscriptionPrice)
console.log('Asset Price with fees: ', subscriptionPriceWithFees)
assert.isTrue(nvmApp.isNetworkFeeIncluded(subscriptionPriceWithFees))
})
it('I want to create a time subscription', async () => {
const timeSubscriptionMetadata = NvmAppMetadata.getTimeSubscriptionMetadataTemplate(
'NVM App Time only Subscription test',
'Nevermined',
'hours',
)
const ddo = await nvmApp.createTimeSubscription(
timeSubscriptionMetadata,
subscriptionPrice,
100, // 100 blocks duration
)
assert.isDefined(ddo)
const ddoFound = await nvmApp.search.byDID(ddo.id)
assert.equal(ddo.id, ddoFound.id)
})
it('I want to create a credits subscription', async () => {
const creditsSubscriptionMetadata = NvmAppMetadata.getCreditsSubscriptionMetadataTemplate(
'NVM App Credits Subscription test',
'Nevermined',
)
const ddo = await nvmApp.createCreditsSubscription(
creditsSubscriptionMetadata,
subscriptionPrice,
50n, // blocks duration
)
assert.isDefined(ddo)
const ddoFound = await nvmApp.search.byDID(ddo.id)
assert.equal(ddo.id, ddoFound.id)
subscriptionDid = ddo.id
})
it('I want to register an Agent', async () => {
const agentMetadata = NvmAppMetadata.getServiceMetadataTemplate(
'Nevermined Ageeeent',
'Nevermined',
[
{
GET: `${SERVICE_ENDPOINT}(.*)`,
},
],
[OPEN_ENDPOINT],
OPEN_ENDPOINT,
'RESTful',
AUTHORIZATION_TYPE,
AUTHORIZATION_TOKEN,
AUTHORIZATION_USER,
AUTHORIZATION_PASSWORD,
true,
)
const ddo = await nvmApp.registerServiceAsset(
agentMetadata,
subscriptionDid,
// We are gonna configure the agent usage costs in a dynamic manner:
// The cost in credits for every succesful query to the agent will be between 1 and 5 credits being 2 credits the default cost
2n, // default cost in credits for every succesful query to the agent
1n, // min amount of credits to be consumed
5n, // max amount of credits to be consumed
subscriptionNFTAddress,
)
assert.isDefined(ddo)
const ddoFound = await nvmApp.search.byDID(ddo.id)
assert.equal(ddo.id, ddoFound.id)
agentDid = ddo.id
})
it('I want to register a Dataset', async () => {
const datasetMetadata = NvmAppMetadata.getFileMetadataTemplate(
'NVM App Dataset test',
'Nevermined',
)
datasetMetadata.main.files = [
{
index: 0,
contentType: 'application/json',
url: 'https://storage.googleapis.com/nvm-static-assets/files/ci/ddo-example.json',
},
{
index: 1,
contentType: 'text/plain',
url: 'https://storage.googleapis.com/nvm-static-assets/files/ci/README.md',
},
]
const ddo = await nvmApp.registerFileAsset(
datasetMetadata,
subscriptionDid,
1n, // every file download costs 1 credit to the subscriber
subscriptionNFTAddress,
)
assert.isDefined(ddo)
const ddoFound = await nvmApp.search.byDID(ddo.id)
assert.equal(ddo.id, ddoFound.id)
datasetDid = ddo.id
})
it('I want to order a subscription', async () => {
const orderResult = await nvmApp.orderSubscriptionAsync(subscriptionDid)
assert.isDefined(orderResult)
assert.isTrue(orderResult.success)
assert.isTrue(orderResult.agreementId.length > 0)
agreementId = orderResult.agreementId
})
it('I want to check my credits', async () => {
const planBalance = await nvmApp.getBalance(subscriptionDid)
assert.isDefined(planBalance)
assert.isTrue(planBalance.canAccess)
assert.isTrue(planBalance.balance > 0)
})
it('I want to get the token giving access to a remote agent', async () => {
const token = await nvmApp.getServiceAccessToken(agentDid)
assert.isDefined(token)
assert.isTrue(token.accessToken.length > 0)
assert.isTrue(token.neverminedProxyUri.length > 0)
})
it('I want to download a file asset', async () => {
const destination = `/tmp/.nevermined/downloads/${datasetDid}/`
const results = await nvmApp.downloadFiles(datasetDid, agreementId, destination)
assert.isDefined(results)
assert.isTrue(results.success)
console.log(`Files downloaded: ${destination}`)
})
it('I can disconnect and still search', async () => {
await nvmApp.disconnect()
const results = await nvmApp.search.query({})
assert.isTrue(results.totalResults.value > 0)
})
})
})