@tomei/sso
Version:
Tomei SSO Package
204 lines • 10.8 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const system_1 = require("../../../../src/components/system/system");
const system_repository_1 = require("../../../../src/components/system/system.repository");
const login_user_1 = require("../../../../src/components/login-user/login-user");
const general_1 = require("@tomei/general");
const config_1 = require("@tomei/config");
describe('System', () => {
let system;
let systemRepositoryMock;
let loginUserMock;
beforeEach(() => {
systemRepositoryMock = new system_repository_1.SystemRepository();
loginUserMock = new login_user_1.LoginUser.prototype.constructor();
system = new system_1.System.prototype.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', () => __awaiter(void 0, void 0, void 0, function* () {
const dbTransaction = {};
const systemCode = 'TEST';
const findOneMock = jest
.spyOn(system_repository_1.SystemRepository.prototype, 'findByPk')
.mockResolvedValueOnce(Object.assign({}, systemAttr));
const result = yield system_1.System.init(dbTransaction, systemCode);
expect(findOneMock).toBeCalledWith(systemCode, {
transaction: dbTransaction,
});
expect(result).toBeInstanceOf(system_1.System);
}));
it('should throw an error when SystemCode is not found', () => __awaiter(void 0, void 0, void 0, function* () {
const dbTransaction = {};
const systemCode = 'TEST';
jest
.spyOn(system_repository_1.SystemRepository.prototype, 'findByPk')
.mockResolvedValueOnce(null);
yield expect(system_1.System.init(dbTransaction, systemCode)).rejects.toThrow(new general_1.ClassError('System', 'SystemErrMsg01', 'Failed To Initialize System'));
}));
it('should initialize a new system when SystemCode is not provided', () => __awaiter(void 0, void 0, void 0, function* () {
const dbTransaction = {};
const result = yield system_1.System.init(dbTransaction);
expect(result).toBeInstanceOf(system_1.System);
}));
});
describe('createSystem', () => {
const loginUser = new login_user_1.LoginUser.prototype.constructor();
const dbTransaction = {};
const checkPrivilegesMock = jest.spyOn(login_user_1.LoginUser.prototype, 'checkPrivileges').mockResolvedValue(true);
const getComponentConfigMock = jest
.spyOn(config_1.ApplicationConfig, 'getComponentConfigValue')
.mockReturnValue('system-code');
beforeEach(() => {
getComponentConfigMock.mockReturnValue('system-code');
checkPrivilegesMock.mockResolvedValue(true);
loginUser.ObjectId = '1';
});
const createMock = jest
.spyOn(system_repository_1.SystemRepository.prototype, 'create')
.mockResolvedValue({});
it('should create a new system', () => __awaiter(void 0, void 0, void 0, function* () {
const createDate = new Date();
const system = yield system_1.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;
yield 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', () => __awaiter(void 0, void 0, void 0, function* () {
loginUser.checkPrivileges.mockResolvedValue(false);
yield 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', () => __awaiter(void 0, void 0, void 0, function* () {
system.Name = 'Test System';
system.Description = 'This is a test system';
yield expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(new general_1.ClassError('System', 'SystemErrMsg02', 'SystemCode must have value.'));
}));
it('should throw an error when Name is missing', () => __awaiter(void 0, void 0, void 0, function* () {
system.SystemCode = 'TEST';
system.Description = 'This is a test system';
yield expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(new general_1.ClassError('System', 'SystemErrMsg03', 'Name must have value.'));
}));
it('should throw an error when Description is missing', () => __awaiter(void 0, void 0, void 0, function* () {
system.SystemCode = 'TEST';
system.Name = 'Test System';
yield expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(new general_1.ClassError('System', 'SystemErrMsg04', 'Description must have value.'));
}));
it('should throw an error when failed to create system', () => __awaiter(void 0, void 0, void 0, function* () {
system.SystemCode = 'TEST';
system.Name = 'Test System';
system.Description = 'This is a test system';
systemRepositoryMock.create.mockRejectedValueOnce(new Error());
yield expect(system.createSystem(loginUser, dbTransaction)).rejects.toThrow(new Error());
}));
});
describe('setSystemCode', () => {
const dbTransaction = {};
const systemRepositoryMock = jest.spyOn(system_repository_1.SystemRepository.prototype, 'findOne');
beforeEach(() => {
systemRepositoryMock.mockResolvedValue(null);
});
it('should set the SystemCode when there is no duplicate', () => __awaiter(void 0, void 0, void 0, function* () {
const systemCode = 'TEST';
yield 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', () => __awaiter(void 0, void 0, void 0, function* () {
const systemCode = 'TEST';
systemRepositoryMock.mockResolvedValue({});
yield expect(system.setSystemCode(dbTransaction, systemCode)).rejects.toThrow(new general_1.ClassError('System', 'SystemErrMsg05', 'System Code already exists.'));
}));
it('should throw an error when failed to check duplicate SystemCode', () => __awaiter(void 0, void 0, void 0, function* () {
const systemCode = 'TEST';
systemRepositoryMock.mockRejectedValueOnce(new Error());
yield expect(system.setSystemCode(dbTransaction, systemCode)).rejects.toThrow(new Error());
}));
});
describe('findAll', () => {
const loginUser = new login_user_1.LoginUser.prototype.constructor();
const dbTransaction = {};
it('should find all systems based on filter', () => __awaiter(void 0, void 0, void 0, function* () {
const page = 1;
const rows = 10;
const search = { Name: 'exampleName' };
const findAllWithPaginationSpy = jest.spyOn(system_1.System['_Repo'], 'findAllWithPagination').mockResolvedValueOnce({
count: 1,
rows: [{}],
});
const result = yield system_1.System.findAll(dbTransaction, loginUser, page, rows, search);
expect(result.count).toBe(1);
expect(result.systems).toHaveLength(1);
expect(result.systems[0]).toBeInstanceOf(system_1.System);
}));
it('should find all systems without pagination when page and rows are not provided', () => __awaiter(void 0, void 0, void 0, function* () {
const findAllWithPaginationSpy = jest.spyOn(system_1.System['_Repo'], 'findAllWithPagination').mockResolvedValueOnce({
count: 1,
rows: [{}],
});
const result = yield system_1.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_1.System);
}));
});
});
//# sourceMappingURL=system.spec.js.map