@tomei/sso
Version:
Tomei SSO Package
747 lines (694 loc) • 25.1 kB
text/typescript
// import { IUserSession } from '../../../../src/interfaces/user-session.interface';
// import {
// SessionService,
// } from '../../../../src/session/session.service';
// import { SystemRepository } from '../../../../src/components/system/system.repository';
// import { LoginUser } from '../../../../src/components/login-user/login-user';
// import { UserRepository } from '../../../../src/components/login-user/user.repository';
// import { PasswordHashService } from '../../../../src/components/password-hash/password-hash.service';
// import { SMTPMailer } from '@tomei/mailer';
// describe('login-user', () => {
// let isSessionExist = true;
// let user = {
// id: 755,
// Email: 'ezcash+florence@tomei.com.my',
// Password:
// '$argon2id$v=19$m=4096,t=3,p=1$571ilUAi9n5g393m/NqKbQ$2bMnLtMCIVTjHWHGEDxI2wo+A3mrL3N5rTxDJ6ydPi8',
// Status: null,
// DefaultPasswordChanged: false,
// FirstLoginAt: new Date('2023-01-10T07:57:10.000Z'),
// CreatedAt: new Date('2023-01-10T07:57:10.000Z'),
// UpdatedAt: new Date('2023-01-10T08:58:15.000Z'),
// GroupCode: 'EZCFT',
// Staff: [
// {
// id: 740,
// StaffId: 'EZC003',
// FullName: 'EZC Florence',
// PreferredName: 'EZC Florence',
// Email: 'ezcash+florence@tomei.com.my',
// StaffTypeId: 1,
// JobTitle: 'EZC Finance',
// CarPlate: '',
// Mobile: '60123456',
// Floor: null,
// Extension: null,
// IsCharge: false,
// Status: 'active',
// UserId: 755,
// BuildingId: 20,
// DepartmentId: 110,
// CompanyId: 70,
// CreatedById: 74,
// UpdatedById: 74,
// CcreatedAt: new Date('2023-01-10T07:57:10.000Z'),
// UpdatedAt: new Date('2023-01-10T07:57:10.000Z'),
// Image: null,
// IdNo: '123123123',
// FullAddress: 'Lorem Address',
// },
// ],
// SystemPrivileges: [
// {
// PrivilegeId: 'ckymxuh8t000137t011w89zgk',
// SystemId: 175,
// Code: 'Terminate Data',
// Module: null,
// Description: 'Allows the user to terminate a loan.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// ],
// };
// const system = {
// id: 175,
// Code: 'EZC',
// Name: 'EzCash',
// Description: 'Tomei Money Lending System',
// AccessUrl: 'https://app.ezcash.com.my:22443/staff/login',
// GooglePlayUrl: '',
// AppleStoreUrl: '',
// ApiKey: 'VqS9ks2Lwvqd7HrVUMMIP2q7zaEH689HCPaEaFUQLmiRoZnt',
// Logo: 'https://sso-api.tomei.com.my/upload/2023/01/10/FJ3DoHdRZKjf2bGkgA4E-BrowserIcon.png',
// Status: 'active',
// Visible: true,
// CreatedAt: new Date('2023-01-09T00:45:57.000Z'),
// UpdatedAt: new Date('2023-01-10T06:28:56.000Z'),
// ApiSecret:
// '$argon2id$v=19$m=4096,t=3,p=1$lqxfk/ujftswf2jSEu156g$j2IShE/BZUUoH/1jrvt3GGRSy0rA8HilJvK17e6vdFY',
// UpdatedById: 61,
// CreatedById: 100,
// };
// const systemAccess = {
// UserId: 755,
// SystemId: 175,
// UpdatedAt: new Date('2023-01-09T00:45:57.000'),
// };
// const session: IUserSession = {
// systemLogins: [
// {
// id: '175',
// code: 'EZC',
// sessionId: 'ckymxuh8t000137t011w89zgk',
// privileges: [
// 'Terminate',
// 'Download - Agreement',
// 'Disbursement - Confirmation',
// 'Invoice Payment Received',
// 'Download - Disbursement Receipt',
// 'Download - Stamping Receipt',
// 'Installment Payment Received',
// 'View Transaction History',
// 'Menu - Loans',
// 'Loan - Full Settlement',
// 'View Billing',
// 'Loan - Retrieve List',
// 'Loan - View Details',
// 'Loan - Retrieve Outstanding',
// 'Loan - View Mandate Details',
// 'Loan Document - Retrieve List',
// 'Loan Document - View',
// 'Loan Document - Download',
// 'Application - View Details',
// 'Simple Loan Schedule - Get Overdue Schedules',
// 'Simple Loan Schedule - Get Schedule Payment Details',
// 'Retrieve Media List',
// 'Retrieve Customer List',
// 'View Loan Activity',
// 'Activate Installment',
// 'Manual Payment Receipt',
// 'Customer Documents - View',
// ],
// },
// ],
// };
// let ezcft = {
// GroupCode: 'EZCFT',
// GroupDescription: 'EZCASH FINANCE TEAM',
// Status: 'Active',
// PersonInCharge: null,
// FullPath: null,
// ParentGroupCode: 'EZCCS',
// GroupLevel: 2,
// GroupType: null,
// AllowInheritFromParentYN: 'Y',
// CreatedById: '61',
// CreatedAt: new Date(),
// UpdatedById: '61',
// UpdatedAt: new Date(),
// DepartmentId: 110,
// GroupSystemPrivileges: [],
// GroupRolePrivileges: [
// {
// GroupCode: 'EZCFT',
// RoleId: 'clcogtmco00013unyc4ngcnza',
// PrivilegeId: 'clcogtntx00023unyg98a0ah0',
// CreatedById: 74,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogtntx00023unyg98a0ah0',
// SystemId: 175,
// Code: 'Terminate',
// Module: null,
// Description: 'Allows the user to terminate a loan.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCFT',
// RoleId: 'clcogtmco00013unyc4ngcnza',
// PrivilegeId: 'clcogtnys00043wp21ejm068j',
// CreatedById: 74,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogtnys00043wp21ejm068j',
// SystemId: 175,
// Code: 'Download - Agreement',
// Module: null,
// Description: 'Allows the user to download the agreement.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCFT',
// RoleId: 'clcogtmco00013unyc4ngcnza',
// PrivilegeId: 'clcogtoa100063wp2djq10roz',
// CreatedById: 74,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogtoa100063wp2djq10roz',
// SystemId: 175,
// Code: 'Disbursement - Confirmation',
// Module: null,
// Description:
// 'Allows the user to confirm the disbursement of the loan.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCFT',
// RoleId: 'clcogtmco00013unyc4ngcnza',
// PrivilegeId: 'clcogtob000053uny52atenxa',
// CreatedById: 74,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogtob000053uny52atenxa',
// SystemId: 175,
// Code: 'Invoice Payment Received',
// Module: null,
// Description:
// 'Allows the user to confirm payment receipt of an invoice.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// ],
// };
// const ezccs = {
// GroupCode: 'EZCCS',
// GroupDescription: 'EZCASH COMPANY SIGNER',
// Status: 'Active',
// PersonInCharge: null,
// FullPath: null,
// ParentGroupCode: 'EZCAT',
// GroupLevel: 1,
// GroupType: null,
// AllowInheritFromParentYN: 'Y',
// CreatedById: '61',
// CreatedAt: new Date(),
// UpdatedById: '61',
// UpdatedAt: new Date(),
// DepartmentId: 110,
// GroupSystemPrivileges: [],
// GroupRolePrivileges: [
// {
// GroupCode: 'EZCCS',
// RoleId: 'cl34552or00002vlubgs11tc9',
// PrivilegeId: 'cldu3gd7t000g3spe55jmax51',
// CreatedById: 700,
// CreatedAt: new Date(),
// SystemId: 42,
// SystemPrivilege: {
// PrivilegeId: 'cldu3gd7t000g3spe55jmax51',
// SystemId: 42,
// Code: 'View Customer',
// Module: null,
// Description: 'Allow to view customer details',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCCS',
// RoleId: 'clcogtm8000003unybenw69nl',
// PrivilegeId: 'clcogtnsn00023wp21z5n28az',
// CreatedById: 700,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogtnsn00023wp21z5n28az',
// SystemId: 175,
// Code: 'Signer - Company',
// Module: null,
// Description:
// 'Allows the user to sign loan agreements on behalf of the company.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCCS',
// RoleId: 'clcogtm8000003unybenw69nl',
// PrivilegeId: 'clcogtnys00043wp21ejm068j',
// CreatedById: 700,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogtnys00043wp21ejm068j',
// SystemId: 175,
// Code: 'Download - Agreement',
// Module: null,
// Description: 'Allows the user to download the agreement.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCCS',
// RoleId: 'clcogtm8000003unybenw69nl',
// PrivilegeId: 'clcogtqb400074dn47ld564nt',
// CreatedById: 700,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clds7oql500003spe3hubafn6',
// SystemId: 175,
// Code: 'Loan - Retrieve List',
// Module: null,
// Description:
// 'Allows the user to retrieve the loan listing/search for all loans within the system.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// ],
// };
// const ezcat = {
// GroupCode: 'EZCAT',
// GroupDescription: 'EZC ATTESTATOR',
// Status: 'Active',
// PersonInCharge: null,
// FullPath: null,
// ParentGroupCode: null,
// GroupLevel: 0,
// GroupType: null,
// AllowInheritFromParentYN: 'Y',
// CreatedById: '61',
// CreatedAt: new Date(),
// UpdatedById: '61',
// UpdatedAt: new Date(),
// DepartmentId: 110,
// GroupSystemPrivileges: [],
// GroupRolePrivileges: [
// {
// GroupCode: 'EZCAT',
// RoleId: 'cl34552or00002vlubgs11tc9',
// PrivilegeId: 'cldu3gd7t000g3spe55jmax51',
// CreatedById: 700,
// CreatedAt: new Date(),
// SystemId: 42,
// SystemPrivilege: {
// PrivilegeId: 'cldu3gd7t000g3spe55jmax51',
// SystemId: 42,
// Code: 'View Customer',
// Module: null,
// Description: 'Allow to view customer details',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCAT',
// RoleId: 'clcogtmyx00014dn4798pel3n',
// PrivilegeId: 'clcogtnuf00033wp27vwb1vwl',
// CreatedById: 74,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogtnuf00033wp27vwb1vwl',
// SystemId: 175,
// Code: 'Signing Session - Schedule',
// Module: null,
// Description:
// 'Allows the user to schedule or reschedule the signing session.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCAT',
// RoleId: 'clcogtmyx00014dn4798pel3n',
// PrivilegeId: 'clcogto8t00043unyd60c8j6r',
// CreatedById: 74,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogto8t00043unyd60c8j6r',
// SystemId: 175,
// Code: 'Signing Session - No Show',
// Module: null,
// Description:
// 'Allows the user to mark the signing session as "no show".',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// {
// GroupCode: 'EZCAT',
// RoleId: 'clcogtmyx00014dn4798pel3n',
// PrivilegeId: 'clcogtobc00073wp21lanh2qw',
// CreatedById: 74,
// CreatedAt: new Date(),
// SystemId: 175,
// SystemPrivilege: {
// PrivilegeId: 'clcogtobc00073wp21lanh2qw',
// SystemId: 175,
// Code: 'Setup Session',
// Module: null,
// Description:
// 'Allows the users to set up the predefined slots for the live attestation sessions.',
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// },
// },
// ],
// };
// let userUserGroups = [
// {
// UserId: 755,
// GroupCode: 'EZCFT',
// SystemId: 175,
// CreatedAt: new Date(),
// UpdatedAt: new Date(),
// UserGroup: ezcft,
// },
// ];
// jest.mock('crypto', () => {
// return {
// randomUUID: jest.fn().mockReturnValue('ckymxuh8t000137t011w89zgk'),
// };
// });
// jest
// .spyOn(PasswordHashService.prototype, 'verify')
// .mockImplementation(async (password) => {
// if (password === 'Abcd@1234') {
// return true;
// } else {
// return false;
// }
// });
// jest
// .spyOn(SystemRepository.prototype, 'findOne')
// .mockResolvedValue(system as any);
// jest.spyOn(SMTPMailer.prototype, 'send').mockImplementation(async () => { });
// const getUserGroupFromDBMock = jest.spyOn(
// LoginUser.prototype as any,
// 'getUserGroupFromDB',
// );
// const getUserUserGroupFromDBMock = jest.spyOn(
// LoginUser.prototype as any,
// 'getUserUserGroupFromDB',
// );
// const userMock = jest.spyOn(UserRepository.prototype, 'findOne');
// jest
// .spyOn(SessionService.prototype, 'retrieveUserSession')
// .mockImplementation(async (userId: string) => {
// if (userId && userId == '755' && isSessionExist === true) {
// return session;
// } else {
// return {
// systemLogins: [],
// };
// }
// });
// userMock.mockResolvedValue(user as any);
// getUserGroupFromDBMock.mockImplementation(async (groupCode) => {
// if (groupCode === 'EZCFT') {
// return ezcft;
// } else if (groupCode === 'EZCCS') {
// return ezccs;
// } else if (groupCode === 'EZCAT') {
// return ezcat;
// }
// });
// getUserUserGroupFromDBMock.mockImplementation(async () => {
// return userUserGroups;
// });
// describe('login', () => {
// jest.spyOn(SystemAccessRepository.prototype, 'findOne').mockResolvedValue({
// ...systemAccess,
// get: () => {
// return systemAccess;
// },
// } as any);
// jest
// .spyOn(LoginHistoryRepository.prototype, 'findAll')
// .mockImplementation(async () => {
// const data = [];
// return data;
// });
// jest
// .spyOn(LoginHistoryRepository.prototype, 'create')
// .mockResolvedValue({} as any);
// afterAll(() => {
// jest.clearAllMocks();
// });
// it('should return login-user service when instansiated', async () => {
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService);
// expect(loginUser).toBeDefined();
// });
// it('should able to do login process when no session is already available', async () => {
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser.login(
// 'EZC',
// 'ezcash+florence@tomei.com.my',
// 'Abcd@1234',
// '1.1.1.1',
// );
// expect(result).toEqual('755:ckymxuh8t000137t011w89zgk');
// });
// it('should able to do login process when session is already available', async () => {
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser.login(
// 'EZC',
// 'ezcash+florence@tomei.com.my',
// 'Abcd@1234',
// '1.1.1.1',
// );
// expect(result).toEqual('755:ckymxuh8t000137t011w89zgk');
// });
// });
// describe('getPrivileges', () => {
// afterAll(() => {
// jest.clearAllMocks();
// });
// it('should be able to login when user only have one user group with level 1 or higher', async () => {
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser['getPrivileges']('EZC');
// expect(getUserGroupFromDBMock).toBeCalledTimes(2);
// expect(result).toEqual([
// 'View Customer',
// 'Signer - Company',
// 'Download - Agreement',
// 'Loan - Retrieve List',
// 'Signing Session - Schedule',
// 'Signing Session - No Show',
// 'Setup Session',
// 'Terminate',
// 'Disbursement - Confirmation',
// 'Invoice Payment Received',
// 'Terminate Data',
// ]);
// });
// it('should be able to login when user only have multiple user user group', async () => {
// const tempUserUserGroups = userUserGroups;
// userUserGroups.push(userUserGroups[0]);
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser['getPrivileges']('EZC');
// expect(getUserUserGroupFromDBMock).toBeCalledTimes(1);
// expect(getUserGroupFromDBMock).toBeCalledTimes(4);
// expect(result).toEqual([
// 'View Customer',
// 'Signer - Company',
// 'Download - Agreement',
// 'Loan - Retrieve List',
// 'Signing Session - Schedule',
// 'Signing Session - No Show',
// 'Setup Session',
// 'Terminate',
// 'Disbursement - Confirmation',
// 'Invoice Payment Received',
// 'Terminate Data',
// ]);
// userUserGroups = tempUserUserGroups;
// });
// it('should be able to login when user only have user privilege', async () => {
// const tempUserUserGroup = userUserGroups;
// userUserGroups = [];
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser['getPrivileges']('EZC');
// expect(getUserGroupFromDBMock).toBeCalledTimes(0);
// expect(result).toEqual(['Terminate Data']);
// userUserGroups = tempUserUserGroup;
// });
// it('should be able to login when user only have one user group with no parent', async () => {
// const tempEzcft = ezcft;
// ezcft.GroupLevel = 0;
// const tempUser = user;
// user.SystemPrivileges = [];
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser['getPrivileges']('EZC');
// expect(getUserGroupFromDBMock).toBeCalledTimes(0);
// expect(result).toEqual([
// 'Terminate',
// 'Download - Agreement',
// 'Disbursement - Confirmation',
// 'Invoice Payment Received',
// ]);
// ezcft = tempEzcft;
// user = tempUser;
// });
// it('should be able to login when user only have one user group that cannot inherit privilege', async () => {
// const tempEzcft = ezcft;
// ezcft.GroupLevel = 1;
// ezcft.AllowInheritFromParentYN = 'N';
// const tempUser = user;
// user.SystemPrivileges = [];
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser['getPrivileges']('EZC');
// expect(getUserGroupFromDBMock).toBeCalledTimes(0);
// expect(result).toEqual([
// 'Terminate',
// 'Download - Agreement',
// 'Disbursement - Confirmation',
// 'Invoice Payment Received',
// ]);
// ezcft = tempEzcft;
// user = tempUser;
// });
// it('should be able to login when user dont have user privilage or user usergroup', async () => {
// const tempUser = user;
// user.SystemPrivileges = [];
// const tempUserUserGroup = userUserGroups;
// userUserGroups = [];
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser['getPrivileges']('EZC');
// expect(getUserGroupFromDBMock).toBeCalledTimes(0);
// expect(result).toEqual([]);
// userUserGroups = tempUserUserGroup;
// user = tempUser;
// });
// });
// describe('checkPrivileges', () => {
// afterEach(() => {
// isSessionExist = true;
// });
// it('should return false if user session not found', async () => {
// isSessionExist = false;
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser.checkPrivileges('EZC', 'Terminate');
// expect(result).toEqual(false);
// });
// it('should return false if system login not found', async () => {
// isSessionExist = false;
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser.checkPrivileges('EZC', 'Terminate');
// expect(result).toEqual(false);
// });
// it('should return false if privilege not found', async () => {
// isSessionExist = false;
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser.checkPrivileges('EZC', 'Not Terminate');
// expect(result).toEqual(false);
// });
// it('should return true if privilege found', async () => {
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser.checkPrivileges('EZC', 'Terminate');
// expect(result).toEqual(true);
// });
// });
// describe('checkSession', () => {
// afterEach(() => {
// isSessionExist = true;
// });
// it('it should returns session expired if user session not found inside session storage', async () => {
// try {
// isSessionExist = false;
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// await loginUser.checkSession(
// 'EZC',
// session.systemLogins[0].sessionId,
// '755',
// );
// } catch (error) {
// expect(error.message).toEqual('Session expired.');
// }
// });
// it('it should returns session expired if sessionId not matched', async () => {
// try {
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// await loginUser.checkSession(
// 'NOTEZC',
// session.systemLogins[0].sessionId,
// '755',
// );
// } catch (error) {
// expect(error.message).toEqual('Session expired.');
// }
// });
// it('it should refresh session time-to-live if session valid', async () => {
// jest
// .spyOn(SessionService.prototype, 'refreshDuration')
// .mockImplementationOnce(async (userId) => {
// if (!userId || userId !== '755') {
// throw new Error('Refresh session failed.');
// }
// });
// const sessionService = await SessionService.init();
// const loginUser = await LoginUser.init(sessionService, 755);
// const result = await loginUser.checkSession(
// 'EZC',
// session.systemLogins[0].sessionId,
// '755',
// );
// expect(result).toEqual(session.systemLogins[0]);
// });
// });
// });
describe('SystemPrivilege', () => {
it('should be true', () => {
expect(true).toBe(true);
});
});