2018-03-08 12:24:54 -05:00
|
|
|
async = require "async"
|
|
|
|
expect = require("chai").expect
|
2018-03-09 11:36:10 -05:00
|
|
|
_ = require 'underscore'
|
2018-03-08 12:24:54 -05:00
|
|
|
|
|
|
|
ProjectGetter = require "../../../app/js/Features/Project/ProjectGetter.js"
|
|
|
|
|
|
|
|
User = require "./helpers/User"
|
|
|
|
MockProjectHistoryApi = require "./helpers/MockProjectHistoryApi"
|
|
|
|
MockDocstoreApi = require "./helpers/MockDocstoreApi"
|
2018-03-09 11:36:10 -05:00
|
|
|
MockFileStoreApi = require "./helpers/MockFileStoreApi"
|
2018-03-08 12:24:54 -05:00
|
|
|
|
|
|
|
describe "RestoringFiles", ->
|
|
|
|
before (done) ->
|
|
|
|
@owner = new User()
|
|
|
|
@owner.login (error) =>
|
|
|
|
throw error if error?
|
|
|
|
@owner.createProject "example-project", {template: "example"}, (error, @project_id) =>
|
|
|
|
throw error if error?
|
|
|
|
done()
|
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
describe "restoring a deleted doc", ->
|
2018-03-08 12:24:54 -05:00
|
|
|
beforeEach (done) ->
|
2018-03-16 08:21:07 -04:00
|
|
|
@owner.getProject @project_id, (error, project) =>
|
2018-03-08 12:24:54 -05:00
|
|
|
throw error if error?
|
2018-03-16 08:21:07 -04:00
|
|
|
@doc = _.find project.rootFolder[0].docs, (doc) ->
|
|
|
|
doc.name == 'main.tex'
|
|
|
|
@owner.request {
|
|
|
|
method: "DELETE",
|
|
|
|
url: "/project/#{@project_id}/doc/#{@doc._id}",
|
|
|
|
}, (error, response, body) =>
|
|
|
|
throw error if error?
|
|
|
|
expect(response.statusCode).to.equal 204
|
|
|
|
@owner.request {
|
|
|
|
method: "POST",
|
|
|
|
url: "/project/#{@project_id}/doc/#{@doc._id}/restore"
|
|
|
|
json:
|
|
|
|
name: "main.tex"
|
|
|
|
}, (error, response, body) =>
|
|
|
|
throw error if error?
|
|
|
|
expect(response.statusCode).to.equal 200
|
|
|
|
expect(body.doc_id).to.exist
|
|
|
|
@restored_doc_id = body.doc_id
|
|
|
|
done()
|
2018-03-08 12:24:54 -05:00
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
it 'should have restored the doc', (done) ->
|
2018-03-08 12:24:54 -05:00
|
|
|
@owner.getProject @project_id, (error, project) =>
|
|
|
|
throw error if error?
|
2018-03-16 08:21:07 -04:00
|
|
|
restored_doc = _.find project.rootFolder[0].docs, (doc) ->
|
|
|
|
doc.name == 'main.tex'
|
|
|
|
expect(restored_doc._id.toString()).to.equal @restored_doc_id
|
|
|
|
expect(@doc._id).to.not.equal @restored_doc_id
|
|
|
|
# console.log @doc_id, @restored_doc_id, MockDocstoreApi.docs[@project_id]
|
|
|
|
expect(MockDocstoreApi.docs[@project_id][@restored_doc_id].lines).to.deep.equal(
|
|
|
|
MockDocstoreApi.docs[@project_id][@doc._id].lines
|
|
|
|
)
|
2018-03-08 12:24:54 -05:00
|
|
|
done()
|
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
describe "restoring from v2 history", ->
|
|
|
|
describe "restoring a text file", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
MockProjectHistoryApi.addOldFile(@project_id, 42, "foo.tex", "hello world, this is foo.tex!")
|
|
|
|
@owner.request {
|
|
|
|
method: "POST",
|
|
|
|
url: "/project/#{@project_id}/restore_file",
|
|
|
|
json:
|
|
|
|
pathname: "foo.tex"
|
|
|
|
version: 42
|
|
|
|
}, (error, response, body) ->
|
|
|
|
throw error if error?
|
|
|
|
expect(response.statusCode).to.equal 200
|
|
|
|
done()
|
2018-03-09 11:36:10 -05:00
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
it "should have created a doc", (done) ->
|
|
|
|
@owner.getProject @project_id, (error, project) =>
|
|
|
|
throw error if error?
|
|
|
|
doc = _.find project.rootFolder[0].docs, (doc) ->
|
|
|
|
doc.name == 'foo.tex'
|
|
|
|
doc = MockDocstoreApi.docs[@project_id][doc._id]
|
|
|
|
expect(doc.lines).to.deep.equal [
|
|
|
|
"hello world, this is foo.tex!"
|
|
|
|
]
|
|
|
|
done()
|
2018-03-09 11:36:10 -05:00
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
describe "restoring a binary file", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
MockProjectHistoryApi.addOldFile(@project_id, 42, "image.png", "Mock image.png content")
|
2018-03-09 11:36:10 -05:00
|
|
|
@owner.request {
|
|
|
|
method: "POST",
|
|
|
|
url: "/project/#{@project_id}/restore_file",
|
|
|
|
json:
|
2018-03-16 08:21:07 -04:00
|
|
|
pathname: "image.png"
|
2018-03-09 11:36:10 -05:00
|
|
|
version: 42
|
|
|
|
}, (error, response, body) ->
|
|
|
|
throw error if error?
|
2018-03-16 08:21:07 -04:00
|
|
|
expect(response.statusCode).to.equal 200
|
2018-03-09 11:36:10 -05:00
|
|
|
done()
|
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
it "should have created a file", (done) ->
|
|
|
|
@owner.getProject @project_id, (error, project) =>
|
|
|
|
throw error if error?
|
|
|
|
file = _.find project.rootFolder[0].fileRefs, (file) ->
|
|
|
|
file.name == 'image.png'
|
|
|
|
file = MockFileStoreApi.files[@project_id][file._id]
|
|
|
|
expect(file.content).to.equal "Mock image.png content"
|
|
|
|
done()
|
2018-03-08 12:24:54 -05:00
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
describe "restoring to a directory that exists", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
MockProjectHistoryApi.addOldFile(@project_id, 42, "foldername/foo2.tex", "hello world, this is foo-2.tex!")
|
|
|
|
@owner.request.post {
|
|
|
|
uri: "project/#{@project_id}/folder",
|
|
|
|
json:
|
|
|
|
name: 'foldername'
|
|
|
|
}, (error, response, body) =>
|
|
|
|
throw error if error?
|
|
|
|
expect(response.statusCode).to.equal 200
|
|
|
|
@owner.request {
|
|
|
|
method: "POST",
|
|
|
|
url: "/project/#{@project_id}/restore_file",
|
|
|
|
json:
|
|
|
|
pathname: "foldername/foo2.tex"
|
|
|
|
version: 42
|
|
|
|
}, (error, response, body) ->
|
|
|
|
throw error if error?
|
|
|
|
expect(response.statusCode).to.equal 200
|
|
|
|
done()
|
2018-03-09 11:36:10 -05:00
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
it "should have created the doc in the named folder", (done) ->
|
|
|
|
@owner.getProject @project_id, (error, project) =>
|
|
|
|
throw error if error?
|
|
|
|
folder = _.find project.rootFolder[0].folders, (folder) ->
|
|
|
|
folder.name == 'foldername'
|
|
|
|
doc = _.find folder.docs, (doc) ->
|
|
|
|
doc.name == 'foo2.tex'
|
|
|
|
doc = MockDocstoreApi.docs[@project_id][doc._id]
|
|
|
|
expect(doc.lines).to.deep.equal [
|
|
|
|
"hello world, this is foo-2.tex!"
|
|
|
|
]
|
|
|
|
done()
|
2018-03-08 12:24:54 -05:00
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
describe "restoring to a directory that no longer exists", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
MockProjectHistoryApi.addOldFile(@project_id, 42, "nothere/foo3.tex", "hello world, this is foo-3.tex!")
|
|
|
|
@owner.request {
|
|
|
|
method: "POST",
|
|
|
|
url: "/project/#{@project_id}/restore_file",
|
|
|
|
json:
|
|
|
|
pathname: "nothere/foo3.tex"
|
|
|
|
version: 42
|
|
|
|
}, (error, response, body) ->
|
|
|
|
throw error if error?
|
|
|
|
expect(response.statusCode).to.equal 200
|
|
|
|
done()
|
2018-03-09 11:36:10 -05:00
|
|
|
|
2018-04-06 10:20:51 -04:00
|
|
|
it "should have created the folder and restored the doc to it", (done) ->
|
2018-03-16 08:21:07 -04:00
|
|
|
@owner.getProject @project_id, (error, project) =>
|
|
|
|
throw error if error?
|
2018-04-06 10:20:51 -04:00
|
|
|
folder = _.find project.rootFolder[0].folders, (folder) ->
|
|
|
|
folder.name == 'nothere'
|
|
|
|
expect(folder).to.exist
|
|
|
|
doc = _.find folder.docs, (doc) ->
|
2018-03-16 08:21:07 -04:00
|
|
|
doc.name == 'foo3.tex'
|
|
|
|
doc = MockDocstoreApi.docs[@project_id][doc._id]
|
|
|
|
expect(doc.lines).to.deep.equal [
|
|
|
|
"hello world, this is foo-3.tex!"
|
|
|
|
]
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "restoring to a filename that already exists", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
MockProjectHistoryApi.addOldFile(@project_id, 42, "main.tex", "hello world, this is main.tex!")
|
|
|
|
@owner.request {
|
|
|
|
method: "POST",
|
|
|
|
url: "/project/#{@project_id}/restore_file",
|
|
|
|
json:
|
|
|
|
pathname: "main.tex"
|
|
|
|
version: 42
|
|
|
|
}, (error, response, body) ->
|
|
|
|
throw error if error?
|
|
|
|
expect(response.statusCode).to.equal 200
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should have created the doc in the root folder", (done) ->
|
|
|
|
@owner.getProject @project_id, (error, project) =>
|
|
|
|
throw error if error?
|
|
|
|
doc = _.find project.rootFolder[0].docs, (doc) ->
|
|
|
|
doc.name.match(/main \(Restored on/)
|
|
|
|
expect(doc).to.exist
|
|
|
|
doc = MockDocstoreApi.docs[@project_id][doc._id]
|
|
|
|
expect(doc.lines).to.deep.equal [
|
|
|
|
"hello world, this is main.tex!"
|
|
|
|
]
|
|
|
|
done()
|
2018-03-09 11:36:10 -05:00
|
|
|
|