|
1 | 1 | import * as request from 'supertest';
|
2 |
| -import {Controller, Get, INestApplication, Injectable, UseGuards, ValidationPipe} from "@nestjs/common"; |
3 |
| -import {Test, TestingModule} from "@nestjs/testing"; |
4 |
| -import {AuthGuard} from '@nestjs/passport'; |
| 2 | +import {INestApplication, ValidationPipe} from "@nestjs/common"; |
5 | 3 | import {FixturesLoaderService} from "./fixtures-loader.service";
|
6 |
| -import {Oauth2Module} from "../../lib/app/oauth2.module"; |
7 |
| -import {TypeOrmModule} from "@nestjs/typeorm"; |
8 |
| -import {UserLoaderInterface, UserValidatorInterface} from "../../lib/domain/interface"; |
9 |
| -import {InvalidUserException, UserInterface} from "../../lib/domain"; |
| 4 | +import {Oauth2AsyncUseFactoryModule} from "./modules/oauth2-async-use-factory.module"; |
| 5 | +import {Test} from "@nestjs/testing"; |
10 | 6 |
|
11 |
| -@Controller('oauth2-secured') |
12 |
| -export class TestSecuredController { |
13 |
| - @Get('me') |
14 |
| - @UseGuards(AuthGuard('access-token')) |
15 |
| - async auth(): Promise<any> { |
16 |
| - return {message: 'hello'}; |
17 |
| - } |
18 |
| -} |
19 |
| - |
20 |
| -const users: {[s:string]: string} = { |
21 |
| - 'alice@change.me': 'alice', |
22 |
| - 'bob@change.me': 'bob', |
23 |
| - 'kyle@change.me': 'kyle', |
24 |
| -}; |
25 |
| - |
26 |
| -@Injectable() |
27 |
| -export class UserValidator implements UserValidatorInterface { |
28 |
| - async validate(username, password): Promise<UserInterface> { |
29 |
| - if (users[username] !== undefined && users[username] === password) { |
30 |
| - return { |
31 |
| - id: users[username], |
32 |
| - username: users[username], |
33 |
| - email: users[username], |
34 |
| - } |
35 |
| - } |
36 |
| - |
37 |
| - throw InvalidUserException.withUsernameAndPassword(username, password); |
38 |
| - } |
39 |
| -} |
40 |
| - |
41 |
| -@Injectable() |
42 |
| -export class UserLoader implements UserLoaderInterface { |
43 |
| - async load(userId: string): Promise<UserInterface> { |
44 |
| - if (users[userId] !== undefined) { |
45 |
| - return { |
46 |
| - id: users[userId], |
47 |
| - username: users[userId], |
48 |
| - email: users[userId], |
49 |
| - } |
50 |
| - } |
51 |
| - |
52 |
| - throw InvalidUserException.withId(userId); |
53 |
| - } |
54 |
| -} |
55 |
| - |
56 |
| -describe('OAuth2 Controller (e2e)', () => { |
| 7 | +describe('OAuth2 Async Module Use Factory Controller (e2e)', () => { |
57 | 8 | let app: INestApplication;
|
58 | 9 |
|
59 | 10 | beforeEach(async () => {
|
60 |
| - const moduleFixture: TestingModule = await Test.createTestingModule({ |
61 |
| - imports: [ |
62 |
| - TypeOrmModule.forRoot({ |
63 |
| - type: 'postgres', |
64 |
| - host: 'localhost', |
65 |
| - port: 5432, |
66 |
| - username: 'postgres', |
67 |
| - password: 'postgres', |
68 |
| - database: 'oauth2-server', |
69 |
| - entities: [process.cwd() + '/lib/**/*.entity{.ts,.js}'], |
70 |
| - dropSchema: true, |
71 |
| - synchronize: true |
72 |
| - }), |
73 |
| - Oauth2Module.forRoot({ |
74 |
| - userValidator: new UserValidator(), |
75 |
| - userLoader: new UserLoader(), |
76 |
| - }) |
77 |
| - ], |
78 |
| - providers: [ |
79 |
| - FixturesLoaderService, |
80 |
| - ], |
81 |
| - controllers: [ |
82 |
| - TestSecuredController, |
83 |
| - ], |
| 11 | + const module = await Test.createTestingModule({ |
| 12 | + imports: [Oauth2AsyncUseFactoryModule], |
84 | 13 | }).compile();
|
85 | 14 |
|
86 |
| - app = moduleFixture.createNestApplication(); |
| 15 | + app = module.createNestApplication(); |
87 | 16 | app.useGlobalPipes(new ValidationPipe({
|
88 | 17 | transform: true,
|
89 | 18 | }));
|
|
0 commit comments