Skip to content

Commit

Permalink
refactor: 테스트 블록화
Browse files Browse the repository at this point in the history
  • Loading branch information
Conut-1 committed Oct 17, 2024
1 parent 12e5c67 commit a434434
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 68 deletions.
91 changes: 49 additions & 42 deletions nestjs-BE/server/src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,77 @@ describe('AuthController', () => {
authService = module.get<AuthService>(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',
});
});
});
});
56 changes: 30 additions & 26 deletions nestjs-BE/server/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit a434434

Please sign in to comment.