From b0d434250d64de4c3d95ec34702d848efd4d8d9b Mon Sep 17 00:00:00 2001 From: Ciaran Welch Date: Tue, 18 Jun 2024 19:12:21 +1000 Subject: [PATCH] refactor: Conver several files to ESM --- imports/ldap/getLDAPUsers.js | 16 ++++++++-------- imports/ldap/loadLDAPSettings.js | 6 +++--- imports/ldap/saveUsers.js | 17 ++++++++--------- imports/ldap/transformUser.js | 6 +++--- imports/priority.js | 14 +++++++------- tests/unit/imports/ActionItem.test.js | 9 +-------- .../imports/client/ResponsiblePreparer.test.js | 3 +-- tests/unit/imports/helpers/string-utils.test.js | 1 - tests/unit/imports/helpers/subElements.test.js | 1 - tests/unit/imports/ldap/getLDAPUsers.test.js | 1 - 10 files changed, 31 insertions(+), 43 deletions(-) diff --git a/imports/ldap/getLDAPUsers.js b/imports/ldap/getLDAPUsers.js index eab71ffd9..fce83db10 100644 --- a/imports/ldap/getLDAPUsers.js +++ b/imports/ldap/getLDAPUsers.js @@ -1,10 +1,10 @@ -const ldap = require("ldapjs"); -const _ = require("lodash"); +import { createClient } from "ldapjs"; +import { get } from "lodash"; const _createLDAPClient = (settings) => new Promise((resolve, reject) => { try { - const client = ldap.createClient({ + const client = createClient({ url: settings.serverUrl, }); @@ -73,16 +73,16 @@ const _fetchLDAPUsers = (connection) => { const client = connection.client; const settings = connection.settings; const base = settings.serverDn; - const searchDn = _.get(settings, "propertyMap.username", "cn"); - const userLongNameAttribute = _.get( + const searchDn = get(settings, "propertyMap.username", "cn"); + const userLongNameAttribute = get( settings, "propertyMap.longname", searchDn, ); - const emailAttribute = _.get(settings, "propertyMap.email", searchDn); + const emailAttribute = get(settings, "propertyMap.email", searchDn); const filter = `(&(${searchDn}=*)${settings.searchFilter})`; const scope = "sub"; - const allowListedFields = _.get(settings, "allowListedFields", []); + const allowListedFields = get(settings, "allowListedFields", []); const attributes = allowListedFields.concat([ "userAccountControl", searchDn, @@ -149,4 +149,4 @@ const getLDAPUsers = (settings) => .catch(reject); }); -module.exports = getLDAPUsers; +export default getLDAPUsers; diff --git a/imports/ldap/loadLDAPSettings.js b/imports/ldap/loadLDAPSettings.js index 2ae0405b2..cf19d67d6 100644 --- a/imports/ldap/loadLDAPSettings.js +++ b/imports/ldap/loadLDAPSettings.js @@ -1,8 +1,8 @@ -const fs = require("fs"); +import { readFile } from "fs"; const _readSettingsFile = (filename) => new Promise((resolve, reject) => { - fs.readFile(filename, "utf8", (error, data) => { + readFile(filename, "utf8", (error, data) => { if (error) { reject(`Could not read settings file "${filename}"`); } else { @@ -38,4 +38,4 @@ const loadLDAPSettings = (filename) => .catch(reject); }); -module.exports = loadLDAPSettings; +export default loadLDAPSettings; diff --git a/imports/ldap/saveUsers.js b/imports/ldap/saveUsers.js index 1fe9e3fe5..2dc894834 100644 --- a/imports/ldap/saveUsers.js +++ b/imports/ldap/saveUsers.js @@ -1,12 +1,11 @@ -let mongo = require("mongodb").MongoClient, - mongoUriParser = require("mongo-uri"), - transformUser = require("./transformUser"); - -import { _ } from "lodash"; +import { MongoClient as mongo } from "mongodb"; +import { parse } from "mongo-uri"; +import transformUser from "./transformUser"; +import { map, forEach } from "lodash"; import { Random } from "../../tests/performance/fixtures/lib/random"; const _transformUsers = (settings, users) => - _.map(users, (user) => transformUser(settings, user)); + map(users, (user) => transformUser(settings, user)); const _connectMongo = (mongoUrl) => mongo.connect(mongoUrl); @@ -25,12 +24,12 @@ const _insertUsers = (client, mongoUri, users) => { return new Promise((resolve, reject) => { try { - const mongoConnection = mongoUriParser.parse(mongoUri); + const mongoConnection = parse(mongoUri); const bulk = client .db(mongoConnection.database) .collection("users") .initializeUnorderedBulkOp(); - _.forEach(users, (user) => { + forEach(users, (user) => { if (user?.username && user.emails[0] && user.emails[0].address) { user.isLDAPuser = true; const usrRegExp = new RegExp( @@ -93,4 +92,4 @@ const saveUsers = (settings, mongoUrl, users) => { }); }; -module.exports = saveUsers; +export default saveUsers; diff --git a/imports/ldap/transformUser.js b/imports/ldap/transformUser.js index b0b15f587..f6599be4e 100644 --- a/imports/ldap/transformUser.js +++ b/imports/ldap/transformUser.js @@ -1,6 +1,6 @@ -import { _ } from "lodash"; +import { pick, without } from "lodash"; -module.exports = (ldapSettings, userData) => { +export default (ldapSettings, userData) => { ldapSettings.propertyMap = ldapSettings.propertyMap || {}; const usernameAttribute = ldapSettings.searchDn || ldapSettings.propertyMap.username || "cn", @@ -32,7 +32,7 @@ module.exports = (ldapSettings, userData) => { isInactive: false, emails: tmpEMailArray, username: username.toLowerCase(), - profile: _.pick(userData, _.without(profileFields, "mail")), + profile: pick(userData, without(profileFields, "mail")), }; // copy over the LDAP user's long name from "cn" field to the meteor accounts diff --git a/imports/priority.js b/imports/priority.js index 38d1915de..267723185 100644 --- a/imports/priority.js +++ b/imports/priority.js @@ -1,6 +1,5 @@ -import { i18n } from "meteor/universe:i18n"; - -const assert = require("assert"); +import { __ } from "meteor/universe:i18n"; +import assert from "assert"; // #I18N - Attention: the below strings with longer texts will be never be used // in UI! Instead they will be pulled from translation language files via @@ -23,7 +22,7 @@ const PRIORITY_MAP = { * The priority level is represented as an integer between 1 and 5, where 1 is * the highest priority and 5 is the lowest priority. */ -export class Priority { +class Priority { static GET_DEFAULT_PRIORITY() { return new Priority(3); } @@ -50,14 +49,15 @@ export class Priority { if (Object.prototype.hasOwnProperty.call(PRIORITY_MAP, this.value)) { switch (this.value) { case 1: - return i18n.__("Item.Priorities.high"); + return __("Item.Priorities.high"); case 3: - return i18n.__("Item.Priorities.medium"); + return __("Item.Priorities.medium"); case 5: - return i18n.__("Item.Priorities.low"); + return __("Item.Priorities.low"); } return PRIORITY_MAP[this.value]; } throw new Error(`illegal-state: Unknown priority ${this.value}`); } } +export default Priority; diff --git a/tests/unit/imports/ActionItem.test.js b/tests/unit/imports/ActionItem.test.js index 9e00a29f9..7dc3edde3 100644 --- a/tests/unit/imports/ActionItem.test.js +++ b/tests/unit/imports/ActionItem.test.js @@ -7,18 +7,11 @@ const doNothing = () => {}; class MeteorError {} const { Priority } = await esmock("../../../imports/priority", { - "meteor/universe:i18n": { - i18n: { - setLocale: () => sinon.stub(), - getLocale: () => sinon.stub(), - __: () => sinon.stub(), - }, -} + "meteor/universe:i18n": { __: () => sinon.stub() } }, {}, { isModuleNotFoundError: false }); - const { InfoItem } = await esmock("../../../imports/infoitem", { "meteor/meteor": { Meteor: { diff --git a/tests/unit/imports/client/ResponsiblePreparer.test.js b/tests/unit/imports/client/ResponsiblePreparer.test.js index 83fe60b83..b9e02da3b 100644 --- a/tests/unit/imports/client/ResponsiblePreparer.test.js +++ b/tests/unit/imports/client/ResponsiblePreparer.test.js @@ -1,7 +1,6 @@ +import { faker } from "@faker-js/faker"; import { expect } from "chai"; import _ from "lodash"; -const { faker } = require("@faker-js/faker"); - import { ParticipantsPreparer } from "../../../../imports/client/ParticipantsPreparer"; const generateId = () => { diff --git a/tests/unit/imports/helpers/string-utils.test.js b/tests/unit/imports/helpers/string-utils.test.js index 1afc0c919..f4261d085 100644 --- a/tests/unit/imports/helpers/string-utils.test.js +++ b/tests/unit/imports/helpers/string-utils.test.js @@ -1,5 +1,4 @@ import { expect } from "chai"; - import { StringUtils } from "../../../../imports/helpers/string-utils"; // skipcq: JS-0241 diff --git a/tests/unit/imports/helpers/subElements.test.js b/tests/unit/imports/helpers/subElements.test.js index 4ea4e560b..d3e4ac725 100644 --- a/tests/unit/imports/helpers/subElements.test.js +++ b/tests/unit/imports/helpers/subElements.test.js @@ -1,5 +1,4 @@ import { expect } from "chai"; - import { subElementsHelper } from "../../../../imports/helpers/subElements"; // skipcq JS-0241 describe("subElementsHelper", function () { diff --git a/tests/unit/imports/ldap/getLDAPUsers.test.js b/tests/unit/imports/ldap/getLDAPUsers.test.js index 7cfca3b67..dbcf1de60 100644 --- a/tests/unit/imports/ldap/getLDAPUsers.test.js +++ b/tests/unit/imports/ldap/getLDAPUsers.test.js @@ -1,7 +1,6 @@ import { expect } from "chai"; import proxyquire from "proxyquire"; import sinon from "sinon"; - import asyncStubs from "../../../support/lib/asyncStubs"; const ldap = {