-
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.
* chore: add faker dependency and fix jest config * test: 유저 생성 관련 테스트 코드 추가 * test: implement test code about user * test: implement test code about exists and find function * test: 불필요한 테스트 코드 삭제 * test: 유저 수정, 삭제 테스트코드 작성 및 유틸 함수 구현 - Add test code about updating and deleting user. - Implement createUserMock() function to create user mockup. * fix: fix typo * HOTFIX: change column type in Room model from User to string * feat: create test.util.ts for test code * style: 함수 순서 정렬 * test: implement test code of RoomService * chore(jenkins): add test script into Jenkinsfile * test: create test code for AuthService
- Loading branch information
1 parent
7dd276c
commit e112d98
Showing
12 changed files
with
963 additions
and
65 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import faker from 'faker'; | ||
import { JwtModule, JwtService } from '@nestjs/jwt'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import bcrypt from 'bcrypt'; | ||
import { AuthService } from './auth.service'; | ||
import { UserService } from '../user/user.service'; | ||
import { User } from '../user/user.entity'; | ||
import { createUserMock } from '../utils/test.util'; | ||
|
||
const mockRepository = () => ({ | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
create: jest.fn(), | ||
softRemove: jest.fn() | ||
}); | ||
|
||
describe('AuthService', () => { | ||
let authService: AuthService; | ||
let jwtService: JwtService; | ||
let userService: UserService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
JwtModule.register({ | ||
secret: 'asdfghjkl' | ||
}) | ||
], | ||
providers: [ | ||
{ | ||
provide: ConfigService, | ||
useValue: { | ||
get: jest.fn((key: string) => { | ||
switch (key) { | ||
case 'JWT_SECRET_EXPIRE_TIME': | ||
return '5m'; | ||
case 'JWT_REFRESH_EXPIRE_TIME': | ||
return '10m'; | ||
default: | ||
return null; | ||
} | ||
}) | ||
} | ||
}, | ||
UserService, | ||
{ | ||
provide: getRepositoryToken(User), | ||
useValue: mockRepository() | ||
}, | ||
AuthService | ||
] | ||
}).compile(); | ||
|
||
authService = module.get<AuthService>(AuthService); | ||
jwtService = module.get<JwtService>(JwtService); | ||
userService = module.get<UserService>(UserService); | ||
}); | ||
|
||
describe('Generate Token', () => { | ||
it('새로운 AccessToken을 만든다.', () => { | ||
const uuidMock = faker.datatype.uuid(); | ||
|
||
const result = authService.generateToken(uuidMock); | ||
expect(result).toStrictEqual({ | ||
TOKEN: expect.any(String) | ||
}); | ||
}); | ||
|
||
it('새로운 RefreshToken을 만든다.', () => { | ||
const uuidMock = faker.datatype.uuid(); | ||
|
||
const result = authService.refreshToken(uuidMock); | ||
expect(result).toStrictEqual({ | ||
REFRESH_TOKEN: expect.any(String) | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Validate Token', () => { | ||
it('AccessToken을 검증한다.', () => { | ||
const uuidMock = faker.datatype.uuid(); | ||
|
||
const expectResult = { | ||
iat: 1, | ||
exp: 1, | ||
uuid: uuidMock | ||
}; | ||
|
||
const verifySpy = jest.spyOn(jwtService, 'verify').mockReturnValue(expectResult); | ||
|
||
const jwt = authService.generateToken(uuidMock); | ||
const result = authService.validateToken(jwt.TOKEN); | ||
|
||
expect(verifySpy).toBeCalled(); | ||
expect(result).toBe(expectResult); | ||
}); | ||
}); | ||
|
||
it('AccessToken이 잘못 되었다.', () => { | ||
const jwt = 'invalid.token'; | ||
const result = authService.validateToken(jwt); | ||
|
||
expect(result).toBe(undefined); | ||
}); | ||
|
||
it('이메일과 비밀번호를 이용하여 계정을 검증한다.', async () => { | ||
const email = faker.internet.email(); | ||
const password = faker.datatype.string(); | ||
|
||
const existsSpy = jest.spyOn(userService, 'existsUserByEmail').mockResolvedValue(true); | ||
const findOneSpy = jest.spyOn(userService, 'findOneByEmail').mockResolvedValue( | ||
createUserMock( | ||
{ email }, | ||
{ | ||
password | ||
} | ||
) | ||
); | ||
const compareSpy = jest.spyOn(bcrypt, 'compare').mockResolvedValue(true); | ||
|
||
const result = await authService.validateLocalLogin(email, password); | ||
|
||
expect(existsSpy).toHaveBeenCalledWith(email); | ||
expect(findOneSpy).toHaveBeenCalledWith(email); | ||
expect(compareSpy).toBeCalled(); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
it('존재하지 않는 유저가 계정 검증을 시도한다.', async () => { | ||
const email = faker.internet.email(); | ||
const password = faker.datatype.string(); | ||
|
||
const existsSpy = jest.spyOn(userService, 'existsUserByEmail').mockResolvedValue(false); | ||
|
||
const result = await authService.validateLocalLogin(email, password); | ||
|
||
expect(existsSpy).toHaveBeenCalledWith(email); | ||
expect(result).toBeFalsy(); | ||
}); | ||
|
||
it('틀린 비밀번호로 계정 검증을 시도한다.', async () => { | ||
const email = faker.internet.email(); | ||
const password = faker.datatype.string(); | ||
|
||
const existsSpy = jest.spyOn(userService, 'existsUserByEmail').mockResolvedValue(true); | ||
const findOneSpy = jest.spyOn(userService, 'findOneByEmail').mockResolvedValue( | ||
createUserMock( | ||
{ email }, | ||
{ | ||
password | ||
} | ||
) | ||
); | ||
const compareSpy = jest.spyOn(bcrypt, 'compare').mockResolvedValue(false); | ||
|
||
const result = await authService.validateLocalLogin(email, password); | ||
|
||
expect(existsSpy).toHaveBeenCalledWith(email); | ||
expect(findOneSpy).toHaveBeenCalledWith(email); | ||
expect(compareSpy).toBeCalled(); | ||
expect(result).toBeFalsy(); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -33,5 +33,5 @@ export class Room { | |
onDelete: 'CASCADE' | ||
}) | ||
@JoinColumn({ name: 'owner' }) | ||
owner!: User; | ||
owner!: string; | ||
} |
Oops, something went wrong.