overleaf/services/docstore/test/acceptance/js/helpers/DocstoreClient.js

199 lines
4.5 KiB
JavaScript
Raw Normal View History

/* eslint-disable
camelcase,
*/
let DocstoreClient
const request = require('request').defaults({ jar: false })
const settings = require('@overleaf/settings')
const Persistor = require('../../../../app/js/PersistorManager')
async function streamToString(stream) {
const chunks = []
return new Promise((resolve, reject) => {
2021-07-13 07:04:48 -04:00
stream.on('data', chunk => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
})
}
async function getStringFromPersistor(persistor, bucket, key) {
const stream = await persistor.getObjectStream(bucket, key, {})
stream.resume()
return streamToString(stream)
}
module.exports = DocstoreClient = {
createDoc(project_id, doc_id, lines, version, ranges, callback) {
return DocstoreClient.updateDoc(
project_id,
doc_id,
lines,
version,
ranges,
callback
)
},
2014-04-30 08:06:12 -04:00
getDoc(project_id, doc_id, qs, callback) {
request.get(
{
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
json: true,
2021-07-13 07:04:48 -04:00
qs,
},
callback
)
},
2021-07-30 11:06:16 -04:00
peekDoc(project_id, doc_id, qs, callback) {
2021-08-03 05:07:00 -04:00
request.get(
2021-07-30 11:06:16 -04:00
{
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/peek`,
json: true,
qs,
},
callback
)
},
isDocDeleted(project_id, doc_id, callback) {
request.get(
{
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/deleted`,
2021-07-13 07:04:48 -04:00
json: true,
},
callback
)
},
getAllDocs(project_id, callback) {
request.get(
{
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc`,
2021-07-13 07:04:48 -04:00
json: true,
},
(req, res, body) => {
callback(req, res, body)
}
)
},
getAllDeletedDocs(project_id, callback) {
request.get(
{
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc-deleted`,
2021-07-13 07:04:48 -04:00
json: true,
},
(error, res, body) => {
if (error) return callback(error)
if (res.statusCode !== 200) {
return callback(new Error('unexpected statusCode'))
}
callback(null, body)
}
)
},
getAllRanges(project_id, callback) {
request.get(
{
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/ranges`,
2021-07-13 07:04:48 -04:00
json: true,
},
callback
)
},
2014-04-30 08:06:12 -04:00
updateDoc(project_id, doc_id, lines, version, ranges, callback) {
return request.post(
{
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
json: {
lines,
version,
2021-07-13 07:04:48 -04:00
ranges,
},
},
callback
)
},
deleteDoc(project_id, doc_id, callback) {
DocstoreClient.deleteDocWithDateAndName(
project_id,
doc_id,
new Date(),
'main.tex',
callback
)
},
deleteDocWithDate(project_id, doc_id, date, callback) {
DocstoreClient.deleteDocWithDateAndName(
project_id,
doc_id,
date,
'main.tex',
callback
)
},
deleteDocWithName(project_id, doc_id, name, callback) {
DocstoreClient.deleteDocWithDateAndName(
project_id,
doc_id,
new Date(),
name,
callback
)
},
deleteDocWithDateAndName(project_id, doc_id, deletedAt, name, callback) {
request.patch(
{
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
2021-07-13 07:04:48 -04:00
json: { name, deleted: true, deletedAt },
},
callback
)
},
archiveAllDoc(project_id, callback) {
request.post(
{
2021-07-13 07:04:48 -04:00
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/archive`,
},
callback
)
},
archiveDocById(project_id, doc_id, callback) {
request.post(
{
2021-07-13 07:04:48 -04:00
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/archive`,
},
callback
)
},
destroyAllDoc(project_id, callback) {
request.post(
{
2021-07-13 07:04:48 -04:00
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/destroy`,
},
callback
)
},
getS3Doc(project_id, doc_id, callback) {
getStringFromPersistor(
Persistor,
settings.docstore.bucket,
`${project_id}/${doc_id}`
)
2021-07-13 07:04:48 -04:00
.then(data => {
callback(null, JSON.parse(data))
})
.catch(callback)
2021-07-13 07:04:48 -04:00
},
}