RestoringFilesTest.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. // console.log @doc_id, @restored_doc_id, MockDocstoreApi.docs[@project_id]
  98. expect(
  99. MockDocstoreApi.docs[this.project_id][this.restored_doc_id].lines
  100. ).to.deep.equal(
  101. MockDocstoreApi.docs[this.project_id][this.doc._id].lines
  102. )
  103. return done()
  104. })
  105. })
  106. })
  107. describe('restoring from v2 history', function() {
  108. describe('restoring a text file', function() {
  109. beforeEach(function(done) {
  110. MockProjectHistoryApi.addOldFile(
  111. this.project_id,
  112. 42,
  113. 'foo.tex',
  114. 'hello world, this is foo.tex!'
  115. )
  116. return this.owner.request(
  117. {
  118. method: 'POST',
  119. url: `/project/${this.project_id}/restore_file`,
  120. json: {
  121. pathname: 'foo.tex',
  122. version: 42
  123. }
  124. },
  125. function(error, response, body) {
  126. if (error != null) {
  127. throw error
  128. }
  129. expect(response.statusCode).to.equal(200)
  130. return done()
  131. }
  132. )
  133. })
  134. it('should have created a doc', function(done) {
  135. return this.owner.getProject(this.project_id, (error, project) => {
  136. if (error != null) {
  137. throw error
  138. }
  139. let doc = _.find(
  140. project.rootFolder[0].docs,
  141. doc => doc.name === 'foo.tex'
  142. )
  143. doc = MockDocstoreApi.docs[this.project_id][doc._id]
  144. expect(doc.lines).to.deep.equal(['hello world, this is foo.tex!'])
  145. return done()
  146. })
  147. })
  148. })
  149. describe('restoring a binary file', function() {
  150. beforeEach(function(done) {
  151. this.pngData = fs.readFileSync(
  152. Path.resolve(__dirname, '../files/1pixel.png'),
  153. 'binary'
  154. )
  155. MockProjectHistoryApi.addOldFile(
  156. this.project_id,
  157. 42,
  158. 'image.png',
  159. this.pngData
  160. )
  161. return this.owner.request(
  162. {
  163. method: 'POST',
  164. url: `/project/${this.project_id}/restore_file`,
  165. json: {
  166. pathname: 'image.png',
  167. version: 42
  168. }
  169. },
  170. function(error, response, body) {
  171. if (error != null) {
  172. throw error
  173. }
  174. expect(response.statusCode).to.equal(200)
  175. return done()
  176. }
  177. )
  178. })
  179. it('should have created a file', function(done) {
  180. return this.owner.getProject(this.project_id, (error, project) => {
  181. if (error != null) {
  182. throw error
  183. }
  184. let file = _.find(
  185. project.rootFolder[0].fileRefs,
  186. file => file.name === 'image.png'
  187. )
  188. file = MockFileStoreApi.files[this.project_id][file._id]
  189. expect(file.content).to.equal(this.pngData)
  190. return done()
  191. })
  192. })
  193. })
  194. describe('restoring to a directory that exists', function() {
  195. beforeEach(function(done) {
  196. MockProjectHistoryApi.addOldFile(
  197. this.project_id,
  198. 42,
  199. 'foldername/foo2.tex',
  200. 'hello world, this is foo-2.tex!'
  201. )
  202. return this.owner.request.post(
  203. {
  204. uri: `project/${this.project_id}/folder`,
  205. json: {
  206. name: 'foldername'
  207. }
  208. },
  209. (error, response, body) => {
  210. if (error != null) {
  211. throw error
  212. }
  213. expect(response.statusCode).to.equal(200)
  214. return this.owner.request(
  215. {
  216. method: 'POST',
  217. url: `/project/${this.project_id}/restore_file`,
  218. json: {
  219. pathname: 'foldername/foo2.tex',
  220. version: 42
  221. }
  222. },
  223. function(error, response, body) {
  224. if (error != null) {
  225. throw error
  226. }
  227. expect(response.statusCode).to.equal(200)
  228. return done()
  229. }
  230. )
  231. }
  232. )
  233. })
  234. it('should have created the doc in the named folder', function(done) {
  235. return this.owner.getProject(this.project_id, (error, project) => {
  236. if (error != null) {
  237. throw error
  238. }
  239. const folder = _.find(
  240. project.rootFolder[0].folders,
  241. folder => folder.name === 'foldername'
  242. )
  243. let doc = _.find(folder.docs, doc => doc.name === 'foo2.tex')
  244. doc = MockDocstoreApi.docs[this.project_id][doc._id]
  245. expect(doc.lines).to.deep.equal(['hello world, this is foo-2.tex!'])
  246. return done()
  247. })
  248. })
  249. })
  250. describe('restoring to a directory that no longer exists', function() {
  251. beforeEach(function(done) {
  252. MockProjectHistoryApi.addOldFile(
  253. this.project_id,
  254. 42,
  255. 'nothere/foo3.tex',
  256. 'hello world, this is foo-3.tex!'
  257. )
  258. return this.owner.request(
  259. {
  260. method: 'POST',
  261. url: `/project/${this.project_id}/restore_file`,
  262. json: {
  263. pathname: 'nothere/foo3.tex',
  264. version: 42
  265. }
  266. },
  267. function(error, response, body) {
  268. if (error != null) {
  269. throw error
  270. }
  271. expect(response.statusCode).to.equal(200)
  272. return done()
  273. }
  274. )
  275. })
  276. it('should have created the folder and restored the doc to it', function(done) {
  277. return this.owner.getProject(this.project_id, (error, project) => {
  278. if (error != null) {
  279. throw error
  280. }
  281. const folder = _.find(
  282. project.rootFolder[0].folders,
  283. folder => folder.name === 'nothere'
  284. )
  285. expect(folder).to.exist
  286. let doc = _.find(folder.docs, doc => doc.name === 'foo3.tex')
  287. doc = MockDocstoreApi.docs[this.project_id][doc._id]
  288. expect(doc.lines).to.deep.equal(['hello world, this is foo-3.tex!'])
  289. return done()
  290. })
  291. })
  292. })
  293. describe('restoring to a filename that already exists', function() {
  294. beforeEach(function(done) {
  295. MockProjectHistoryApi.addOldFile(
  296. this.project_id,
  297. 42,
  298. 'main.tex',
  299. 'hello world, this is main.tex!'
  300. )
  301. return this.owner.request(
  302. {
  303. method: 'POST',
  304. url: `/project/${this.project_id}/restore_file`,
  305. json: {
  306. pathname: 'main.tex',
  307. version: 42
  308. }
  309. },
  310. function(error, response, body) {
  311. if (error != null) {
  312. throw error
  313. }
  314. expect(response.statusCode).to.equal(200)
  315. return done()
  316. }
  317. )
  318. })
  319. it('should have created the doc in the root folder', function(done) {
  320. return this.owner.getProject(this.project_id, (error, project) => {
  321. if (error != null) {
  322. throw error
  323. }
  324. let doc = _.find(project.rootFolder[0].docs, doc =>
  325. doc.name.match(/main \(Restored on/)
  326. )
  327. expect(doc).to.exist
  328. doc = MockDocstoreApi.docs[this.project_id][doc._id]
  329. expect(doc.lines).to.deep.equal(['hello world, this is main.tex!'])
  330. return done()
  331. })
  332. })
  333. })
  334. })
  335. })