| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- /* eslint-disable
- max-len,
- 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 { expect } = require('chai')
- const _ = require('underscore')
- const fs = require('fs')
- const Path = require('path')
- const User = require('../../../../../test/acceptance/src/helpers/User')
- const MockProjectHistoryApiClass = require('../../../../../test/acceptance/src/mocks/MockProjectHistoryApi')
- const MockDocstoreApiClass = require('../../../../../test/acceptance/src/mocks/MockDocstoreApi')
- const MockFilestoreApiClass = require('../../../../../test/acceptance/src/mocks/MockFilestoreApi')
- let MockProjectHistoryApi, MockDocstoreApi, MockFilestoreApi
- before(function () {
- MockProjectHistoryApi = MockProjectHistoryApiClass.instance()
- MockDocstoreApi = MockDocstoreApiClass.instance()
- MockFilestoreApi = MockFilestoreApiClass.instance()
- })
- describe('RestoringFiles', function () {
- beforeEach(function (done) {
- this.owner = new User()
- return this.owner.login(error => {
- if (error != null) {
- throw error
- }
- return this.owner.createProject(
- 'example-project',
- { template: 'example' },
- (error, projectId) => {
- this.project_id = projectId
- if (error != null) {
- throw error
- }
- return done()
- }
- )
- })
- })
- describe('restoring from v2 history', function () {
- describe('restoring a text file', function () {
- beforeEach(function (done) {
- MockProjectHistoryApi.addOldFile(
- this.project_id,
- 42,
- 'foo.tex',
- 'hello world, this is foo.tex!'
- )
- return this.owner.request(
- {
- method: 'POST',
- url: `/project/${this.project_id}/restore_file`,
- json: {
- pathname: 'foo.tex',
- version: 42,
- },
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return done()
- }
- )
- })
- it('should have created a doc', function (done) {
- return this.owner.getProject(this.project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- let doc = _.find(
- project.rootFolder[0].docs,
- doc => doc.name === 'foo.tex'
- )
- doc = MockDocstoreApi.docs[this.project_id][doc._id]
- expect(doc.lines).to.deep.equal(['hello world, this is foo.tex!'])
- return done()
- })
- })
- })
- describe('restoring a binary file', function () {
- beforeEach(function (done) {
- this.pngData = fs.readFileSync(
- Path.resolve(
- __dirname,
- '../../../../../test/acceptance/files/1pixel.png'
- ),
- 'binary'
- )
- MockProjectHistoryApi.addOldFile(
- this.project_id,
- 42,
- 'image.png',
- this.pngData
- )
- return this.owner.request(
- {
- method: 'POST',
- url: `/project/${this.project_id}/restore_file`,
- json: {
- pathname: 'image.png',
- version: 42,
- },
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return done()
- }
- )
- })
- it('should have created a file', function (done) {
- return this.owner.getProject(this.project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- let file = _.find(
- project.rootFolder[0].fileRefs,
- file => file.name === 'image.png'
- )
- file = MockFilestoreApi.files[this.project_id][file._id]
- expect(file.content).to.equal(this.pngData)
- return done()
- })
- })
- })
- describe('restoring to a directory that exists', function () {
- beforeEach(function (done) {
- MockProjectHistoryApi.addOldFile(
- this.project_id,
- 42,
- 'foldername/foo2.tex',
- 'hello world, this is foo-2.tex!'
- )
- return this.owner.request.post(
- {
- uri: `project/${this.project_id}/folder`,
- json: {
- name: 'foldername',
- },
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return this.owner.request(
- {
- method: 'POST',
- url: `/project/${this.project_id}/restore_file`,
- json: {
- pathname: 'foldername/foo2.tex',
- version: 42,
- },
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return done()
- }
- )
- }
- )
- })
- it('should have created the doc in the named folder', function (done) {
- return this.owner.getProject(this.project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- const folder = _.find(
- project.rootFolder[0].folders,
- folder => folder.name === 'foldername'
- )
- let doc = _.find(folder.docs, doc => doc.name === 'foo2.tex')
- doc = MockDocstoreApi.docs[this.project_id][doc._id]
- expect(doc.lines).to.deep.equal(['hello world, this is foo-2.tex!'])
- return done()
- })
- })
- })
- describe('restoring to a directory that no longer exists', function () {
- beforeEach(function (done) {
- MockProjectHistoryApi.addOldFile(
- this.project_id,
- 42,
- 'nothere/foo3.tex',
- 'hello world, this is foo-3.tex!'
- )
- return this.owner.request(
- {
- method: 'POST',
- url: `/project/${this.project_id}/restore_file`,
- json: {
- pathname: 'nothere/foo3.tex',
- version: 42,
- },
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return done()
- }
- )
- })
- it('should have created the folder and restored the doc to it', function (done) {
- return this.owner.getProject(this.project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- const folder = _.find(
- project.rootFolder[0].folders,
- folder => folder.name === 'nothere'
- )
- expect(folder).to.exist
- let doc = _.find(folder.docs, doc => doc.name === 'foo3.tex')
- doc = MockDocstoreApi.docs[this.project_id][doc._id]
- expect(doc.lines).to.deep.equal(['hello world, this is foo-3.tex!'])
- return done()
- })
- })
- })
- describe('restoring to a filename that already exists', function () {
- beforeEach(function (done) {
- MockProjectHistoryApi.addOldFile(
- this.project_id,
- 42,
- 'main.tex',
- 'hello world, this is main.tex!'
- )
- return this.owner.request(
- {
- method: 'POST',
- url: `/project/${this.project_id}/restore_file`,
- json: {
- pathname: 'main.tex',
- version: 42,
- },
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return done()
- }
- )
- })
- it('should have created the doc in the root folder', function (done) {
- return this.owner.getProject(this.project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- let doc = _.find(project.rootFolder[0].docs, doc =>
- doc.name.match(/main \(Restored on/)
- )
- expect(doc).to.exist
- doc = MockDocstoreApi.docs[this.project_id][doc._id]
- expect(doc.lines).to.deep.equal(['hello world, this is main.tex!'])
- return done()
- })
- })
- })
- })
- })
|