2021-03-31 08:20:55 -04:00
|
|
|
const Path = require('path')
|
2019-05-28 07:04:44 -04:00
|
|
|
const chai = require('chai')
|
2019-11-18 09:37:05 -05:00
|
|
|
const sinon = require('sinon')
|
2019-05-28 07:04:44 -04:00
|
|
|
|
2021-03-31 08:20:55 -04:00
|
|
|
/*
|
|
|
|
* Chai configuration
|
|
|
|
*/
|
|
|
|
|
2020-05-22 06:34:57 -04:00
|
|
|
// add chai.should()
|
|
|
|
chai.should()
|
|
|
|
|
2019-05-28 07:04:44 -04: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 10:18:56 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
// Load promise support for chai
|
|
|
|
chai.use(require('chai-as-promised'))
|
|
|
|
|
2019-10-07 04:30:51 -04:00
|
|
|
// Do not truncate assertion errors
|
|
|
|
chai.config.truncateThreshold = 0
|
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
// add support for mongoose in sinon
|
|
|
|
require('sinon-mongoose')
|
2019-11-07 05:28:27 -05:00
|
|
|
|
2021-03-31 08:20:55 -04: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 03:52:58 -04:00
|
|
|
fatal: globalStubsSandbox.stub(),
|
|
|
|
},
|
2020-11-27 08:11:12 -05:00
|
|
|
}
|
|
|
|
|
2021-03-31 08:20:55 -04:00
|
|
|
/*
|
|
|
|
* Sandboxed module configuration
|
|
|
|
*/
|
|
|
|
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-11-27 08:11:12 -05:00
|
|
|
SandboxedModule.configure({
|
2022-02-07 06:46:38 -05:00
|
|
|
ignoreMissing: true,
|
2021-03-31 08:20:55 -04:00
|
|
|
requires: getSandboxedModuleRequires(),
|
|
|
|
globals: { Buffer, Promise, console, process },
|
2020-11-27 08:11:12 -05:00
|
|
|
})
|
2021-02-03 07:36:39 -05:00
|
|
|
|
2021-03-31 08:20:55 -04:00
|
|
|
function getSandboxedModuleRequires() {
|
|
|
|
const requires = {
|
2021-11-10 08:40:18 -05:00
|
|
|
'@overleaf/logger': globalStubs.logger,
|
2021-03-31 08:20:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const internalModules = [
|
|
|
|
'../../app/src/util/promises',
|
|
|
|
'../../app/src/Features/Errors/Errors',
|
2021-04-27 03:52:58 -04:00
|
|
|
'../../app/src/Features/Helpers/Mongo',
|
2021-03-31 08:20:55 -04:00
|
|
|
]
|
|
|
|
const externalLibs = [
|
|
|
|
'async',
|
2021-09-20 08:20:33 -04:00
|
|
|
'bull',
|
2021-03-31 08:20:55 -04:00
|
|
|
'json2csv',
|
|
|
|
'lodash',
|
|
|
|
'marked',
|
|
|
|
'moment',
|
|
|
|
'@overleaf/o-error',
|
|
|
|
'sanitize-html',
|
|
|
|
'sshpk',
|
|
|
|
'underscore',
|
2021-04-27 03:52:58 -04:00
|
|
|
'xml2js',
|
2021-03-31 08:20:55 -04: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 07:36:39 -05: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 08:20:55 -04: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 03:52:58 -04:00
|
|
|
},
|
2021-03-31 08:20:55 -04:00
|
|
|
}
|