-
Notifications
You must be signed in to change notification settings - Fork 1
/
AssetOwners.test.ts
159 lines (127 loc) Β· 5.34 KB
/
AssetOwners.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
import { assert } from 'chai'
import config from '../../test/config'
import { decodeJwt } from 'jose'
import { mineBlocks } from '../utils/utils'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { NeverminedOptions } from '../../src/models/NeverminedOptions'
import { NvmAccount } from '../../src/models/NvmAccount'
import { MetaData } from '../../src/types/DDOTypes'
import { getMetadata } from '../utils/ddo-metadata-generator'
import { generateId } from '../../src/common/helpers'
import { AssetAttributes } from '../../src/models/AssetAttributes'
describe('Asset Owners', () => {
let nevermined: Nevermined
let nevermined2: Nevermined
let nevermined3: Nevermined
let config2: NeverminedOptions
let config3: NeverminedOptions
let account1: NvmAccount
let account2: NvmAccount
let account3: NvmAccount
let newMetadata: (token: string) => MetaData
before(async () => {
config2 = { ...config }
config3 = { ...config }
nevermined = await Nevermined.getInstance(config)
nevermined2 = await Nevermined.getInstance(config2)
nevermined3 = await Nevermined.getInstance(config3)
// Accounts
;[account1, account2, account3] = nevermined.accounts.list()
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(account1)
const clientAssertion2 = await nevermined2.utils.jwt.generateClientAssertion(account2)
const clientAssertion3 = await nevermined2.utils.jwt.generateClientAssertion(account3)
await nevermined.services.marketplace.login(clientAssertion)
await nevermined2.services.marketplace.login(clientAssertion2)
await nevermined3.services.marketplace.login(clientAssertion3)
newMetadata = (token: string) => {
const metadata = getMetadata()
const jwtPayload = decodeJwt(token)
metadata.userId = jwtPayload.sub
metadata.main.name = `Test Metadata ${generateId()}`
return metadata
}
})
it('should set the owner of an asset', async () => {
const assetAttributes = AssetAttributes.getInstance({
metadata: newMetadata(config.marketplaceAuthToken),
})
const ddo = await nevermined.assets.create(assetAttributes, account1)
const owner = await nevermined.assets.owner(ddo.id)
assert.equal(owner, account1.getId())
})
it('should set the provider of an asset', async () => {
const assetAttributes = AssetAttributes.getInstance({
metadata: newMetadata(config.marketplaceAuthToken),
providers: [config.neverminedNodeAddress],
})
const ddo = await nevermined.assets.create(assetAttributes, account1)
const isProvider = await nevermined.keeper.didRegistry.isDIDProvider(
ddo.id,
config.neverminedNodeAddress,
)
assert.isTrue(isProvider)
})
it('should be added correctly a permission on an asset', async () => {
const assetAttributes = AssetAttributes.getInstance({
metadata: newMetadata(config.marketplaceAuthToken),
})
const ddo = await nevermined.assets.create(assetAttributes, account1)
assert.isFalse(await nevermined.keeper.didRegistry.getPermission(ddo.id, account2.getId()))
await nevermined.keeper.didRegistry.grantPermission(ddo.id, account2.getId(), account1)
assert.isTrue(await nevermined.keeper.didRegistry.getPermission(ddo.id, account2.getId()))
})
it('should get the assets owned by a user', async () => {
await nevermined.assets.create(
AssetAttributes.getInstance({
metadata: newMetadata(config.marketplaceAuthToken),
}),
account1,
)
await nevermined.assets.create(
AssetAttributes.getInstance({
metadata: newMetadata(config.marketplaceAuthToken),
}),
account1,
)
const ddo = await nevermined2.assets.create(
AssetAttributes.getInstance({
metadata: newMetadata(config2.marketplaceAuthToken!),
}),
account2,
)
await mineBlocks(nevermined, account1, 1)
const result = await nevermined.assets.ownerAssets(account2.getId())
assert.includeMembers(result, [ddo.id])
})
describe('Ownership transfer', () => {
let did: string
it('should be able to transfer ownership to account2', async () => {
const assetDDO = await nevermined.assets.create(
AssetAttributes.getInstance({
metadata: newMetadata(config.marketplaceAuthToken),
}),
account1,
)
did = assetDDO.id
assert.equal(assetDDO._nvm.versions.length, 1)
// transfer
await nevermined.assets.transferOwnership(did, account2.getId(), account1)
const newOwner = await nevermined.keeper.didRegistry.getDIDOwner(did)
assert.equal(newOwner, account2.getId())
const ddo = await nevermined.assets.resolve(did)
assert.equal(ddo._nvm.versions.length, 2)
assert.equal(ddo.publicKey[0].owner, account2.getId())
assert.equal(ddo.proof.creator, account2.getId())
})
it('account2 should be able to transfer ownership to account3', async () => {
// transfer
await nevermined2.assets.transferOwnership(did, account3.getId(), account2)
const newOwner = await nevermined.keeper.didRegistry.getDIDOwner(did)
assert.equal(newOwner, account3.getId())
const ddo = await nevermined.assets.resolve(did)
assert.equal(ddo._nvm.versions.length, 3)
assert.equal(ddo.publicKey[0].owner, account3.getId())
assert.equal(ddo.proof.creator, account3.getId())
})
})
})