Skip to content

Commit 8b6a312

Browse files
committed
wip deduplicate test logic
1 parent 3f14288 commit 8b6a312

7 files changed

+31
-72
lines changed

test/test/controllers-without-policy.e2e-spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { NestExpressApplication } from '@nestjs/platform-express';
2-
import { Test, TestingModule } from '@nestjs/testing';
32
import * as request from 'supertest';
43
import { needleStrings, NUMBER_OF_TEST_USER_SEEDS, seed, TestSeed } from '../prisma/seed';
5-
import { AppModule } from '../src/app.module';
64
import { PrismaService } from '../src/prisma.service';
75
import { UsersService } from '../src/users/users.service';
6+
import { createTestingApp } from './helpers';
87

98
// TODO: Rename file and split into outlined test groups
109
describe('CRUD controllers (without policy) e2e', () => {
@@ -16,12 +15,7 @@ describe('CRUD controllers (without policy) e2e', () => {
1615
let countries;
1716

1817
beforeAll(async () => {
19-
const moduleFixture: TestingModule = await Test.createTestingModule({
20-
imports: [AppModule],
21-
}).compile();
22-
23-
app = moduleFixture.createNestApplication();
24-
app.set('query parser', 'extended');
18+
app = await createTestingApp({ strict: false });
2519
await app.init();
2620
prismaService = app.get(PrismaService);
2721
});

test/test/helpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { NestExpressApplication } from '@nestjs/platform-express';
2+
import { Test, TestingModule } from '@nestjs/testing';
3+
import { AppModule, StrictModeAppModule } from '../src/app.module';
4+
15
/**
26
* Useful when same test must be run across different apps
37
*/
@@ -9,3 +13,14 @@ export async function multiAppTest(apps: any[], testFn: (app: any) => Promise<an
913
}
1014
return results;
1115
}
16+
17+
export async function createTestingApp(opts: { strict: boolean }) {
18+
const appModule = opts.strict ? StrictModeAppModule : AppModule;
19+
const moduleFixture: TestingModule = await Test.createTestingModule({
20+
imports: [appModule],
21+
}).compile();
22+
23+
const app = moduleFixture.createNestApplication<NestExpressApplication>();
24+
app.set('query parser', 'extended');
25+
return app;
26+
}

test/test/misuse-resistance.e2e-spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { NestExpressApplication } from '@nestjs/platform-express';
22
import { Test, TestingModule } from '@nestjs/testing';
33
import * as request from 'supertest';
44
import { needleStrings, seed, TestSeed } from '../prisma/seed';
5-
import { StrictModeAppModule } from '../src/app.module';
65
import { CommentsModule, InvalidCommentsModule } from '../src/comments/comments.module';
6+
import { createTestingApp } from './helpers';
77

88
/**
99
* This test outlines the added sane defaults to help developers with the more common careless mistakes
@@ -15,12 +15,7 @@ describe('Misuse resistance', () => {
1515
const [needleString0] = needleStrings;
1616

1717
beforeAll(async () => {
18-
const moduleFixture: TestingModule = await Test.createTestingModule({
19-
imports: [StrictModeAppModule],
20-
}).compile();
21-
22-
app = moduleFixture.createNestApplication();
23-
app.set('query parser', 'extended');
18+
app = await createTestingApp({ strict: true });
2419
await app.init();
2520
});
2621

test/test/multiple-policies.e2e-spec.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { NestExpressApplication } from '@nestjs/platform-express';
2-
import { Test, TestingModule } from '@nestjs/testing';
32
import * as request from 'supertest';
43
import { needleStrings, seed, TestSeed } from '../prisma/seed';
5-
import { AppModule, StrictModeAppModule } from '../src/app.module';
6-
import { multiAppTest } from './helpers';
4+
import { createTestingApp, multiAppTest } from './helpers';
75

86
describe('Multiple policies e2e', () => {
97
let nonStrictApp: NestExpressApplication;
@@ -12,17 +10,8 @@ describe('Multiple policies e2e', () => {
1210
const [needleString0] = needleStrings;
1311

1412
beforeAll(async () => {
15-
const moduleFixture: TestingModule = await Test.createTestingModule({
16-
imports: [AppModule],
17-
}).compile();
18-
const strictModuleFixture: TestingModule = await Test.createTestingModule({
19-
imports: [StrictModeAppModule],
20-
}).compile();
21-
22-
nonStrictApp = moduleFixture.createNestApplication<NestExpressApplication>();
23-
nonStrictApp.set('query parser', 'extended');
24-
strictApp = strictModuleFixture.createNestApplication<NestExpressApplication>();
25-
strictApp.set('query parser', 'extended');
13+
nonStrictApp = await createTestingApp({ strict: false });
14+
strictApp = await createTestingApp({ strict: true });
2615

2716
await nonStrictApp.init();
2817
await strictApp.init();

test/test/must-match-auth-attribute-policy.e2e-spec.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { NestExpressApplication } from '@nestjs/platform-express';
2-
import { Test, TestingModule } from '@nestjs/testing';
32
import * as request from 'supertest';
43
import { needleStrings, seed, TestSeed } from '../prisma/seed';
5-
import { AppModule, StrictModeAppModule } from '../src/app.module';
64
import { RoleID } from '../src/authentication.middleware';
7-
import { multiAppTest } from './helpers';
5+
import { createTestingApp, multiAppTest } from './helpers';
86

97
describe('MustMatchAuthAttributePolicy e2e', () => {
108
let nonStrictApp: NestExpressApplication;
@@ -13,17 +11,8 @@ describe('MustMatchAuthAttributePolicy e2e', () => {
1311
const [needleString0] = needleStrings;
1412

1513
beforeAll(async () => {
16-
const moduleFixture: TestingModule = await Test.createTestingModule({
17-
imports: [AppModule],
18-
}).compile();
19-
const strictModuleFixture: TestingModule = await Test.createTestingModule({
20-
imports: [StrictModeAppModule],
21-
}).compile();
22-
23-
nonStrictApp = moduleFixture.createNestApplication();
24-
nonStrictApp.set('query parser', 'extended');
25-
strictApp = strictModuleFixture.createNestApplication();
26-
strictApp.set('query parser', 'extended');
14+
nonStrictApp = await createTestingApp({ strict: false });
15+
strictApp = await createTestingApp({ strict: true });
2716
await nonStrictApp.init();
2817
await strictApp.init();
2918
});

test/test/must-match-value-policy.e2e-spec.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { NestExpressApplication } from '@nestjs/platform-express';
2-
import { Test, TestingModule } from '@nestjs/testing';
32
import * as request from 'supertest';
43
import { needleStrings, seed, TestSeed } from '../prisma/seed';
5-
import { AppModule, StrictModeAppModule } from '../src/app.module';
64
import { RoleID } from '../src/authentication.middleware';
7-
import { multiAppTest } from './helpers';
5+
import { createTestingApp, multiAppTest } from './helpers';
86

97
describe('MustMatchValue e2e', () => {
108
let nonStrictApp: NestExpressApplication;
@@ -15,17 +13,8 @@ describe('MustMatchValue e2e', () => {
1513
const [needleString0] = needleStrings;
1614

1715
beforeAll(async () => {
18-
const moduleFixture: TestingModule = await Test.createTestingModule({
19-
imports: [AppModule],
20-
}).compile();
21-
const strictModuleFixture: TestingModule = await Test.createTestingModule({
22-
imports: [StrictModeAppModule],
23-
}).compile();
24-
25-
nonStrictApp = moduleFixture.createNestApplication();
26-
nonStrictApp.set('query parser', 'extended');
27-
strictApp = strictModuleFixture.createNestApplication();
28-
strictApp.set('query parser', 'extended');
16+
nonStrictApp = await createTestingApp({ strict: false });
17+
strictApp = await createTestingApp({ strict: true });
2918
await nonStrictApp.init();
3019
await strictApp.init();
3120
});

test/test/rbac.e2e-spec.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import { NestExpressApplication } from '@nestjs/platform-express';
2-
import { Test, TestingModule } from '@nestjs/testing';
32
import * as request from 'supertest';
43
import { needleStrings, seed } from '../prisma/seed';
5-
import { AppModule, StrictModeAppModule } from '../src/app.module';
64
import { RoleID } from '../src/authentication.middleware';
7-
import { multiAppTest } from './helpers';
5+
import { createTestingApp, multiAppTest } from './helpers';
86

97
describe('AccessPolicy RBAC e2e', () => {
108
let nonStrictApp: NestExpressApplication;
119
let strictApp: NestExpressApplication;
1210
const [needleString0] = needleStrings;
1311

1412
beforeAll(async () => {
15-
const moduleFixture: TestingModule = await Test.createTestingModule({
16-
imports: [AppModule],
17-
}).compile();
18-
const strictModuleFixture: TestingModule = await Test.createTestingModule({
19-
imports: [StrictModeAppModule],
20-
}).compile();
21-
22-
// TODO: deduplicate this logic across all tests
23-
nonStrictApp = moduleFixture.createNestApplication();
24-
nonStrictApp.set('query parser', 'extended');
25-
strictApp = strictModuleFixture.createNestApplication();
26-
strictApp.set('query parser', 'extended');
13+
nonStrictApp = await createTestingApp({ strict: false });
14+
strictApp = await createTestingApp({ strict: true });
2715
await nonStrictApp.init();
2816
await strictApp.init();
2917
});

0 commit comments

Comments
 (0)