UpdatingDocsTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* eslint-disable
  2. handle-callback-err,
  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 sinon = require('sinon')
  14. const { ObjectId } = require('mongodb')
  15. const DocstoreApp = require('./helpers/DocstoreApp')
  16. const DocstoreClient = require('./helpers/DocstoreClient')
  17. describe('Applying updates to a doc', function () {
  18. beforeEach(function (done) {
  19. this.project_id = ObjectId()
  20. this.doc_id = ObjectId()
  21. this.originalLines = ['original', 'lines']
  22. this.newLines = ['new', 'lines']
  23. this.originalRanges = {
  24. changes: [
  25. {
  26. id: ObjectId().toString(),
  27. op: { i: 'foo', p: 3 },
  28. meta: {
  29. user_id: ObjectId().toString(),
  30. ts: new Date().toString(),
  31. },
  32. },
  33. ],
  34. }
  35. this.newRanges = {
  36. changes: [
  37. {
  38. id: ObjectId().toString(),
  39. op: { i: 'bar', p: 6 },
  40. meta: {
  41. user_id: ObjectId().toString(),
  42. ts: new Date().toString(),
  43. },
  44. },
  45. ],
  46. }
  47. this.version = 42
  48. return DocstoreApp.ensureRunning(() => {
  49. return DocstoreClient.createDoc(
  50. this.project_id,
  51. this.doc_id,
  52. this.originalLines,
  53. this.version,
  54. this.originalRanges,
  55. error => {
  56. if (error != null) {
  57. throw error
  58. }
  59. return done()
  60. }
  61. )
  62. })
  63. })
  64. describe('when nothing has been updated', function () {
  65. beforeEach(function (done) {
  66. return DocstoreClient.updateDoc(
  67. this.project_id,
  68. this.doc_id,
  69. this.originalLines,
  70. this.version,
  71. this.originalRanges,
  72. (error, res, body) => {
  73. this.body = body
  74. return done()
  75. }
  76. )
  77. })
  78. it('should return modified = false', function () {
  79. return this.body.modified.should.equal(false)
  80. })
  81. return it('should not update the doc in the API', function (done) {
  82. return DocstoreClient.getDoc(
  83. this.project_id,
  84. this.doc_id,
  85. {},
  86. (error, res, doc) => {
  87. doc.lines.should.deep.equal(this.originalLines)
  88. doc.version.should.equal(this.version)
  89. doc.ranges.should.deep.equal(this.originalRanges)
  90. return done()
  91. }
  92. )
  93. })
  94. })
  95. describe('when the lines have changed', function () {
  96. beforeEach(function (done) {
  97. return DocstoreClient.updateDoc(
  98. this.project_id,
  99. this.doc_id,
  100. this.newLines,
  101. this.version,
  102. this.originalRanges,
  103. (error, res, body) => {
  104. this.body = body
  105. return done()
  106. }
  107. )
  108. })
  109. it('should return modified = true', function () {
  110. return this.body.modified.should.equal(true)
  111. })
  112. it('should return the rev', function () {
  113. return this.body.rev.should.equal(2)
  114. })
  115. return it('should update the doc in the API', function (done) {
  116. return DocstoreClient.getDoc(
  117. this.project_id,
  118. this.doc_id,
  119. {},
  120. (error, res, doc) => {
  121. doc.lines.should.deep.equal(this.newLines)
  122. doc.version.should.equal(this.version)
  123. doc.ranges.should.deep.equal(this.originalRanges)
  124. return done()
  125. }
  126. )
  127. })
  128. })
  129. describe('when the version has changed', function () {
  130. beforeEach(function (done) {
  131. return DocstoreClient.updateDoc(
  132. this.project_id,
  133. this.doc_id,
  134. this.originalLines,
  135. this.version + 1,
  136. this.originalRanges,
  137. (error, res, body) => {
  138. this.body = body
  139. return done()
  140. }
  141. )
  142. })
  143. it('should return modified = true', function () {
  144. return this.body.modified.should.equal(true)
  145. })
  146. it('should return the rev', function () {
  147. return this.body.rev.should.equal(1)
  148. })
  149. return it('should update the doc in the API', function (done) {
  150. return DocstoreClient.getDoc(
  151. this.project_id,
  152. this.doc_id,
  153. {},
  154. (error, res, doc) => {
  155. doc.lines.should.deep.equal(this.originalLines)
  156. doc.version.should.equal(this.version + 1)
  157. doc.ranges.should.deep.equal(this.originalRanges)
  158. return done()
  159. }
  160. )
  161. })
  162. })
  163. describe('when the ranges have changed', function () {
  164. beforeEach(function (done) {
  165. return DocstoreClient.updateDoc(
  166. this.project_id,
  167. this.doc_id,
  168. this.originalLines,
  169. this.version,
  170. this.newRanges,
  171. (error, res, body) => {
  172. this.body = body
  173. return done()
  174. }
  175. )
  176. })
  177. it('should return modified = true', function () {
  178. return this.body.modified.should.equal(true)
  179. })
  180. it('should return the rev', function () {
  181. return this.body.rev.should.equal(2)
  182. })
  183. return it('should update the doc in the API', function (done) {
  184. return DocstoreClient.getDoc(
  185. this.project_id,
  186. this.doc_id,
  187. {},
  188. (error, res, doc) => {
  189. doc.lines.should.deep.equal(this.originalLines)
  190. doc.version.should.equal(this.version)
  191. doc.ranges.should.deep.equal(this.newRanges)
  192. return done()
  193. }
  194. )
  195. })
  196. })
  197. describe('when the doc does not exist', function () {
  198. beforeEach(function (done) {
  199. this.missing_doc_id = ObjectId()
  200. return DocstoreClient.updateDoc(
  201. this.project_id,
  202. this.missing_doc_id,
  203. this.originalLines,
  204. 0,
  205. this.originalRanges,
  206. (error, res, body) => {
  207. this.res = res
  208. this.body = body
  209. return done()
  210. }
  211. )
  212. })
  213. it('should create the doc', function () {
  214. return this.body.rev.should.equal(1)
  215. })
  216. return it('should be retreivable', function (done) {
  217. return DocstoreClient.getDoc(
  218. this.project_id,
  219. this.missing_doc_id,
  220. {},
  221. (error, res, doc) => {
  222. doc.lines.should.deep.equal(this.originalLines)
  223. doc.version.should.equal(0)
  224. doc.ranges.should.deep.equal(this.originalRanges)
  225. return done()
  226. }
  227. )
  228. })
  229. })
  230. describe('when malformed doc lines are provided', function () {
  231. describe('when the lines are not an array', function () {
  232. beforeEach(function (done) {
  233. return DocstoreClient.updateDoc(
  234. this.project_id,
  235. this.doc_id,
  236. { foo: 'bar' },
  237. this.version,
  238. this.originalRanges,
  239. (error, res, body) => {
  240. this.res = res
  241. this.body = body
  242. return done()
  243. }
  244. )
  245. })
  246. it('should return 400', function () {
  247. return this.res.statusCode.should.equal(400)
  248. })
  249. return it('should not update the doc in the API', function (done) {
  250. return DocstoreClient.getDoc(
  251. this.project_id,
  252. this.doc_id,
  253. {},
  254. (error, res, doc) => {
  255. doc.lines.should.deep.equal(this.originalLines)
  256. return done()
  257. }
  258. )
  259. })
  260. })
  261. return describe('when the lines are not present', function () {
  262. beforeEach(function (done) {
  263. return DocstoreClient.updateDoc(
  264. this.project_id,
  265. this.doc_id,
  266. null,
  267. this.version,
  268. this.originalRanges,
  269. (error, res, body) => {
  270. this.res = res
  271. this.body = body
  272. return done()
  273. }
  274. )
  275. })
  276. it('should return 400', function () {
  277. return this.res.statusCode.should.equal(400)
  278. })
  279. return it('should not update the doc in the API', function (done) {
  280. return DocstoreClient.getDoc(
  281. this.project_id,
  282. this.doc_id,
  283. {},
  284. (error, res, doc) => {
  285. doc.lines.should.deep.equal(this.originalLines)
  286. return done()
  287. }
  288. )
  289. })
  290. })
  291. })
  292. describe('when no version is provided', function () {
  293. beforeEach(function (done) {
  294. return DocstoreClient.updateDoc(
  295. this.project_id,
  296. this.doc_id,
  297. this.originalLines,
  298. null,
  299. this.originalRanges,
  300. (error, res, body) => {
  301. this.res = res
  302. this.body = body
  303. return done()
  304. }
  305. )
  306. })
  307. it('should return 400', function () {
  308. return this.res.statusCode.should.equal(400)
  309. })
  310. return it('should not update the doc in the API', function (done) {
  311. return DocstoreClient.getDoc(
  312. this.project_id,
  313. this.doc_id,
  314. {},
  315. (error, res, doc) => {
  316. doc.lines.should.deep.equal(this.originalLines)
  317. doc.version.should.equal(this.version)
  318. return done()
  319. }
  320. )
  321. })
  322. })
  323. describe('when the content is large', function () {
  324. beforeEach(function (done) {
  325. const line = new Array(1025).join('x') // 1kb
  326. this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1mb
  327. return DocstoreClient.updateDoc(
  328. this.project_id,
  329. this.doc_id,
  330. this.largeLines,
  331. this.version,
  332. this.originalRanges,
  333. (error, res, body) => {
  334. this.body = body
  335. return done()
  336. }
  337. )
  338. })
  339. it('should return modified = true', function () {
  340. return this.body.modified.should.equal(true)
  341. })
  342. return it('should update the doc in the API', function (done) {
  343. return DocstoreClient.getDoc(
  344. this.project_id,
  345. this.doc_id,
  346. {},
  347. (error, res, doc) => {
  348. doc.lines.should.deep.equal(this.largeLines)
  349. return done()
  350. }
  351. )
  352. })
  353. })
  354. describe('when there is a large json payload', function () {
  355. beforeEach(function (done) {
  356. const line = new Array(1025).join('x') // 1kb
  357. this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1kb
  358. this.originalRanges.padding = Array.apply(null, Array(2049)).map(
  359. () => line
  360. ) // 2mb + 1kb
  361. return DocstoreClient.updateDoc(
  362. this.project_id,
  363. this.doc_id,
  364. this.largeLines,
  365. this.version,
  366. this.originalRanges,
  367. (error, res, body) => {
  368. this.res = res
  369. this.body = body
  370. return done()
  371. }
  372. )
  373. })
  374. it('should return modified = true', function () {
  375. return this.body.modified.should.equal(true)
  376. })
  377. return it('should update the doc in the API', function (done) {
  378. return DocstoreClient.getDoc(
  379. this.project_id,
  380. this.doc_id,
  381. {},
  382. (error, res, doc) => {
  383. doc.lines.should.deep.equal(this.largeLines)
  384. return done()
  385. }
  386. )
  387. })
  388. })
  389. describe('when the document body is too large', function () {
  390. beforeEach(function (done) {
  391. const line = new Array(1025).join('x') // 1kb
  392. this.largeLines = Array.apply(null, Array(2049)).map(() => line) // 2mb + 1kb
  393. return DocstoreClient.updateDoc(
  394. this.project_id,
  395. this.doc_id,
  396. this.largeLines,
  397. this.version,
  398. this.originalRanges,
  399. (error, res, body) => {
  400. this.res = res
  401. this.body = body
  402. return done()
  403. }
  404. )
  405. })
  406. it('should return 413', function () {
  407. return this.res.statusCode.should.equal(413)
  408. })
  409. it('should report body too large', function () {
  410. return this.res.body.should.equal('document body too large')
  411. })
  412. return it('should not update the doc in the API', function (done) {
  413. return DocstoreClient.getDoc(
  414. this.project_id,
  415. this.doc_id,
  416. {},
  417. (error, res, doc) => {
  418. doc.lines.should.deep.equal(this.originalLines)
  419. return done()
  420. }
  421. )
  422. })
  423. })
  424. return describe('when the json payload is too large', function () {
  425. beforeEach(function (done) {
  426. const line = new Array(1025).join('x') // 1kb
  427. this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1kb
  428. this.originalRanges.padding = Array.apply(null, Array(4096)).map(
  429. () => line
  430. ) // 4mb
  431. return DocstoreClient.updateDoc(
  432. this.project_id,
  433. this.doc_id,
  434. this.largeLines,
  435. this.version,
  436. this.originalRanges,
  437. (error, res, body) => {
  438. this.res = res
  439. this.body = body
  440. return done()
  441. }
  442. )
  443. })
  444. return it('should not update the doc in the API', function (done) {
  445. return DocstoreClient.getDoc(
  446. this.project_id,
  447. this.doc_id,
  448. {},
  449. (error, res, doc) => {
  450. doc.lines.should.deep.equal(this.originalLines)
  451. return done()
  452. }
  453. )
  454. })
  455. })
  456. })