-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server E2E test 기반 테스트 작성 및 동작 확인 성공 (#279)
* allow synthetic default import * install dotenv * move all global settings in main.ts to a app.module.ts for better e2e integration test * move passport to auth guard for better modularity * provide global exception filter * provide global validation pipe through injection * refactor: organize redis & session middleware creation * rename custom logger and modules to make more senses * minor: socket import esmoduleinterop * test(!): write a working e2e test for the project for automation!
- Loading branch information
Showing
14 changed files
with
163 additions
and
89 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GithubStrategy, MockStrategy } from './auth.strategy'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { UserModule } from '../user/user.module'; | ||
import { AuthController } from './auth.controller'; | ||
import { GithubAuthGuard, MockAuthGuard } from './auth.guard'; | ||
import { LocalSerializer } from './auth.serializer'; | ||
import { UserModule } from '../user/user.module'; | ||
import { AuthService } from './auth.service'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { GithubAuthGuard, MockAuthGuard } from './auth.guard'; | ||
import { GithubStrategy, MockStrategy } from './auth.strategy'; | ||
|
||
@Module({ | ||
providers: [ | ||
GithubStrategy, | ||
GithubAuthGuard, | ||
MockAuthGuard, | ||
// MockAuthGuard, | ||
|
||
// SessionAuthGuard, | ||
MockStrategy, | ||
|
||
AuthService, | ||
LocalSerializer, | ||
], | ||
controllers: [AuthController], | ||
imports: [UserModule, PassportModule], | ||
imports: [UserModule, PassportModule.register({ session: true })], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { APP_FILTER } from '@nestjs/core'; | ||
import { GlobalExceptionFilter } from 'src/common/exception.filter'; | ||
|
||
export const provideGlobalExceptionFilter = () => { | ||
return { | ||
provide: APP_FILTER, | ||
useClass: GlobalExceptionFilter, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import RedisStore from 'connect-redis'; | ||
import session from 'express-session'; | ||
import Redis from 'ioredis'; | ||
|
||
export const makeRedisStore = () => { | ||
const REDIS_HOST = process.env.REDIS_HOSTNAME; | ||
if (REDIS_HOST == null) throw new Error('REDIS_HOST is not defined'); | ||
|
||
const redis = new Redis({ | ||
host: REDIS_HOST, | ||
port: parseInt(process.env.REDIS_PORT ?? '6379'), | ||
}); | ||
|
||
const redisStore = new RedisStore({ | ||
client: redis, | ||
prefix: 'baekjoonrooms:', | ||
}); | ||
return { redis, redisStore }; | ||
}; | ||
|
||
export const makeSessionMiddleware = (redisStore: RedisStore) => { | ||
const sessionMiddleware = session({ | ||
secret: 'example-session-secret', | ||
resave: false, | ||
saveUninitialized: false, | ||
cookie: { | ||
httpOnly: true, | ||
}, | ||
store: redisStore, | ||
}); | ||
return sessionMiddleware; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ValidationPipe } from '@nestjs/common'; | ||
|
||
export const globalValidationPipe = () => { | ||
const pipe = new ValidationPipe({ | ||
transform: true, | ||
whitelist: true, | ||
forbidNonWhitelisted: true, | ||
disableErrorMessages: false, // production 환경에서는 보통 true로 설정 | ||
}); | ||
|
||
return { | ||
provide: 'APP_PIPE', | ||
useValue: pipe, | ||
}; | ||
}; |
6 changes: 3 additions & 3 deletions
6
.../src/short-logger/short-logger.service.ts → server/src/logger/custom.logger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CustomLogger } from './custom.logger'; | ||
|
||
@Module({ | ||
providers: [CustomLogger], | ||
exports: [CustomLogger], | ||
}) | ||
export class LoggerModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,45 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import Redis from 'ioredis'; | ||
import { | ||
makeRedisStore, | ||
makeSessionMiddleware, | ||
} from 'src/common/middleware/session'; | ||
import { CustomLogger } from 'src/logger/custom.logger'; | ||
import request from 'supertest'; | ||
import { AppModule } from './../src/app.module'; | ||
|
||
Error.stackTraceLimit = Infinity; | ||
|
||
describe('AppController (e2e)', () => { | ||
let app: INestApplication; | ||
let redisClient: Redis; | ||
const logger = new CustomLogger(); | ||
|
||
beforeEach(async () => { | ||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
|
||
const { redis, redisStore } = makeRedisStore(); | ||
const sessionMiddleware = makeSessionMiddleware(redisStore); | ||
redisClient = redis; | ||
app.use(sessionMiddleware); | ||
|
||
app.useLogger(logger); | ||
|
||
await app.init(); | ||
}); | ||
|
||
it('/ (GET)', () => { | ||
return request(app.getHttpServer()) | ||
.get('/') | ||
.expect(200) | ||
.expect('Hello World!'); | ||
afterAll(async () => { | ||
await app.close(); | ||
await redisClient.quit(); | ||
}); | ||
|
||
it('/ (GET)', async () => { | ||
logger.log('test'); | ||
return request(app.getHttpServer()).get('/').expect(403); | ||
}); | ||
}); |
Oops, something went wrong.