-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProvenance.test.ts
166 lines (145 loc) Β· 5.24 KB
/
Provenance.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
// @ts-nocheck
import { assert } from 'chai'
import { decodeJwt } from 'jose'
import config from '../../test/config'
import { getMetadata } from '../utils'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { NvmAccount } from '../../src/models/NvmAccount'
import { DDO } from '../../src/ddo/DDO'
import { generateId } from '../../src/common/helpers'
import { AssetAttributes } from '../../src/models/AssetAttributes'
import { zeroX } from '../../src/utils/ConversionTypeHelpers'
import { ProvenanceMethod } from '../../src/keeper/contracts/Provenance'
import { zeroPadValue } from '../../src/nevermined/utils/BlockchainViemUtils'
describe('Provenance', () => {
let nevermined: Nevermined
let publisher: NvmAccount
let intermediary: NvmAccount
let ddo: DDO
const activitiesIds = {
publisher: generateId(),
intermediary: generateId(),
}
before(async () => {
nevermined = await Nevermined.getInstance(config)
// Accounts
;[publisher, intermediary] = nevermined.accounts.list()
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(publisher)
await nevermined.services.marketplace.login(clientAssertion)
})
it('should be able to get the provenance data from a new asset', async () => {
const payload = decodeJwt(config.marketplaceAuthToken)
const metadata = getMetadata()
metadata.userId = payload.sub
ddo = await nevermined.assets.create(AssetAttributes.getInstance({ metadata }), publisher)
const provenance = await nevermined.provenance.getProvenanceEntry(ddo.shortId())
assert.equal(zeroX(provenance.did), zeroX(ddo.shortId()))
assert.equal(zeroX(provenance.agentId), zeroX(publisher.getId()))
assert.equal(provenance.method, ProvenanceMethod.WAS_GENERATED_BY)
})
it('should register an association', async () => {
const provId = generateId()
await nevermined.provenance.wasAssociatedWith(
provId,
ddo.shortId(),
publisher.getId(),
activitiesIds.publisher,
'PublisherStuff',
publisher,
)
const provenance = await nevermined.provenance.getProvenanceEntry(provId)
assert.equal(zeroX(provenance.did), zeroX(ddo.shortId()))
assert.equal(zeroX(provenance.agentId), zeroX(publisher.getId()))
assert.equal(provenance.method, ProvenanceMethod.WAS_ASSOCIATED_WITH)
})
it('should delegate the next step of the provenance', async () => {
const provId = generateId()
await nevermined.provenance.actedOnBehalf(
provId,
ddo.shortId(),
intermediary.getId(),
publisher.getId(),
activitiesIds.intermediary,
zeroPadValue('0x', 32),
'FirstIntermediaryStuff',
publisher,
)
const provenance = await nevermined.provenance.getProvenanceEntry(provId)
assert.equal(zeroX(provenance.did), zeroX(ddo.shortId()))
assert.equal(zeroX(provenance.agentId), zeroX(intermediary.getId()))
assert.equal(zeroX(provenance.agentInvolvedId), zeroX(publisher.getId()))
assert.equal(provenance.method, ProvenanceMethod.ACTED_ON_BEHALF)
})
it('should register an intermediary association', async () => {
const provId = generateId()
await nevermined.provenance.wasAssociatedWith(
provId,
ddo.shortId(),
intermediary.getId(),
activitiesIds.intermediary,
'FirstIntermediaryStuff',
intermediary,
)
const provenance = await nevermined.provenance.getProvenanceEntry(provId)
assert.equal(zeroX(provenance.did), zeroX(ddo.shortId()))
assert.equal(zeroX(provenance.agentId), zeroX(intermediary.getId()))
assert.equal(provenance.method, ProvenanceMethod.WAS_ASSOCIATED_WITH)
})
it('should register a use', async () => {
const provId = generateId()
await nevermined.provenance.used(
provId,
ddo.shortId(),
intermediary.getId(),
activitiesIds.intermediary,
zeroPadValue('0x', 32),
'FirstIntermediaryStuff',
intermediary,
)
const provenance = await nevermined.provenance.getProvenanceEntry(provId)
assert.equal(zeroX(provenance.did), zeroX(ddo.shortId()))
assert.equal(zeroX(provenance.agentId), zeroX(intermediary.getId()))
assert.equal(provenance.method, ProvenanceMethod.USED)
})
it('should return the events associated to DID', async () => {
const pm = ProvenanceMethod
const events = await nevermined.provenance.getDIDProvenanceEvents(ddo.shortId())
assert.deepEqual(
events.map((_) => _.method),
[
pm.WAS_GENERATED_BY,
pm.WAS_ASSOCIATED_WITH,
pm.ACTED_ON_BEHALF,
pm.WAS_ASSOCIATED_WITH,
pm.USED,
],
)
})
it('should return the events of an specific method by DID', async () => {
const events = await Promise.all(
[
'WAS_GENERATED_BY',
'USED',
'WAS_DERIVED_FROM',
'WAS_ASSOCIATED_WITH',
'ACTED_ON_BEHALF',
].map(async (_) => [
_,
(
await nevermined.provenance.getProvenanceMethodEvents(
ProvenanceMethod[_] as any,
ddo.shortId(),
)
).length,
]),
)
const expected = [
['WAS_GENERATED_BY', 1],
['USED', 1],
['WAS_DERIVED_FROM', 0],
['WAS_ASSOCIATED_WITH', 2],
['ACTED_ON_BEHALF', 1],
]
assert.deepEqual(events, expected)
})
})