RestoringFilesTest.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const async = require('async')
  15. const { expect } = require('chai')
  16. const _ = require('underscore')
  17. const fs = require('fs')
  18. const Path = require('path')
  19. const ProjectGetter = require('../../../app/src/Features/Project/ProjectGetter.js')
  20. const User = require('./helpers/User')
  21. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  22. const MockDocstoreApi = require('./helpers/MockDocstoreApi')
  23. const MockFileStoreApi = require('./helpers/MockFileStoreApi')
  24. describe('RestoringFiles', function() {
  25. beforeEach(function(done) {
  26. this.owner = new User()
  27. return this.owner.login(error => {
  28. if (error != null) {
  29. throw error
  30. }
  31. return this.owner.createProject(
  32. 'example-project',
  33. { template: 'example' },
  34. (error, project_id) => {
  35. this.project_id = project_id
  36. if (error != null) {
  37. throw error
  38. }
  39. return done()
  40. }
  41. )
  42. })
  43. })
  44. describe('restoring a deleted doc', function() {
  45. beforeEach(function(done) {
  46. return this.owner.getProject(this.project_id, (error, project) => {
  47. if (error != null) {
  48. throw error
  49. }
  50. this.doc = _.find(
  51. project.rootFolder[0].docs,
  52. doc => doc.name === 'main.tex'
  53. )
  54. return this.owner.request(
  55. {
  56. method: 'DELETE',
  57. url: `/project/${this.project_id}/doc/${this.doc._id}`
  58. },
  59. (error, response, body) => {
  60. if (error != null) {
  61. throw error
  62. }
  63. expect(response.statusCode).to.equal(204)
  64. return this.owner.request(
  65. {
  66. method: 'POST',
  67. url: `/project/${this.project_id}/doc/${this.doc._id}/restore`,
  68. json: {
  69. name: 'main.tex'
  70. }
  71. },
  72. (error, response, body) => {
  73. if (error != null) {
  74. throw error
  75. }
  76. expect(response.statusCode).to.equal(200)
  77. expect(body.doc_id).to.exist
  78. this.restored_doc_id = body.doc_id
  79. return done()
  80. }
  81. )
  82. }
  83. )
  84. })
  85. })
  86. it('should have restored the doc', function(done) {
  87. return this.owner.getProject(this.project_id, (error, project) => {
  88. if (error != null) {
  89. throw error
  90. }
  91. const restored_doc = _.find(
  92. project.rootFolder[0].docs,
  93. doc => doc.name === 'main.tex'
  94. )
  95. expect(restored_doc._id.toString()).to.equal(this.restored_doc_id)
  96. expect(this.doc._id).to.not.equal(this.restored_doc_id)
  97. expect(
  98. MockDocstoreApi.docs[this.project_id][this.restored_doc_id].lines
  99. ).to.deep.equal(
  100. MockDocstoreApi.docs[this.project_id][this.doc._id].lines
  101. )
  102. return done()
  103. })
  104. })
  105. })
  106. describe('restoring from v2 history', function() {
  107. describe('restoring a text file', function() {
  108. beforeEach(function(done) {
  109. MockProjectHistoryApi.addOldFile(
  110. this.project_id,
  111. 42,
  112. 'foo.tex',
  113. 'hello world, this is foo.tex!'
  114. )
  115. return this.owner.request(
  116. {
  117. method: 'POST',
  118. url: `/project/${this.project_id}/restore_file`,
  119. json: {
  120. pathname: 'foo.tex',
  121. version: 42
  122. }
  123. },
  124. (error, response, body) => {
  125. if (error != null) {
  126. throw error
  127. }
  128. expect(response.statusCode).to.equal(200)
  129. return done()
  130. }
  131. )
  132. })
  133. it('should have created a doc', function(done) {
  134. return this.owner.getProject(this.project_id, (error, project) => {
  135. if (error != null) {
  136. throw error
  137. }
  138. let doc = _.find(
  139. project.rootFolder[0].docs,
  140. doc => doc.name === 'foo.tex'
  141. )
  142. doc = MockDocstoreApi.docs[this.project_id][doc._id]
  143. expect(doc.lines).to.deep.equal(['hello world, this is foo.tex!'])
  144. return done()
  145. })
  146. })
  147. })
  148. describe('restoring a binary file', function() {
  149. beforeEach(function(done) {
  150. this.pngData = fs.readFileSync(
  151. Path.resolve(__dirname, '../files/1pixel.png'),
  152. 'binary'
  153. )
  154. MockProjectHistoryApi.addOldFile(
  155. this.project_id,
  156. 42,
  157. 'image.png',
  158. this.pngData
  159. )
  160. return this.owner.request(
  161. {
  162. method: 'POST',
  163. url: `/project/${this.project_id}/restore_file`,
  164. json: {
  165. pathname: 'image.png',
  166. version: 42
  167. }
  168. },
  169. (error, response, body) => {
  170. if (error != null) {
  171. throw error
  172. }
  173. expect(response.statusCode).to.equal(200)
  174. return done()
  175. }
  176. )
  177. })
  178. it('should have created a file', function(done) {
  179. return this.owner.getProject(this.project_id, (error, project) => {
  180. if (error != null) {
  181. throw error
  182. }
  183. let file = _.find(
  184. project.rootFolder[0].fileRefs,
  185. file => file.name === 'image.png'
  186. )
  187. file = MockFileStoreApi.files[this.project_id][file._id]
  188. expect(file.content).to.equal(this.pngData)
  189. return done()
  190. })
  191. })
  192. })
  193. describe('restoring to a directory that exists', function() {
  194. beforeEach(function(done) {
  195. MockProjectHistoryApi.addOldFile(
  196. this.project_id,
  197. 42,
  198. 'foldername/foo2.tex',
  199. 'hello world, this is foo-2.tex!'
  200. )
  201. return this.owner.request.post(
  202. {
  203. uri: `project/${this.project_id}/folder`,
  204. json: {
  205. name: 'foldername'
  206. }
  207. },
  208. (error, response, body) => {
  209. if (error != null) {
  210. throw error
  211. }
  212. expect(response.statusCode).to.equal(200)
  213. return this.owner.request(
  214. {
  215. method: 'POST',
  216. url: `/project/${this.project_id}/restore_file`,
  217. json: {
  218. pathname: 'foldername/foo2.tex',
  219. version: 42
  220. }
  221. },
  222. (error, response, body) => {
  223. if (error != null) {
  224. throw error
  225. }
  226. expect(response.statusCode).to.equal(200)
  227. return done()
  228. }
  229. )
  230. }
  231. )
  232. })
  233. it('should have created the doc in the named folder', function(done) {
  234. return this.owner.getProject(this.project_id, (error, project) => {
  235. if (error != null) {
  236. throw error
  237. }
  238. const folder = _.find(
  239. project.rootFolder[0].folders,
  240. folder => folder.name === 'foldername'
  241. )
  242. let doc = _.find(folder.docs, doc => doc.name === 'foo2.tex')
  243. doc = MockDocstoreApi.docs[this.project_id][doc._id]
  244. expect(doc.lines).to.deep.equal(['hello world, this is foo-2.tex!'])
  245. return done()
  246. })
  247. })
  248. })
  249. describe('restoring to a directory that no longer exists', function() {
  250. beforeEach(function(done) {
  251. MockProjectHistoryApi.addOldFile(
  252. this.project_id,
  253. 42,
  254. 'nothere/foo3.tex',
  255. 'hello world, this is foo-3.tex!'
  256. )
  257. return this.owner.request(
  258. {
  259. method: 'POST',
  260. url: `/project/${this.project_id}/restore_file`,
  261. json: {
  262. pathname: 'nothere/foo3.tex',
  263. version: 42
  264. }
  265. },
  266. (error, response, body) => {
  267. if (error != null) {
  268. throw error
  269. }
  270. expect(response.statusCode).to.equal(200)
  271. return done()
  272. }
  273. )
  274. })
  275. it('should have created the folder and restored the doc to it', function(done) {
  276. return this.owner.getProject(this.project_id, (error, project) => {
  277. if (error != null) {
  278. throw error
  279. }
  280. const folder = _.find(
  281. project.rootFolder[0].folders,
  282. folder => folder.name === 'nothere'
  283. )
  284. expect(folder).to.exist
  285. let doc = _.find(folder.docs, doc => doc.name === 'foo3.tex')
  286. doc = MockDocstoreApi.docs[this.project_id][doc._id]
  287. expect(doc.lines).to.deep.equal(['hello world, this is foo-3.tex!'])
  288. return done()
  289. })
  290. })
  291. })
  292. describe('restoring to a filename that already exists', function() {
  293. beforeEach(function(done) {
  294. MockProjectHistoryApi.addOldFile(
  295. this.project_id,
  296. 42,
  297. 'main.tex',
  298. 'hello world, this is main.tex!'
  299. )
  300. return this.owner.request(
  301. {
  302. method: 'POST',
  303. url: `/project/${this.project_id}/restore_file`,
  304. json: {
  305. pathname: 'main.tex',
  306. version: 42
  307. }
  308. },
  309. (error, response, body) => {
  310. if (error != null) {
  311. throw error
  312. }
  313. expect(response.statusCode).to.equal(200)
  314. return done()
  315. }
  316. )
  317. })
  318. it('should have created the doc in the root folder', function(done) {
  319. return this.owner.getProject(this.project_id, (error, project) => {
  320. if (error != null) {
  321. throw error
  322. }
  323. let doc = _.find(project.rootFolder[0].docs, doc =>
  324. doc.name.match(/main \(Restored on/)
  325. )
  326. expect(doc).to.exist
  327. doc = MockDocstoreApi.docs[this.project_id][doc._id]
  328. expect(doc.lines).to.deep.equal(['hello world, this is main.tex!'])
  329. return done()
  330. })
  331. })
  332. })
  333. })
  334. })