@@ -9,7 +9,6 @@ import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
9
9
import { QUICClient , QUICServer , events as quicEvents } from '@matrixai/quic' ;
10
10
import { DB } from '@matrixai/db' ;
11
11
import { RPCClient , RPCServer } from '@matrixai/rpc' ;
12
- import { IdInternal } from '@matrixai/id' ;
13
12
import NodesAuditEventsGet from '@/nodes/agent/handlers/NodesAuditEventsGet' ;
14
13
import { nodesAuditEventsGet } from '@/nodes/agent/callers' ;
15
14
import * as nodesUtils from '@/nodes/utils' ;
@@ -18,7 +17,6 @@ import Audit from '@/audit/Audit';
18
17
import * as keysUtils from '@/keys/utils' ;
19
18
import * as networkUtils from '@/network/utils' ;
20
19
import * as auditUtils from '@/audit/utils' ;
21
- import { TopicPath } from '@/audit/types' ;
22
20
import * as tlsTestsUtils from '../../../utils/tls' ;
23
21
import * as testNodesUtils from '../../../nodes/utils' ;
24
22
@@ -209,7 +207,7 @@ function generateMockAuditEvents(
209
207
210
208
test ( 'should get audit events' , async ( ) => {
211
209
// Generate valid AuditEventIds
212
- const mockAuditEvents = generateMockAuditEvents ( 200 , callProtectedGenerateAuditEventId ) ;
210
+ const mockAuditEvents = generateMockAuditEvents ( 100 , callProtectedGenerateAuditEventId ) ;
213
211
214
212
// Add events with correct topicPath and full path in event data
215
213
for ( const event of mockAuditEvents ) {
@@ -230,14 +228,11 @@ function generateMockAuditEvents(
230
228
//Parameters
231
229
let seekValue = 0 ;
232
230
let seekEndVal = Date . now ( ) ;
233
- let limitVal = 50 ;
234
231
235
232
try {
236
- console . log ( 'attempt rpc call' ) ;
237
233
const response = await rpcClient . methods . nodesAuditEventsGet ( {
238
234
seek : seekValue ,
239
235
seekEnd : seekEndVal ,
240
- //limit: limitVal,
241
236
} ) ;
242
237
243
238
// Collect results
@@ -246,23 +241,217 @@ function generateMockAuditEvents(
246
241
auditIds . push ( result . id ) ;
247
242
}
248
243
249
- console . log ( auditIds ) ;
250
-
251
244
const mappedMockedAuditEvents = mockAuditEvents . map ( ( event ) => auditUtils . encodeAuditEventId ( event . id ) )
252
- console . log ( mappedMockedAuditEvents ) ;
253
-
254
- // Verify all events were returned with correct encoded IDs
255
- //expect(auditIds).toHaveLength(limitVal);
256
245
257
246
//Check if the audits grabbed from the rpc handler is the same as the generated audits from mockAuditEvents
258
247
expect ( auditIds ) . toEqual (
259
248
mappedMockedAuditEvents
260
249
) ;
261
250
} catch ( error ) {
262
- // console.log('error');
263
- // console.error(error);
264
251
265
252
throw error ;
266
253
}
267
254
} ) ;
255
+
256
+ test ( 'should get audit events with limit' , async ( ) => {
257
+ // Generate valid AuditEventIds
258
+ const mockAuditEvents = generateMockAuditEvents ( 100 , callProtectedGenerateAuditEventId ) ;
259
+
260
+ // Add events with correct topicPath and full path in event data
261
+ for ( const event of mockAuditEvents ) {
262
+ // @ts -ignore: accessing a protected method
263
+ await audit . setAuditEvent ( [ 'node' , 'connection' , 'forward' ] , {
264
+ // @ts -ignore: protected
265
+ id : event . id ,
266
+ data : {
267
+ remoteNodeId : 'asdasd' ,
268
+ remoteHost : '127.0.0.1' ,
269
+ remotePort : 54321 ,
270
+ type : 'forward' ,
271
+ } ,
272
+ path : [ 'node' , 'connection' , 'forward' ] ,
273
+ } ) ;
274
+ }
275
+ //Parameters
276
+ let seekValue = 0 ;
277
+ let seekEndVal = Date . now ( ) ;
278
+ let limitVal = 50 ;
279
+
280
+ try {
281
+ const response = await rpcClient . methods . nodesAuditEventsGet ( {
282
+ seek : seekValue ,
283
+ seekEnd : seekEndVal ,
284
+ limit : limitVal ,
285
+ } ) ;
286
+
287
+ // Collect results
288
+ const auditIds : Array < string > = [ ] ;
289
+ for await ( const result of response ) {
290
+ auditIds . push ( result . id ) ;
291
+ }
292
+
293
+ // Verify that the number of events returned is equal to the limit
294
+ expect ( auditIds ) . toHaveLength ( limitVal ) ;
295
+
296
+ } catch ( error ) {
297
+
298
+ throw error ;
299
+ }
300
+ } ) ;
301
+
302
+
303
+ test ( 'should get audit events with specific seek = 50' , async ( ) => {
304
+ // Generate valid AuditEventIds
305
+ const mockAuditEvents = generateMockAuditEvents ( 100 , callProtectedGenerateAuditEventId ) ;
306
+
307
+ // Add events with correct topicPath and full path in event data
308
+ for ( const event of mockAuditEvents ) {
309
+ // @ts -ignore: accessing a protected method
310
+ await audit . setAuditEvent ( [ 'node' , 'connection' , 'forward' ] , {
311
+ // @ts -ignore: protected
312
+ id : event . id ,
313
+ data : {
314
+ remoteNodeId : 'asdasd' ,
315
+ remoteHost : '127.0.0.1' ,
316
+ remotePort : 54321 ,
317
+ type : 'forward' ,
318
+ } ,
319
+ path : [ 'node' , 'connection' , 'forward' ] ,
320
+ } ) ;
321
+ }
322
+
323
+ //Pick some value to seek from the mockAuditEvents selected from the mockAuditEvents
324
+ let seekIndex = 50 ;
325
+ let seekValueEncoded = auditUtils . encodeAuditEventId ( mockAuditEvents [ seekIndex ] . id ) ;
326
+
327
+
328
+ try {
329
+ const response = await rpcClient . methods . nodesAuditEventsGet ( {
330
+ seek : seekValueEncoded ,
331
+ } ) ;
332
+
333
+ // Collect results
334
+ const auditIds : Array < string > = [ ] ;
335
+ for await ( const result of response ) {
336
+ auditIds . push ( result . id ) ;
337
+ }
338
+
339
+ //Verify that the results are the same as the mockAuditEvents from the seek value onwards
340
+ expect ( auditIds ) . toEqual (
341
+ mockAuditEvents . slice ( seekIndex + 1 ) . map ( ( event ) => auditUtils . encodeAuditEventId ( event . id ) )
342
+ ) ;
343
+
344
+ //Additionally, verify the seek value is exclusive and should be excluded from the results.
345
+ expect ( auditIds ) . not . toContain ( seekValueEncoded ) ;
346
+
347
+
348
+ } catch ( error ) {
349
+ console . error ( error ) ;
350
+ throw error ;
351
+ }
352
+
353
+ } ) ;
354
+
355
+ test ( 'should get audit events with specific seek at index 0 (exclude the first event)' , async ( ) => {
356
+ // Generate valid AuditEventIds
357
+ const mockAuditEvents = generateMockAuditEvents ( 100 , callProtectedGenerateAuditEventId ) ;
358
+
359
+ // Insert them all
360
+ for ( const event of mockAuditEvents ) {
361
+ // @ts -ignore: protected
362
+ await audit . setAuditEvent ( [ 'node' , 'connection' , 'forward' ] , {
363
+ // @ts -ignore: protected
364
+ id : event . id ,
365
+ data : {
366
+ remoteNodeId : 'asdasd' ,
367
+ remoteHost : '127.0.0.1' ,
368
+ remotePort : 54321 ,
369
+ type : 'forward' ,
370
+ } ,
371
+ path : [ 'node' , 'connection' , 'forward' ] ,
372
+ } ) ;
373
+ }
374
+
375
+ // Seek the event at index 0
376
+ const seekIndex = 0 ;
377
+ const seekId = mockAuditEvents [ seekIndex ] . id ;
378
+ const seekIdEncoded = auditUtils . encodeAuditEventId ( seekId ) ;
379
+
380
+ try {
381
+ // Make the RPC call
382
+ const response = await rpcClient . methods . nodesAuditEventsGet ( {
383
+ seek : seekIdEncoded ,
384
+ } ) ;
385
+
386
+ // Collect results
387
+ const auditIds : Array < string > = [ ] ;
388
+ for await ( const result of response ) {
389
+ auditIds . push ( result . id ) ;
390
+ }
391
+
392
+ // Expect everything from index 1 onward
393
+ // (index 0 is excluded because we said exclusive).
394
+ const expectedIds = mockAuditEvents
395
+ . slice ( seekIndex + 1 )
396
+ . map ( ( event ) => auditUtils . encodeAuditEventId ( event . id ) ) ;
397
+
398
+ expect ( auditIds ) . toEqual ( expectedIds ) ;
399
+ // And confirm index 0 is NOT in the list
400
+ expect ( auditIds ) . not . toContain ( seekIdEncoded ) ;
401
+ } catch ( error ) {
402
+ console . error ( error ) ;
403
+ throw error ;
404
+ }
405
+ } ) ;
406
+
407
+ test ( 'should get audit events with specific seek at index 99 (exclude the last event)' , async ( ) => {
408
+ // Generate valid AuditEventIds
409
+ const mockAuditEvents = generateMockAuditEvents ( 100 , callProtectedGenerateAuditEventId ) ;
410
+
411
+ // Insert them all
412
+ for ( const event of mockAuditEvents ) {
413
+ // @ts -ignore: protected
414
+ await audit . setAuditEvent ( [ 'node' , 'connection' , 'forward' ] , {
415
+ // @ts -ignore: protected
416
+ id : event . id ,
417
+ data : {
418
+ remoteNodeId : 'asdasd' ,
419
+ remoteHost : '127.0.0.1' ,
420
+ remotePort : 54321 ,
421
+ type : 'forward' ,
422
+ } ,
423
+ path : [ 'node' , 'connection' , 'forward' ] ,
424
+ } ) ;
425
+ }
426
+
427
+ // Seek the event at index 99
428
+ const seekIndex = 99 ;
429
+ const seekId = mockAuditEvents [ seekIndex ] . id ;
430
+ const seekIdEncoded = auditUtils . encodeAuditEventId ( seekId ) ;
431
+
432
+ try {
433
+ // Make the RPC call
434
+ const response = await rpcClient . methods . nodesAuditEventsGet ( {
435
+ seek : seekIdEncoded ,
436
+ } ) ;
437
+
438
+ // Collect results
439
+ const auditIds : Array < string > = [ ] ;
440
+ for await ( const result of response ) {
441
+ auditIds . push ( result . id ) ;
442
+ }
443
+
444
+ // We expect an EMPTY result, because there's nothing after index 99
445
+ // (the last event is excluded).
446
+ expect ( auditIds ) . toHaveLength ( 0 ) ;
447
+ // Confirm that the last event’s ID is not present
448
+ expect ( auditIds ) . not . toContain ( seekIdEncoded ) ;
449
+ } catch ( error ) {
450
+ console . error ( error ) ;
451
+ throw error ;
452
+ }
453
+ } ) ;
454
+
455
+
456
+
268
457
} ) ;
0 commit comments