| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505 |
- /* eslint-disable
- camelcase,
- max-len,
- no-path-concat,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const async = require('async')
- 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')
- const MockFileStoreApi = require('./helpers/MockFileStoreApi')
- const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
- const request = require('./helpers/request')
- const User = require('./helpers/User')
- describe('ProjectStructureChanges', function() {
- let example_project_id = null
- let example_doc_id = null
- let example_file_id = null
- let example_folder_id_1 = null
- let example_folder_id_2 = null
- before(function(done) {
- this.owner = new User()
- return this.owner.login(done)
- })
- describe('creating a project from the example template', function() {
- before(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return this.owner.createProject(
- 'example-project',
- { template: 'example' },
- (error, project_id) => {
- if (error != null) {
- throw error
- }
- example_project_id = project_id
- return done()
- }
- )
- })
- it('should version creating a doc', function() {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- expect(updates.length).to.equal(2)
- _.each(updates, update => {
- expect(update.userId).to.equal(this.owner._id)
- return 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
- )
- return expect(version).to.equal(3)
- })
- it('should version creating a file', function() {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- 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')
- return expect(version).to.equal(3)
- })
- })
- describe('duplicating a project', function() {
- before(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return this.owner.request.post(
- {
- uri: `/Project/${example_project_id}/clone`,
- json: {
- projectName: 'new.tex'
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to add doc ${res.statusCode}`)
- }
- this.dup_project_id = body.project_id
- return done()
- }
- )
- })
- it('should version the docs created', function() {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.dup_project_id)
- expect(updates.length).to.equal(2)
- _.each(updates, update => {
- expect(update.userId).to.equal(this.owner._id)
- return 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
- )
- return expect(version).to.equal(3)
- })
- it('should version the files created', function() {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.dup_project_id)
- 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')
- return expect(version).to.equal(3)
- })
- })
- describe('adding a doc', function() {
- before(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return ProjectGetter.getProject(example_project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- this.project_0 = project
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/doc`,
- json: {
- name: 'new.tex',
- parent_folder_id: project.rootFolder[0]._id
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to add doc ${res.statusCode}`)
- }
- example_doc_id = body._id
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- return done()
- }
- )
- }
- )
- })
- })
- it('should version the doc added', function() {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- 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')
- return expect(version).to.equal(this.project_0.version + 1)
- })
- it('should increment the project structure version number', function() {
- return expect(this.project_1.version).to.equal(this.project_0.version + 1)
- })
- })
- describe('uploading a project', function() {
- before(function(done) {
- let req
- MockDocUpdaterApi.clearProjectStructureUpdates()
- const zip_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/test_project.zip')
- )
- this.test_project_name = 'wombat'
- return (req = this.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zip_file
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload project ${res.statusCode}`)
- }
- this.uploaded_project_id = JSON.parse(body).project_id
- return done()
- }
- ))
- })
- it('should version the docs created', function() {
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploaded_project_id)
- 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')
- return expect(version).to.equal(2)
- })
- it('should version the files created', function() {
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploaded_project_id)
- 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')
- return expect(version).to.equal(2)
- })
- })
- describe('uploading a project with a name', function() {
- before(function(done) {
- let req
- MockDocUpdaterApi.clearProjectStructureUpdates()
- const zip_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/test_project_with_name.zip')
- )
- this.test_project_name = 'wombat'
- return (req = this.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zip_file
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload project ${res.statusCode}`)
- }
- this.uploaded_project_id = JSON.parse(body).project_id
- return done()
- }
- ))
- })
- it('should set the project name from the zip contents', function(done) {
- return ProjectGetter.getProject(
- this.uploaded_project_id,
- (error, project) => {
- expect(error).not.to.exist
- expect(project.name).to.equal(this.test_project_name)
- return done()
- }
- )
- })
- })
- describe('uploading a project with an invalid name', function() {
- before(function(done) {
- let req
- MockDocUpdaterApi.clearProjectStructureUpdates()
- const zip_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/test_project_with_invalid_name.zip')
- )
- this.test_project_match = /^bad[^\\]+name$/
- return (req = this.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zip_file
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload project ${res.statusCode}`)
- }
- this.uploaded_project_id = JSON.parse(body).project_id
- return done()
- }
- ))
- })
- it('should set the project name from the zip contents', function(done) {
- return ProjectGetter.getProject(
- this.uploaded_project_id,
- (error, project) => {
- expect(error).not.to.exist
- expect(project.name).to.match(this.test_project_match)
- return done()
- }
- )
- })
- })
- describe('uploading an empty zipfile', function() {
- let zipFile = null
- before(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- zipFile = fs.createReadStream(
- Path.resolve(__dirname + '/../files/test_project_empty.zip')
- )
- done()
- })
- it('should fail with 422 error', function(done) {
- this.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zipFile
- }
- },
- (error, res) => {
- if (error != null) {
- throw error
- }
- expect(res.statusCode).to.equal(422)
- done()
- }
- )
- })
- })
- describe('uploading a zipfile containing only empty directories', function() {
- let zipFile = null
- before(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- zipFile = fs.createReadStream(
- Path.resolve(__dirname + '/../files/test_project_with_empty_folder.zip')
- )
- done()
- })
- it('should fail with 422 error', function(done) {
- this.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zipFile
- }
- },
- (error, res) => {
- if (error != null) {
- throw error
- }
- expect(res.statusCode).to.equal(422)
- done()
- }
- )
- })
- })
- describe('uploading a project with a shared top-level folder', function() {
- before(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- const zip_file = fs.createReadStream(
- Path.resolve(
- __dirname + '/../files/test_project_with_shared_top_level_folder.zip'
- )
- )
- return this.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zip_file
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload project ${res.statusCode}`)
- }
- this.uploaded_project_id = JSON.parse(body).project_id
- return done()
- }
- )
- })
- it('should not create the top-level folder', function(done) {
- return ProjectGetter.getProject(this.uploaded_project_id, function(
- 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)
- return done()
- })
- })
- })
- describe('uploading a project with backslashes in the path names', function() {
- before(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- const zip_file = fs.createReadStream(
- Path.resolve(
- __dirname + '/../files/test_project_with_backslash_in_filename.zip'
- )
- )
- return this.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zip_file
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload project ${res.statusCode}`)
- }
- this.uploaded_project_id = JSON.parse(body).project_id
- return done()
- }
- )
- })
- it('should treat the backslash as a directory separator', function(done) {
- return ProjectGetter.getProject(this.uploaded_project_id, function(
- 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')
- return done()
- })
- })
- })
- describe('uploading a project with files in different encodings', function() {
- before(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- const zip_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/charsets/charsets.zip')
- )
- return this.owner.request.post(
- {
- uri: 'project/new/upload',
- formData: {
- qqfile: zip_file
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload project ${res.statusCode}`)
- }
- this.uploaded_project_id = JSON.parse(body).project_id
- return done()
- }
- )
- })
- it('should correctly parse windows-1252', function() {
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploaded_project_id)
- const update = _.find(
- updates,
- update => update.pathname === '/test-german-windows-1252.tex'
- )
- return 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.uploaded_project_id)
- const update = _.find(
- updates,
- update => update.pathname === '/test-german-utf8x.tex'
- )
- return 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.uploaded_project_id)
- const update = _.find(
- updates,
- update => update.pathname === '/test-greek-utf16-le-bom.tex'
- )
- return expect(update.docLines).to.contain(
- 'Η γρήγορη καστανή αλεπού πήδηξε χαλαρά πάνω από το σκυλί.'
- )
- })
- it('should correctly parse Greek utf8', function() {
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(this.uploaded_project_id)
- const update = _.find(
- updates,
- update => update.pathname === '/test-greek-utf8x.tex'
- )
- return expect(update.docLines).to.contain(
- 'Η γρήγορη καστανή αλεπού πήδηξε χαλαρά πάνω από το σκυλί.'
- )
- })
- })
- describe('uploading a file', function() {
- beforeEach(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return ProjectGetter.getProject(example_project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- this.root_folder_id = project.rootFolder[0]._id.toString()
- this.project_0 = project
- return done()
- })
- })
- it('should version a newly uploaded file', function(done) {
- let req
- const image_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/1pixel.png')
- )
- return (req = this.owner.request.post(
- {
- uri: `project/${example_project_id}/upload`,
- qs: {
- folder_id: this.root_folder_id
- },
- formData: {
- qqfile: {
- value: image_file,
- options: {
- filename: '1pixel.png',
- contentType: 'image/png'
- }
- }
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload file ${res.statusCode}`)
- }
- example_file_id = JSON.parse(body).entity_id
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- 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')
- this.original_file_url = update.url
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // uploading a new file does change the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 1
- )
- return done()
- }
- )
- }
- ))
- })
- it('should version a replacement file', function(done) {
- let req
- const image_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/2pixel.png')
- )
- return (req = this.owner.request.post(
- {
- uri: `project/${example_project_id}/upload`,
- qs: {
- folder_id: this.root_folder_id
- },
- formData: {
- qqfile: {
- value: image_file,
- options: {
- filename: '1pixel.png',
- contentType: 'image/png'
- }
- }
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload file ${res.statusCode}`)
- }
- example_file_id = JSON.parse(body).entity_id
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- 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')
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 1
- )
- return done()
- }
- )
- }
- ))
- })
- })
- describe('moving entities', function() {
- before(function(done) {
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/folder`,
- json: {
- name: 'foo'
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- example_folder_id_1 = body._id
- return done()
- }
- )
- })
- beforeEach(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return ProjectGetter.getProject(example_project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- this.root_folder_id = project.rootFolder[0]._id.toString()
- this.project_0 = project
- return done()
- })
- })
- it('should version moving a doc', function(done) {
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/Doc/${example_doc_id}/move`,
- json: {
- folder_id: example_folder_id_1
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to move doc ${res.statusCode}`)
- }
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- 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')
- expect(version).to.equal(this.project_0.version + 2)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 2
- ) // 2 because it's a delete and then add
- return done()
- }
- )
- }
- )
- })
- it('should version moving a file', function(done) {
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/File/${example_file_id}/move`,
- json: {
- folder_id: example_folder_id_1
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to move file ${res.statusCode}`)
- }
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- 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')
- expect(version).to.equal(this.project_0.version + 2)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 2
- ) // 2 because it's a delete and then add
- return done()
- }
- )
- }
- )
- })
- it('should version moving a folder', function(done) {
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/folder`,
- json: {
- name: 'bar'
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- example_folder_id_2 = body._id
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/Folder/${example_folder_id_1}/move`,
- json: {
- folder_id: example_folder_id_2
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to move folder ${res.statusCode}`)
- }
- let {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(
- example_project_id
- )
- 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')
- expect(version).to.equal(this.project_0.version + 3)
- ;({
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(
- example_project_id
- ))
- expect(updates.length).to.equal(1)
- update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/foo/1pixel.png')
- expect(update.newPathname).to.equal('/bar/foo/1pixel.png')
- expect(version).to.equal(this.project_0.version + 3)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 3
- ) // because folder and 2 files move
- return done()
- }
- )
- }
- )
- }
- )
- })
- })
- describe('renaming entities', function() {
- beforeEach(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return ProjectGetter.getProject(example_project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- this.root_folder_id = project.rootFolder[0]._id.toString()
- this.project_0 = project
- return done()
- })
- })
- it('should version renaming a doc', function(done) {
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/Doc/${example_doc_id}/rename`,
- json: {
- name: 'new_renamed.tex'
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to move doc ${res.statusCode}`)
- }
- const {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/bar/foo/new.tex')
- expect(update.newPathname).to.equal('/bar/foo/new_renamed.tex')
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 1
- )
- return done()
- }
- )
- }
- )
- })
- it('should version renaming a file', function(done) {
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/File/${example_file_id}/rename`,
- json: {
- name: '1pixel_renamed.png'
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to move file ${res.statusCode}`)
- }
- const {
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- expect(updates.length).to.equal(1)
- const update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/bar/foo/1pixel.png')
- expect(update.newPathname).to.equal('/bar/foo/1pixel_renamed.png')
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 1
- )
- return done()
- }
- )
- }
- )
- })
- it('should version renaming a folder', function(done) {
- return this.owner.request.post(
- {
- uri: `project/${example_project_id}/Folder/${example_folder_id_1}/rename`,
- json: {
- name: 'foo_renamed'
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to move folder ${res.statusCode}`)
- }
- let {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- expect(updates.length).to.equal(1)
- let update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/bar/foo/new_renamed.tex')
- expect(update.newPathname).to.equal(
- '/bar/foo_renamed/new_renamed.tex'
- )
- expect(version).to.equal(this.project_0.version + 1)
- ;({
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id))
- expect(updates.length).to.equal(1)
- update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/bar/foo/1pixel_renamed.png')
- expect(update.newPathname).to.equal(
- '/bar/foo_renamed/1pixel_renamed.png'
- )
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 1
- )
- return done()
- }
- )
- }
- )
- })
- })
- describe('deleting entities', function() {
- beforeEach(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return ProjectGetter.getProject(example_project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- this.root_folder_id = project.rootFolder[0]._id.toString()
- this.project_0 = project
- return done()
- })
- })
- it('should version deleting a folder', function(done) {
- return this.owner.request.delete(
- {
- uri: `project/${example_project_id}/Folder/${example_folder_id_2}`
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to delete folder ${res.statusCode}`)
- }
- let {
- docUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- expect(updates.length).to.equal(1)
- let update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal('/bar/foo_renamed/new_renamed.tex')
- expect(update.newPathname).to.equal('')
- expect(version).to.equal(this.project_0.version + 1)
- ;({
- fileUpdates: updates,
- version
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id))
- expect(updates.length).to.equal(1)
- update = updates[0]
- expect(update.userId).to.equal(this.owner._id)
- expect(update.pathname).to.equal(
- '/bar/foo_renamed/1pixel_renamed.png'
- )
- expect(update.newPathname).to.equal('')
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- example_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 1
- )
- return done()
- }
- )
- }
- )
- })
- })
- describe('tpds', function() {
- before(function(done) {
- this.tpds_project_name = `tpds-project-${new ObjectId().toString()}`
- return this.owner.createProject(
- this.tpds_project_name,
- (error, project_id) => {
- if (error != null) {
- throw error
- }
- this.tpds_project_id = project_id
- return mkdirp(Settings.path.dumpFolder, done)
- }
- )
- })
- beforeEach(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return ProjectGetter.getProject(
- this.tpds_project_id,
- (error, project) => {
- if (error != null) {
- throw error
- }
- this.root_folder_id = project.rootFolder[0]._id.toString()
- this.project_0 = project
- return done()
- }
- )
- })
- it('should version adding a doc', function(done) {
- const tex_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/test.tex')
- )
- const req = this.owner.request.post({
- uri: `/user/${this.owner._id}/update/${
- this.tpds_project_name
- }/test.tex`,
- auth: {
- user: _.keys(Settings.httpAuthUsers)[0],
- pass: _.values(Settings.httpAuthUsers)[0],
- sendImmediately: true
- }
- })
- tex_file.on('error', function(err) {
- throw err
- })
- req.on('error', function(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.tpds_project_id)
- 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')
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- this.tpds_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(this.project_0.version + 1)
- return done()
- }
- )
- })
- return tex_file.pipe(req)
- })
- it('should version adding a new file', function(done) {
- const image_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/1pixel.png')
- )
- const req = this.owner.request.post({
- uri: `/user/${this.owner._id}/update/${
- this.tpds_project_name
- }/1pixel.png`,
- auth: {
- user: _.keys(Settings.httpAuthUsers)[0],
- pass: _.values(Settings.httpAuthUsers)[0],
- sendImmediately: true
- }
- })
- image_file.on('error', function(err) {
- throw err
- })
- req.on('error', function(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.tpds_project_id)
- 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(this.project_0.version + 1)
- return ProjectGetter.getProject(
- this.tpds_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(this.project_0.version + 1)
- return done()
- }
- )
- })
- return image_file.pipe(req)
- })
- it('should version replacing a file', function(done) {
- const image_file = fs.createReadStream(
- Path.resolve(__dirname + '/../files/2pixel.png')
- )
- const req = this.owner.request.post({
- uri: `/user/${this.owner._id}/update/${
- this.tpds_project_name
- }/1pixel.png`,
- auth: {
- user: _.keys(Settings.httpAuthUsers)[0],
- pass: _.values(Settings.httpAuthUsers)[0],
- sendImmediately: true
- }
- })
- image_file.on('error', function(err) {
- throw err
- })
- req.on('error', function(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.tpds_project_id)
- 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')
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- this.tpds_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(this.project_0.version + 1)
- return done()
- }
- )
- })
- return image_file.pipe(req)
- })
- it('should version deleting a doc', function(done) {
- let req
- return (req = this.owner.request.delete(
- {
- uri: `/user/${this.owner._id}/update/${
- this.tpds_project_name
- }/test.tex`,
- auth: {
- user: _.keys(Settings.httpAuthUsers)[0],
- pass: _.values(Settings.httpAuthUsers)[0],
- sendImmediately: true
- }
- },
- (error, res, body) => {
- if (error != null) {
- 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.tpds_project_id)
- 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.newPathname).to.equal('')
- expect(version).to.equal(this.project_0.version + 1)
- return ProjectGetter.getProject(
- this.tpds_project_id,
- (error, newProject) => {
- if (error != null) {
- throw error
- }
- this.project_1 = newProject
- // replacing a file should update the project structure
- expect(this.project_1.version).to.equal(
- this.project_0.version + 1
- )
- return done()
- }
- )
- }
- ))
- })
- })
- describe('uploading a document', function() {
- beforeEach(function(done) {
- MockDocUpdaterApi.clearProjectStructureUpdates()
- return ProjectGetter.getProject(example_project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- this.root_folder_id = project.rootFolder[0]._id.toString()
- this.project_0 = project
- return done()
- })
- })
- describe('with an unusual character set', function() {
- it('should correctly handle utf16-le data', function(done) {
- let req
- const document_file = fs.createReadStream(
- Path.resolve(
- __dirname + '/../files/charsets/test-greek-utf16-le-bom.tex'
- )
- )
- return (req = this.owner.request.post(
- {
- uri: `project/${example_project_id}/upload`,
- qs: {
- folder_id: this.root_folder_id
- },
- formData: {
- qqfile: {
- value: document_file,
- options: {
- filename: 'test-greek-utf16-le-bom.tex',
- contentType: 'text/x-tex'
- }
- }
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload file ${res.statusCode}`)
- }
- example_file_id = JSON.parse(body).entity_id
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- const update = updates[0]
- expect(update.pathname).to.equal('/test-greek-utf16-le-bom.tex')
- expect(update.docLines).to.contain(
- 'Η γρήγορη καστανή αλεπού πήδηξε χαλαρά πάνω από το σκυλί.'
- )
- return done()
- }
- ))
- })
- it('should correctly handle windows1252/iso-8859-1/latin1 data', function(done) {
- let req
- const document_file = fs.createReadStream(
- Path.resolve(
- __dirname + '/../files/charsets/test-german-windows-1252.tex'
- )
- )
- return (req = this.owner.request.post(
- {
- uri: `project/${example_project_id}/upload`,
- qs: {
- folder_id: this.root_folder_id
- },
- formData: {
- qqfile: {
- value: document_file,
- options: {
- filename: 'test-german-windows-1252.tex',
- contentType: 'text/x-tex'
- }
- }
- }
- },
- (error, res, body) => {
- if (error != null) {
- throw error
- }
- if (res.statusCode < 200 || res.statusCode >= 300) {
- throw new Error(`failed to upload file ${res.statusCode}`)
- }
- example_file_id = JSON.parse(body).entity_id
- const {
- docUpdates: updates
- } = MockDocUpdaterApi.getProjectStructureUpdates(example_project_id)
- 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.'
- )
- return done()
- }
- ))
- })
- })
- })
- })
|