From b290510faf88bdff7002946b6b85d26ad9dd4c26 Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Mon, 28 Oct 2024 12:39:16 +0100 Subject: [PATCH] Merge pull request #21394 from overleaf/msm-esm-user-activation [web] Migrate `modules/user-activate` to ESM GitOrigin-RevId: e5ab3e9c53ea2d3f4ddfbc2200a889b8bb0db83c --- ...ntroller.js => UserActivateController.mjs} | 13 +++++--- ...tivateRouter.js => UserActivateRouter.mjs} | 10 +++--- .../user-activate/{index.js => index.mjs} | 4 +-- ...sts.js => UserActivateControllerTests.mjs} | 31 +++++++++---------- 4 files changed, 30 insertions(+), 28 deletions(-) rename services/web/modules/user-activate/app/src/{UserActivateController.js => UserActivateController.mjs} (81%) rename services/web/modules/user-activate/app/src/{UserActivateRouter.js => UserActivateRouter.mjs} (65%) rename services/web/modules/user-activate/{index.js => index.mjs} (59%) rename services/web/modules/user-activate/test/unit/src/{UserActivateControllerTests.js => UserActivateControllerTests.mjs} (83%) diff --git a/services/web/modules/user-activate/app/src/UserActivateController.js b/services/web/modules/user-activate/app/src/UserActivateController.mjs similarity index 81% rename from services/web/modules/user-activate/app/src/UserActivateController.js rename to services/web/modules/user-activate/app/src/UserActivateController.mjs index 5399a0cf6f..644fae42db 100644 --- a/services/web/modules/user-activate/app/src/UserActivateController.js +++ b/services/web/modules/user-activate/app/src/UserActivateController.mjs @@ -1,9 +1,12 @@ -const Path = require('path') -const UserGetter = require('../../../../app/src/Features/User/UserGetter') -const UserRegistrationHandler = require('../../../../app/src/Features/User/UserRegistrationHandler') -const ErrorController = require('../../../../app/src/Features/Errors/ErrorController') +import Path from 'node:path' +import { fileURLToPath } from 'node:url' +import UserGetter from '../../../../app/src/Features/User/UserGetter.js' +import UserRegistrationHandler from '../../../../app/src/Features/User/UserRegistrationHandler.js' +import ErrorController from '../../../../app/src/Features/Errors/ErrorController.js' -module.exports = { +const __dirname = Path.dirname(fileURLToPath(import.meta.url)) + +export default { registerNewUser(req, res, next) { res.render(Path.resolve(__dirname, '../views/user/register')) }, diff --git a/services/web/modules/user-activate/app/src/UserActivateRouter.js b/services/web/modules/user-activate/app/src/UserActivateRouter.mjs similarity index 65% rename from services/web/modules/user-activate/app/src/UserActivateRouter.js rename to services/web/modules/user-activate/app/src/UserActivateRouter.mjs index a9088dd544..402d05c562 100644 --- a/services/web/modules/user-activate/app/src/UserActivateRouter.js +++ b/services/web/modules/user-activate/app/src/UserActivateRouter.mjs @@ -1,9 +1,9 @@ -const logger = require('@overleaf/logger') -const UserActivateController = require('./UserActivateController') -const AuthenticationController = require('../../../../app/src/Features/Authentication/AuthenticationController') -const AuthorizationMiddleware = require('../../../../app/src/Features/Authorization/AuthorizationMiddleware') +import logger from '@overleaf/logger' +import UserActivateController from './UserActivateController.mjs' +import AuthenticationController from '../../../../app/src/Features/Authentication/AuthenticationController.js' +import AuthorizationMiddleware from '../../../../app/src/Features/Authorization/AuthorizationMiddleware.js' -module.exports = { +export default { apply(webRouter) { logger.debug({}, 'Init UserActivate router') diff --git a/services/web/modules/user-activate/index.js b/services/web/modules/user-activate/index.mjs similarity index 59% rename from services/web/modules/user-activate/index.js rename to services/web/modules/user-activate/index.mjs index d6ef502eb8..ca87af1582 100644 --- a/services/web/modules/user-activate/index.js +++ b/services/web/modules/user-activate/index.mjs @@ -1,4 +1,4 @@ -const UserActivateRouter = require('./app/src/UserActivateRouter') +import UserActivateRouter from './app/src/UserActivateRouter.mjs' /** * @import { WebModule } from "../../types/web-module" @@ -9,4 +9,4 @@ const UserActivateModule = { router: UserActivateRouter, } -module.exports = UserActivateModule +export default UserActivateModule diff --git a/services/web/modules/user-activate/test/unit/src/UserActivateControllerTests.js b/services/web/modules/user-activate/test/unit/src/UserActivateControllerTests.mjs similarity index 83% rename from services/web/modules/user-activate/test/unit/src/UserActivateControllerTests.js rename to services/web/modules/user-activate/test/unit/src/UserActivateControllerTests.mjs index 9259b48043..8e4f37e886 100644 --- a/services/web/modules/user-activate/test/unit/src/UserActivateControllerTests.js +++ b/services/web/modules/user-activate/test/unit/src/UserActivateControllerTests.mjs @@ -1,15 +1,16 @@ -const Path = require('path') -const SandboxedModule = require('sandboxed-module') -const sinon = require('sinon') +import Path from 'node:path' +import { fileURLToPath } from 'node:url' +import { strict as esmock } from 'esmock' +import sinon from 'sinon' + +const __dirname = Path.dirname(fileURLToPath(import.meta.url)) + +const MODULE_PATH = '../../../app/src/UserActivateController.mjs' -const MODULE_PATH = Path.join( - __dirname, - '../../../app/src/UserActivateController.js' -) const VIEW_PATH = Path.join(__dirname, '../../../app/views/user/activate') describe('UserActivateController', function () { - beforeEach(function () { + beforeEach(async function () { this.user = { _id: (this.user_id = 'kwjewkl'), features: {}, @@ -19,14 +20,12 @@ describe('UserActivateController', function () { this.UserGetter = { getUser: sinon.stub() } this.UserRegistrationHandler = {} this.ErrorController = { notFound: sinon.stub() } - this.UserActivateController = SandboxedModule.require(MODULE_PATH, { - requires: { - '../../../../app/src/Features/User/UserGetter': this.UserGetter, - '../../../../app/src/Features/User/UserRegistrationHandler': - this.UserRegistrationHandler, - '../../../../app/src/Features/Errors/ErrorController': - this.ErrorController, - }, + this.UserActivateController = await esmock(MODULE_PATH, { + '../../../../../app/src/Features/User/UserGetter.js': this.UserGetter, + '../../../../../app/src/Features/User/UserRegistrationHandler.js': + this.UserRegistrationHandler, + '../../../../../app/src/Features/Errors/ErrorController.js': + this.ErrorController, }) this.req = { body: {},