ArchiveDocsTests.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  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. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. process.env.BACKEND = 'gcs'
  16. const Settings = require('settings-sharelatex')
  17. const chai = require('chai')
  18. const { expect } = chai
  19. const should = chai.should()
  20. const { db, ObjectId } = require('../../../app/js/mongodb')
  21. const async = require('async')
  22. const DocstoreApp = require('./helpers/DocstoreApp')
  23. const DocstoreClient = require('./helpers/DocstoreClient')
  24. const { Storage } = require('@google-cloud/storage')
  25. const Persistor = require('../../../app/js/PersistorManager')
  26. const Streamifier = require('streamifier')
  27. function uploadContent(path, json, callback) {
  28. const stream = Streamifier.createReadStream(JSON.stringify(json))
  29. Persistor.sendStream(Settings.docstore.bucket, path, stream)
  30. .then(() => callback())
  31. .catch(callback)
  32. }
  33. describe('Archiving', function () {
  34. before(function (done) {
  35. return DocstoreApp.ensureRunning(done)
  36. })
  37. before(async function () {
  38. const storage = new Storage(Settings.docstore.gcs.endpoint)
  39. await storage.createBucket(Settings.docstore.bucket)
  40. await storage.createBucket(`${Settings.docstore.bucket}-deleted`)
  41. })
  42. describe('multiple docs in a project', function () {
  43. before(function (done) {
  44. this.project_id = ObjectId()
  45. this.docs = [
  46. {
  47. _id: ObjectId(),
  48. lines: ['one', 'two', 'three'],
  49. ranges: {},
  50. version: 2
  51. },
  52. {
  53. _id: ObjectId(),
  54. lines: ['aaa', 'bbb', 'ccc'],
  55. ranges: {},
  56. version: 4
  57. }
  58. ]
  59. const jobs = Array.from(this.docs).map((doc) =>
  60. ((doc) => {
  61. return (callback) => {
  62. return DocstoreClient.createDoc(
  63. this.project_id,
  64. doc._id,
  65. doc.lines,
  66. doc.version,
  67. doc.ranges,
  68. callback
  69. )
  70. }
  71. })(doc)
  72. )
  73. return async.series(jobs, (error) => {
  74. if (error != null) {
  75. throw error
  76. }
  77. return DocstoreClient.archiveAllDoc(this.project_id, (error, res) => {
  78. this.res = res
  79. return done()
  80. })
  81. })
  82. })
  83. it('should archive all the docs', function (done) {
  84. this.res.statusCode.should.equal(204)
  85. return done()
  86. })
  87. it('should set inS3 and unset lines and ranges in each doc', function (done) {
  88. const jobs = Array.from(this.docs).map((doc) =>
  89. ((doc) => {
  90. return (callback) => {
  91. return db.docs.findOne({ _id: doc._id }, (error, doc) => {
  92. should.not.exist(doc.lines)
  93. should.not.exist(doc.ranges)
  94. doc.inS3.should.equal(true)
  95. return callback()
  96. })
  97. }
  98. })(doc)
  99. )
  100. return async.series(jobs, done)
  101. })
  102. it('should set the docs in s3 correctly', function (done) {
  103. const jobs = Array.from(this.docs).map((doc) =>
  104. ((doc) => {
  105. return (callback) => {
  106. return DocstoreClient.getS3Doc(
  107. this.project_id,
  108. doc._id,
  109. (error, s3_doc) => {
  110. s3_doc.lines.should.deep.equal(doc.lines)
  111. s3_doc.ranges.should.deep.equal(doc.ranges)
  112. callback()
  113. }
  114. )
  115. }
  116. })(doc)
  117. )
  118. return async.series(jobs, done)
  119. })
  120. return describe('after unarchiving from a request for the project', function () {
  121. before(function (done) {
  122. return DocstoreClient.getAllDocs(
  123. this.project_id,
  124. (error, res, fetched_docs) => {
  125. this.fetched_docs = fetched_docs
  126. if (error != null) {
  127. throw error
  128. }
  129. return done()
  130. }
  131. )
  132. })
  133. it('should return the docs', function (done) {
  134. for (let i = 0; i < this.fetched_docs.length; i++) {
  135. const doc = this.fetched_docs[i]
  136. doc.lines.should.deep.equal(this.docs[i].lines)
  137. }
  138. return done()
  139. })
  140. return it('should restore the docs to mongo', function (done) {
  141. const jobs = Array.from(this.docs).map((doc, i) =>
  142. ((doc, i) => {
  143. return (callback) => {
  144. return db.docs.findOne({ _id: doc._id }, (error, doc) => {
  145. doc.lines.should.deep.equal(this.docs[i].lines)
  146. doc.ranges.should.deep.equal(this.docs[i].ranges)
  147. should.not.exist(doc.inS3)
  148. return callback()
  149. })
  150. }
  151. })(doc, i)
  152. )
  153. return async.series(jobs, done)
  154. })
  155. })
  156. })
  157. describe('a deleted doc', function () {
  158. before(function (done) {
  159. this.project_id = ObjectId()
  160. this.doc = {
  161. _id: ObjectId(),
  162. lines: ['one', 'two', 'three'],
  163. ranges: {},
  164. version: 2
  165. }
  166. return DocstoreClient.createDoc(
  167. this.project_id,
  168. this.doc._id,
  169. this.doc.lines,
  170. this.doc.version,
  171. this.doc.ranges,
  172. (error) => {
  173. if (error != null) {
  174. throw error
  175. }
  176. return DocstoreClient.deleteDoc(
  177. this.project_id,
  178. this.doc._id,
  179. (error) => {
  180. if (error != null) {
  181. throw error
  182. }
  183. return DocstoreClient.archiveAllDoc(
  184. this.project_id,
  185. (error, res) => {
  186. this.res = res
  187. if (error != null) {
  188. throw error
  189. }
  190. return done()
  191. }
  192. )
  193. }
  194. )
  195. }
  196. )
  197. })
  198. it('should successully archive the docs', function (done) {
  199. this.res.statusCode.should.equal(204)
  200. return done()
  201. })
  202. it('should set inS3 and unset lines and ranges in each doc', function (done) {
  203. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  204. if (error != null) {
  205. throw error
  206. }
  207. should.not.exist(doc.lines)
  208. should.not.exist(doc.ranges)
  209. doc.inS3.should.equal(true)
  210. doc.deleted.should.equal(true)
  211. return done()
  212. })
  213. })
  214. it('should set the doc in s3 correctly', function (done) {
  215. return DocstoreClient.getS3Doc(
  216. this.project_id,
  217. this.doc._id,
  218. (error, s3_doc) => {
  219. if (error != null) {
  220. throw error
  221. }
  222. s3_doc.lines.should.deep.equal(this.doc.lines)
  223. s3_doc.ranges.should.deep.equal(this.doc.ranges)
  224. return done()
  225. }
  226. )
  227. })
  228. return describe('after unarchiving from a request for the project', function () {
  229. before(function (done) {
  230. return DocstoreClient.getAllDocs(
  231. this.project_id,
  232. (error, res, fetched_docs) => {
  233. this.fetched_docs = fetched_docs
  234. if (error != null) {
  235. throw error
  236. }
  237. return done()
  238. }
  239. )
  240. })
  241. it('should not included the deleted', function (done) {
  242. this.fetched_docs.length.should.equal(0)
  243. return done()
  244. })
  245. return it('should restore the doc to mongo', function (done) {
  246. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  247. if (error != null) {
  248. throw error
  249. }
  250. doc.lines.should.deep.equal(this.doc.lines)
  251. doc.ranges.should.deep.equal(this.doc.ranges)
  252. should.not.exist(doc.inS3)
  253. doc.deleted.should.equal(true)
  254. return done()
  255. })
  256. })
  257. })
  258. })
  259. describe('a doc with large lines', function () {
  260. before(function (done) {
  261. this.project_id = ObjectId()
  262. this.timeout(1000 * 30)
  263. const quarterMegInBytes = 250000
  264. const big_line = require('crypto')
  265. .randomBytes(quarterMegInBytes)
  266. .toString('hex')
  267. this.doc = {
  268. _id: ObjectId(),
  269. lines: [big_line, big_line, big_line, big_line],
  270. ranges: {},
  271. version: 2
  272. }
  273. return DocstoreClient.createDoc(
  274. this.project_id,
  275. this.doc._id,
  276. this.doc.lines,
  277. this.doc.version,
  278. this.doc.ranges,
  279. (error) => {
  280. if (error != null) {
  281. throw error
  282. }
  283. return DocstoreClient.archiveAllDoc(this.project_id, (error, res) => {
  284. this.res = res
  285. if (error != null) {
  286. throw error
  287. }
  288. return done()
  289. })
  290. }
  291. )
  292. })
  293. it('should successully archive the docs', function (done) {
  294. this.res.statusCode.should.equal(204)
  295. return done()
  296. })
  297. it('should set inS3 and unset lines and ranges in each doc', function (done) {
  298. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  299. if (error != null) {
  300. throw error
  301. }
  302. should.not.exist(doc.lines)
  303. should.not.exist(doc.ranges)
  304. doc.inS3.should.equal(true)
  305. return done()
  306. })
  307. })
  308. it('should set the doc in s3 correctly', function (done) {
  309. return DocstoreClient.getS3Doc(
  310. this.project_id,
  311. this.doc._id,
  312. (error, s3_doc) => {
  313. if (error != null) {
  314. throw error
  315. }
  316. s3_doc.lines.should.deep.equal(this.doc.lines)
  317. s3_doc.ranges.should.deep.equal(this.doc.ranges)
  318. return done()
  319. }
  320. )
  321. })
  322. return describe('after unarchiving from a request for the project', function () {
  323. before(function (done) {
  324. return DocstoreClient.getAllDocs(
  325. this.project_id,
  326. (error, res, fetched_docs) => {
  327. this.fetched_docs = fetched_docs
  328. if (error != null) {
  329. throw error
  330. }
  331. return done()
  332. }
  333. )
  334. })
  335. return it('should restore the doc to mongo', function (done) {
  336. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  337. if (error != null) {
  338. throw error
  339. }
  340. doc.lines.should.deep.equal(this.doc.lines)
  341. doc.ranges.should.deep.equal(this.doc.ranges)
  342. should.not.exist(doc.inS3)
  343. return done()
  344. })
  345. })
  346. })
  347. })
  348. describe('a doc with naughty strings', function () {
  349. before(function (done) {
  350. this.project_id = ObjectId()
  351. this.doc = {
  352. _id: ObjectId(),
  353. lines: [
  354. '',
  355. 'undefined',
  356. 'undef',
  357. 'null',
  358. 'NULL',
  359. '(null)',
  360. 'nil',
  361. 'NIL',
  362. 'true',
  363. 'false',
  364. 'True',
  365. 'False',
  366. 'None',
  367. '\\',
  368. '\\\\',
  369. '0',
  370. '1',
  371. '1.00',
  372. '$1.00',
  373. '1/2',
  374. '1E2',
  375. '1E02',
  376. '1E+02',
  377. '-1',
  378. '-1.00',
  379. '-$1.00',
  380. '-1/2',
  381. '-1E2',
  382. '-1E02',
  383. '-1E+02',
  384. '1/0',
  385. '0/0',
  386. '-2147483648/-1',
  387. '-9223372036854775808/-1',
  388. '0.00',
  389. '0..0',
  390. '.',
  391. '0.0.0',
  392. '0,00',
  393. '0,,0',
  394. ',',
  395. '0,0,0',
  396. '0.0/0',
  397. '1.0/0.0',
  398. '0.0/0.0',
  399. '1,0/0,0',
  400. '0,0/0,0',
  401. '--1',
  402. '-',
  403. '-.',
  404. '-,',
  405. '999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999',
  406. 'NaN',
  407. 'Infinity',
  408. '-Infinity',
  409. '0x0',
  410. '0xffffffff',
  411. '0xffffffffffffffff',
  412. '0xabad1dea',
  413. '123456789012345678901234567890123456789',
  414. '1,000.00',
  415. '1 000.00',
  416. "1'000.00",
  417. '1,000,000.00',
  418. '1 000 000.00',
  419. "1'000'000.00",
  420. '1.000,00',
  421. '1 000,00',
  422. "1'000,00",
  423. '1.000.000,00',
  424. '1 000i̳̞v̢͇ḙ͎͟-҉̭̩̼͔m̤̭̫i͕͇̝̦n̗͙ḍ̟ ̯̲͕͞ǫ̟̯̰̲͙̻̝f ̪̰̰̗̖̭̘͘c̦͍̲̞͍̩̙ḥ͚a̮͎̟̙͜ơ̩̹͎s̤.̝̝ ҉Z̡̖̜͖̰̣͉̜a͖̰͙̬͡l̲̫̳͍̩g̡̟̼̱͚̞̬ͅo̗͜.̟',
  425. '̦H̬̤̗̤͝e͜ ̜̥̝̻͍̟́w̕h̖̯͓o̝͙̖͎̱̮ ҉̺̙̞̟͈W̷̼̭a̺̪͍į͈͕̭͙̯̜t̶̼̮s̘͙͖̕ ̠̫̠B̻͍͙͉̳ͅe̵h̵̬͇̫͙i̹͓̳̳̮͎̫̕n͟d̴̪̜̖ ̰͉̩͇͙̲͞ͅT͖̼͓̪͢h͏͓̮̻e̬̝̟ͅ ̤̹̝W͙̞̝͔͇͝ͅa͏͓͔̹̼̣l̴͔̰̤̟͔ḽ̫.͕',
  426. 'Z̮̞̠͙͔ͅḀ̗̞͈̻̗Ḷ͙͎̯̹̞͓G̻O̭̗̮',
  427. "˙ɐnbᴉlɐ ɐuƃɐɯ ǝɹolop ʇǝ ǝɹoqɐl ʇn ʇunpᴉpᴉɔuᴉ ɹodɯǝʇ poɯsnᴉǝ op pǝs 'ʇᴉlǝ ƃuᴉɔsᴉdᴉpɐ ɹnʇǝʇɔǝsuoɔ 'ʇǝɯɐ ʇᴉs ɹolop ɯnsdᴉ ɯǝɹo˥",
  428. '00˙Ɩ$-',
  429. 'The quick brown fox jumps over the lazy dog',
  430. '𝐓𝐡𝐞 𝐪𝐮𝐢𝐜𝐤 𝐛𝐫𝐨𝐰𝐧 𝐟𝐨𝐱 𝐣𝐮𝐦𝐩𝐬 𝐨𝐯𝐞𝐫 𝐭𝐡𝐞 𝐥𝐚𝐳𝐲 𝐝𝐨𝐠',
  431. '𝕿𝖍𝖊 𝖖𝖚𝖎𝖈𝖐 𝖇𝖗𝖔𝖜𝖓 𝖋𝖔𝖝 𝖏𝖚𝖒𝖕𝖘 𝖔𝖛𝖊𝖗 𝖙𝖍𝖊 𝖑𝖆𝖟𝖞 𝖉𝖔𝖌',
  432. '𝑻𝒉𝒆 𝒒𝒖𝒊𝒄𝒌 𝒃𝒓𝒐𝒘𝒏 𝒇𝒐𝒙 𝒋𝒖𝒎𝒑𝒔 𝒐𝒗𝒆𝒓 𝒕𝒉𝒆 𝒍𝒂𝒛𝒚 𝒅𝒐𝒈',
  433. '𝓣𝓱𝓮 𝓺𝓾𝓲𝓬𝓴 𝓫𝓻𝓸𝔀𝓷 𝓯𝓸𝔁 𝓳𝓾𝓶𝓹𝓼 𝓸𝓿𝓮𝓻 𝓽𝓱𝓮 𝓵𝓪𝔃𝔂 𝓭𝓸𝓰',
  434. '𝕋𝕙𝕖 𝕢𝕦𝕚𝕔𝕜 𝕓𝕣𝕠𝕨𝕟 𝕗𝕠𝕩 𝕛𝕦𝕞𝕡𝕤 𝕠𝕧𝕖𝕣 𝕥𝕙𝕖 𝕝𝕒𝕫𝕪 𝕕𝕠𝕘',
  435. '𝚃𝚑𝚎 𝚚𝚞𝚒𝚌𝚔 𝚋𝚛𝚘𝚠𝚗 𝚏𝚘𝚡 𝚓𝚞𝚖𝚙𝚜 𝚘𝚟𝚎𝚛 𝚝𝚑𝚎 𝚕𝚊𝚣𝚢 𝚍𝚘𝚐',
  436. '⒯⒣⒠ ⒬⒰⒤⒞⒦ ⒝⒭⒪⒲⒩ ⒡⒪⒳ ⒥⒰⒨⒫⒮ ⒪⒱⒠⒭ ⒯⒣⒠ ⒧⒜⒵⒴ ⒟⒪⒢',
  437. '<script>alert(123)</script>',
  438. '&lt;script&gt;alert(&#39;123&#39;);&lt;/script&gt;',
  439. '<img src=x onerror=alert(123) />',
  440. '<svg><script>123<1>alert(123)</script> ',
  441. '"><script>alert(123)</script>',
  442. "'><script>alert(123)</script>",
  443. '><script>alert(123)</script>',
  444. '</script><script>alert(123)</script>',
  445. '< / script >< script >alert(123)< / script >',
  446. ' onfocus=JaVaSCript:alert(123) autofocus ',
  447. '" onfocus=JaVaSCript:alert(123) autofocus ',
  448. "' onfocus=JaVaSCript:alert(123) autofocus ",
  449. '<script>alert(123)</script>',
  450. '<sc<script>ript>alert(123)</sc</script>ript>',
  451. '--><script>alert(123)</script>',
  452. '";alert(123);t="',
  453. "';alert(123);t='",
  454. 'JavaSCript:alert(123)',
  455. ';alert(123);',
  456. 'src=JaVaSCript:prompt(132)',
  457. '"><script>alert(123);</script x="',
  458. "'><script>alert(123);</script x='",
  459. '><script>alert(123);</script x=',
  460. '" autofocus onkeyup="javascript:alert(123)',
  461. "' autofocus onkeyup='javascript:alert(123)",
  462. '<script\\x20type="text/javascript">javascript:alert(1);</script>',
  463. '<script\\x3Etype="text/javascript">javascript:alert(1);</script>',
  464. '<script\\x0Dtype="text/javascript">javascript:alert(1);</script>',
  465. '<script\\x09type="text/javascript">javascript:alert(1);</script>',
  466. '<script\\x0Ctype="text/javascript">javascript:alert(1);</script>',
  467. '<script\\x2Ftype="text/javascript">javascript:alert(1);</script>',
  468. '<script\\x0Atype="text/javascript">javascript:alert(1);</script>',
  469. '\'`"><\\x3Cscript>javascript:alert(1)</script> ',
  470. '\'`"><\\x00script>javascript:alert(1)</script>',
  471. 'ABC<div style="x\\x3Aexpression(javascript:alert(1)">DEF',
  472. 'ABC<div style="x:expression\\x5C(javascript:alert(1)">DEF',
  473. 'ABC<div style="x:expression\\x00(javascript:alert(1)">DEF',
  474. 'ABC<div style="x:exp\\x00ression(javascript:alert(1)">DEF',
  475. 'ABC<div style="x:exp\\x5Cression(javascript:alert(1)">DEF',
  476. 'ABC<div style="x:\\x0Aexpression(javascript:alert(1)">DEF',
  477. 'ABC<div style="x:\\x09expression(javascript:alert(1)">DEF',
  478. 'ABC<div style="x:\\xE3\\x80\\x80expression(javascript:alert(1)">DEF',
  479. 'ABC<div style="x:\\xE2\\x80\\x84expression(javascript:alert(1)">DEF',
  480. 'ABC<div style="x:\\xC2\\xA0expression(javascript:alert(1)">DEF',
  481. 'ABC<div style="x:\\xE2\\x80\\x80expression(javascript:alert(1)">DEF',
  482. 'ABC<div style="x:\\xE2\\x80\\x8Aexpression(javascript:alert(1)">DEF',
  483. 'ABC<div style="x:\\x0Dexpression(javascript:alert(1)">DEF',
  484. 'ABC<div style="x:\\x0Cexpression(javascript:alert(1)">DEF',
  485. 'ABC<div style="x:\\xE2\\x80\\x87expression(javascript:alert(1)">DEF',
  486. 'ABC<div style="x:\\xEF\\xBB\\xBFexpression(javascript:alert(1)">DEF',
  487. 'ABC<div style="x:\\x20expression(javascript:alert(1)">DEF',
  488. 'ABC<div style="x:\\xE2\\x80\\x88expression(javascript:alert(1)">DEF',
  489. 'ABC<div style="x:\\x00expression(javascript:alert(1)">DEF',
  490. 'ABC<div style="x:\\xE2\\x80\\x8Bexpression(javascript:alert(1)">DEF',
  491. 'ABC<div style="x:\\xE2\\x80\\x86expression(javascript:alert(1)">DEF',
  492. 'ABC<div style="x:\\xE2\\x80\\x85expression(javascript:alert(1)">DEF',
  493. 'ABC<div style="x:\\xE2\\x80\\x82expression(javascript:alert(1)">DEF',
  494. 'ABC<div style="x:\\x0Bexpression(javascript:alert(1)">DEF',
  495. 'ABC<div style="x:\\xE2\\x80\\x81expression(javascript:alert(1)">DEF',
  496. 'ABC<div style="x:\\xE2\\x80\\x83expression(javascript:alert(1)">DEF',
  497. 'ABC<div style="x:\\xE2\\x80\\x89expression(javascript:alert(1)">DEF',
  498. '<a href="\\x0Bjavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  499. '<a href="\\x0Fjavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  500. '<a href="\\xC2\\xA0javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  501. '<a href="\\x05javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  502. '<a href="\\xE1\\xA0\\x8Ejavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  503. '<a href="\\x18javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  504. '<a href="\\x11javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  505. '<a href="\\xE2\\x80\\x88javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  506. '<a href="\\xE2\\x80\\x89javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  507. '<a href="\\xE2\\x80\\x80javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  508. '<a href="\\x17javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  509. '<a href="\\x03javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  510. '<a href="\\x0Ejavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  511. '<a href="\\x1Ajavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  512. '<a href="\\x00javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  513. '<a href="\\x10javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  514. '<a href="\\xE2\\x80\\x82javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  515. '<a href="\\x20javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  516. '<a href="\\x13javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  517. '<a href="\\x09javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  518. '<a href="\\xE2\\x80\\x8Ajavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  519. '<a href="\\x14javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  520. '<a href="\\x19javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  521. '<a href="\\xE2\\x80\\xAFjavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  522. '<a href="\\x1Fjavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  523. '<a href="\\xE2\\x80\\x81javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  524. '<a href="\\x1Djavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  525. '<a href="\\xE2\\x80\\x87javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  526. '<a href="\\x07javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  527. '<a href="\\xE1\\x9A\\x80javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  528. '<a href="\\xE2\\x80\\x83javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  529. '<a href="\\x04javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  530. '<a href="\\x01javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  531. '<a href="\\x08javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  532. '<a href="\\xE2\\x80\\x84javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  533. '<a href="\\xE2\\x80\\x86javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  534. '<a href="\\xE3\\x80\\x80javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  535. '<a href="\\x12javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  536. '<a href="\\x0Djavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  537. '<a href="\\x0Ajavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  538. '<a href="\\x0Cjavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  539. '<a href="\\x15javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  540. '<a href="\\xE2\\x80\\xA8javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  541. '<a href="\\x16javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  542. '<a href="\\x02javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  543. '<a href="\\x1Bjavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  544. '<a href="\\x06javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  545. '<a href="\\xE2\\x80\\xA9javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  546. '<a href="\\xE2\\x80\\x85javascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  547. '<a href="\\x1Ejavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  548. '<a href="\\xE2\\x81\\x9Fjavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  549. '<a href="\\x1Cjavascript:javascript:alert(1)" id="fuzzelement1">test</a>',
  550. '<a href="javascript\\x00:javascript:alert(1)" id="fuzzelement1">test</a>',
  551. '<a href="javascript\\x3A:javascript:alert(1)" id="fuzzelement1">test</a>',
  552. '<a href="javascript\\x09:javascript:alert(1)" id="fuzzelement1">test</a>',
  553. '<a href="javascript\\x0D:javascript:alert(1)" id="fuzzelement1">test</a>',
  554. '<a href="javascript\\x0A:javascript:alert(1)" id="fuzzelement1">test</a>',
  555. '`"\'><img src=xxx:x \\x0Aonerror=javascript:alert(1)>',
  556. '`"\'><img src=xxx:x \\x22onerror=javascript:alert(1)>',
  557. '`"\'><img src=xxx:x \\x0Bonerror=javascript:alert(1)>',
  558. '`"\'><img src=xxx:x \\x0Donerror=javascript:alert(1)>',
  559. '`"\'><img src=xxx:x \\x2Fonerror=javascript:alert(1)>',
  560. '`"\'><img src=xxx:x \\x09onerror=javascript:alert(1)>',
  561. '`"\'><img src=xxx:x \\x0Conerror=javascript:alert(1)>',
  562. '`"\'><img src=xxx:x \\x00onerror=javascript:alert(1)>',
  563. '`"\'><img src=xxx:x \\x27onerror=javascript:alert(1)>',
  564. '`"\'><img src=xxx:x \\x20onerror=javascript:alert(1)>',
  565. '"`\'><script>\\x3Bjavascript:alert(1)</script>',
  566. '"`\'><script>\\x0Djavascript:alert(1)</script>',
  567. '"`\'><script>\\xEF\\xBB\\xBFjavascript:alert(1)</script>',
  568. '"`\'><script>\\xE2\\x80\\x81javascript:alert(1)</script>',
  569. '"`\'><script>\\xE2\\x80\\x84javascript:alert(1)</script>',
  570. '"`\'><script>\\xE3\\x80\\x80javascript:alert(1)</script>',
  571. '"`\'><script>\\x09javascript:alert(1)</script>',
  572. '"`\'><script>\\xE2\\x80\\x89javascript:alert(1)</script>',
  573. '"`\'><script>\\xE2\\x80\\x85javascript:alert(1)</script>',
  574. '"`\'><script>\\xE2\\x80\\x88javascript:alert(1)</script>',
  575. '"`\'><script>\\x00javascript:alert(1)</script>',
  576. '"`\'><script>\\xE2\\x80\\xA8javascript:alert(1)</script>',
  577. '"`\'><script>\\xE2\\x80\\x8Ajavascript:alert(1)</script>',
  578. '"`\'><script>\\xE1\\x9A\\x80javascript:alert(1)</script>',
  579. '"`\'><script>\\x0Cjavascript:alert(1)</script>',
  580. '"`\'><script>\\x2Bjavascript:alert(1)</script>',
  581. '"`\'><script>\\xF0\\x90\\x96\\x9Ajavascript:alert(1)</script>',
  582. '"`\'><script>-javascript:alert(1)</script>',
  583. '"`\'><script>\\x0Ajavascript:alert(1)</script>',
  584. '"`\'><script>\\xE2\\x80\\xAFjavascript:alert(1)</script>',
  585. '"`\'><script>\\x7Ejavascript:alert(1)</script>',
  586. '"`\'><script>\\xE2\\x80\\x87javascript:alert(1)</script>',
  587. '"`\'><script>\\xE2\\x81\\x9Fjavascript:alert(1)</script>',
  588. '"`\'><script>\\xE2\\x80\\xA9javascript:alert(1)</script>',
  589. '"`\'><script>\\xC2\\x85javascript:alert(1)</script>',
  590. '"`\'><script>\\xEF\\xBF\\xAEjavascript:alert(1)</script>',
  591. '"`\'><script>\\xE2\\x80\\x83javascript:alert(1)</script>',
  592. '"`\'><script>\\xE2\\x80\\x8Bjavascript:alert(1)</script>',
  593. '"`\'><script>\\xEF\\xBF\\xBEjavascript:alert(1)</script>',
  594. '"`\'><script>\\xE2\\x80\\x80javascript:alert(1)</script>',
  595. '"`\'><script>\\x21javascript:alert(1)</script>',
  596. '"`\'><script>\\xE2\\x80\\x82javascript:alert(1)</script>',
  597. '"`\'><script>\\xE2\\x80\\x86javascript:alert(1)</script>',
  598. '"`\'><script>\\xE1\\xA0\\x8Ejavascript:alert(1)</script>',
  599. '"`\'><script>\\x0Bjavascript:alert(1)</script>',
  600. '"`\'><script>\\x20javascript:alert(1)</script>',
  601. '"`\'><script>\\xC2\\xA0javascript:alert(1)</script>',
  602. '<img \\x00src=x onerror="alert(1)">',
  603. '<img \\x47src=x onerror="javascript:alert(1)">',
  604. '<img \\x11src=x onerror="javascript:alert(1)">',
  605. '<img \\x12src=x onerror="javascript:alert(1)">',
  606. '<img\\x47src=x onerror="javascript:alert(1)">',
  607. '<img\\x10src=x onerror="javascript:alert(1)">',
  608. '<img\\x13src=x onerror="javascript:alert(1)">',
  609. '<img\\x32src=x onerror="javascript:alert(1)">',
  610. '<img\\x47src=x onerror="javascript:alert(1)">',
  611. '<img\\x11src=x onerror="javascript:alert(1)">',
  612. '<img \\x47src=x onerror="javascript:alert(1)">',
  613. '<img \\x34src=x onerror="javascript:alert(1)">',
  614. '<img \\x39src=x onerror="javascript:alert(1)">',
  615. '<img \\x00src=x onerror="javascript:alert(1)">',
  616. '<img src\\x09=x onerror="javascript:alert(1)">',
  617. '<img src\\x10=x onerror="javascript:alert(1)">',
  618. '<img src\\x13=x onerror="javascript:alert(1)">',
  619. '<img src\\x32=x onerror="javascript:alert(1)">',
  620. '<img src\\x12=x onerror="javascript:alert(1)">',
  621. '<img src\\x11=x onerror="javascript:alert(1)">',
  622. '<img src\\x00=x onerror="javascript:alert(1)">',
  623. '<img src\\x47=x onerror="javascript:alert(1)">',
  624. '<img src=x\\x09onerror="javascript:alert(1)">',
  625. '<img src=x\\x10onerror="javascript:alert(1)">',
  626. '<img src=x\\x11onerror="javascript:alert(1)">',
  627. '<img src=x\\x12onerror="javascript:alert(1)">',
  628. '<img src=x\\x13onerror="javascript:alert(1)">',
  629. '<img[a][b][c]src[d]=x[e]onerror=[f]"alert(1)">',
  630. '<img src=x onerror=\\x09"javascript:alert(1)">',
  631. '<img src=x onerror=\\x10"javascript:alert(1)">',
  632. '<img src=x onerror=\\x11"javascript:alert(1)">',
  633. '<img src=x onerror=\\x12"javascript:alert(1)">',
  634. '<img src=x onerror=\\x32"javascript:alert(1)">',
  635. '<img src=x onerror=\\x00"javascript:alert(1)">',
  636. '<a href=java&#1&#2&#3&#4&#5&#6&#7&#8&#11&#12script:javascript:alert(1)>XXX</a>',
  637. '<img src="x` `<script>javascript:alert(1)</script>"` `>',
  638. '<img src onerror /" \'"= alt=javascript:alert(1)//">',
  639. '<title onpropertychange=javascript:alert(1)></title><title title=>',
  640. '<a href=http://foo.bar/#x=`y></a><img alt="`><img src=x:x onerror=javascript:alert(1)></a>">',
  641. '<!--[if]><script>javascript:alert(1)</script -->',
  642. '<!--[if<img src=x onerror=javascript:alert(1)//]> -->',
  643. '<script src="/\\%(jscript)s"></script>',
  644. '<script src="\\\\%(jscript)s"></script>',
  645. '<IMG """><SCRIPT>alert("XSS")</SCRIPT>">',
  646. '<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>',
  647. '<IMG SRC=# onmouseover="alert(\'xxs\')">',
  648. '<IMG SRC= onmouseover="alert(\'xxs\')">',
  649. '<IMG onmouseover="alert(\'xxs\')">',
  650. '<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>',
  651. '<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>',
  652. '<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>',
  653. '<IMG SRC="jav ascript:alert(\'XSS\');">',
  654. '<IMG SRC="jav&#x09;ascript:alert(\'XSS\');">',
  655. '<IMG SRC="jav&#x0A;ascript:alert(\'XSS\');">',
  656. '<IMG SRC="jav&#x0D;ascript:alert(\'XSS\');">',
  657. 'perl -e \'print "<IMG SRC=java\\0script:alert(\\"XSS\\")>";\' > out',
  658. '<IMG SRC=" &#14; javascript:alert(\'XSS\');">',
  659. '<SCRIPT/XSS SRC="http://ha.ckers.org/xss.js"></SCRIPT>',
  660. '<BODY onload!#$%&()*~+-_.,:;?@[/|\\]^`=alert("XSS")>',
  661. '<SCRIPT/SRC="http://ha.ckers.org/xss.js"></SCRIPT>',
  662. '<<SCRIPT>alert("XSS");//<</SCRIPT>',
  663. '<SCRIPT SRC=http://ha.ckers.org/xss.js?< B >',
  664. '<SCRIPT SRC=//ha.ckers.org/.j>',
  665. '<IMG SRC="javascript:alert(\'XSS\')"',
  666. '<iframe src=http://ha.ckers.org/scriptlet.html <',
  667. "\\\";alert('XSS');//",
  668. '<plaintext>',
  669. '1;DROP TABLE users',
  670. "1'; DROP TABLE users-- 1",
  671. "' OR 1=1 -- 1",
  672. "' OR '1'='1",
  673. '-',
  674. '--',
  675. '--version',
  676. '--help',
  677. '$USER',
  678. '/dev/null; touch /tmp/blns.fail ; echo',
  679. '`touch /tmp/blns.fail`',
  680. '$(touch /tmp/blns.fail)',
  681. '@{[system "touch /tmp/blns.fail"]}',
  682. 'eval("puts \'hello world\'")',
  683. 'System("ls -al /")',
  684. '`ls -al /`',
  685. 'Kernel.exec("ls -al /")',
  686. 'Kernel.exit(1)',
  687. "%x('ls -al /')",
  688. '<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE foo [ <!ELEMENT foo ANY ><!ENTITY xxe SYSTEM "file:///etc/passwd" >]><foo>&xxe;</foo>',
  689. '$HOME',
  690. "$ENV{'HOME'}",
  691. '%d',
  692. '%s',
  693. '%*.*s',
  694. '../../../../../../../../../../../etc/passwd%00',
  695. '../../../../../../../../../../../etc/hosts',
  696. '() { 0; }; touch /tmp/blns.shellshock1.fail;',
  697. '() { _; } >_[$($())] { touch /tmp/blns.shellshock2.fail; }',
  698. 'CON',
  699. 'PRN',
  700. 'AUX',
  701. 'CLOCK$',
  702. 'NUL',
  703. 'A:',
  704. 'ZZ:',
  705. 'COM1',
  706. 'LPT1',
  707. 'LPT2',
  708. 'LPT3',
  709. 'COM2',
  710. 'COM3',
  711. 'COM4',
  712. 'Scunthorpe General Hospital',
  713. 'Penistone Community Church',
  714. 'Lightwater Country Park',
  715. 'Jimmy Clitheroe',
  716. 'Horniman Museum',
  717. 'shitake mushrooms',
  718. 'RomansInSussex.co.uk',
  719. 'http://www.cum.qc.ca/',
  720. 'Craig Cockburn, Software Specialist',
  721. 'Linda Callahan',
  722. 'Dr. Herman I. Libshitz',
  723. 'magna cum laude',
  724. 'Super Bowl XXX',
  725. 'medieval erection of parapets',
  726. 'evaluate',
  727. 'mocha',
  728. 'expression',
  729. 'Arsenal canal',
  730. 'classic',
  731. 'Tyson Gay',
  732. "If you're reading this, you've been in a coma for almost 20 years now. We're trying a new technique. We don't know where this message will end up in your dream, but we hope it works. Please wake up, we miss you.",
  733. 'Roses are \u001b[0;31mred\u001b[0m, violets are \u001b[0;34mblue. Hope you enjoy terminal hue',
  734. 'But now...\u001b[20Cfor my greatest trick...\u001b[8m',
  735. 'The quic\b\b\b\b\b\bk brown fo\u0007\u0007\u0007\u0007\u0007\u0007\u0007\u0007\u0007\u0007\u0007x... [Beeeep]',
  736. 'Powerلُلُصّبُلُلصّبُررً ॣ ॣh ॣ ॣ冗'
  737. ],
  738. ranges: {},
  739. version: 2
  740. }
  741. return DocstoreClient.createDoc(
  742. this.project_id,
  743. this.doc._id,
  744. this.doc.lines,
  745. this.doc.version,
  746. this.doc.ranges,
  747. (error) => {
  748. if (error != null) {
  749. throw error
  750. }
  751. return DocstoreClient.archiveAllDoc(this.project_id, (error, res) => {
  752. this.res = res
  753. if (error != null) {
  754. throw error
  755. }
  756. return done()
  757. })
  758. }
  759. )
  760. })
  761. it('should successully archive the docs', function (done) {
  762. this.res.statusCode.should.equal(204)
  763. return done()
  764. })
  765. it('should set inS3 and unset lines and ranges in each doc', function (done) {
  766. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  767. if (error != null) {
  768. throw error
  769. }
  770. should.not.exist(doc.lines)
  771. should.not.exist(doc.ranges)
  772. doc.inS3.should.equal(true)
  773. return done()
  774. })
  775. })
  776. it('should set the doc in s3 correctly', function (done) {
  777. return DocstoreClient.getS3Doc(
  778. this.project_id,
  779. this.doc._id,
  780. (error, s3_doc) => {
  781. if (error != null) {
  782. throw error
  783. }
  784. s3_doc.lines.should.deep.equal(this.doc.lines)
  785. s3_doc.ranges.should.deep.equal(this.doc.ranges)
  786. return done()
  787. }
  788. )
  789. })
  790. return describe('after unarchiving from a request for the project', function () {
  791. before(function (done) {
  792. return DocstoreClient.getAllDocs(
  793. this.project_id,
  794. (error, res, fetched_docs) => {
  795. this.fetched_docs = fetched_docs
  796. if (error != null) {
  797. throw error
  798. }
  799. return done()
  800. }
  801. )
  802. })
  803. return it('should restore the doc to mongo', function (done) {
  804. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  805. if (error != null) {
  806. throw error
  807. }
  808. doc.lines.should.deep.equal(this.doc.lines)
  809. doc.ranges.should.deep.equal(this.doc.ranges)
  810. should.not.exist(doc.inS3)
  811. return done()
  812. })
  813. })
  814. })
  815. })
  816. describe('a doc with ranges', function () {
  817. before(function (done) {
  818. this.project_id = ObjectId()
  819. this.doc = {
  820. _id: ObjectId(),
  821. lines: ['one', 'two', 'three'],
  822. ranges: {
  823. changes: [
  824. {
  825. id: ObjectId(),
  826. op: { i: 'foo', p: 24 },
  827. metadata: {
  828. user_id: ObjectId(),
  829. ts: new Date('2017-01-27T16:10:44.194Z')
  830. }
  831. },
  832. {
  833. id: ObjectId(),
  834. op: { d: 'bar', p: 50 },
  835. metadata: {
  836. user_id: ObjectId(),
  837. ts: new Date('2017-01-27T18:10:44.194Z')
  838. }
  839. }
  840. ],
  841. comments: [
  842. {
  843. id: ObjectId(),
  844. op: { c: 'comment', p: 284, t: ObjectId() },
  845. metadata: {
  846. user_id: ObjectId(),
  847. ts: new Date('2017-01-26T14:22:04.869Z')
  848. }
  849. }
  850. ]
  851. },
  852. version: 2
  853. }
  854. return DocstoreClient.createDoc(
  855. this.project_id,
  856. this.doc._id,
  857. this.doc.lines,
  858. this.doc.version,
  859. this.doc.ranges,
  860. (error) => {
  861. if (error != null) {
  862. throw error
  863. }
  864. return DocstoreClient.archiveAllDoc(this.project_id, (error, res) => {
  865. this.res = res
  866. if (error != null) {
  867. throw error
  868. }
  869. return done()
  870. })
  871. }
  872. )
  873. })
  874. it('should successully archive the docs', function (done) {
  875. this.res.statusCode.should.equal(204)
  876. return done()
  877. })
  878. it('should set inS3 and unset lines and ranges in each doc', function (done) {
  879. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  880. if (error != null) {
  881. throw error
  882. }
  883. should.not.exist(doc.lines)
  884. should.not.exist(doc.ranges)
  885. doc.inS3.should.equal(true)
  886. return done()
  887. })
  888. })
  889. it('should set the doc in s3 correctly', function (done) {
  890. return DocstoreClient.getS3Doc(
  891. this.project_id,
  892. this.doc._id,
  893. (error, s3_doc) => {
  894. if (error != null) {
  895. throw error
  896. }
  897. s3_doc.lines.should.deep.equal(this.doc.lines)
  898. const ranges = JSON.parse(JSON.stringify(this.doc.ranges)) // ObjectId -> String
  899. s3_doc.ranges.should.deep.equal(ranges)
  900. return done()
  901. }
  902. )
  903. })
  904. return describe('after unarchiving from a request for the project', function () {
  905. before(function (done) {
  906. return DocstoreClient.getAllDocs(
  907. this.project_id,
  908. (error, res, fetched_docs) => {
  909. this.fetched_docs = fetched_docs
  910. if (error != null) {
  911. throw error
  912. }
  913. return done()
  914. }
  915. )
  916. })
  917. return it('should restore the doc to mongo', function (done) {
  918. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  919. if (error != null) {
  920. throw error
  921. }
  922. doc.lines.should.deep.equal(this.doc.lines)
  923. doc.ranges.should.deep.equal(this.doc.ranges)
  924. should.not.exist(doc.inS3)
  925. return done()
  926. })
  927. })
  928. })
  929. })
  930. describe('a doc that is archived twice', function () {
  931. before(function (done) {
  932. this.project_id = ObjectId()
  933. this.doc = {
  934. _id: ObjectId(),
  935. lines: ['abc', 'def', 'ghi'],
  936. ranges: {},
  937. version: 2
  938. }
  939. return DocstoreClient.createDoc(
  940. this.project_id,
  941. this.doc._id,
  942. this.doc.lines,
  943. this.doc.version,
  944. this.doc.ranges,
  945. (error) => {
  946. if (error != null) {
  947. throw error
  948. }
  949. return DocstoreClient.archiveAllDoc(this.project_id, (error, res) => {
  950. this.res = res
  951. if (error != null) {
  952. throw error
  953. }
  954. this.res.statusCode.should.equal(204)
  955. return DocstoreClient.archiveAllDoc(
  956. this.project_id,
  957. (error, res1) => {
  958. this.res = res1
  959. if (error != null) {
  960. throw error
  961. }
  962. this.res.statusCode.should.equal(204)
  963. return done()
  964. }
  965. )
  966. })
  967. }
  968. )
  969. })
  970. it('should set inS3 and unset lines and ranges in each doc', function (done) {
  971. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  972. if (error != null) {
  973. throw error
  974. }
  975. should.not.exist(doc.lines)
  976. should.not.exist(doc.ranges)
  977. doc.inS3.should.equal(true)
  978. return done()
  979. })
  980. })
  981. it('should set the doc in s3 correctly', function (done) {
  982. return DocstoreClient.getS3Doc(
  983. this.project_id,
  984. this.doc._id,
  985. (error, s3_doc) => {
  986. if (error != null) {
  987. throw error
  988. }
  989. s3_doc.lines.should.deep.equal(this.doc.lines)
  990. s3_doc.ranges.should.deep.equal(this.doc.ranges)
  991. return done()
  992. }
  993. )
  994. })
  995. return describe('after unarchiving from a request for the project', function () {
  996. before(function (done) {
  997. return DocstoreClient.getAllDocs(
  998. this.project_id,
  999. (error, res, fetched_docs) => {
  1000. this.fetched_docs = fetched_docs
  1001. if (error != null) {
  1002. throw error
  1003. }
  1004. return done()
  1005. }
  1006. )
  1007. })
  1008. return it('should restore the doc to mongo', function (done) {
  1009. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  1010. if (error != null) {
  1011. throw error
  1012. }
  1013. doc.lines.should.deep.equal(this.doc.lines)
  1014. doc.ranges.should.deep.equal(this.doc.ranges)
  1015. should.not.exist(doc.inS3)
  1016. return done()
  1017. })
  1018. })
  1019. })
  1020. })
  1021. return describe('a doc with the old schema (just an array of lines)', function () {
  1022. before(function (done) {
  1023. this.project_id = ObjectId()
  1024. this.doc = {
  1025. _id: ObjectId(),
  1026. lines: ['abc', 'def', 'ghi'],
  1027. ranges: {},
  1028. version: 2
  1029. }
  1030. uploadContent(
  1031. `${this.project_id}/${this.doc._id}`,
  1032. this.doc.lines,
  1033. (error) => {
  1034. expect(error).not.to.exist
  1035. db.docs.insert(
  1036. {
  1037. project_id: this.project_id,
  1038. _id: this.doc._id,
  1039. rev: this.doc.version,
  1040. inS3: true
  1041. },
  1042. (error) => {
  1043. if (error != null) {
  1044. throw error
  1045. }
  1046. DocstoreClient.getAllDocs(
  1047. this.project_id,
  1048. (error, res, fetched_docs) => {
  1049. this.fetched_docs = fetched_docs
  1050. if (error != null) {
  1051. throw error
  1052. }
  1053. return done()
  1054. }
  1055. )
  1056. }
  1057. )
  1058. }
  1059. )
  1060. })
  1061. it('should restore the doc to mongo', function (done) {
  1062. return db.docs.findOne({ _id: this.doc._id }, (error, doc) => {
  1063. if (error != null) {
  1064. throw error
  1065. }
  1066. doc.lines.should.deep.equal(this.doc.lines)
  1067. should.not.exist(doc.inS3)
  1068. return done()
  1069. })
  1070. })
  1071. return it('should return the doc', function (done) {
  1072. this.fetched_docs[0].lines.should.deep.equal(this.doc.lines)
  1073. return done()
  1074. })
  1075. })
  1076. })