|
| 1 | +/** |
| 2 | + * Copyright (C) 2025 3D Repo Ltd |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +const { |
| 19 | + determineTestGroup, |
| 20 | + db: { reset: resetDB, createUser, createTeamspace, addLoginRecords }, |
| 21 | + generateRandomString, |
| 22 | + generateUserCredentials, |
| 23 | + fileExists, |
| 24 | + generateRandomNumber, |
| 25 | +} = require('../../helper/services'); |
| 26 | + |
| 27 | +const { times } = require('lodash'); |
| 28 | +const { readFileSync } = require('fs'); |
| 29 | +const DayJS = require('dayjs'); |
| 30 | + |
| 31 | +const { src, utilScripts, tmpDir } = require('../../helper/path'); |
| 32 | + |
| 33 | +const DBHandler = require(`${src}/handler/db`); |
| 34 | +const { ADMIN_DB } = require(`${src}/handler/db.constants`); |
| 35 | +const { USERS_COL } = require(`${src}/models/users.constants`); |
| 36 | + |
| 37 | +const IdentifyInactiveUsers = require(`${utilScripts}/users/identifyInactiveUsers`); |
| 38 | +const { disconnect } = require(`${src}/handler/db`); |
| 39 | + |
| 40 | +const generateLoginRecord = (user) => { |
| 41 | + const loginTime = new Date(); |
| 42 | + loginTime.setMonth(loginTime.getMonth() - Math.round(generateRandomNumber(1, 12))); |
| 43 | + return { _id: generateRandomString(), user, loginTime }; |
| 44 | +}; |
| 45 | + |
| 46 | +const setupData = async () => { |
| 47 | + const teamspace = generateRandomString(); |
| 48 | + const teamspace2 = generateRandomString(); |
| 49 | + const users = times(20, () => generateUserCredentials()); |
| 50 | + delete users[1].basicData.billing.billingInfo.company; |
| 51 | + |
| 52 | + await Promise.all([ |
| 53 | + createTeamspace(teamspace, [], undefined, false), |
| 54 | + createTeamspace(teamspace2, [], undefined, false), |
| 55 | + ]); |
| 56 | + |
| 57 | + await Promise.all(users.map(async (user, index) => { |
| 58 | + await createUser(user, [teamspace, teamspace2]); |
| 59 | + |
| 60 | + if (index % 2 === 0) { |
| 61 | + const loginRecords = times(5, () => generateLoginRecord(user.user)); |
| 62 | + await addLoginRecords(loginRecords); |
| 63 | + |
| 64 | + const [lastLoginRecord] = loginRecords.sort((a, b) => b.loginTime - a.loginTime); |
| 65 | + // eslint-disable-next-line no-param-reassign |
| 66 | + user.lastLogin = lastLoginRecord.loginTime; |
| 67 | + } |
| 68 | + |
| 69 | + if (index % 10 === 0) { |
| 70 | + await DBHandler.updateOne(ADMIN_DB, USERS_COL, { user: user.user }, { $set: { roles: [] } }); |
| 71 | + // eslint-disable-next-line no-param-reassign |
| 72 | + user.invalidUser = true; |
| 73 | + } |
| 74 | + })); |
| 75 | + |
| 76 | + return users; |
| 77 | +}; |
| 78 | + |
| 79 | +const runTest = () => { |
| 80 | + describe('Identify inactive users', () => { |
| 81 | + let users; |
| 82 | + |
| 83 | + beforeAll(async () => { |
| 84 | + await resetDB(); |
| 85 | + users = await setupData(); |
| 86 | + }); |
| 87 | + |
| 88 | + const formatDate = (date) => (date ? DayJS(date).format('DD/MM/YYYY') : ''); |
| 89 | + |
| 90 | + test('should provide a list of users that have not logged in for a specified number of months', async () => { |
| 91 | + const outFile = `${tmpDir}/${generateRandomString()}.csv`; |
| 92 | + const monthsOfInactivity = Math.round(generateRandomNumber(1, 5)); |
| 93 | + |
| 94 | + await IdentifyInactiveUsers.run(monthsOfInactivity, outFile); |
| 95 | + |
| 96 | + expect(fileExists(outFile)).toBeTruthy(); |
| 97 | + |
| 98 | + // first line is csv titles, last line is always empty |
| 99 | + const content = readFileSync(outFile).toString().split('\n').slice(1, -1); |
| 100 | + const res = content.map((str) => { |
| 101 | + const [user, firstName, lastName, email, company, lastLogin, teamspaceNumber] = str.split(','); |
| 102 | + return { user, firstName, lastName, email, company, lastLogin, teamspaceNumber }; |
| 103 | + }); |
| 104 | + |
| 105 | + const thresholdDate = new Date(); |
| 106 | + thresholdDate.setMonth(thresholdDate.getMonth() - monthsOfInactivity); |
| 107 | + |
| 108 | + const expectedResult = users.flatMap(({ user, invalidUser, basicData, lastLogin }) => { |
| 109 | + if (!invalidUser && (!lastLogin || lastLogin < thresholdDate)) { |
| 110 | + const { email, firstName, lastName, billing } = basicData; |
| 111 | + return { user, email, firstName, lastName, company: billing.billingInfo.company ?? '', lastLogin: formatDate(lastLogin), teamspaceNumber: '2' }; |
| 112 | + } |
| 113 | + |
| 114 | + return []; |
| 115 | + }); |
| 116 | + |
| 117 | + expect(res.length).toBe(expectedResult.length); |
| 118 | + expect(res.sort((a, b) => a.user.localeCompare(b.user))) |
| 119 | + .toEqual(expectedResult.sort((a, b) => a.user.localeCompare(b.user))); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}; |
| 123 | + |
| 124 | +describe(determineTestGroup(__filename), () => { |
| 125 | + runTest(); |
| 126 | + afterAll(disconnect); |
| 127 | +}); |
0 commit comments