2019-05-29 05:21:06 -04:00
|
|
|
const { expect } = require('chai')
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Path = require('path')
|
|
|
|
const fs = require('fs')
|
|
|
|
|
2020-02-27 07:46:37 -05:00
|
|
|
const { Project } = require('../../../app/src/models/Project')
|
2019-05-29 05:21:06 -04:00
|
|
|
const ProjectGetter = require('../../../app/src/Features/Project/ProjectGetter.js')
|
|
|
|
|
|
|
|
const User = require('./helpers/User')
|
2021-04-06 05:15:00 -04:00
|
|
|
const MockDocStoreApiClass = require('./mocks/MockDocstoreApi')
|
2021-02-25 07:22:24 -05:00
|
|
|
const MockDocUpdaterApiClass = require('./mocks/MockDocUpdaterApi')
|
|
|
|
|
2021-04-06 05:15:00 -04:00
|
|
|
let MockDocStoreApi, MockDocUpdaterApi
|
2021-02-25 07:22:24 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
before(function () {
|
2021-02-25 07:22:24 -05:00
|
|
|
MockDocUpdaterApi = MockDocUpdaterApiClass.instance()
|
2021-04-06 05:15:00 -04:00
|
|
|
MockDocStoreApi = MockDocStoreApiClass.instance()
|
2021-02-25 07:22:24 -05:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('ProjectStructureChanges', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
let owner
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function (done) {
|
2020-01-27 08:53:08 -05:00
|
|
|
owner = new User()
|
|
|
|
owner.login(done)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-01-27 08:53:08 -05:00
|
|
|
function createExampleProject(owner, callback) {
|
|
|
|
owner.createProject(
|
2019-08-06 08:20:41 -04:00
|
|
|
'example-project',
|
|
|
|
{ template: 'example' },
|
|
|
|
(error, projectId) => {
|
|
|
|
if (error) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return callback(error)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
|
|
|
|
2020-01-27 08:53:08 -05:00
|
|
|
ProjectGetter.getProject(projectId, (error, project) => {
|
2019-08-06 08:20:41 -04:00
|
|
|
if (error) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return callback(error)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
2020-01-27 08:53:08 -05:00
|
|
|
const rootFolderId = project.rootFolder[0]._id.toString()
|
|
|
|
callback(null, projectId, rootFolderId)
|
2019-08-06 08:20:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-27 08:53:08 -05:00
|
|
|
function createExampleDoc(owner, projectId, callback) {
|
|
|
|
ProjectGetter.getProject(projectId, (error, project) => {
|
2019-08-06 08:20:41 -04:00
|
|
|
if (error) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return callback(error)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
2020-01-27 08:53:08 -05:00
|
|
|
owner.request.post(
|
2019-08-06 08:20:41 -04:00
|
|
|
{
|
2020-01-27 08:53:08 -05:00
|
|
|
uri: `project/${projectId}/doc`,
|
2019-08-06 08:20:41 -04:00
|
|
|
json: {
|
|
|
|
name: 'new.tex',
|
2021-04-27 03:52:58 -04:00
|
|
|
parent_folder_id: project.rootFolder[0]._id,
|
|
|
|
},
|
2019-08-06 08:20:41 -04:00
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-06 08:20:41 -04:00
|
|
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return callback(new Error(`failed to add doc ${res.statusCode}`))
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
2020-01-27 08:53:08 -05:00
|
|
|
callback(null, body._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
|
|
|
|
2020-01-27 08:53:08 -05:00
|
|
|
function createExampleFolder(owner, projectId, callback) {
|
|
|
|
owner.request.post(
|
2019-08-06 08:20:41 -04:00
|
|
|
{
|
2020-01-27 08:53:08 -05:00
|
|
|
uri: `project/${projectId}/folder`,
|
2019-08-06 08:20:41 -04:00
|
|
|
json: {
|
2021-04-27 03:52:58 -04:00
|
|
|
name: 'foo',
|
|
|
|
},
|
2019-08-06 08:20:41 -04:00
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return callback(error)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
|
|
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return callback(new Error(`failed to add doc ${res.statusCode}`))
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
2020-01-27 08:53:08 -05:00
|
|
|
callback(null, body._id)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-27 08:53:08 -05:00
|
|
|
function uploadExampleProject(owner, zipFilename, options, callback) {
|
2019-08-06 08:20:41 -04:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const zipFile = fs.createReadStream(
|
|
|
|
Path.resolve(Path.join(__dirname, '..', 'files', zipFilename))
|
|
|
|
)
|
|
|
|
|
2020-01-27 08:53:08 -05:00
|
|
|
owner.request.post(
|
2019-08-06 08:20:41 -04:00
|
|
|
{
|
|
|
|
uri: 'project/new/upload',
|
|
|
|
formData: {
|
2021-04-27 03:52:58 -04:00
|
|
|
qqfile: zipFile,
|
|
|
|
},
|
2019-08-06 08:20:41 -04:00
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return callback(error)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
!options.allowBadStatus &&
|
|
|
|
(res.statusCode < 200 || res.statusCode >= 300)
|
|
|
|
) {
|
2020-01-27 08:53:08 -05:00
|
|
|
return new Error(`failed to upload project ${res.statusCode}`)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
2020-01-27 08:53:08 -05:00
|
|
|
callback(null, JSON.parse(body).project_id, res)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-27 08:53:08 -05:00
|
|
|
function deleteItem(owner, projectId, type, itemId, callback) {
|
2021-01-11 06:58:16 -05:00
|
|
|
owner.deleteItemInProject(projectId, type, itemId, callback)
|
2019-08-06 08:20:41 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('uploading a project with a name', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
let exampleProjectId
|
|
|
|
const testProjectName = 'wombat'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function (done) {
|
2020-01-27 08:53:08 -05:00
|
|
|
uploadExampleProject(
|
|
|
|
owner,
|
|
|
|
'test_project_with_name.zip',
|
|
|
|
(err, projectId) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
exampleProjectId = projectId
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the project name from the zip contents', function (done) {
|
2020-01-27 08:53:08 -05:00
|
|
|
ProjectGetter.getProject(exampleProjectId, (error, project) => {
|
2019-08-06 08:20:41 -04:00
|
|
|
expect(error).not.to.exist
|
2020-01-27 08:53:08 -05:00
|
|
|
expect(project.name).to.equal(testProjectName)
|
2019-08-06 08:20:41 -04:00
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('uploading a project with an invalid name', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
let exampleProjectId
|
|
|
|
const testProjectMatch = /^bad[^\\]+name$/
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function (done) {
|
2020-01-27 08:53:08 -05:00
|
|
|
uploadExampleProject(
|
|
|
|
owner,
|
|
|
|
'test_project_with_invalid_name.zip',
|
|
|
|
(error, projectId) => {
|
|
|
|
if (error) {
|
|
|
|
return done(error)
|
|
|
|
}
|
|
|
|
exampleProjectId = projectId
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the project name from the zip contents', function (done) {
|
2020-01-27 08:53:08 -05:00
|
|
|
ProjectGetter.getProject(exampleProjectId, (error, project) => {
|
2019-08-06 08:20:41 -04:00
|
|
|
expect(error).not.to.exist
|
2020-01-27 08:53:08 -05:00
|
|
|
expect(project.name).to.match(testProjectMatch)
|
2019-08-06 08:20:41 -04:00
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('uploading an empty zipfile', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
let res
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function (done) {
|
2019-08-06 08:20:41 -04:00
|
|
|
uploadExampleProject(
|
2020-01-27 08:53:08 -05:00
|
|
|
owner,
|
2019-08-06 08:20:41 -04:00
|
|
|
'test_project_empty.zip',
|
|
|
|
{ allowBadStatus: true },
|
2020-01-27 08:53:08 -05:00
|
|
|
(err, projectId, response) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
res = response
|
|
|
|
done()
|
|
|
|
}
|
2019-07-02 06:48:57 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should fail with 422 error', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
expect(res.statusCode).to.equal(422)
|
2019-07-02 06:48:57 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('uploading a zipfile containing only empty directories', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
let res
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function (done) {
|
2019-08-06 08:20:41 -04:00
|
|
|
uploadExampleProject(
|
2020-01-27 08:53:08 -05:00
|
|
|
owner,
|
2019-08-06 08:20:41 -04:00
|
|
|
'test_project_with_empty_folder.zip',
|
|
|
|
{ allowBadStatus: true },
|
2020-01-27 08:53:08 -05:00
|
|
|
|
|
|
|
(err, projectId, response) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
res = response
|
|
|
|
done()
|
|
|
|
}
|
2019-07-02 06:48:57 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should fail with 422 error', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
expect(res.statusCode).to.equal(422)
|
2019-07-02 06:48:57 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('uploading a project with a shared top-level folder', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
let exampleProjectId
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function (done) {
|
2019-08-06 08:20:41 -04:00
|
|
|
uploadExampleProject(
|
2020-01-27 08:53:08 -05:00
|
|
|
owner,
|
2019-08-06 08:20:41 -04:00
|
|
|
'test_project_with_shared_top_level_folder.zip',
|
2020-01-27 08:53:08 -05:00
|
|
|
(err, projectId) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
exampleProjectId = projectId
|
|
|
|
done()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not create the top-level folder', function (done) {
|
2020-01-27 08:53:08 -05:00
|
|
|
ProjectGetter.getProject(exampleProjectId, (error, project) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(error).not.to.exist
|
|
|
|
expect(project.rootFolder[0].folders.length).to.equal(0)
|
|
|
|
expect(project.rootFolder[0].docs.length).to.equal(2)
|
2019-08-06 08:20:41 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('uploading a project with backslashes in the path names', function () {
|
2020-01-27 08:53:08 -05:00
|
|
|
let exampleProjectId
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function (done) {
|
2019-08-07 10:04:04 -04:00
|
|
|
uploadExampleProject(
|
2020-01-27 08:53:08 -05:00
|
|
|
owner,
|
2019-08-07 10:04:04 -04:00
|
|
|
'test_project_with_backslash_in_filename.zip',
|
2020-01-27 08:53:08 -05:00
|
|
|
(err, projectId) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
exampleProjectId = projectId
|
|
|
|
done()
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should treat the backslash as a directory separator', function (done) {
|
2020-01-27 08:53:08 -05:00
|
|
|
ProjectGetter.getProject(exampleProjectId, (error, project) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(error).not.to.exist
|
|
|
|
expect(project.rootFolder[0].folders[0].name).to.equal('styles')
|
|
|
|
expect(project.rootFolder[0].folders[0].docs[0].name).to.equal('ao.sty')
|
2019-08-06 08:20:41 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('deleting docs', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-27 07:46:37 -05:00
|
|
|
createExampleProject(owner, (err, projectId) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
this.exampleProjectId = projectId
|
|
|
|
createExampleFolder(owner, projectId, (err, folderId) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
this.exampleFolderId = folderId
|
|
|
|
createExampleDoc(owner, projectId, (err, docId) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
this.exampleDocId = docId
|
2021-02-25 07:22:24 -05:00
|
|
|
MockDocUpdaterApi.reset()
|
2020-02-27 07:46:37 -05:00
|
|
|
ProjectGetter.getProject(
|
|
|
|
this.exampleProjectId,
|
|
|
|
(error, project) => {
|
|
|
|
if (error) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
this.project0 = project
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should pass the doc name to docstore', function (done) {
|
2021-04-06 05:15:00 -04:00
|
|
|
deleteItem(
|
|
|
|
owner,
|
|
|
|
this.exampleProjectId,
|
|
|
|
'doc',
|
|
|
|
this.exampleDocId,
|
|
|
|
error => {
|
|
|
|
if (error) return done(error)
|
|
|
|
expect(
|
|
|
|
MockDocStoreApi.getDeletedDocs(this.exampleProjectId)
|
|
|
|
).to.deep.equal([{ _id: this.exampleDocId, name: 'new.tex' }])
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when rootDoc_id matches doc being deleted', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-11-03 04:19:05 -05:00
|
|
|
Project.updateOne(
|
2020-02-27 07:46:37 -05:00
|
|
|
{ _id: this.exampleProjectId },
|
|
|
|
{ $set: { rootDoc_id: this.exampleDocId } },
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should clear rootDoc_id', function (done) {
|
2020-02-27 07:46:37 -05:00
|
|
|
deleteItem(
|
|
|
|
owner,
|
|
|
|
this.exampleProjectId,
|
|
|
|
'doc',
|
|
|
|
this.exampleDocId,
|
|
|
|
() => {
|
|
|
|
ProjectGetter.getProject(
|
|
|
|
this.exampleProjectId,
|
|
|
|
(error, project) => {
|
|
|
|
if (error) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
expect(project.rootDoc_id).to.be.undefined
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when rootDoc_id does not match doc being deleted', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-27 07:46:37 -05:00
|
|
|
this.exampleRootDocId = new ObjectId()
|
2020-11-03 04:19:05 -05:00
|
|
|
Project.updateOne(
|
2020-02-27 07:46:37 -05:00
|
|
|
{ _id: this.exampleProjectId },
|
|
|
|
{ $set: { rootDoc_id: this.exampleRootDocId } },
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not clear rootDoc_id', function (done) {
|
2020-02-27 07:46:37 -05:00
|
|
|
deleteItem(
|
|
|
|
owner,
|
|
|
|
this.exampleProjectId,
|
|
|
|
'doc',
|
|
|
|
this.exampleDocId,
|
|
|
|
() => {
|
|
|
|
ProjectGetter.getProject(
|
|
|
|
this.exampleProjectId,
|
|
|
|
(error, project) => {
|
|
|
|
if (error) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
expect(project.rootDoc_id.toString()).to.equal(
|
|
|
|
this.exampleRootDocId.toString()
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|