UpdatingDocsTests.js 13 KB

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