11import { Test , TestingModule } from '@nestjs/testing' ;
2- import { getName } from '../../../../test/test.utils' ;
32import { AppModule } from '../../../app.module' ;
43import { TagsDocument } from '../../tags/tag.model' ;
54import { TagsService } from '../../tags/tag.service' ;
@@ -44,7 +43,7 @@ describe('Port Service', () => {
4443 await tagsService . delete ( t . _id ) ;
4544 }
4645
47- project1 = await project ( 'project 1 ' ) ;
46+ project1 = await project ( 'my first project ' ) ;
4847 } ) ;
4948
5049 describe ( 'Add ports' , ( ) => {
@@ -313,58 +312,113 @@ describe('Port Service', () => {
313312 } ) ;
314313
315314 describe ( 'Get all' , ( ) => {
315+ let project1 : ProjectDocument ;
316+ let project2 : ProjectDocument ;
317+
318+ let foo : TagsDocument ;
319+ let bar : TagsDocument ;
320+ let baz : TagsDocument ;
321+ let qux : TagsDocument ;
322+
323+ let h1 : HostDocument ;
324+ let h2 : HostDocument ;
325+ let h3 : HostDocument ;
326+
327+ let p1 : PortDocument ;
328+ let p2 : PortDocument ;
329+ let p3 : PortDocument ;
330+ let p4 : PortDocument ;
331+ let p5 : PortDocument ;
332+ let p6 : PortDocument ;
333+
334+ beforeEach ( async ( ) => {
335+ // Arrange
336+ project1 = await project ( 'project 1' ) ;
337+ project2 = await project ( 'project 2' ) ;
338+ [ foo , bar , baz , qux ] = await tags ( 'foo' , 'bar' , 'baz' , 'qux' ) ;
339+
340+ h1 = await host ( '1.1.1.1' , project1 ) ;
341+ [ p1 , p2 ] = await port ( [ 1 , 2 ] , h1 , project1 , [ foo , qux ] ) ;
342+
343+ h2 = await host ( '1.2.2.2' , project1 ) ;
344+ [ p3 , p4 ] = await port ( [ 3 , 4 ] , h2 , project1 , [ bar , qux ] ) ;
345+
346+ h3 = await host ( '1.2.2.3' , project2 ) ;
347+ [ p5 ] = await port ( [ 5 ] , h3 , project2 , [ baz , qux ] ) ;
348+ [ p6 ] = await port ( [ 6 ] , h3 , project2 , [ baz , qux ] , 'udp' ) ;
349+ await block ( p6 ) ;
350+ } ) ;
351+
316352 it . each ( [
317353 [ '' , [ 1 , 2 , 3 , 4 , 5 , 6 ] ] ,
354+
355+ // Projects
356+ [ 'project: "project*"' , [ 1 , 2 , 3 , 4 , 5 , 6 ] ] ,
318357 [ 'project: "project 1"' , [ 1 , 2 , 3 , 4 ] ] ,
319358 [ 'project: "project 2"' , [ 5 , 6 ] ] ,
320359 [ '-project: "project 2"' , [ 1 , 2 , 3 , 4 ] ] ,
321- [ 'project: "project*"' , [ 1 , 2 , 3 , 4 , 5 , 6 ] ] ,
360+ [ 'project.name: "project*"' , [ 1 , 2 , 3 , 4 , 5 , 6 ] ] ,
361+ [ 'project.name: "project 1"' , [ 1 , 2 , 3 , 4 ] ] ,
362+ [ 'project.name: "project 2"' , [ 5 , 6 ] ] ,
363+ [ '-project.name: "project 2"' , [ 1 , 2 , 3 , 4 ] ] ,
364+ [ ( ) => `project.id: ${ project1 . id } ` , [ 1 , 2 , 3 , 4 ] ] ,
365+ [ ( ) => `project.id: ${ project2 . id } ` , [ 5 , 6 ] ] ,
366+ [ ( ) => `-project.id: ${ project2 . id } ` , [ 1 , 2 , 3 , 4 ] ] ,
367+
368+ // Host
322369 [ 'host: 1.1.1.1' , [ 1 , 2 ] ] ,
323370 [ 'host.ip: 1.1.1.1' , [ 1 , 2 ] ] ,
371+ [ ( ) => `host.id: ${ h1 . _id } ` , [ 1 , 2 ] ] ,
324372 [ 'host: 1.*' , [ 1 , 2 , 3 , 4 , 5 , 6 ] ] ,
325373 [ 'host: 1.2.2*' , [ 3 , 4 , 5 , 6 ] ] ,
326374 [ '-host: 1.1.1.1' , [ 3 , 4 , 5 , 6 ] ] ,
375+ [ '-host.ip: 1.1.1.1' , [ 3 , 4 , 5 , 6 ] ] ,
376+ [ ( ) => `-host.id: ${ h1 . id } ` , [ 3 , 4 , 5 , 6 ] ] ,
327377 [ '-host: 1.2.2*' , [ 1 , 2 ] ] ,
378+
379+ // Port
328380 [ 'port: 1' , [ 1 ] ] ,
329381 [ 'port.number: 1' , [ 1 ] ] ,
382+ [ ( ) => `port.id: ${ p1 . id } ` , [ 1 ] ] ,
383+ [ ( ) => `-port.id: ${ p1 . id } -port.id: ${ p3 . id } ` , [ 2 , 4 , 5 , 6 ] ] ,
330384 [ '-port: 1' , [ 2 , 3 , 4 , 5 , 6 ] ] ,
331385 [ 'port.protocol: tcp' , [ 1 , 2 , 3 , 4 , 5 ] ] ,
332386 [ '-port.protocol: tcp' , [ 6 ] ] ,
333387 [ 'port.protocol: udp' , [ 6 ] ] ,
334388 [ '-port.protocol: udp' , [ 1 , 2 , 3 , 4 , 5 ] ] ,
389+
390+ // Tag
335391 [ 'tag: foo' , [ 1 , 2 ] ] ,
392+ [ ( ) => `tag.id: ${ foo . _id } ` , [ 1 , 2 ] ] ,
393+ [ ( ) => `-tag.id: ${ foo . _id } ` , [ 3 , 4 , 5 , 6 ] ] ,
336394 [ '-tag: ba*' , [ 1 , 2 ] ] ,
337395 [ '-tag: foo' , [ 3 , 4 , 5 , 6 ] ] ,
338396 [ 'tag: qux' , [ 1 , 2 , 3 , 4 , 5 , 6 ] ] ,
339397 [ 'tag: foo tag: qux' , [ 1 , 2 ] ] ,
340398 [ '-tag: foo tag: qux' , [ 3 , 4 , 5 , 6 ] ] ,
399+
400+ // Is
341401 [ 'is: blocked' , [ 6 ] ] ,
342402 [ '-is: blocked' , [ 1 , 2 , 3 , 4 , 5 ] ] ,
343- ] ) ( 'Filter by "%s"' , async ( query : string , expected : number [ ] ) => {
344- // Arrange
345- const project2 = await project ( 'project 2' ) ;
346- const [ foo , bar , baz , qux ] = await tags ( 'foo' , 'bar' , 'baz' , 'qux' ) ;
347-
348- const h1 = await host ( '1.1.1.1' , project1 ) ;
349- await port ( [ 1 , 2 ] , h1 , project1 , [ foo , qux ] ) ;
350-
351- const h2 = await host ( '1.2.2.2' , project1 ) ;
352- await port ( [ 3 , 4 ] , h2 , project1 , [ bar , qux ] ) ;
353-
354- const h3 = await host ( '1.2.2.3' , project2 ) ;
355- await port ( [ 5 ] , h3 , project2 , [ baz , qux ] ) ;
356- await block ( ...( await port ( [ 6 ] , h3 , project2 , [ baz , qux ] , 'udp' ) ) ) ;
357-
358- // Act
359- const allPorts = await portService . getAll ( 0 , 10 , { query } ) ;
360-
361- // Assert
362- expect ( allPorts . map ( ( x ) => x . port ) . sort ( ) ) . toStrictEqual ( expected . sort ( ) ) ;
363- } ) ;
403+ ] ) (
404+ 'Filter by "%s"' ,
405+ async ( query : string | ( ( ) => string ) , expected : number [ ] ) => {
406+ // Arrange
407+ if ( typeof query !== 'string' ) query = query ( ) ;
408+
409+ // Act
410+ const allPorts = await portService . getAll ( 0 , 10 , { query } ) ;
411+
412+ // Assert
413+ expect ( allPorts . map ( ( x ) => x . port ) . sort ( ) ) . toStrictEqual (
414+ expected . sort ( ) ,
415+ ) ;
416+ } ,
417+ ) ;
364418 } ) ;
365419
366420 async function project ( name : string = '' ) {
367- const ccDto : CreateProjectDto = { name : ` ${ getName ( testPrefix ) } ` } ;
421+ const ccDto : CreateProjectDto = { name } ;
368422 return await projectService . addProject ( ccDto ) ;
369423 }
370424
@@ -383,7 +437,7 @@ describe('Port Service', () => {
383437 }
384438
385439 async function tags ( ...tags : string [ ] ) {
386- const createdTags = [ ] ;
440+ const createdTags : TagsDocument [ ] = [ ] ;
387441 for ( const tag of tags ) {
388442 createdTags . push ( await tagsService . create ( tag , '#ffffff' ) ) ;
389443 }
0 commit comments