@@ -4,24 +4,26 @@ import { GuardDutyScanResult } from '../helpers/eventbridge/eventbridge-helper';
44import { TemplateStorageHelper } from '../helpers/db/template-storage-helper' ;
55import { Template , TemplateFile } from '../helpers/types' ;
66
7- type AssertConfig = {
7+ type EventConfig = {
88 key : { id : string ; owner : string } ;
99 scanResult : GuardDutyScanResult ;
10- timeout ?: number ;
1110} ;
1211
1312type FileConfig = {
1413 pathPrefix : 'pdf-template' | 'test-data' | 'proofs' ;
15- extension : '.pdf' | '.csv' ;
1614 getFile : ( template : Template ) => TemplateFile | undefined ;
15+ getPath : ( template : Template , file ?: TemplateFile ) => string ;
1716} ;
1817
18+ const guardDutyScan = new SimulateGuardDutyScan ( ) ;
19+ const templateStorageHelper = new TemplateStorageHelper ( ) ;
20+
1921function assertGuardDutyEventForFile (
20- { key , scanResult , timeout = 60_000 } : AssertConfig ,
21- { pathPrefix , extension , getFile } : FileConfig
22+ event : EventConfig ,
23+ fileConfig : FileConfig
2224) {
23- const guardDutyScan = new SimulateGuardDutyScan ( ) ;
24- const templateStorageHelper = new TemplateStorageHelper ( ) ;
25+ const { key , scanResult } = event ;
26+ const { pathPrefix , getFile , getPath } = fileConfig ;
2527
2628 return test . step ( `when user uploads ${ pathPrefix } file, then guardduty triggers ${ scanResult } ` , async ( ) => {
2729 await expect ( async ( ) => {
@@ -33,54 +35,48 @@ function assertGuardDutyEventForFile(
3335 return true ;
3436 }
3537
38+ const path = getPath ( template , file ) ;
39+
3640 const published = await guardDutyScan . publish ( {
3741 type : scanResult ,
38- path : ` ${ pathPrefix } / ${ template . owner } / ${ template . id } / ${ file ?. currentVersion } ${ extension } ` ,
42+ path,
3943 } ) ;
4044
4145 expect ( published ) . toEqual ( true ) ;
42- } ) . toPass ( { timeout } ) ;
46+ } ) . toPass ( { timeout : 60_000 } ) ;
4347 } ) ;
4448}
4549
46- export function assertPdfTemplateGuardDutyEvent ( props : AssertConfig ) {
50+ export function assertPdfTemplateGuardDutyEvent ( props : EventConfig ) {
4751 return assertGuardDutyEventForFile ( props , {
4852 pathPrefix : 'pdf-template' ,
49- extension : '.pdf' ,
5053 getFile : ( template ) => template . files ?. pdfTemplate ,
54+ getPath : ( template , file ) =>
55+ `pdf-template/${ template . owner } /${ template . id } /${ file ?. currentVersion } .pdf` ,
5156 } ) ;
5257}
5358
54- export function assertTestDataGuardDutyEvent ( props : AssertConfig ) {
59+ export function assertTestDataGuardDutyEvent ( props : EventConfig ) {
5560 return assertGuardDutyEventForFile ( props , {
5661 pathPrefix : 'test-data' ,
57- extension : '.csv' ,
5862 getFile : ( template ) => template . files ?. testDataCsv ,
63+ getPath : ( template , file ) =>
64+ `test-data/${ template . owner } /${ template . id } /${ file ?. currentVersion } .csv` ,
5965 } ) ;
6066}
6167
6268export function assertProofGuardDutyEvent (
63- props : AssertConfig & { fileName : string }
69+ props : EventConfig & { fileName : string }
6470) {
65- const guardDutyScan = new SimulateGuardDutyScan ( ) ;
66- const templateStorageHelper = new TemplateStorageHelper ( ) ;
67-
68- return test . step ( `when user receives proof file, then guardduty triggers ${ props . scanResult } ` , async ( ) => {
69- await expect ( async ( ) => {
70- const template = await templateStorageHelper . getTemplate ( props . key ) ;
71-
72- const proof = template . files ?. proofs ?. [ props . fileName ] ;
71+ return assertGuardDutyEventForFile ( props , {
72+ pathPrefix : 'proofs' ,
73+ getFile : ( template ) => {
74+ const file = template . files ?. proofs ?. [ props . fileName ] ;
7375
74- if ( proof && proof . virusScanStatus !== 'PENDING' ) {
75- return true ;
76+ if ( file ) {
77+ return { ... file , currentVersion : 'unknown' } ;
7678 }
77-
78- const published = await guardDutyScan . publish ( {
79- type : props . scanResult ,
80- path : `proofs/${ props . key . id } /${ props . fileName } ` ,
81- } ) ;
82-
83- expect ( published ) . toEqual ( true ) ;
84- } ) . toPass ( { timeout : 60_000 } ) ;
79+ } ,
80+ getPath : ( ) => `proofs/${ props . key . id } /${ props . fileName } ` ,
8581 } ) ;
8682}
0 commit comments