| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127 |
- const { expect } = require('chai')
- const mkdirp = require('mkdirp')
- const { ObjectId } = require('mongojs')
- const Path = require('path')
- const fs = require('fs')
- const Settings = require('settings-sharelatex')
- const _ = require('underscore')
- const ProjectGetter = require('../../../app/src/Features/Project/ProjectGetter.js')
- const MockDocUpdaterApi = require('./helpers/MockDocUpdaterApi')
- require('./helpers/MockFileStoreApi')
- require('./helpers/MockProjectHistoryApi')
- const User = require('./helpers/User')
- describe('ProjectStructureChanges', function() {
- beforeEach(function(done) {
- this.owner = new User()
- this.owner.login(done)
- })
- function createExampleProject(test, callback) {
- test.owner.createProject(
- 'example-project',
- { template: 'example' },
- (error, projectId) => {
- if (error) {
- throw error
- }
- test.exampleProjectId = projectId
- ProjectGetter.getProject(test.exampleProjectId, (error, project) => {
- if (error) {
- throw error
- }
- test.rootFolderId = project.rootFolder[0]._id.toString()
- callback()
- })
- }
- )
- }
- function createExampleDoc(test, callback) {
- ProjectGetter.getProject(test.exampleProjectId, (error, project) => {
- if (error) {
- throw error
- }
- test.owner.request.post(
- {
- uri: `project/${test.exampleProjectId}/doc`,
- json: {
- name: 'new.tex',
- parent_folder_id: project.rootFolder[0]._id
- }
- },
- (error, res, body) => {
- if (error) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to add doc ${res.statusCode}`)
- }
- test.exampleDocId = body._id
- callback()
- }
- )
- })
- }
- function createExampleFolder(test, callback) {
- test.owner.request.post(
- {
- uri: `project/${test.exampleProjectId}/folder`,
- json: {
- name: 'foo'
- }
- },
- (error, res, body) => {
- if (error) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to add doc ${res.statusCode}`)
- }
- test.exampleFolderId = body._id
- callback()
- }
- )
- }
- function uploadFile(test, file, name, contentType, callback) {
- const imageFile = fs.createReadStream(
- Path.resolve(Path.join(__dirname, '..', 'files', file))
- )
- test.owner.request.post(
- {
- uri: `project/${test.exampleProjectId}/upload`,
- qs: {
- folder_id: test.rootFolderId
- },
- formData: {
- qqfile: {
- value: imageFile,
- options: {
- filename: name,
- contentType: contentType
- }
- }
- }
- },
- (error, res, body) => {
- if (error) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload file ${res.statusCode}`)
- }
- test.exampleFileId = JSON.parse(body).entity_id
- callback()
- }
- )
- }
- function uploadExampleFile(test, callback) {
- uploadFile(test, '1pixel.png', '1pixel.png', 'image/png', callback)
- }
- function uploadExampleProject(test, zipFilename, options, callback) {
- if (typeof options === 'function') {
- callback = options
- options = {}
- }
- const zipFile = fs.createReadStream(
- Path.resolve(Path.join(__dirname, '..', 'files', zipFilename))
- )
- test.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zipFile
- }
- },
- (error, res, body) => {
- if (error) {
- throw error
- }
- if (
- !options.allowBadStatus &&
- (res.statusCode < 200 || res.statusCode >= 300)
- ) {
- throw new Error(`failed to upload project ${res.statusCode}`)
- }
- test.uploadedProjectId = JSON.parse(body).project_id
- test.res = res
- callback()
- }
- )
- }
- function moveItem(test, type, itemId, folderId, callback) {
- test.owner.request.post(
- {
- uri: `project/${test.exampleProjectId}/${type}/${itemId}/move`,
- json: {
- folder_id: folderId
- }
- },
- (error, res) => {
- if (error) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to move ${type} ${res.statusCode}`)
- }
- callback()
- }
- )
- }
- function renameItem(test, type, itemId, name, callback) {
- test.owner.request.post(
- {
- uri: `project/${test.exampleProjectId}/${type}/${itemId}/rename`,
- json: {
- name: name
- }
- },
- (error, res) => {
- if (error) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to rename ${type} ${res.statusCode}`)
- }
- callback()
- }
- )
- }
- function deleteItem(test, type, itemId, callback) {
- test.owner.request.delete(
- {
- uri: `project/${test.exampleProjectId}/${type}/${itemId}`
- },
- (error, res) => {
- if (error) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to delete folder ${res.statusCode}`)
- }
- callback()
- }
- )
- }
- function verifyVersionIncremented(test, updateVersion, increment, callback) {
- expect(updateVersion).to.equal(test.project0.version + increment)
- ProjectGetter.getProject(test.exampleProjectId, (error, newProject) => {
- if (error) {
- throw error
- }
- expect(newProject.version).to.equal(test.project0.version + increment)
- callback()
- })
- }
- describe('creating a project from the example template', function() {
- beforeEach(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- createExampleProject(this, done)
- })
- it('should version creating a doc', function() {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(2)
- _.each(updates, update => {
- expect(update.userId).to.equal(this.owner._id)
- expect(update.docLines).to.be.a('string')
- })
- expect(_.where(updates, { pathname: '/main.tex' }).length).to.equal(1)
- expect(_.where(updates, { pathname: '/references.bib' }).length).to.equal(
- 1
- )
- expect(version).to.equal(3)
- })
- it('should version creating a file', function() {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/universe.jpg')
- expect(update.url).to.be.a('string')
- expect(version).to.equal(3)
- })
- })
- describe('duplicating a project', function() {
- beforeEach(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- createExampleProject(this, () => {
- this.owner.request.post(
- {
- uri: `/Project/${this.exampleProjectId}/clone`,
- json: {
- projectName: 'new.tex'
- }
- },
- (error, res, body) => {
- if (error) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to clone project ${res.statusCode}`)
- }
- this.dupProjectId = body.project_id
- done()
- }
- )
- })
- })
- it('should version the docs created', function() {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.dupProjectId)
- expect(updates.length).to.equal(2)
- _.each(updates, update => {
- expect(update.userId).to.equal(this.owner._id)
- expect(update.docLines).to.be.a('string')
- })
- expect(_.where(updates, { pathname: '/main.tex' }).length).to.equal(1)
- expect(_.where(updates, { pathname: '/references.bib' }).length).to.equal(
- 1
- )
- expect(version).to.equal(3)
- })
- it('should version the files created', function() {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.dupProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/universe.jpg')
- expect(update.url).to.be.a('string')
- expect(version).to.equal(3)
- })
- })
- describe('adding a doc', function() {
- beforeEach(function(done) {
- createExampleProject(this, () => {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- ProjectGetter.getProject(this.exampleProjectId, (error, project) => {
- if (error) {
- throw error
- }
- this.project0 = project
- createExampleDoc(this, done)
- })
- })
- })
- it('should version the doc added', function(done) {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/new.tex')
- expect(update.docLines).to.be.a('string')
- verifyVersionIncremented(this, version, 1, done)
- })
- })
- describe('uploading a project', function() {
- beforeEach(function(done) {
- uploadExampleProject(this, 'test_project.zip', done)
- })
- it('should version the docs created', function() {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploadedProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/main.tex')
- expect(update.docLines).to.equal('Test')
- expect(version).to.equal(2)
- })
- it('should version the files created', function() {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploadedProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/1pixel.png')
- expect(update.url).to.be.a('string')
- expect(version).to.equal(2)
- })
- })
- describe('uploading a project with a name', function() {
- beforeEach(function(done) {
- this.testProjectName = 'wombat'
- uploadExampleProject(this, 'test_project_with_name.zip', done)
- })
- it('should set the project name from the zip contents', function(done) {
- ProjectGetter.getProject(this.uploadedProjectId, (error, project) => {
- expect(error).not.to.exist
- expect(project.name).to.equal(this.testProjectName)
- done()
- })
- })
- })
- describe('uploading a project with an invalid name', function() {
- beforeEach(function(done) {
- this.testProjectMatch = /^bad[^\\]+name$/
- uploadExampleProject(this, 'test_project_with_invalid_name.zip', done)
- })
- it('should set the project name from the zip contents', function(done) {
- ProjectGetter.getProject(this.uploadedProjectId, (error, project) => {
- expect(error).not.to.exist
- expect(project.name).to.match(this.testProjectMatch)
- done()
- })
- })
- })
- describe('uploading an empty zipfile', function() {
- beforeEach(function(done) {
- uploadExampleProject(
- this,
- 'test_project_empty.zip',
- { allowBadStatus: true },
- done
- )
- })
- it('should fail with 422 error', function() {
- expect(this.res.statusCode).to.equal(422)
- })
- })
- describe('uploading a zipfile containing only empty directories', function() {
- beforeEach(function(done) {
- uploadExampleProject(
- this,
- 'test_project_with_empty_folder.zip',
- { allowBadStatus: true },
- done
- )
- })
- it('should fail with 422 error', function() {
- expect(this.res.statusCode).to.equal(422)
- })
- })
- describe('uploading a project with a shared top-level folder', function() {
- beforeEach(function(done) {
- uploadExampleProject(
- this,
- 'test_project_with_shared_top_level_folder.zip',
- done
- )
- })
- it('should not create the top-level folder', function(done) {
- ProjectGetter.getProject(this.uploadedProjectId, (error, project) => {
- expect(error).not.to.exist
- expect(project.rootFolder[0].folders.length).to.equal(0)
- expect(project.rootFolder[0].docs.length).to.equal(2)
- done()
- })
- })
- })
- describe('uploading a project with backslashes in the path names', function() {
- beforeEach(function(done) {
- uploadExampleProject(
- this,
- 'test_project_with_backslash_in_filename.zip',
- done
- )
- })
- it('should treat the backslash as a directory separator', function(done) {
- ProjectGetter.getProject(this.uploadedProjectId, (error, project) => {
- 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')
- done()
- })
- })
- })
- describe('uploading a project with files in different encodings', function() {
- beforeEach(function(done) {
- uploadExampleProject(this, 'charsets/charsets.zip', done)
- })
- it('should correctly parse windows-1252', function() {
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploadedProjectId)
- const update = _.find(
- updates,
- update => update.pathname === '/test-german-windows-1252.tex'
- )
- expect(update.docLines).to.contain(
- 'Der schnelle braune Fuchs sprang träge über den Hund.'
- )
- })
- it('should correctly parse German utf8', function() {
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploadedProjectId)
- const update = _.find(
- updates,
- update => update.pathname === '/test-german-utf8x.tex'
- )
- expect(update.docLines).to.contain(
- 'Der schnelle braune Fuchs sprang träge über den Hund.'
- )
- })
- it('should correctly parse little-endian utf16', function() {
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploadedProjectId)
- const update = _.find(
- updates,
- update => update.pathname === '/test-greek-utf16-le-bom.tex'
- )
- expect(update.docLines).to.contain(
- 'Η γρήγορη καστανή αλεπού πήδηξε χαλαρά πάνω από το σκυλί.'
- )
- })
- it('should correctly parse Greek utf8', function() {
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploadedProjectId)
- const update = _.find(
- updates,
- update => update.pathname === '/test-greek-utf8x.tex'
- )
- expect(update.docLines).to.contain(
- 'Η γρήγορη καστανή αλεπού πήδηξε χαλαρά πάνω από το σκυλί.'
- )
- })
- })
- describe('uploading a file', function() {
- beforeEach(function(done) {
- createExampleProject(this, () => {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- ProjectGetter.getProject(this.exampleProjectId, (error, project) => {
- if (error) {
- throw error
- }
- this.project0 = project
- uploadExampleFile(this, done)
- })
- })
- })
- it('should version a newly uploaded file', function(done) {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/1pixel.png')
- expect(update.url).to.be.a('string')
- // one file upload
- verifyVersionIncremented(this, version, 1, done)
- })
- it('should version a replacement file', function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- uploadFile(this, '2pixel.png', '1pixel.png', 'image/png', () => {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(2)
- let update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/1pixel.png')
- update = updates[1]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/1pixel.png')
- expect(update.url).to.be.a('string')
- // two file uploads
- verifyVersionIncremented(this, version, 2, done)
- })
- })
- })
- describe('moving entities', function() {
- beforeEach(function(done) {
- createExampleProject(this, () => {
- createExampleDoc(this, () => {
- uploadExampleFile(this, () => {
- createExampleFolder(this, () => {
- ProjectGetter.getProject(
- this.exampleProjectId,
- (error, project) => {
- if (error) {
- throw error
- }
- this.project0 = project
- MockDocUpdaterApi.clearProjectStructureUpdates()
- done()
- }
- )
- })
- })
- })
- })
- })
- it('should version moving a doc', function(done) {
- moveItem(this, 'doc', this.exampleDocId, this.exampleFolderId, () => {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/new.tex')
- expect(update.newPathname).to.equal('/foo/new.tex')
- // 2, because it's a delete and then add
- verifyVersionIncremented(this, version, 2, done)
- })
- })
- it('should version moving a file', function(done) {
- moveItem(this, 'file', this.exampleFileId, this.exampleFolderId, () => {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/1pixel.png')
- expect(update.newPathname).to.equal('/foo/1pixel.png')
- // 2, because it's a delete and then add
- verifyVersionIncremented(this, version, 2, done)
- })
- })
- it('should version moving a folder', function(done) {
- moveItem(this, 'doc', this.exampleDocId, this.exampleFolderId, () => {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- this.owner.request.post(
- {
- uri: `project/${this.exampleProjectId}/folder`,
- json: {
- name: 'bar'
- }
- },
- (error, res, body) => {
- if (error) {
- throw error
- }
- const newFolderId = body._id
- moveItem(this, 'folder', this.exampleFolderId, newFolderId, () => {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(
- this.exampleProjectId
- )
- expect(updates.length).to.equal(1)
- let update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/foo/new.tex')
- expect(update.newPathname).to.equal('/bar/foo/new.tex')
- // 5, because it's two file moves plus a folder
- verifyVersionIncremented(this, version, 5, done)
- })
- }
- )
- })
- })
- })
- describe('renaming entities', function() {
- beforeEach(function(done) {
- createExampleProject(this, () => {
- createExampleDoc(this, () => {
- uploadExampleFile(this, () => {
- createExampleFolder(this, () => {
- moveItem(
- this,
- 'doc',
- this.exampleDocId,
- this.exampleFolderId,
- () => {
- moveItem(
- this,
- 'file',
- this.exampleFileId,
- this.exampleFolderId,
- () => {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- ProjectGetter.getProject(
- this.exampleProjectId,
- (error, project) => {
- if (error) {
- throw error
- }
- this.project0 = project
- done()
- }
- )
- }
- )
- }
- )
- })
- })
- })
- })
- })
- it('should version renaming a doc', function(done) {
- renameItem(this, 'Doc', this.exampleDocId, 'wombat.tex', () => {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/foo/new.tex')
- expect(update.newPathname).to.equal('/foo/wombat.tex')
- verifyVersionIncremented(this, version, 1, done)
- })
- })
- it('should version renaming a file', function(done) {
- renameItem(this, 'file', this.exampleFileId, 'potato.png', () => {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/foo/1pixel.png')
- expect(update.newPathname).to.equal('/foo/potato.png')
- verifyVersionIncremented(this, version, 1, done)
- })
- })
- it('should version renaming a folder', function(done) {
- renameItem(this, 'folder', this.exampleFolderId, 'giraffe', () => {
- const {
- docUpdates,
- fileUpdates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(docUpdates.length).to.equal(1)
- const docUpdate = docUpdates[0]
- expect(docUpdate.userId).to.equal(this.owner._id)
- expect(docUpdate.pathname).to.equal('/foo/new.tex')
- expect(docUpdate.newPathname).to.equal('/giraffe/new.tex')
- expect(fileUpdates.length).to.equal(1)
- const fileUpdate = fileUpdates[0]
- expect(fileUpdate.userId).to.equal(this.owner._id)
- expect(fileUpdate.pathname).to.equal('/foo/1pixel.png')
- expect(fileUpdate.newPathname).to.equal('/giraffe/1pixel.png')
- verifyVersionIncremented(this, version, 1, done)
- })
- })
- })
- describe('deleting entities', function() {
- beforeEach(function(done) {
- createExampleProject(this, () => {
- createExampleFolder(this, () => {
- createExampleDoc(this, () => {
- uploadExampleFile(this, () => {
- moveItem(
- this,
- 'doc',
- this.exampleDocId,
- this.exampleFolderId,
- () => {
- moveItem(
- this,
- 'file',
- this.exampleFileId,
- this.exampleFolderId,
- () => {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- ProjectGetter.getProject(
- this.exampleProjectId,
- (error, project) => {
- if (error) {
- throw error
- }
- this.project0 = project
- done()
- }
- )
- }
- )
- }
- )
- })
- })
- })
- })
- })
- it('should version deleting a folder', function(done) {
- deleteItem(this, 'folder', this.exampleFolderId, () => {
- const {
- docUpdates,
- fileUpdates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(docUpdates.length).to.equal(1)
- const docUpdate = docUpdates[0]
- expect(docUpdate.userId).to.equal(this.owner._id)
- expect(docUpdate.pathname).to.equal('/foo/new.tex')
- expect(docUpdate.newPathname).to.equal('')
- expect(fileUpdates.length).to.equal(1)
- const fileUpdate = fileUpdates[0]
- expect(fileUpdate.userId).to.equal(this.owner._id)
- expect(fileUpdate.pathname).to.equal('/foo/1pixel.png')
- expect(fileUpdate.newPathname).to.equal('')
- verifyVersionIncremented(this, version, 1, done)
- })
- })
- })
- describe('tpds', function() {
- beforeEach(function(done) {
- this.tpdsProjectName = `tpds-project-${new ObjectId().toString()}`
- this.owner.createProject(this.tpdsProjectName, (error, projectId) => {
- if (error) {
- throw error
- }
- this.exampleProjectId = projectId
- mkdirp(Settings.path.dumpFolder, () => {
- ProjectGetter.getProject(this.exampleProjectId, (error, project) => {
- if (error) {
- throw error
- }
- MockDocUpdaterApi.clearProjectStructureUpdates()
- this.rootFolderId = project.rootFolder[0]._id.toString()
- this.project0 = project
- done()
- })
- })
- })
- })
- it('should version adding a doc', function(done) {
- const texFile = fs.createReadStream(
- Path.resolve(Path.join(__dirname, '..', 'files', 'test.tex'))
- )
- const req = this.owner.request.post({
- uri: `/user/${this.owner._id}/update/${this.tpdsProjectName}/test.tex`,
- auth: {
- user: _.keys(Settings.httpAuthUsers)[0],
- pass: _.values(Settings.httpAuthUsers)[0],
- sendImmediately: true
- }
- })
- texFile.on('error', err => {
- throw err
- })
- req.on('error', err => {
- throw err
- })
- req.on('response', res => {
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload file ${res.statusCode}`)
- }
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/test.tex')
- expect(update.docLines).to.equal('Test')
- verifyVersionIncremented(this, version, 1, done)
- })
- texFile.pipe(req)
- })
- it('should version adding a new file', function(done) {
- const imageFile = fs.createReadStream(
- Path.resolve(Path.join(__dirname, '..', 'files', '1pixel.png'))
- )
- const req = this.owner.request.post({
- uri: `/user/${this.owner._id}/update/${
- this.tpdsProjectName
- }/1pixel.png`,
- auth: {
- user: _.keys(Settings.httpAuthUsers)[0],
- pass: _.values(Settings.httpAuthUsers)[0],
- sendImmediately: true
- }
- })
- imageFile.on('error', err => {
- throw err
- })
- req.on('error', err => {
- throw err
- })
- req.on('response', res => {
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload file ${res.statusCode}`)
- }
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.exampleProjectId)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/1pixel.png')
- expect(update.url).to.be.a('string')
- verifyVersionIncremented(this, version, 1, done)
- })
- imageFile.pipe(req)
- })
- describe('when there are files in the project', function() {
- beforeEach(function(done) {
- uploadExampleFile(this, () => {
- createExampleDoc(this, () => {
- ProjectGetter.getProject(
- this.exampleProjectId,
- (error, project) => {
- if (error) {
- throw error
- }
- MockDocUpdaterApi.clearProjectStructureUpdates()
- this.project0 = project
- done()
- }
- )
- })
- })
- })
- it('should version replacing a file', function(done) {
- const imageFile = fs.createReadStream(
- Path.resolve(Path.join(__dirname, '..', 'files', '2pixel.png'))
- )
- const req = this.owner.request.post({
- uri: `/user/${this.owner._id}/update/${
- this.tpdsProjectName
- }/1pixel.png`,
- auth: {
- user: _.keys(Settings.httpAuthUsers)[0],
- pass: _.values(Settings.httpAuthUsers)[0],
- sendImmediately: true
- }
- })
- imageFile.on('error', err => {
- throw err
- })
- req.on('error', err => {
- throw err
- })
- req.on('response', res => {
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload file ${res.statusCode}`)
- }
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(
- this.exampleProjectId
- )
- expect(updates.length).to.equal(2)
- let update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/1pixel.png')
- // expect(update.url).to.be.a('string');
- update = updates[1]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/1pixel.png')
- expect(update.url).to.be.a('string')
- verifyVersionIncremented(this, version, 1, done)
- })
- imageFile.pipe(req)
- })
- it('should version deleting a doc', function(done) {
- this.owner.request.delete(
- {
- uri: `/user/${this.owner._id}/update/${
- this.tpdsProjectName
- }/new.tex`,
- auth: {
- user: _.keys(Settings.httpAuthUsers)[0],
- pass: _.values(Settings.httpAuthUsers)[0],
- sendImmediately: true
- }
- },
- (error, res) => {
- if (error) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to delete doc ${res.statusCode}`)
- }
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(
- this.exampleProjectId
- )
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/new.tex')
- expect(update.newPathname).to.equal('')
- verifyVersionIncremented(this, version, 1, done)
- }
- )
- })
- })
- })
- describe('uploading a document', function() {
- beforeEach(function(done) {
- createExampleProject(this, () => {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- done()
- })
- })
- describe('with an unusual character set', function() {
- it('should correctly handle utf16-le data', function(done) {
- uploadFile(
- this,
- 'charsets/test-greek-utf16-le-bom.tex',
- 'test-greek-utf16-le-bom.tex',
- 'text/x-tex',
- () => {
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(
- this.exampleProjectId
- )
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.pathname).to.equal('/test-greek-utf16-le-bom.tex')
- expect(update.docLines).to.contain(
- 'Η γρήγορη καστανή αλεπού πήδηξε χαλαρά πάνω από το σκυλί.'
- )
- done()
- }
- )
- })
- it('should correctly handle windows1252/iso-8859-1/latin1 data', function(done) {
- uploadFile(
- this,
- 'charsets/test-german-windows-1252.tex',
- 'test-german-windows-1252.tex',
- 'text/x-tex',
- () => {
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(
- this.exampleProjectId
- )
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.pathname).to.equal('/test-german-windows-1252.tex')
- expect(update.docLines).to.contain(
- 'Der schnelle braune Fuchs sprang träge über den Hund.'
- )
- done()
- }
- )
- })
- })
- })
- })
|