RestoringFilesTest.js 8.7 KB

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