DocUpdateTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const User = require('./helpers/User')
  2. const request = require('./helpers/request')
  3. const { expect } = require('chai')
  4. const settings = require('settings-sharelatex')
  5. const { ObjectId } = require('mongodb')
  6. require('./helpers/MockDocstoreApi')
  7. require('./helpers/MockV1Api')
  8. require('./helpers/MockProjectHistoryApi')
  9. describe('DocUpdate', function() {
  10. beforeEach(function(done) {
  11. this.user = new User()
  12. this.projectName = 'wombat'
  13. this.user.ensureUserExists(() => {
  14. this.user.login(() => {
  15. this.user.createProject(this.projectName, (error, projectId) => {
  16. if (error) return done(error)
  17. this.projectId = projectId
  18. this.user.getProject(this.projectId, (error, project) => {
  19. if (error) return done(error)
  20. this.project = project
  21. this.user.createDocInProject(
  22. this.projectId,
  23. this.project.rootFolder[0]._id,
  24. 'potato',
  25. (error, docId) => {
  26. this.docId = docId
  27. done(error)
  28. }
  29. )
  30. })
  31. })
  32. })
  33. })
  34. })
  35. function writeContent(
  36. { projectId, docId, lines, version, ranges, lastUpdatedAt, lastUpdatedBy },
  37. callback
  38. ) {
  39. request(
  40. {
  41. method: 'POST',
  42. url: `/project/${projectId}/doc/${docId}`,
  43. auth: {
  44. user: settings.apis.web.user,
  45. pass: settings.apis.web.pass,
  46. sendImmediately: true
  47. },
  48. json: { lines, version, ranges, lastUpdatedAt, lastUpdatedBy }
  49. },
  50. (error, res) => {
  51. if (error) return callback(error)
  52. if (res.statusCode !== 200)
  53. return callback(
  54. new Error(`non-success statusCode: ${res.statusCode}`)
  55. )
  56. callback()
  57. }
  58. )
  59. }
  60. function updateContent(options, callback) {
  61. writeContent(options, err => {
  62. if (err) return callback(err)
  63. options.lines.push('foo')
  64. options.version++
  65. writeContent(options, callback)
  66. })
  67. }
  68. function writeContentTwice(options, callback) {
  69. writeContent(options, err => {
  70. if (err) return callback(err)
  71. writeContent(options, callback)
  72. })
  73. }
  74. let writeOptions
  75. beforeEach(function() {
  76. writeOptions = {
  77. projectId: this.projectId,
  78. docId: this.docId,
  79. lines: ['a'],
  80. version: 1,
  81. ranges: {},
  82. lastUpdatedAt: new Date(),
  83. lastUpdatedBy: this.user.id
  84. }
  85. })
  86. function shouldAcceptChanges() {
  87. it('should accept writes', function(done) {
  88. writeContent(writeOptions, done)
  89. })
  90. it('should accept updates', function(done) {
  91. updateContent(writeOptions, done)
  92. })
  93. it('should accept same write twice', function(done) {
  94. writeContentTwice(writeOptions, done)
  95. })
  96. }
  97. function shouldBlockChanges() {
  98. it('should block writes', function(done) {
  99. writeContent(writeOptions, err => {
  100. expect(err).to.exist
  101. expect(err.message).to.equal('non-success statusCode: 404')
  102. done()
  103. })
  104. })
  105. it('should block updates', function(done) {
  106. updateContent(writeOptions, err => {
  107. expect(err).to.exist
  108. expect(err.message).to.equal('non-success statusCode: 404')
  109. done()
  110. })
  111. })
  112. }
  113. describe('a regular doc', function() {
  114. shouldAcceptChanges()
  115. })
  116. describe('after deleting the doc', function() {
  117. beforeEach(function(done) {
  118. this.user.deleteItemInProject(this.projectId, 'doc', this.docId, done)
  119. })
  120. shouldAcceptChanges()
  121. })
  122. describe('unknown doc', function() {
  123. beforeEach(function() {
  124. writeOptions.docId = ObjectId()
  125. })
  126. shouldBlockChanges()
  127. })
  128. describe('doc in another project', function() {
  129. beforeEach(function(done) {
  130. this.user.createProject('foo', (error, projectId) => {
  131. if (error) return done(error)
  132. writeOptions.projectId = projectId
  133. done()
  134. })
  135. })
  136. shouldBlockChanges()
  137. })
  138. })