-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from Quickchive/test-#226/post-login
[Test] e2e test 용 유틸 객체/의존성 추가 및 login e2e test
- Loading branch information
Showing
10 changed files
with
1,570 additions
and
27 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
|
||
@Controller() | ||
export class AppController { | ||
@Get() | ||
healthCheck() { | ||
return { data: true }; | ||
} | ||
} |
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,135 @@ | ||
import { getBuilder } from '../common/application-builder'; | ||
import { CACHE_MANAGER, HttpStatus, INestApplication } from '@nestjs/common'; | ||
import { cacheManagerMock } from '../mock/cache-manager.mock'; | ||
import { User } from '../../src/users/entities/user.entity'; | ||
import { DataSource, Repository } from 'typeorm'; | ||
import { StartedPostgreSqlContainer } from '@testcontainers/postgresql'; | ||
import * as request from 'supertest'; | ||
import { Seeder } from '../seeder/seeder.interface'; | ||
import { LoginBodyDto } from '../../src/auth/dtos/login.dto'; | ||
import { UserSeeder } from '../seeder/user.seeder'; | ||
import * as bcrypt from 'bcrypt'; | ||
|
||
jest.setTimeout(30_000); | ||
describe('[POST] /api/auth/login', () => { | ||
// Application | ||
let app: INestApplication; | ||
let container: StartedPostgreSqlContainer; // TODO 결합도 낮추기 | ||
let dataSource: DataSource; | ||
|
||
// Repository | ||
let userRepository: Repository<User>; | ||
|
||
// Seeder | ||
const userSeeder: Seeder<User> = new UserSeeder(); | ||
|
||
// Stub | ||
let userStub: User; | ||
|
||
beforeAll(async () => { | ||
const { | ||
builder, | ||
container: _container, | ||
dataSource: _dataSource, | ||
} = await getBuilder(); | ||
container = _container; | ||
dataSource = _dataSource; | ||
|
||
const module = await builder | ||
.overrideProvider(DataSource) | ||
.useValue(dataSource) | ||
.overrideProvider(CACHE_MANAGER) | ||
.useValue(cacheManagerMock) | ||
.compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
|
||
userRepository = dataSource.getRepository(User); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await dataSource.synchronize(true); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
await container.stop(); | ||
}); | ||
|
||
it('testHealthCheck', async () => { | ||
const { status, body } = await request(app.getHttpServer()).get(''); | ||
|
||
expect(status).toBe(HttpStatus.OK); | ||
expect(body.data).toBe(true); | ||
}); | ||
|
||
/** | ||
* 1. 로그인에 성공한다. | ||
* 2. 유저가 존재하지 않아 실패한다. | ||
* 3. 비밀번호가 일치하지 않아 실패한다. | ||
*/ | ||
|
||
describe('로그인에 성공한다.', () => { | ||
beforeEach(async () => { | ||
userStub = userSeeder.generateOne(); | ||
const hashedPassword = await bcrypt.hash(userStub.password, 10); | ||
await userRepository.save({ ...userStub, password: hashedPassword }); | ||
}); | ||
|
||
it('access token 및 refresh token과 함께 201을 반환한다.', async () => { | ||
const loginBodyDto: LoginBodyDto = { | ||
email: userStub.email, | ||
password: userStub.password, | ||
auto_login: true, | ||
}; | ||
|
||
const { status, body } = await request(app.getHttpServer()) | ||
.post('/auth/login') | ||
.send(loginBodyDto); | ||
|
||
expect(status).toBe(HttpStatus.CREATED); | ||
expect(typeof body.access_token).toBe('string'); | ||
expect(typeof body.refresh_token).toBe('string'); | ||
}); | ||
}); | ||
|
||
describe('유저가 존재하지 않아 실패한다.', () => { | ||
it('404 예외를 던진다.', async () => { | ||
const loginBodyDto: LoginBodyDto = { | ||
email: userStub.email, | ||
password: userStub.password, | ||
auto_login: true, | ||
}; | ||
|
||
const { status, body } = await request(app.getHttpServer()) | ||
.post('/auth/login') | ||
.send(loginBodyDto); | ||
|
||
expect(status).toBe(HttpStatus.NOT_FOUND); | ||
expect(body.message).toBe('User Not Found'); | ||
}); | ||
}); | ||
|
||
describe('비밀번호가 일치하지 않아 실패한다.', () => { | ||
beforeEach(async () => { | ||
userStub = userSeeder.generateOne(); | ||
await userRepository.save(userStub); | ||
}); | ||
|
||
it('400 예외를 던진다.', async () => { | ||
const loginBodyDto: LoginBodyDto = { | ||
email: userStub.email, | ||
password: userStub.password + 'fail', | ||
auto_login: true, | ||
}; | ||
|
||
const { status, body } = await request(app.getHttpServer()) | ||
.post('/auth/login') | ||
.send(loginBodyDto); | ||
|
||
expect(status).toBe(HttpStatus.BAD_REQUEST); | ||
expect(body.message).toBe('Wrong Password'); | ||
}); | ||
}); | ||
}); |
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 { Test } from '@nestjs/testing'; | ||
import { AppModule } from '../../src/app.module'; | ||
import { PostgreSqlContainer } from '@testcontainers/postgresql'; | ||
import { DataSource, DataSourceOptions } from 'typeorm'; | ||
|
||
export const getBuilder = async () => { | ||
const container = await new PostgreSqlContainer() | ||
.withDatabase('quickchive_test') | ||
.withReuse() | ||
.start(); | ||
|
||
const dbConfigOption: DataSourceOptions = { | ||
type: 'postgres', | ||
host: container.getHost(), | ||
port: container.getMappedPort(5432), | ||
username: container.getUsername(), | ||
password: container.getPassword(), | ||
database: container.getDatabase(), | ||
entities: [__dirname + '/../../src/**/*.entity{.ts,.js}'], | ||
synchronize: true, | ||
}; | ||
|
||
const dataSource = await new DataSource(dbConfigOption).initialize(); | ||
|
||
return { | ||
builder: Test.createTestingModule({ | ||
imports: [AppModule], | ||
}), | ||
container, | ||
dataSource, | ||
}; | ||
}; |
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,3 @@ | ||
export const cacheManagerMock = { | ||
set: jest.fn(), | ||
}; |
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,3 @@ | ||
export interface Seeder<T> { | ||
generateOne(options?: { [K in keyof T]: any }): T; | ||
} |
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,23 @@ | ||
import { Seeder } from './seeder.interface'; | ||
import { User, UserRole } from '../../src/users/entities/user.entity'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
export class UserSeeder implements Seeder<User> { | ||
private readonly userStub = { | ||
name: faker.person.lastName(), | ||
email: faker.internet.email(), | ||
profileImage: faker.internet.url(), | ||
password: | ||
'1' + | ||
faker.internet.password({ | ||
length: 10, | ||
// pattern: /^(?=.*\d)[A-Za-z\d@$!%*?&]{8,}$/, | ||
}), | ||
role: UserRole.Client, | ||
verified: true, | ||
}; | ||
|
||
generateOne(options?: { [K in keyof User]: any }): User { | ||
return { ...this.userStub, ...options } as User; | ||
} | ||
} |