@tomei/sso
Version:
Tomei SSO Package
255 lines (209 loc) • 8.53 kB
text/typescript
import { System } from '../../../../src/components/system/system';
import { SystemRepository } from '../../../../src/components/system/system.repository';
import { LoginUser } from '../../../../src/components/login-user/login-user';
import { ClassError } from '@tomei/general';
import { ApplicationConfig } from '@tomei/config';
describe('System', () => {
let system: System;
let systemRepositoryMock: jest.Mocked<SystemRepository>;
let loginUserMock: jest.Mocked<LoginUser>;
beforeEach(() => {
systemRepositoryMock = new SystemRepository() as jest.Mocked<SystemRepository>;
loginUserMock = new (LoginUser.prototype as any).constructor() as jest.Mocked<LoginUser>;
system = new (System.prototype as any).constructor();
});
afterEach(() => {
jest.clearAllMocks();
});
const systemAttr = {
SystemCode: 'TEST',
Name: 'Test System',
Description: 'This is a test system',
AccessURL: undefined,
GooglePlayURL: undefined,
AppleStoreURL: undefined,
APIKey: undefined,
APISecret: undefined,
Status: undefined,
CreatedById: 1,
CreatedAt: expect.any(Date),
UpdatedById: 1,
UpdatedAt: expect.any(Date),
}
describe('init', () => {
it('should initialize a system when SystemCode is provided', async () => {
const dbTransaction = {};
const systemCode = 'TEST';
const findOneMock = jest
.spyOn(SystemRepository.prototype, 'findByPk')
.mockResolvedValueOnce({ ...systemAttr } as any);
const result = await System.init(dbTransaction, systemCode);
expect(findOneMock).toBeCalledWith(systemCode, {
transaction: dbTransaction,
});
expect(result).toBeInstanceOf(System);
});
it('should throw an error when SystemCode is not found', async () => {
const dbTransaction = {};
const systemCode = 'TEST';
jest
.spyOn(SystemRepository.prototype, 'findByPk')
.mockResolvedValueOnce(null);
await expect(System.init(dbTransaction, systemCode)).rejects.toThrow(
new ClassError('System', 'SystemErrMsg01', 'Failed To Initialize System')
);
});
it('should initialize a new system when SystemCode is not provided', async () => {
const dbTransaction = {};
const result = await System.init(dbTransaction);
expect(result).toBeInstanceOf(System);
});
});
describe('createSystem', () => {
const loginUser = new (LoginUser.prototype as any).constructor();
const dbTransaction = {};
const checkPrivilegesMock = jest.spyOn(LoginUser.prototype, 'checkPrivileges').mockResolvedValue(true);
const getComponentConfigMock = jest
.spyOn(ApplicationConfig, 'getComponentConfigValue')
.mockReturnValue('system-code');
beforeEach(() => {
getComponentConfigMock.mockReturnValue('system-code');
checkPrivilegesMock.mockResolvedValue(true);
loginUser.ObjectId = '1';
});
const createMock = jest
.spyOn(SystemRepository.prototype, 'create')
.mockResolvedValue({} as any);
it('should create a new system', async () => {
const createDate = new Date();
const system = await System.init(dbTransaction);
system.SystemCode = 'TEST';
system.Name = 'Test System';
system.Description = 'This is a test system';
system['_CreatedById'] = 1;
system['__CreatedAt'] = createDate;
system['__UpdatedById'] = 1;
system['__UpdatedAt'] = createDate;
await system.createSystem(loginUser, dbTransaction);
expect(checkPrivilegesMock).toBeCalledWith('system-code', 'System - Create');
expect(createMock).toBeCalledWith(
{
SystemCode: 'TEST',
Name: 'Test System',
Description: 'This is a test system',
AccessURL: undefined,
GooglePlayURL: undefined,
AppleStoreURL: undefined,
APIKey: undefined,
APISecret: undefined,
Status: undefined,
CreatedById: 1,
CreatedAt: expect.any(Date),
UpdatedById: 1,
UpdatedAt: expect.any(Date),
}, {
transaction: dbTransaction,
});
});
it('should throw an error when user does not have permission', async () => {
loginUser.checkPrivileges.mockResolvedValue(false);
await expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(
new Error('You do not have permission to list UserGroup.')
);
});
it('should throw an error when SystemCode is missing', async () => {
system.Name = 'Test System';
system.Description = 'This is a test system';
await expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(
new ClassError('System', 'SystemErrMsg02', 'SystemCode must have value.')
);
});
it('should throw an error when Name is missing', async () => {
system.SystemCode = 'TEST';
system.Description = 'This is a test system';
await expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(
new ClassError('System', 'SystemErrMsg03', 'Name must have value.')
);
});
it('should throw an error when Description is missing', async () => {
system.SystemCode = 'TEST';
system.Name = 'Test System';
await expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(
new ClassError('System', 'SystemErrMsg04', 'Description must have value.')
);
});
it('should throw an error when failed to create system', async () => {
system.SystemCode = 'TEST';
system.Name = 'Test System';
system.Description = 'This is a test system';
systemRepositoryMock.create.mockRejectedValueOnce(new Error());
await expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(
new Error()
);
});
});
describe('setSystemCode', () => {
const dbTransaction = {};
const systemRepositoryMock = jest.spyOn(SystemRepository.prototype, 'findOne');
beforeEach(() => {
systemRepositoryMock.mockResolvedValue(null);
});
it('should set the SystemCode when there is no duplicate', async () => {
const systemCode = 'TEST';
await system.setSystemCode(dbTransaction, systemCode);
expect(systemRepositoryMock).toBeCalledWith({
where: {
SystemCode: systemCode,
},
transaction: dbTransaction,
});
expect(system.SystemCode).toBe(systemCode);
});
it('should throw an error when SystemCode already exists', async () => {
const systemCode = 'TEST';
systemRepositoryMock.mockResolvedValue({} as any);
await expect(system.setSystemCode(dbTransaction, systemCode)).rejects.toThrow(
new ClassError('System', 'SystemErrMsg05', 'System Code already exists.')
);
});
it('should throw an error when failed to check duplicate SystemCode', async () => {
const systemCode = 'TEST';
systemRepositoryMock.mockRejectedValueOnce(new Error());
await expect(system.setSystemCode(dbTransaction, systemCode)).rejects.toThrow(
new Error()
);
});
});
describe('findAll', () => {
const loginUser = new (LoginUser.prototype as any).constructor();
const dbTransaction = {};
it('should find all systems based on filter', async () => {
const page = 1;
const rows = 10;
const search = { Name: 'exampleName' };
const findAllWithPaginationSpy = jest.spyOn(System['_Repo'], 'findAllWithPagination').mockResolvedValueOnce({
count: 1,
rows: [{} as any],
});
const result = await System.findAll(dbTransaction, loginUser, page, rows, search);
expect(result.count).toBe(1);
expect(result.systems).toHaveLength(1);
expect(result.systems[0]).toBeInstanceOf(System);
});
it('should find all systems without pagination when page and rows are not provided', async () => {
const findAllWithPaginationSpy = jest.spyOn(System['_Repo'], 'findAllWithPagination').mockResolvedValueOnce({
count: 1,
rows: [{} as any],
});
const result = await System.findAll(dbTransaction, loginUser);
expect(findAllWithPaginationSpy).toHaveBeenCalledWith({
where: {},
order: [['CreatedAt', 'DESC']],
transaction: dbTransaction,
});
expect(result.count).toBe(1);
expect(result.systems).toHaveLength(1);
expect(result.systems[0]).toBeInstanceOf(System);
});
});
});