From a4344347071e65022f719b948402d5b8118193f8 Mon Sep 17 00:00:00 2001 From: Conut-1 <1mim1@naver.com> Date: Thu, 17 Oct 2024 16:36:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=B8=94=EB=A1=9D=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/src/auth/auth.controller.spec.ts | 91 ++++++++++--------- .../server/src/auth/auth.service.spec.ts | 56 ++++++------ 2 files changed, 79 insertions(+), 68 deletions(-) diff --git a/nestjs-BE/server/src/auth/auth.controller.spec.ts b/nestjs-BE/server/src/auth/auth.controller.spec.ts index 87e0c788..5d0483fc 100644 --- a/nestjs-BE/server/src/auth/auth.controller.spec.ts +++ b/nestjs-BE/server/src/auth/auth.controller.spec.ts @@ -28,70 +28,77 @@ describe('AuthController', () => { authService = module.get(AuthService); }); - it('kakaoLogin', async () => { + describe('kakaoLogin', () => { const requestMock = { kakaoUserId: 0 }; - const tokenMock = { - refresh_token: 'refresh token', - access_token: 'access token', - }; - jest.spyOn(authService, 'kakaoLogin').mockResolvedValue(tokenMock); + it('success', async () => { + const tokenMock = { + refresh_token: 'refresh token', + access_token: 'access token', + }; - const response = controller.kakaoLogin(requestMock); + jest.spyOn(authService, 'kakaoLogin').mockResolvedValue(tokenMock); - await expect(response).resolves.toEqual({ - statusCode: HttpStatus.OK, - message: 'OK', - data: tokenMock, - }); - }); + const response = controller.kakaoLogin(requestMock); - it('kakaoLogin kakao login fail', async () => { - const requestMock = { kakaoUserId: 0 }; + await expect(response).resolves.toEqual({ + statusCode: HttpStatus.OK, + message: 'OK', + data: tokenMock, + }); + }); - jest - .spyOn(authService, 'kakaoLogin') - .mockRejectedValue(new NotFoundException()); + it('kakao login fail', async () => { + jest + .spyOn(authService, 'kakaoLogin') + .mockRejectedValue(new NotFoundException()); - const response = controller.kakaoLogin(requestMock); + const response = controller.kakaoLogin(requestMock); - await expect(response).rejects.toThrow(NotFoundException); + await expect(response).rejects.toThrow(NotFoundException); + }); }); - it('renewAccessToken respond new access token', async () => { + describe('renewAccessToken', () => { const requestMock = { refreshToken: 'refresh token' }; - jest - .spyOn(authService, 'renewAccessToken') - .mockResolvedValue('new access token'); - const response = controller.renewAccessToken(requestMock); + it('respond new access token', async () => { + jest + .spyOn(authService, 'renewAccessToken') + .mockResolvedValue('new access token'); - await expect(response).resolves.toEqual({ - statusCode: HttpStatus.OK, - message: 'OK', - data: { access_token: 'new access token' }, + const response = controller.renewAccessToken(requestMock); + + await expect(response).resolves.toEqual({ + statusCode: HttpStatus.OK, + message: 'OK', + data: { access_token: 'new access token' }, + }); }); - }); - it('renewAccessToken received expired token', async () => { - const requestMock = { refreshToken: 'refresh token' }; - jest.spyOn(authService, 'renewAccessToken').mockRejectedValue(new Error()); + it('received expired token', async () => { + jest + .spyOn(authService, 'renewAccessToken') + .mockRejectedValue(new Error()); - const response = controller.renewAccessToken(requestMock); + const response = controller.renewAccessToken(requestMock); - await expect(response).rejects.toThrow(Error); + await expect(response).rejects.toThrow(Error); + }); }); - it('logout received token deleted', async () => { - const requestMock = { refreshToken: 'refresh token' }; + describe('logout', () => { + it('received token deleted', async () => { + const requestMock = { refreshToken: 'refresh token' }; - jest.spyOn(authService, 'logout'); + jest.spyOn(authService, 'logout'); - const response = controller.logout(requestMock); + const response = controller.logout(requestMock); - await expect(response).resolves.toEqual({ - statusCode: HttpStatus.NO_CONTENT, - message: 'No Content', + await expect(response).resolves.toEqual({ + statusCode: HttpStatus.NO_CONTENT, + message: 'No Content', + }); }); }); }); diff --git a/nestjs-BE/server/src/auth/auth.service.spec.ts b/nestjs-BE/server/src/auth/auth.service.spec.ts index 28444947..877bf416 100644 --- a/nestjs-BE/server/src/auth/auth.service.spec.ts +++ b/nestjs-BE/server/src/auth/auth.service.spec.ts @@ -81,39 +81,43 @@ describe('AuthService', () => { }); }); - it('login success', async () => { - jest.spyOn(jwtService, 'signAsync').mockResolvedValue('access token'); - jest.spyOn(refreshTokensService, 'createRefreshToken').mockResolvedValue({ - token: 'refresh token', - } as unknown as RefreshToken); - - const tokens = service.login('user uuid'); - - await expect(tokens).resolves.toEqual({ - access_token: 'access token', - refresh_token: 'refresh token', + describe('login', () => { + it('success', async () => { + jest.spyOn(jwtService, 'signAsync').mockResolvedValue('access token'); + jest.spyOn(refreshTokensService, 'createRefreshToken').mockResolvedValue({ + token: 'refresh token', + } as unknown as RefreshToken); + + const tokens = service.login('user uuid'); + + await expect(tokens).resolves.toEqual({ + access_token: 'access token', + refresh_token: 'refresh token', + }); }); }); - it('renewAccessToken success', async () => { - jest.spyOn(jwtService, 'verify').mockReturnValue({}); - jest.spyOn(jwtService, 'signAsync').mockResolvedValue('access token'); - jest.spyOn(refreshTokensService, 'findRefreshToken').mockResolvedValue({ - userUuid: 'user uuid', - } as RefreshToken); + describe('renewAccessToken', () => { + it('success', async () => { + jest.spyOn(jwtService, 'verify').mockReturnValue({}); + jest.spyOn(jwtService, 'signAsync').mockResolvedValue('access token'); + jest.spyOn(refreshTokensService, 'findRefreshToken').mockResolvedValue({ + userUuid: 'user uuid', + } as RefreshToken); - const token = service.renewAccessToken('refresh token'); + const token = service.renewAccessToken('refresh token'); - await expect(token).resolves.toBe('access token'); - }); - - it('renewAccessToken fail', async () => { - jest.spyOn(jwtService, 'verify').mockImplementation(() => { - throw new Error(); + await expect(token).resolves.toBe('access token'); }); - const token = service.renewAccessToken('refresh token'); + it('fail', async () => { + jest.spyOn(jwtService, 'verify').mockImplementation(() => { + throw new Error(); + }); - await expect(token).rejects.toThrow(); + const token = service.renewAccessToken('refresh token'); + + await expect(token).rejects.toThrow(); + }); }); });