Merge pull request #27936 from overleaf/em-unit-tests-mongo

Make Mongo available to unit tests in all services

GitOrigin-RevId: b65bbb69883d5bba31d09802b89f35bdc523fe4d
This commit is contained in:
Eric Mc Sween
2025-08-25 08:05:19 +00:00
committed by Copybot
parent 7dc7b322a8
commit 8f0913fafe
57 changed files with 404 additions and 162 deletions
@@ -20,18 +20,7 @@ function client(feature) {
async function cleanupTestRedis() {
const rclient = client()
ensureTestRedis(rclient)
await rclient.flushall()
}
function ensureTestRedis(rclient) {
const host = rclient.options.host
const env = process.env.NODE_ENV
if (host !== 'redis_test' || env !== 'test') {
throw new Error(
`Refusing to clear Redis instance '${host}' in environment '${env}'`
)
}
await redis.cleanupTestRedis(rclient)
}
module.exports = { client, cleanupTestRedis }
+3 -23
View File
@@ -1,6 +1,7 @@
const mongodb = require('mongodb-legacy')
const OError = require('@overleaf/o-error')
const Settings = require('@overleaf/settings')
const MongoUtils = require('@overleaf/mongo-utils')
const Mongoose = require('./Mongoose')
const { addConnectionDrainer } = require('./GracefulShutdown')
@@ -93,32 +94,11 @@ async function getCollectionNames() {
}
async function cleanupTestDatabase() {
ensureTestDatabase()
const collectionNames = await getCollectionNames()
const collections = []
for (const name of collectionNames) {
if (name in db && name !== 'migrations') {
collections.push(db[name])
}
}
await Promise.all(collections.map(coll => coll.deleteMany({})))
await MongoUtils.cleanupTestDatabase(mongoClient)
}
async function dropTestDatabase() {
ensureTestDatabase()
await mongoClient.db().dropDatabase()
}
function ensureTestDatabase() {
const internalDb = mongoClient.db()
const dbName = internalDb.databaseName
const env = process.env.NODE_ENV
if (dbName !== 'test-overleaf' || env !== 'test') {
throw new OError(
`Refusing to clear database '${dbName}' in environment '${env}'`
)
}
await MongoUtils.dropTestDatabase(mongoClient)
}
/**