Skip to content

Commit

Permalink
refactor: Conver several files to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
bubonicfred committed Jun 18, 2024
1 parent 6fc95f6 commit b0d4342
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 43 deletions.
16 changes: 8 additions & 8 deletions imports/ldap/getLDAPUsers.js
Original file line number Diff line number Diff line change
@@ -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,
});

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -149,4 +149,4 @@ const getLDAPUsers = (settings) =>
.catch(reject);
});

module.exports = getLDAPUsers;
export default getLDAPUsers;
6 changes: 3 additions & 3 deletions imports/ldap/loadLDAPSettings.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -38,4 +38,4 @@ const loadLDAPSettings = (filename) =>
.catch(reject);
});

module.exports = loadLDAPSettings;
export default loadLDAPSettings;
17 changes: 8 additions & 9 deletions imports/ldap/saveUsers.js
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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(
Expand Down Expand Up @@ -93,4 +92,4 @@ const saveUsers = (settings, mongoUrl, users) => {
});
};

module.exports = saveUsers;
export default saveUsers;
6 changes: 3 additions & 3 deletions imports/ldap/transformUser.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions imports/priority.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
Expand All @@ -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;
9 changes: 1 addition & 8 deletions tests/unit/imports/ActionItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/imports/client/ResponsiblePreparer.test.js
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand Down
1 change: 0 additions & 1 deletion tests/unit/imports/helpers/string-utils.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { expect } from "chai";

import { StringUtils } from "../../../../imports/helpers/string-utils";

// skipcq: JS-0241
Expand Down
1 change: 0 additions & 1 deletion tests/unit/imports/helpers/subElements.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { expect } from "chai";

import { subElementsHelper } from "../../../../imports/helpers/subElements";
// skipcq JS-0241
describe("subElementsHelper", function () {
Expand Down
1 change: 0 additions & 1 deletion tests/unit/imports/ldap/getLDAPUsers.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from "chai";
import proxyquire from "proxyquire";
import sinon from "sinon";

import asyncStubs from "../../../support/lib/asyncStubs";

const ldap = {
Expand Down

0 comments on commit b0d4342

Please sign in to comment.