From 44d0b947ce834d82ff3996ea05dec06d9e60c09d Mon Sep 17 00:00:00 2001 From: Mathias Jakobsen Date: Wed, 27 Mar 2024 09:17:26 +0000 Subject: [PATCH] Merge pull request #17553 from overleaf/mj-admin-history-ranges-enable [web] Add button for adding history ranges support in admin panel GitOrigin-RevId: fa85b25719fff753f0d0806ccb02ad2f9db2ee82 --- .../Features/Project/ProjectOptionsHandler.js | 12 ++++++++ .../src/Project/ProjectOptionsHandlerTests.js | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/services/web/app/src/Features/Project/ProjectOptionsHandler.js b/services/web/app/src/Features/Project/ProjectOptionsHandler.js index 58b1828ddd..7b6fe4118e 100644 --- a/services/web/app/src/Features/Project/ProjectOptionsHandler.js +++ b/services/web/app/src/Features/Project/ProjectOptionsHandler.js @@ -1,6 +1,7 @@ const { Project } = require('../../models/Project') const settings = require('@overleaf/settings') const { callbackify } = require('util') +const { db, ObjectId } = require('../../infrastructure/mongodb') const safeCompilers = ['xelatex', 'pdflatex', 'latex', 'lualatex'] const ProjectOptionsHandler = { @@ -62,6 +63,14 @@ const ProjectOptionsHandler = { const update = { $unset: { brandVariationId: 1 } } return Project.updateOne(conditions, update, {}) }, + + async enableHistoryRangesSupport(projectId) { + const conditions = { _id: new ObjectId(projectId) } + const update = { $set: { 'overleaf.history.rangesSupportEnabled': true } } + // NOTE: Updating the Mongoose model with the same query doesn't work. Maybe + // because rangesSupportEnabled is not part of the schema? + return db.projects.updateOne(conditions, update) + }, } module.exports = { @@ -74,5 +83,8 @@ module.exports = { unsetBrandVariationId: callbackify( ProjectOptionsHandler.unsetBrandVariationId ), + enableHistoryRangesSupport: callbackify( + ProjectOptionsHandler.enableHistoryRangesSupport + ), promises: ProjectOptionsHandler, } diff --git a/services/web/test/unit/src/Project/ProjectOptionsHandlerTests.js b/services/web/test/unit/src/Project/ProjectOptionsHandlerTests.js index 40326ea4a4..cb5cc2d5c8 100644 --- a/services/web/test/unit/src/Project/ProjectOptionsHandlerTests.js +++ b/services/web/test/unit/src/Project/ProjectOptionsHandlerTests.js @@ -17,6 +17,7 @@ const { expect } = require('chai') const modulePath = '../../../../app/src/Features/Project/ProjectOptionsHandler.js' const SandboxedModule = require('sandboxed-module') +const { ObjectId } = require('mongodb') describe('ProjectOptionsHandler', function () { const projectId = '4eecaffcbffa66588e000008' @@ -28,6 +29,12 @@ describe('ProjectOptionsHandler', function () { } this.projectModel.updateOne = sinon.stub().resolves() + this.db = { + projects: { + updateOne: sinon.stub().resolves(), + }, + } + this.handler = SandboxedModule.require(modulePath, { requires: { '../../models/Project': { Project: this.projectModel }, @@ -42,6 +49,7 @@ describe('ProjectOptionsHandler', function () { { imageName: 'texlive-1234.5', imageDesc: 'test image 1' }, ], }, + '../../infrastructure/mongodb': { db: this.db, ObjectId }, }, }) }) @@ -181,6 +189,28 @@ describe('ProjectOptionsHandler', function () { }) }) + describe('setting the rangesSupportEnabled', function () { + it('should perform and update on mongo', async function () { + await this.handler.promises.enableHistoryRangesSupport(projectId) + sinon.assert.calledWith( + this.db.projects.updateOne, + { _id: new ObjectId(projectId) }, + { $set: { 'overleaf.history.rangesSupportEnabled': true } } + ) + }) + + describe('when mongo update error occurs', function () { + beforeEach(function () { + this.db.projects.updateOne = sinon.stub().yields('error') + }) + + it('should be rejected', async function () { + expect(this.handler.promises.enableHistoryRangesSupport(projectId)).to + .be.rejected + }) + }) + }) + describe('unsetting the brandVariationId', function () { it('should perform and update on mongo', async function () { await this.handler.promises.unsetBrandVariationId(projectId)