import { Injectable } from '@angular/core'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { UserFromApi } from '@core/typings/client-user.typing'; import { PermissionSet } from '@core/typings/permission.typing'; import { UserRoleExport, UserWorkflowExport } from '@core/typings/user.typing'; import { WorkflowLevelPermissions } from '@core/typings/workflow.typing'; import { BeforeEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect } from 'chai'; import { UserExportService } from './user-export.service'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(UserExportService, { imports: [GCMockModule] }) export class UserExportServiceSpec implements Spec { mockUserReturn: UserFromApi[] = [{ firstName: 'First', lastName: 'User', email: 'first@user.com', jobTitle: 'First Title', profileImageUrl: '', isRootUser: false, active: false, requirePasswordReset: false, isNewUser: false, id: 1, culture: '', roles: [{ clientRoleId: 1, clientRoleDescription: 'First Description', clientRoleName: 'First Role', policies: [{ clientRoleId: 1, allow: true, permissionSetType: 1, permissionType: 1 }] }], acceptedTermsOfService: true, dateAcceptedTermsOfService: '', fullName: 'First User', isDeactivated: false, userId: 1, clientHasNominations: false, isInNominationWorkFlow: false, isIntegratedWithCsrZone: false, workflows: [], workFlowLevels: [{ clientId: 1, description: 'First Description', name: 'First Workflow Level', id: 1, workflow: { description: 'First Description', name: 'First Workflow', id: 1, workflowManager: true }, workflowLevelUserAccessType: WorkflowLevelPermissions.MANAGER }], ssoId: '1', isSSO: true }, { firstName: 'Second', lastName: 'User', email: 'second@user.com', jobTitle: 'Second Title', profileImageUrl: '', isRootUser: false, active: false, requirePasswordReset: false, isNewUser: false, id: 1, culture: '', roles: [{ clientRoleId: 1, clientRoleDescription: 'First Description', clientRoleName: 'First Role', policies: [{ clientRoleId: 1, allow: true, permissionSetType: 1, permissionType: 1 }] }, { clientRoleId: 1, clientRoleDescription: 'Second Description', clientRoleName: 'Second Role', policies: [{ clientRoleId: 1, allow: true, permissionSetType: 2, permissionType: 2 }, { clientRoleId: 1, allow: true, permissionSetType: 3, permissionType: 3 }] }], acceptedTermsOfService: true, dateAcceptedTermsOfService: '', fullName: 'Second User', isDeactivated: false, userId: 1, clientHasNominations: false, isInNominationWorkFlow: false, isIntegratedWithCsrZone: false, workflows: [], workFlowLevels: [{ clientId: 1, description: 'First Description', name: 'First Workflow Level', id: 1, workflow: { description: 'First Description', name: 'First Workflow', id: 1, workflowManager: false }, workflowLevelUserAccessType: WorkflowLevelPermissions.LEVEL_ONLY }, { clientId: 1, description: 'Second Description', name: 'Second Workflow Level', id: 1, workflow: { description: 'Second Description', name: 'Second Workflow', id: 1, workflowManager: true }, workflowLevelUserAccessType: WorkflowLevelPermissions.MANAGER }, { clientId: 1, description: 'Third Description', name: 'Third Workflow Level', id: 1, workflow: { description: 'Third Description', name: 'Third Workflow', id: 1, workflowManager: true }, workflowLevelUserAccessType: WorkflowLevelPermissions.PASSTHROUGH }], ssoId: null, isSSO: false }]; private readonly mockPermissions: PermissionSet[] = [{ type: 1, name: 'First Set', description: '', permissions: [{ type: 1, name: 'First Permission', description: '' }, { type: 2, name: 'Second Permission', description: '' }, { type: 3, name: 'Third Permission', description: '' }] }, { type: 2, name: 'Second Set', description: '', permissions: [{ type: 1, name: 'First Permission', description: '' }, { type: 2, name: 'Second Permission', description: '' }, { type: 3, name: 'Third Permission', description: '' }] }, { type: 3, name: 'Third Set', description: '', permissions: [{ type: 1, name: 'First Permission', description: '' }, { type: 2, name: 'Second Permission', description: '' }, { type: 3, name: 'Third Permission', description: '' }] }]; @BeforeEach() setupFetch (service: UserExportService) { service['getAllUsersWithRoles'] = async () => { return this.mockUserReturn; }; service['roleService']['set']('permissionSets', this.mockPermissions); } @TestCase('should be able to export user roles') async testShouldExportUserRoles (service: UserExportService) { const exportedUsers = await service.getUserRoles(); const user1 = this.mockUserReturn[0]; const user1Role1 = user1.roles[0]; const user2 = this.mockUserReturn[1]; const user2Role1 = user2.roles[0]; const user2Role2 = user2.roles[1]; // three items because of 2 users having 3 total assignments const expected: UserRoleExport[] = [{ 'Employee Email': user1.email, 'Employee First Name': user1.firstName, 'Employee Last Name': user1.lastName, 'Is Employee Active': true, Role: user1Role1.clientRoleName, Permissions: user1Role1.policies.map(p => { const set = this.mockPermissions.find(_set => { return _set.type === p.permissionSetType; }); const permission = set.permissions.find(_permission => { return _permission.type === p.permissionType; }); return set.name + ' - ' + permission.name; }).join(', '), SSO: 'Yes' // yes because user has SSO ID }, { 'Employee Email': user2.email, 'Employee First Name': user2.firstName, 'Employee Last Name': user2.lastName, 'Is Employee Active': true, Role: user2Role1.clientRoleName, Permissions: user2Role1.policies.map(p => { const set = this.mockPermissions.find(_set => { return _set.type === p.permissionSetType; }); const permission = set.permissions.find(_permission => { return _permission.type === p.permissionType; }); return set.name + ' - ' + permission.name; }).join(', '), SSO: 'No' // no because user does not have SSO ID }, { 'Employee Email': user2.email, 'Employee First Name': user2.firstName, 'Employee Last Name': user2.lastName, 'Is Employee Active': true, Role: user2Role2.clientRoleName, Permissions: user2Role2.policies.map(p => { const set = this.mockPermissions.find(_set => { return _set.type === p.permissionSetType; }); const permission = set.permissions.find(_permission => { return _permission.type === p.permissionType; }); return set.name + ' - ' + permission.name; }).join(', '), SSO: 'No' }]; expect(exportedUsers).to.deep.equal(expected); } @TestCase('should be able to export user WFLs') async testShouldExportUserWFLs (service: UserExportService) { const userWorkflows = await service.getUserWFLs(); const user1 = this.mockUserReturn[0]; const user1WFL1 = user1.workFlowLevels[0]; const user2 = this.mockUserReturn[1]; const user2WFL1 = user2.workFlowLevels[0]; const user2WFL2 = user2.workFlowLevels[1]; const user2WFL3 = user2.workFlowLevels[2]; // three items because of 2 users having 3 total assignments const expected: UserWorkflowExport[] = [{ 'Employee Email': user1.email, 'Employee First Name': user1.firstName, 'Employee Last Name': user1.lastName, 'Is Employee Active': true, Workflow: user1WFL1.workflow.name, 'Workflow Level': user1WFL1.name, 'Workflow Level Permission': 'Workflow manager' // workflow manager because the mock has it set to true }, { 'Employee Email': user2.email, 'Employee First Name': user2.firstName, 'Employee Last Name': user2.lastName, 'Is Employee Active': true, Workflow: user2WFL1.workflow.name, 'Workflow Level': user2WFL1.name, 'Workflow Level Permission': 'Workflow level only' // workflow level only because the mock has it set to true }, { 'Employee Email': user2.email, 'Employee First Name': user2.firstName, 'Employee Last Name': user2.lastName, 'Is Employee Active': true, Workflow: user2WFL2.workflow.name, 'Workflow Level': user2WFL2.name, 'Workflow Level Permission': 'Workflow manager' }, { 'Employee Email': user2.email, 'Employee First Name': user2.firstName, 'Employee Last Name': user2.lastName, 'Is Employee Active': true, Workflow: user2WFL3.workflow.name, 'Workflow Level': user2WFL3.name, 'Workflow Level Permission': 'Workflow passthrough' }]; expect(userWorkflows).to.deep.equal(expected); } }