|
1 | | -import { Tree } from '@angular-devkit/schematics'; |
2 | 1 | import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; |
3 | 2 | import * as path from 'path'; |
4 | 3 |
|
5 | | -const collectionPath = path.join(__dirname, '../collection.json'); |
| 4 | +describe('Security Factory', () => { |
| 5 | + const runner: SchematicTestRunner = new SchematicTestRunner('.', path.join(process.cwd(), 'src/collection.json')); |
| 6 | + it('should work', () => { |
| 7 | + const app: object = { |
| 8 | + name: '', |
| 9 | + }; |
| 10 | + const options: object = { |
| 11 | + path: '', |
| 12 | + }; |
| 13 | + runner.runSchematicAsync('application', app).subscribe(tree => { |
| 14 | + runner.runSchematicAsync('security', options, tree).subscribe(tree => { |
| 15 | + const files: string[] = tree.files; |
| 16 | + expect(files.find(filename => filename === '/src/main.ts')).toBeDefined(); |
| 17 | + expect(files.find(filename => filename === '/package.json')).toBeDefined(); |
6 | 18 |
|
7 | | -describe('schematics', () => { |
8 | | - it('works', done => { |
9 | | - const runner = new SchematicTestRunner('schematics', collectionPath); |
10 | | - const obsTree = runner.runSchematicAsync('schematics', {}, Tree.empty()); |
11 | | - |
12 | | - obsTree.subscribe(tree => { |
13 | | - expect(tree.files).toEqual([]); |
14 | | - done(); |
| 19 | + expect(tree.readContent('/package.json')).toContain('@types/helmet'); |
| 20 | + expect(tree.readContent('/src/main.ts')).toEqual( |
| 21 | + "import { NestFactory } from '@nestjs/core';\n" + |
| 22 | + "import { AppModule } from './app/app.module';\n" + |
| 23 | + "import { WinstonLogger } from './app/shared/logger/winston.logger';\n" + |
| 24 | + "import { ValidationPipe } from '@nestjs/common';\n" + |
| 25 | + "import * as helmet from 'helmet';\n" + |
| 26 | + '\n' + |
| 27 | + 'async function bootstrap(): Promise<void> {\n' + |
| 28 | + ' const app = await NestFactory.create(AppModule, { logger: new WinstonLogger() });\n' + |
| 29 | + ' app.useGlobalPipes(\n' + |
| 30 | + ' new ValidationPipe({\n' + |
| 31 | + ' transform: true,\n' + |
| 32 | + ' }),\n' + |
| 33 | + ' );\n' + |
| 34 | + " app.setGlobalPrefix('v1');\n" + |
| 35 | + ' app.use(helmet());\n' + |
| 36 | + ' app.enableCors({\n' + |
| 37 | + " origin: '*',\n" + |
| 38 | + ' credentials: true,\n' + |
| 39 | + " exposedHeaders: 'Authorization',\n" + |
| 40 | + " allowedHeaders: 'authorization, content-type',\n" + |
| 41 | + ' });\n' + |
| 42 | + ' await app.listen(3000);\n' + |
| 43 | + '}\n' + |
| 44 | + 'bootstrap();\n', |
| 45 | + ); |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + it('should manage path', () => { |
| 50 | + const app: object = { |
| 51 | + name: 'foo', |
| 52 | + }; |
| 53 | + const options: object = { |
| 54 | + path: 'foo', |
| 55 | + }; |
| 56 | + runner.runSchematicAsync('application', app).subscribe(tree => { |
| 57 | + runner.runSchematicAsync('security', options, tree).subscribe(tree => { |
| 58 | + const files: string[] = tree.files; |
| 59 | + expect(files.find(filename => filename === '/foo/src/main.ts')).toBeDefined(); |
| 60 | + expect(files.find(filename => filename === '/foo/package.json')).toBeDefined(); |
| 61 | + expect(tree.readContent('/foo/package.json')).toContain('@types/helmet'); |
| 62 | + }); |
15 | 63 | }); |
16 | 64 | }); |
17 | 65 | }); |
0 commit comments