RestoringFilesTest.js 11 KB

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