2021-03-31 12:20:55 +00:00
|
|
|
const Path = require('path')
|
2019-05-28 11:04:44 +00:00
|
|
|
const chai = require('chai')
|
2019-11-18 14:37:05 +00:00
|
|
|
const sinon = require('sinon')
|
2019-05-28 11:04:44 +00:00
|
|
|
|
2021-03-31 12:20:55 +00:00
|
|
|
/*
|
|
|
|
* Chai configuration
|
|
|
|
*/
|
|
|
|
|
2020-05-22 10:34:57 +00:00
|
|
|
// add chai.should()
|
|
|
|
chai.should()
|
|
|
|
|
2019-05-28 11:04:44 +00:00
|
|
|
// Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
|
|
|
|
// has a nicer failure messages
|
|
|
|
chai.use(require('sinon-chai'))
|
2019-07-18 14:18:56 +00:00
|
|
|
|
2019-08-28 12:59:41 +00:00
|
|
|
// Load promise support for chai
|
|
|
|
chai.use(require('chai-as-promised'))
|
|
|
|
|
2019-10-07 08:30:51 +00:00
|
|
|
// Do not truncate assertion errors
|
|
|
|
chai.config.truncateThreshold = 0
|
|
|
|
|
2019-07-18 14:18:56 +00:00
|
|
|
// add support for mongoose in sinon
|
|
|
|
require('sinon-mongoose')
|
2019-11-07 10:28:27 +00:00
|
|
|
|
2024-02-26 13:44:01 +00:00
|
|
|
// ensure every ObjectId has the id string as a property for correct comparisons
|
|
|
|
require('mongodb').ObjectId.cacheHexString = true
|
|
|
|
|
2021-03-31 12:20:55 +00:00
|
|
|
/*
|
|
|
|
* Global stubs
|
|
|
|
*/
|
|
|
|
const globalStubsSandbox = sinon.createSandbox()
|
|
|
|
const globalStubs = {
|
|
|
|
logger: {
|
|
|
|
debug: globalStubsSandbox.stub(),
|
|
|
|
info: globalStubsSandbox.stub(),
|
|
|
|
log: globalStubsSandbox.stub(),
|
|
|
|
warn: globalStubsSandbox.stub(),
|
|
|
|
err: globalStubsSandbox.stub(),
|
|
|
|
error: globalStubsSandbox.stub(),
|
2021-04-27 07:52:58 +00:00
|
|
|
fatal: globalStubsSandbox.stub(),
|
|
|
|
},
|
2020-11-27 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 12:20:55 +00:00
|
|
|
/*
|
|
|
|
* Sandboxed module configuration
|
|
|
|
*/
|
|
|
|
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-11-27 13:11:12 +00:00
|
|
|
SandboxedModule.configure({
|
2022-02-07 11:46:38 +00:00
|
|
|
ignoreMissing: true,
|
2021-03-31 12:20:55 +00:00
|
|
|
requires: getSandboxedModuleRequires(),
|
2023-02-28 13:26:18 +00:00
|
|
|
globals: {
|
2024-05-29 12:19:10 +00:00
|
|
|
AbortController,
|
2023-02-28 13:26:18 +00:00
|
|
|
AbortSignal,
|
|
|
|
Buffer,
|
|
|
|
Promise,
|
|
|
|
console,
|
|
|
|
process,
|
|
|
|
URL,
|
|
|
|
TextEncoder,
|
|
|
|
TextDecoder,
|
|
|
|
},
|
2020-11-27 13:11:12 +00:00
|
|
|
})
|
2021-02-03 12:36:39 +00:00
|
|
|
|
2021-03-31 12:20:55 +00:00
|
|
|
function getSandboxedModuleRequires() {
|
|
|
|
const requires = {
|
2021-11-10 13:40:18 +00:00
|
|
|
'@overleaf/logger': globalStubs.logger,
|
2021-03-31 12:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const internalModules = [
|
|
|
|
'../../app/src/Features/Errors/Errors',
|
2021-04-27 07:52:58 +00:00
|
|
|
'../../app/src/Features/Helpers/Mongo',
|
2021-03-31 12:20:55 +00:00
|
|
|
]
|
|
|
|
const externalLibs = [
|
|
|
|
'async',
|
2021-09-20 12:20:33 +00:00
|
|
|
'bull',
|
2021-03-31 12:20:55 +00:00
|
|
|
'json2csv',
|
|
|
|
'lodash',
|
|
|
|
'marked',
|
|
|
|
'moment',
|
|
|
|
'@overleaf/o-error',
|
|
|
|
'sanitize-html',
|
|
|
|
'sshpk',
|
2021-04-27 07:52:58 +00:00
|
|
|
'xml2js',
|
2024-02-26 13:44:01 +00:00
|
|
|
'mongodb',
|
2021-03-31 12:20:55 +00:00
|
|
|
]
|
|
|
|
for (const modulePath of internalModules) {
|
|
|
|
requires[Path.resolve(__dirname, modulePath)] = require(modulePath)
|
|
|
|
}
|
|
|
|
for (const lib of externalLibs) {
|
|
|
|
requires[lib] = require(lib)
|
|
|
|
}
|
|
|
|
return requires
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mocha hooks
|
|
|
|
*/
|
|
|
|
|
2021-02-03 12:36:39 +00:00
|
|
|
// sandboxed-module somehow registers every fake module it creates in this
|
|
|
|
// module's children array, which uses quite a big amount of memory. We'll take
|
|
|
|
// a copy of the module children array and restore it after each test so that
|
|
|
|
// the garbage collector has a chance to reclaim the fake modules.
|
|
|
|
let initialModuleChildren
|
|
|
|
|
2021-03-31 12:20:55 +00:00
|
|
|
exports.mochaHooks = {
|
|
|
|
beforeAll() {
|
|
|
|
// Record initial module children
|
|
|
|
initialModuleChildren = module.children.slice()
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeEach() {
|
|
|
|
// Install logger stub
|
|
|
|
this.logger = globalStubs.logger
|
|
|
|
},
|
|
|
|
|
|
|
|
afterEach() {
|
|
|
|
// Delete leaking sandboxed modules
|
|
|
|
module.children = initialModuleChildren.slice()
|
|
|
|
|
|
|
|
// Reset global stubs
|
|
|
|
globalStubsSandbox.reset()
|
|
|
|
|
|
|
|
// Restore other stubs
|
|
|
|
sinon.restore()
|
2021-04-27 07:52:58 +00:00
|
|
|
},
|
2021-03-31 12:20:55 +00:00
|
|
|
}
|