2021-02-09 09:55:41 -05:00
|
|
|
const User = require('./helpers/User')
|
|
|
|
const request = require('./helpers/request')
|
|
|
|
const { expect } = require('chai')
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2021-02-09 09:55:41 -05:00
|
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('DocUpdate', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-02-09 09:55:41 -05:00
|
|
|
this.user = new User()
|
|
|
|
this.projectName = 'wombat'
|
|
|
|
this.user.ensureUserExists(() => {
|
|
|
|
this.user.login(() => {
|
|
|
|
this.user.createProject(this.projectName, (error, projectId) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
this.projectId = projectId
|
|
|
|
|
|
|
|
this.user.getProject(this.projectId, (error, project) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
this.project = project
|
|
|
|
this.user.createDocInProject(
|
|
|
|
this.projectId,
|
|
|
|
this.project.rootFolder[0]._id,
|
|
|
|
'potato',
|
|
|
|
(error, docId) => {
|
|
|
|
this.docId = docId
|
|
|
|
done(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function writeContent(
|
|
|
|
{ projectId, docId, lines, version, ranges, lastUpdatedAt, lastUpdatedBy },
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
request(
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
url: `/project/${projectId}/doc/${docId}`,
|
|
|
|
auth: {
|
|
|
|
user: settings.apis.web.user,
|
|
|
|
pass: settings.apis.web.pass,
|
2021-04-27 03:52:58 -04:00
|
|
|
sendImmediately: true,
|
2021-02-09 09:55:41 -05:00
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
json: { lines, version, ranges, lastUpdatedAt, lastUpdatedBy },
|
2021-02-09 09:55:41 -05:00
|
|
|
},
|
|
|
|
(error, res) => {
|
|
|
|
if (error) return callback(error)
|
|
|
|
if (res.statusCode !== 200)
|
|
|
|
return callback(
|
|
|
|
new Error(`non-success statusCode: ${res.statusCode}`)
|
|
|
|
)
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateContent(options, callback) {
|
|
|
|
writeContent(options, err => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
options.lines.push('foo')
|
|
|
|
options.version++
|
|
|
|
writeContent(options, callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeContentTwice(options, callback) {
|
|
|
|
writeContent(options, err => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
writeContent(options, callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let writeOptions
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2021-02-09 09:55:41 -05:00
|
|
|
writeOptions = {
|
|
|
|
projectId: this.projectId,
|
|
|
|
docId: this.docId,
|
|
|
|
lines: ['a'],
|
|
|
|
version: 1,
|
|
|
|
ranges: {},
|
|
|
|
lastUpdatedAt: new Date(),
|
2021-04-27 03:52:58 -04:00
|
|
|
lastUpdatedBy: this.user.id,
|
2021-02-09 09:55:41 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function shouldAcceptChanges() {
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should accept writes', function (done) {
|
2021-02-09 09:55:41 -05:00
|
|
|
writeContent(writeOptions, done)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should accept updates', function (done) {
|
2021-02-09 09:55:41 -05:00
|
|
|
updateContent(writeOptions, done)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should accept same write twice', function (done) {
|
2021-02-09 09:55:41 -05:00
|
|
|
writeContentTwice(writeOptions, done)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldBlockChanges() {
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should block writes', function (done) {
|
2021-02-09 09:55:41 -05:00
|
|
|
writeContent(writeOptions, err => {
|
|
|
|
expect(err).to.exist
|
|
|
|
expect(err.message).to.equal('non-success statusCode: 404')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should block updates', function (done) {
|
2021-02-09 09:55:41 -05:00
|
|
|
updateContent(writeOptions, err => {
|
|
|
|
expect(err).to.exist
|
|
|
|
expect(err.message).to.equal('non-success statusCode: 404')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('a regular doc', function () {
|
2021-02-09 09:55:41 -05:00
|
|
|
shouldAcceptChanges()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('after deleting the doc', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-02-09 09:55:41 -05:00
|
|
|
this.user.deleteItemInProject(this.projectId, 'doc', this.docId, done)
|
|
|
|
})
|
|
|
|
|
|
|
|
shouldAcceptChanges()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('unknown doc', function () {
|
|
|
|
beforeEach(function () {
|
2021-02-09 09:55:41 -05:00
|
|
|
writeOptions.docId = ObjectId()
|
|
|
|
})
|
|
|
|
|
|
|
|
shouldBlockChanges()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('doc in another project', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-02-09 09:55:41 -05:00
|
|
|
this.user.createProject('foo', (error, projectId) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
writeOptions.projectId = projectId
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
shouldBlockChanges()
|
|
|
|
})
|
|
|
|
})
|