Files
overleaf-cep/services/web/test/acceptance/src/RemoveDeletedUsersFromTokenAccessRefsTests.mjs
T
Brian Gough 89898b0e9a move migrations to shared location (#28306)
* fix: correct typedef for Document in helpers.mjs

* add move-migrations codemod

* update migration paths to use shared migrations directory

* move migrations to shared location

* fix: update Dockerfile and docker-compose.ci.yml to include migrations directory

* feat: add migrations tool to workspaces in package.json

* [monorepo] Fix order of docker ignore rules

* [web] remove unused docker ignore file

* [monorepo] replace old references to migrations folder

* [server-ce] copy migrations from new place

* [migrations] Inline web scripts

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] move three web scripts over

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] add missing collection

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] remove lodash dependency

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] avoid mongodb-legacy dependency

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [monorepo] run migrations from tools/migrations

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] simplify migration for adding gitBridge feature to users

* [monorepo] run migrations from tests in all the services

* [migrations] add Jenkins pipeline for linting/formatting

* [monorepo] fixup running web migrations everywhere

* [monorepo] trigger Jenkins builds on changes to mongo migrations

* [migrations] add Jenkins pipeline for linting/formatting

* [monorepo] build scripts: update devDependencies before deps scanning

* [monorepo] build scripts: formerly depend on tools/migrations

* [monorepo] run eslint on .mjs files

* [migrations] enable more eslint rules and fix all the errors

* [rake] fix migrations:list task

---------

Co-authored-by: Jakob Ackermann <jakob.ackermann@overleaf.com>
GitOrigin-RevId: 14cf69cc1b9405bbc75adbb9a000e555500e0614
2025-10-16 08:07:37 +00:00

149 lines
4.3 KiB
JavaScript

import { db, ObjectId } from '../../../app/src/infrastructure/mongodb.js'
import { promisify } from 'node:util'
import { exec } from 'node:child_process'
import logger from '@overleaf/logger'
import { expect } from 'chai'
describe('RemoveDeletedUsersFromTokenAccessRefsTests', function () {
const userId1 = new ObjectId()
const userId2 = new ObjectId()
const userId3 = new ObjectId()
let insertedUsersCount
beforeEach('insert users', async function () {
const users = await db.users.insertMany([
{ _id: userId1, email: 'user1@example.com' },
])
insertedUsersCount = users.insertedCount
})
const projectId1 = new ObjectId('65d726e807c024c8db43be22')
const projectId2 = new ObjectId('65d726e807c024c8db43be23')
const projectId3 = new ObjectId('65d726e807c024c8db43be24')
const projectId4 = new ObjectId('65d726e807c024c8db43be25')
beforeEach('insert projects', async function () {
await db.projects.insertMany([
{
_id: projectId1,
tokenAccessReadAndWrite_refs: [userId1],
tokenAccessReadOnly_refs: [],
},
{
_id: projectId2,
tokenAccessReadAndWrite_refs: [userId2],
tokenAccessReadOnly_refs: [],
},
{
_id: projectId3,
tokenAccessReadAndWrite_refs: [userId3],
},
{
_id: projectId4,
},
])
})
let stdOut
const runScript = async () => {
let result
try {
result = await promisify(exec)(
'cd ../../tools/migrations && east migrate -t saas --force 20240220130452_remove_deleted_users_from_token_access_refs'
)
} catch (error) {
// dump details like exit code, stdErr and stdOut
logger.error({ error }, 'script failed')
throw error
}
const { stdout } = result
stdOut = stdout
expect(stdOut).to.match(new RegExp(`User ids count: ${insertedUsersCount}`))
}
describe('dry-run=false', function () {
beforeEach('run script', async function () {
await runScript('--dry-run=false')
expect(stdOut).to.not.match(/dry run/i)
})
it('should show current user id to be removed', function () {
expect(stdOut).to.match(
new RegExp(
`Found deleted user id: ${userId2.toString()} in project: ${projectId2.toString()}`
)
)
expect(stdOut).to.match(
new RegExp(
`Removing deleted ${userId2.toString()} from all projects \\(found in project ${projectId2.toString()}\\)`
)
)
expect(stdOut).to.match(
new RegExp(
`Found deleted user id: ${userId3.toString()} in project: ${projectId3.toString()}`
)
)
expect(stdOut).to.match(
new RegExp(
`Removing deleted ${userId3.toString()} from all projects \\(found in project ${projectId3.toString()}\\)`
)
)
})
it('should show fixed projects with non-existing token access fields', function () {
expect(stdOut)
.to.match(
new RegExp(
`Fixed non-existing token access fields in project ${projectId3.toString()}`
)
)
.and.match(
new RegExp(
`Fixed non-existing token access fields in project ${projectId4.toString()}`
)
)
})
it('should show the deleted user ids (and their count) that were removed', function () {
expect(stdOut).to.match(
new RegExp(
`Deleted user ids \\(2\\)\\n${userId2.toString()}\\n${userId3.toString()}`
)
)
})
it('should show the project ids (and their count) that were fixed', function () {
expect(stdOut).to.match(
new RegExp(
`Projects with deleted user ids \\(2\\)\\n${projectId2.toString()}\\n${projectId3.toString()}`
)
)
})
it('should fix the token access fields of projects', async function () {
const [, ...fixedProjects] = await db.projects
.find({}, { $sort: { _id: 1 } })
.toArray()
expect(fixedProjects).to.deep.equal([
{
_id: projectId2,
tokenAccessReadAndWrite_refs: [],
tokenAccessReadOnly_refs: [],
},
{
_id: projectId3,
tokenAccessReadAndWrite_refs: [],
tokenAccessReadOnly_refs: [],
},
{
_id: projectId4,
tokenAccessReadOnly_refs: [],
tokenAccessReadAndWrite_refs: [],
},
])
})
})
})