2023-01-13 07:42:29 -05:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import request from 'request'
|
|
|
|
import Settings from '@overleaf/settings'
|
|
|
|
import RedisWrapper from '@overleaf/redis-wrapper'
|
|
|
|
import { db } from '../../../../app/js/mongodb.js'
|
|
|
|
|
|
|
|
const rclient = RedisWrapper.createClient(Settings.redis.project_history)
|
|
|
|
const Keys = Settings.redis.project_history.key_schema
|
|
|
|
|
|
|
|
export function resetDatabase(callback) {
|
|
|
|
rclient.flushdb(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initializeProject(historyId, callback) {
|
|
|
|
request.post(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: 'http://127.0.0.1:3054/project',
|
2023-01-13 07:42:29 -05:00
|
|
|
json: { historyId },
|
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(200)
|
|
|
|
callback(null, body.project)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function flushProject(projectId, options, callback) {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = null
|
|
|
|
}
|
|
|
|
if (!options) {
|
|
|
|
options = { allowErrors: false }
|
|
|
|
}
|
|
|
|
request.post(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/flush`,
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (!options.allowErrors) {
|
|
|
|
expect(res.statusCode).to.equal(204)
|
|
|
|
}
|
|
|
|
callback(error, res)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSummarizedUpdates(projectId, query, callback) {
|
|
|
|
request.get(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/updates`,
|
2023-01-13 07:42:29 -05:00
|
|
|
qs: query,
|
|
|
|
json: true,
|
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(200)
|
|
|
|
callback(error, body)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDiff(projectId, pathname, from, to, callback) {
|
|
|
|
request.get(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/diff`,
|
2023-01-13 07:42:29 -05:00
|
|
|
qs: {
|
|
|
|
pathname,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(200)
|
|
|
|
callback(error, body)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFileTreeDiff(projectId, from, to, callback) {
|
|
|
|
request.get(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/filetree/diff`,
|
2023-01-13 07:42:29 -05:00
|
|
|
qs: {
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
callback(error, body, res.statusCode)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSnapshot(projectId, pathname, version, options, callback) {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = null
|
|
|
|
}
|
|
|
|
if (!options) {
|
|
|
|
options = { allowErrors: false }
|
|
|
|
}
|
|
|
|
request.get(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/version/${version}/${encodeURIComponent(
|
2023-01-13 07:42:29 -05:00
|
|
|
pathname
|
|
|
|
)}`,
|
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (!options.allowErrors) {
|
|
|
|
expect(res.statusCode).to.equal(200)
|
|
|
|
}
|
|
|
|
callback(error, body, res.statusCode)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pushRawUpdate(projectId, update, callback) {
|
|
|
|
rclient.rpush(
|
|
|
|
Keys.projectHistoryOps({ project_id: projectId }),
|
|
|
|
JSON.stringify(update),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setFirstOpTimestamp(projectId, timestamp, callback) {
|
|
|
|
rclient.set(
|
|
|
|
Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
|
|
|
|
timestamp,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFirstOpTimestamp(projectId, callback) {
|
|
|
|
rclient.get(
|
|
|
|
Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearFirstOpTimestamp(projectId, callback) {
|
|
|
|
rclient.del(
|
|
|
|
Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getQueueLength(projectId, callback) {
|
|
|
|
rclient.llen(Keys.projectHistoryOps({ project_id: projectId }), callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getQueueCounts(callback) {
|
|
|
|
return request.get(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: 'http://127.0.0.1:3054/status/queue',
|
2023-01-13 07:42:29 -05:00
|
|
|
json: true,
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resyncHistory(projectId, callback) {
|
|
|
|
request.post(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/resync`,
|
2023-01-13 07:42:29 -05:00
|
|
|
json: true,
|
|
|
|
body: { origin: { kind: 'test-origin' } },
|
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(204)
|
|
|
|
callback(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createLabel(
|
|
|
|
projectId,
|
|
|
|
userId,
|
|
|
|
version,
|
|
|
|
comment,
|
|
|
|
createdAt,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
request.post(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/user/${userId}/labels`,
|
2023-01-13 07:42:29 -05:00
|
|
|
json: { comment, version, created_at: createdAt },
|
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(200)
|
|
|
|
callback(null, body)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLabels(projectId, callback) {
|
|
|
|
request.get(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/labels`,
|
2023-01-13 07:42:29 -05:00
|
|
|
json: true,
|
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(200)
|
|
|
|
callback(null, body)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-22 04:02:51 -05:00
|
|
|
export function deleteLabelForUser(projectId, userId, labelId, callback) {
|
2023-01-13 07:42:29 -05:00
|
|
|
request.delete(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/user/${userId}/labels/${labelId}`,
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(204)
|
|
|
|
callback(null, body)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-22 04:02:51 -05:00
|
|
|
export function deleteLabel(projectId, labelId, callback) {
|
|
|
|
request.delete(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/project/${projectId}/labels/${labelId}`,
|
2024-01-22 04:02:51 -05:00
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(204)
|
|
|
|
callback(null, body)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
export function setFailure(failureEntry, callback) {
|
2023-12-15 05:51:11 -05:00
|
|
|
db.projectHistoryFailures.deleteOne(
|
2023-01-13 07:42:29 -05:00
|
|
|
{ project_id: { $exists: true } },
|
|
|
|
(err, result) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2023-12-15 05:51:11 -05:00
|
|
|
db.projectHistoryFailures.insertOne(failureEntry, callback)
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function transferLabelOwnership(fromUser, toUser, callback) {
|
|
|
|
request.post(
|
|
|
|
{
|
2024-04-25 08:56:00 -04:00
|
|
|
url: `http://127.0.0.1:3054/user/${fromUser}/labels/transfer/${toUser}`,
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(204)
|
|
|
|
callback(null, body)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDump(projectId, callback) {
|
|
|
|
request.get(
|
2024-04-25 08:56:00 -04:00
|
|
|
`http://127.0.0.1:3054/project/${projectId}/dump`,
|
2023-01-13 07:42:29 -05:00
|
|
|
(err, res, body) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(200)
|
|
|
|
callback(null, JSON.parse(body))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteProject(projectId, callback) {
|
2024-04-25 08:56:00 -04:00
|
|
|
request.delete(`http://127.0.0.1:3054/project/${projectId}`, (err, res) => {
|
2023-01-13 07:42:29 -05:00
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(204)
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|