Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test case 추가 #2

Merged
merged 20 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Account/Test] usecase test 작성
  • Loading branch information
rojiwon0325 committed Nov 6, 2022
commit ae67ce8e7098ee90946115a5061b91e4a6205470
95 changes: 87 additions & 8 deletions src/api/account/test/account.usecase.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Crypto } from '@CRYPTO/domain';
import { Account } from '@ACCOUNT/domain';
import { IAccountUsecase } from '@ACCOUNT/application/port/account.usecase.port';
import { mockRepository } from './repository.mock';
import { Test } from '@nestjs/testing';
Expand All @@ -10,13 +8,26 @@ import { AccountRepository } from '@ACCOUNT/infrastructure/adapter/account.repos
import { AccountService } from '@ACCOUNT/application/adapter/account.service';
import { AccountUsecase } from '@ACCOUNT/application/adapter/account.usecase';
import { JwtService } from '@nestjs/jwt';
import { Account } from '@ACCOUNT/domain';
import { Crypto } from '@CRYPTO/domain';
import { ExceptionMessage } from '@COMMON/provider/message.provider';
import { IAccountService } from '@ACCOUNT/application/port/account.service.port';
jest.mock('@CRYPTO/domain');

describe('Account Usecase Integration Test', () => {
describe('Account Usecase Unit Test', () => {
let usecase: IAccountUsecase;
const mockRepo = mockRepository();
const now1 = new Date();
const now2 = new Date();
const mockRepo = mockRepository<AccountEntity>();
const mockJwtService = {
sign: jest.fn(),
};
const mockAccountService: Record<keyof IAccountService, jest.Mock> = {
findOne: jest.fn(),
checkPassword: jest.fn(),
checkPermission: jest.fn(),
signInLocal: jest.fn(),
checkDuplicate: jest.fn(),
};

beforeAll(async () => {
const module = await Test.createTestingModule({
providers: [
Expand All @@ -25,9 +36,15 @@ describe('Account Usecase Integration Test', () => {
provide: getRepositoryToken(AccountEntity),
useValue: mockRepo,
},
{
provide: JwtService,
useValue: mockJwtService,
},
{
provide: AccountService,
useValue: mockAccountService,
},
AccountRepository,
AccountService,
JwtService,
AccountUsecase,
],
}).compile();
Expand All @@ -41,4 +58,66 @@ describe('Account Usecase Integration Test', () => {
it('Usecase should be defined', () => {
expect(usecase).toBeDefined();
});

it('signIn', async () => {
mockAccountService.signInLocal.mockResolvedValue({ id: 12 });
await usecase.signIn({ username: '', password: '' });
expect(mockJwtService.sign).toBeCalledWith({ id: 12 });
return;
});

it('signUp', async () => {
mockAccountService.checkDuplicate.mockResolvedValue(true);
mockRepo.save.mockImplementationOnce((value) => ({ ...value, id: 12 }));
(Crypto.encrypt as any).mockResolvedValue('hashed password');
const received = await usecase.signUp({
email: 'test@test.com',
username: 'signUpTest',
password: '1234avcd',
});
expect(received).toEqual({
id: 12,
email: 'test@test.com',
username: 'signUpTest',
role: 'Normal',
});
expect(mockRepo.save).toBeCalledWith({
username: 'signUpTest',
password: 'hashed password',
role: 'Normal',
email: 'test@test.com',
verified: false,
});
return;
});

describe('remove', () => {
const where: Account.Public = {
id: 2,
username: 'test',
email: 'test@test.com',
role: 'Manager',
};

it('사용자명과 인증정보가 일치하지 않는 경우', async () => {
await expect(() =>
usecase.remove(where, { username: 'testname', password: '213' }),
).rejects.toThrowError(ExceptionMessage.FBD);
return;
});

it('이메일과 인증정보가 일치하지 않는 경우', async () => {
await expect(() =>
usecase.remove(where, { email: 'test', password: '213' }),
).rejects.toThrowError(ExceptionMessage.FBD);
return;
});

it('요청 성공', async () => {
mockAccountService.signInLocal.mockResolvedValue({ id: 13 });
await usecase.remove(where, { username: 'test', password: '' });
expect(mockRepo.delete).toBeCalledTimes(1);
return;
});
});
});
7 changes: 6 additions & 1 deletion src/api/account/test/repository.mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export const mockRepository = () => ({
import { Repository } from 'typeorm';

export const mockRepository = <T extends object>(): Pick<
Record<keyof Repository<T>, jest.Mock>,
'findOne' | 'save' | 'delete'
> => ({
findOne: jest.fn(),
save: jest.fn(),
delete: jest.fn(),
Expand Down