-
Notifications
You must be signed in to change notification settings - Fork 135
/
data-store-orm.test.ts
405 lines (359 loc) · 11.4 KB
/
data-store-orm.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// noinspection ES6PreferShortImport
import {
FindArgs,
IDataStore,
IDataStoreORM,
IMessage,
TAgent,
TCredentialColumns,
TMessageColumns,
TPresentationColumns,
VerifiableCredential,
VerifiablePresentation,
} from '../../../core-types/src'
import { Agent } from '../../../core/src'
import { DataSource } from 'typeorm'
import { DataStoreORM } from '../data-store-orm.js'
import { DataStore } from '../data-store.js'
import { Entities } from '../index.js'
import * as fs from 'fs'
const did1 = 'did:test:111'
const did2 = 'did:test:222'
const did3 = 'did:test:333'
const did4 = 'did:test:444'
async function populateDB(agent: TAgent<IDataStore & IDataStoreORM>) {
const vc1: VerifiableCredential = {
'@context': ['https://www.w3.org/2018/credentials/v1323', 'https://www.w3.org/2020/demo/4342323'],
type: ['VerifiableCredential', 'PublicProfile'],
issuer: { id: did1 },
issuanceDate: new Date().toISOString(),
id: 'vc1',
credentialSubject: {
id: did2,
name: 'Alice',
profilePicture: 'https://example.com/a.png',
address: {
street: 'Some str.',
house: 1,
},
},
proof: {
jwt: 'mockJWT',
},
}
const vp1: VerifiablePresentation = {
'@context': ['https://www.w3.org/2018/credentials/v1323', 'https://www.w3.org/2020/demo/4342323'],
type: ['VerifiablePresentation', 'PublicProfile'],
holder: did1,
verifier: [did2],
issuanceDate: new Date().toISOString(),
verifiableCredential: [vc1],
proof: {
jwt: 'mockJWT',
},
}
const vp2: VerifiablePresentation = {
'@context': ['https://www.w3.org/2018/credentials/v1323', 'https://www.w3.org/2020/demo/4342323'],
type: ['VerifiablePresentation', 'PublicProfileMultiAudience'],
holder: did1,
verifier: [did2, did4],
issuanceDate: new Date().toISOString(),
verifiableCredential: [vc1],
proof: {
jwt: 'mockJWT',
},
}
const m1: IMessage = {
id: 'm1',
from: did1,
to: did2,
createdAt: '2020-06-16T11:06:51.680Z',
type: 'mock',
raw: 'mock',
credentials: [vc1],
presentations: [vp1],
}
const m2: IMessage = {
id: 'm2',
from: did1,
to: did1,
createdAt: '2020-06-16T11:07:51.680Z',
type: 'mock',
raw: 'mock234',
}
const m3: IMessage = {
id: 'm3',
from: did3,
to: did2,
createdAt: '2020-06-16T11:08:51.680Z',
type: 'mock',
raw: 'mock678',
}
const m4: IMessage = {
id: 'm4',
from: did1,
to: did2,
createdAt: '2020-06-16T11:09:51.680Z',
type: 'mock',
raw: 'mockmoreaudienct',
credentials: [vc1],
presentations: [vp2],
}
await agent.dataStoreSaveMessage({ message: m1 })
await agent.dataStoreSaveMessage({ message: m2 })
await agent.dataStoreSaveMessage({ message: m3 })
await agent.dataStoreSaveMessage({ message: m4 })
}
describe('@veramo/data-store queries', () => {
let dbConnection: Promise<DataSource>
const databaseFile = './tmp/test-db2.sqlite'
function makeAgent(context?: Record<string, any>): TAgent<IDataStore & IDataStoreORM> {
// @ts-ignore
return new Agent({
context,
plugins: [new DataStore(dbConnection), new DataStoreORM(dbConnection)],
})
}
beforeAll(async () => {
dbConnection = new DataSource({
type: 'sqlite',
database: databaseFile,
entities: Entities,
}).initialize()
})
beforeEach(async () => {
await (await dbConnection).dropDatabase()
await (await dbConnection).synchronize()
await populateDB(makeAgent())
})
afterAll(async () => {
;(await dbConnection).close()
fs.unlinkSync(databaseFile)
})
test('search presentations by verifier', async () => {
const agent = makeAgent()
const args: FindArgs<TPresentationColumns> = {
where: [
{
column: 'verifier',
value: [did4],
op: 'In',
},
],
}
let presentations = await makeAgent().dataStoreORMGetVerifiablePresentations(args)
expect(presentations.length).toBe(1)
let count = await makeAgent().dataStoreORMGetVerifiablePresentationsCount(args)
expect(count).toBe(1)
// search when authenticated as the issuer
let authorizedDID = did1
presentations = await makeAgent({ authorizedDID }).dataStoreORMGetVerifiablePresentations(args)
expect(presentations.length).toBe(1)
count = await makeAgent({ authorizedDID }).dataStoreORMGetVerifiablePresentationsCount(args)
expect(count).toBe(1)
// search when authenticated as another did
authorizedDID = did3
presentations = await makeAgent({ authorizedDID }).dataStoreORMGetVerifiablePresentations(args)
expect(presentations.length).toBe(0)
count = await makeAgent({ authorizedDID }).dataStoreORMGetVerifiablePresentationsCount(args)
expect(count).toBe(0)
})
test('without auth it fetches all messages that match the query', async () => {
const args: FindArgs<TMessageColumns> = {
where: [
{
column: 'type',
value: ['mock'],
op: 'In',
},
],
}
const messages = await makeAgent().dataStoreORMGetMessages(args)
expect(messages.length).toBe(4)
const count = await makeAgent().dataStoreORMGetMessagesCount(args)
expect(count).toBe(4)
})
test('with auth it only gets messages for the authorized identifier', async () => {
const args: FindArgs<TMessageColumns> = {
where: [
{
column: 'type',
value: ['mock'],
op: 'In',
},
],
}
const authorizedDID = did1
const messages = await makeAgent({ authorizedDID }).dataStoreORMGetMessages(args)
expect(messages.length).toBe(3)
const count = await makeAgent({ authorizedDID }).dataStoreORMGetMessagesCount(args)
expect(count).toBe(3)
})
test('supports ordering', async () => {
const agent = makeAgent()
const args: FindArgs<TMessageColumns> = {
where: [
{
column: 'type',
value: ['mock'],
op: 'In',
},
],
order: [
{
column: 'createdAt',
direction: 'DESC',
},
],
}
const messages = await agent.dataStoreORMGetMessages(args)
expect(new Date('' + messages[0].createdAt).getTime()).toBeGreaterThan(
new Date('' + messages[1].createdAt).getTime(),
)
const args2: FindArgs<TMessageColumns> = {
where: [
{
column: 'type',
value: ['mock'],
op: 'In',
},
],
order: [
{
column: 'createdAt',
direction: 'ASC',
},
],
}
const messages2 = await agent.dataStoreORMGetMessages(args2)
expect(new Date('' + messages2[0].createdAt).getTime()).toBeLessThan(
new Date('' + messages2[1].createdAt).getTime(),
)
})
test('works with relations', async () => {
const credentials = await makeAgent().dataStoreORMGetVerifiableCredentialsByClaims({})
expect(credentials.length).toBe(3)
expect(credentials[0].verifiableCredential.id).toBe('vc1')
const count = await makeAgent().dataStoreORMGetVerifiableCredentialsByClaimsCount({})
expect(count).toBe(3)
const credentials2 = await makeAgent({
authorizedDID: did3,
}).dataStoreORMGetVerifiableCredentialsByClaims({})
expect(credentials2.length).toBe(0)
const count2 = await makeAgent({
authorizedDID: did3,
}).dataStoreORMGetVerifiableCredentialsByClaimsCount({})
expect(count2).toBe(0)
})
test('multiple audience members can retrieve a credential', async () => {
const args: FindArgs<TPresentationColumns> = {
where: [
{
column: 'type',
value: ['VerifiablePresentation,PublicProfileMultiAudience'],
op: 'Equal',
},
],
}
let presentations = await makeAgent({ authorizedDID: did1 }).dataStoreORMGetVerifiablePresentations(args)
expect(presentations.length).toBe(1)
presentations = await makeAgent({ authorizedDID: did2 }).dataStoreORMGetVerifiablePresentations(args)
expect(presentations.length).toBe(1)
presentations = await makeAgent({ authorizedDID: did4 }).dataStoreORMGetVerifiablePresentations(args)
expect(presentations.length).toBe(1)
presentations = await makeAgent({ authorizedDID: did3 }).dataStoreORMGetVerifiablePresentations(args)
expect(presentations.length).toBe(0)
})
test('store credential and retrieve by id', async () => {
const vc5: VerifiableCredential = {
'@context': ['https://www.w3.org/2018/credentials/v1323', 'https://www.w3.org/2020/demo/4342323'],
type: ['VerifiableCredential', 'PublicProfile'],
issuer: { id: did1 },
issuanceDate: new Date().toISOString(),
id: 'vc5',
credentialSubject: {
id: did2,
name: 'Alice',
profilePicture: 'https://example.com/a.png',
address: {
street: 'Some str.',
house: 1,
},
},
proof: {
jwt: 'mockJWT',
},
}
const agent = makeAgent()
await agent.dataStoreSaveVerifiableCredential({ verifiableCredential: vc5 })
const args: FindArgs<TCredentialColumns> = {
where: [
{
column: 'id',
value: ['vc5'],
},
],
order: [{ column: 'issuanceDate', direction: 'DESC' }],
}
const credentials = await agent.dataStoreORMGetVerifiableCredentials(args)
expect(credentials[0].verifiableCredential.id).toEqual('vc5')
const count = await agent.dataStoreORMGetVerifiableCredentialsCount(args)
expect(count).toEqual(1)
})
test('store presentation and retrieve by context and type', async () => {
const vc6: VerifiableCredential = {
'@context': ['https://www.w3.org/2018/credentials/v1323', 'https://www.w3.org/2020/demo/666'],
type: ['VerifiableCredential', 'PublicProfile6'],
issuer: { id: did1 },
issuanceDate: new Date().toISOString(),
id: 'vc6',
credentialSubject: {
id: did2,
name: 'Alice',
profilePicture: 'https://example.com/a.png',
address: {
street: 'Some str.',
house: 1,
},
},
proof: {
jwt: 'mockJWT',
},
}
const vp6: VerifiablePresentation = {
'@context': ['https://www.w3.org/2018/credentials/v1323', 'https://www.w3.org/2020/demo/99999966666'],
type: ['VerifiablePresentation', 'PublicProfile6666'],
holder: did1,
verifier: [did2],
issuanceDate: new Date().toISOString(),
verifiableCredential: [vc6],
proof: {
jwt: 'mockJWT',
},
}
const agent = makeAgent()
await agent.dataStoreSaveVerifiablePresentation({ verifiablePresentation: vp6 })
const args: FindArgs<TPresentationColumns> = {
where: [
{
column: 'type',
value: ['VerifiablePresentation,PublicProfile6666'],
},
{
column: 'context',
value: ['https://www.w3.org/2018/credentials/v1323,https://www.w3.org/2020/demo/99999966666'],
},
],
}
const presentations = await agent.dataStoreORMGetVerifiablePresentations(args)
const cred0 = presentations[0].verifiablePresentation.verifiableCredential?.[0] as VerifiableCredential
expect(cred0.id).toEqual('vc6')
})
it('should query identifiers', async () => {
const agent = makeAgent()
const identifiers = await agent.dataStoreORMGetIdentifiers()
expect(identifiers.length).toEqual(4)
const count = await agent.dataStoreORMGetIdentifiersCount()
expect(count).toEqual(4)
})
})