| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- /* eslint-disable
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon')
- const { ObjectId } = require('mongodb-legacy')
- const DocstoreApp = require('./helpers/DocstoreApp')
- const DocstoreClient = require('./helpers/DocstoreClient')
- describe('Applying updates to a doc', function () {
- beforeEach(function (done) {
- this.project_id = new ObjectId()
- this.doc_id = new ObjectId()
- this.originalLines = ['original', 'lines']
- this.newLines = ['new', 'lines']
- this.originalRanges = {
- changes: [
- {
- id: new ObjectId().toString(),
- op: { i: 'foo', p: 3 },
- meta: {
- user_id: new ObjectId().toString(),
- ts: new Date().toString(),
- },
- },
- ],
- }
- this.newRanges = {
- changes: [
- {
- id: new ObjectId().toString(),
- op: { i: 'bar', p: 6 },
- meta: {
- user_id: new ObjectId().toString(),
- ts: new Date().toString(),
- },
- },
- ],
- }
- this.version = 42
- return DocstoreApp.ensureRunning(() => {
- return DocstoreClient.createDoc(
- this.project_id,
- this.doc_id,
- this.originalLines,
- this.version,
- this.originalRanges,
- error => {
- if (error != null) {
- throw error
- }
- return done()
- }
- )
- })
- })
- describe('when nothing has been updated', function () {
- beforeEach(function (done) {
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.originalLines,
- this.version,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.body = body
- return done()
- }
- )
- })
- it('should return modified = false', function () {
- return this.body.modified.should.equal(false)
- })
- return it('should not update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- doc.version.should.equal(this.version)
- doc.ranges.should.deep.equal(this.originalRanges)
- return done()
- }
- )
- })
- })
- describe('when the lines have changed', function () {
- beforeEach(function (done) {
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.newLines,
- this.version,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.body = body
- return done()
- }
- )
- })
- it('should return modified = true', function () {
- return this.body.modified.should.equal(true)
- })
- it('should return the rev', function () {
- return this.body.rev.should.equal(2)
- })
- return it('should update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.newLines)
- doc.version.should.equal(this.version)
- doc.ranges.should.deep.equal(this.originalRanges)
- return done()
- }
- )
- })
- })
- describe('when the version has changed', function () {
- beforeEach(function (done) {
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.originalLines,
- this.version + 1,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.body = body
- return done()
- }
- )
- })
- it('should return modified = true', function () {
- return this.body.modified.should.equal(true)
- })
- it('should return the rev', function () {
- return this.body.rev.should.equal(1)
- })
- return it('should update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- doc.version.should.equal(this.version + 1)
- doc.ranges.should.deep.equal(this.originalRanges)
- return done()
- }
- )
- })
- })
- describe('when the version was decremented', function () {
- beforeEach(function (done) {
- DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.newLines,
- this.version - 1,
- this.newRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.res = res
- this.body = body
- done()
- }
- )
- })
- it('should return 409', function () {
- this.res.statusCode.should.equal(409)
- })
- it('should not update the doc in the API', function (done) {
- DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- doc.version.should.equal(this.version)
- doc.ranges.should.deep.equal(this.originalRanges)
- done()
- }
- )
- })
- })
- describe('when the ranges have changed', function () {
- beforeEach(function (done) {
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.originalLines,
- this.version,
- this.newRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.body = body
- return done()
- }
- )
- })
- it('should return modified = true', function () {
- return this.body.modified.should.equal(true)
- })
- it('should return the rev', function () {
- return this.body.rev.should.equal(2)
- })
- return it('should update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- doc.version.should.equal(this.version)
- doc.ranges.should.deep.equal(this.newRanges)
- return done()
- }
- )
- })
- })
- describe('when the doc does not exist', function () {
- beforeEach(function (done) {
- this.missing_doc_id = new ObjectId()
- return DocstoreClient.updateDoc(
- this.project_id,
- this.missing_doc_id,
- this.originalLines,
- 0,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.res = res
- this.body = body
- return done()
- }
- )
- })
- it('should create the doc', function () {
- return this.body.rev.should.equal(1)
- })
- return it('should be retreivable', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.missing_doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- doc.version.should.equal(0)
- doc.ranges.should.deep.equal(this.originalRanges)
- return done()
- }
- )
- })
- })
- describe('when malformed doc lines are provided', function () {
- describe('when the lines are not an array', function () {
- beforeEach(function (done) {
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- { foo: 'bar' },
- this.version,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.res = res
- this.body = body
- return done()
- }
- )
- })
- it('should return 400', function () {
- return this.res.statusCode.should.equal(400)
- })
- return it('should not update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- return done()
- }
- )
- })
- })
- return describe('when the lines are not present', function () {
- beforeEach(function (done) {
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- null,
- this.version,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.res = res
- this.body = body
- return done()
- }
- )
- })
- it('should return 400', function () {
- return this.res.statusCode.should.equal(400)
- })
- return it('should not update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- return done()
- }
- )
- })
- })
- })
- describe('when no version is provided', function () {
- beforeEach(function (done) {
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.originalLines,
- null,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.res = res
- this.body = body
- return done()
- }
- )
- })
- it('should return 400', function () {
- return this.res.statusCode.should.equal(400)
- })
- return it('should not update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- doc.version.should.equal(this.version)
- return done()
- }
- )
- })
- })
- describe('when the content is large', function () {
- beforeEach(function (done) {
- const line = new Array(1025).join('x') // 1kb
- this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1mb
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.largeLines,
- this.version,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.body = body
- return done()
- }
- )
- })
- it('should return modified = true', function () {
- return this.body.modified.should.equal(true)
- })
- return it('should update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.largeLines)
- return done()
- }
- )
- })
- })
- describe('when there is a large json payload', function () {
- beforeEach(function (done) {
- const line = new Array(1025).join('x') // 1kb
- this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1kb
- this.originalRanges.padding = Array.apply(null, Array(2049)).map(
- () => line
- ) // 2mb + 1kb
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.largeLines,
- this.version,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.res = res
- this.body = body
- return done()
- }
- )
- })
- it('should return modified = true', function () {
- return this.body.modified.should.equal(true)
- })
- return it('should update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.largeLines)
- return done()
- }
- )
- })
- })
- describe('when the document body is too large', function () {
- beforeEach(function (done) {
- const line = new Array(1025).join('x') // 1kb
- this.largeLines = Array.apply(null, Array(2049)).map(() => line) // 2mb + 1kb
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.largeLines,
- this.version,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.res = res
- this.body = body
- return done()
- }
- )
- })
- it('should return 413', function () {
- return this.res.statusCode.should.equal(413)
- })
- it('should report body too large', function () {
- return this.res.body.should.equal('document body too large')
- })
- return it('should not update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- return done()
- }
- )
- })
- })
- return describe('when the json payload is too large', function () {
- beforeEach(function (done) {
- const line = new Array(1025).join('x') // 1kb
- this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1kb
- this.originalRanges.padding = Array.apply(null, Array(6144)).map(
- () => line
- ) // 6mb
- return DocstoreClient.updateDoc(
- this.project_id,
- this.doc_id,
- this.largeLines,
- this.version,
- this.originalRanges,
- (error, res, body) => {
- if (error) return done(error)
- this.res = res
- this.body = body
- return done()
- }
- )
- })
- return it('should not update the doc in the API', function (done) {
- return DocstoreClient.getDoc(
- this.project_id,
- this.doc_id,
- {},
- (error, res, doc) => {
- if (error) return done(error)
- doc.lines.should.deep.equal(this.originalLines)
- return done()
- }
- )
- })
- })
- })
|