overleaf/services/web/test/acceptance/src/mocks/MockDocstoreApi.js
Mathias Jakobsen c371732e6e Merge pull request #16186 from overleaf/mj-mongo-object-id
[web] Use constructor for ObjectId

GitOrigin-RevId: 9eb8b377ea599605b72af237d1ab12f4d8287162
2023-12-19 09:04:02 +00:00

108 lines
3.1 KiB
JavaScript

const { db, ObjectId } = require('../../../../app/src/infrastructure/mongodb')
const AbstractMockApi = require('./AbstractMockApi')
class MockDocstoreApi extends AbstractMockApi {
reset() {
this.docs = {}
}
createLegacyDeletedDoc(projectId, docId) {
if (!this.docs[projectId]) {
this.docs[projectId] = {}
}
this.docs[projectId][docId] = {
lines: [],
version: 1,
ranges: {},
deleted: true,
}
}
getDeletedDocs(projectId) {
return Object.entries(this.docs[projectId] || {})
.filter(([_, doc]) => doc.deleted)
.map(([docId, doc]) => {
return { _id: docId, name: doc.name }
})
}
applyRoutes() {
this.app.post('/project/:projectId/doc/:docId', (req, res) => {
const { projectId, docId } = req.params
const { lines, version, ranges } = req.body
if (this.docs[projectId] == null) {
this.docs[projectId] = {}
}
if (this.docs[projectId][docId] == null) {
this.docs[projectId][docId] = {}
}
const { version: oldVersion, deleted } = this.docs[projectId][docId]
this.docs[projectId][docId] = { lines, version, ranges, deleted }
if (this.docs[projectId][docId].rev == null) {
this.docs[projectId][docId].rev = 0
}
this.docs[projectId][docId].rev += 1
this.docs[projectId][docId]._id = docId
res.json({
modified: oldVersion !== version,
rev: this.docs[projectId][docId].rev,
})
})
this.app.get('/project/:projectId/doc', (req, res) => {
res.json(Object.values(this.docs[req.params.projectId] || {}))
})
this.app.get('/project/:projectId/doc-deleted', (req, res) => {
res.json(this.getDeletedDocs(req.params.projectId))
})
this.app.get('/project/:projectId/doc/:docId', (req, res) => {
const { projectId, docId } = req.params
const doc = this.docs[projectId][docId]
if (!doc || (doc.deleted && !req.query.include_deleted)) {
res.sendStatus(404)
} else {
res.json(doc)
}
})
this.app.get('/project/:projectId/doc/:docId/deleted', (req, res) => {
const { projectId, docId } = req.params
if (!this.docs[projectId] || !this.docs[projectId][docId]) {
res.sendStatus(404)
} else {
res.json({ deleted: Boolean(this.docs[projectId][docId].deleted) })
}
})
this.app.patch('/project/:projectId/doc/:docId', (req, res) => {
const { projectId, docId } = req.params
if (!this.docs[projectId]) {
res.sendStatus(404)
} else if (!this.docs[projectId][docId]) {
res.sendStatus(404)
} else {
Object.assign(this.docs[projectId][docId], req.body)
res.sendStatus(204)
}
})
this.app.post('/project/:projectId/destroy', async (req, res) => {
const { projectId } = req.params
delete this.docs[projectId]
await db.docs.deleteMany({ project_id: new ObjectId(projectId) })
res.sendStatus(204)
})
}
}
module.exports = MockDocstoreApi
// type hint for the inherited `instance` method
/**
* @function instance
* @memberOf MockDocstoreApi
* @static
* @returns {MockDocstoreApi}
*/