LeaveDocTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* eslint-disable
  2. no-return-assign,
  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. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const { expect } = require('chai')
  15. const sinon = require('sinon')
  16. const RealTimeClient = require('./helpers/RealTimeClient')
  17. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  18. const FixturesManager = require('./helpers/FixturesManager')
  19. const logger = require('@overleaf/logger')
  20. const async = require('async')
  21. describe('leaveDoc', function () {
  22. before(function () {
  23. this.lines = ['test', 'doc', 'lines']
  24. this.version = 42
  25. this.ops = ['mock', 'doc', 'ops']
  26. sinon.spy(logger, 'error')
  27. sinon.spy(logger, 'warn')
  28. sinon.spy(logger, 'debug')
  29. return (this.other_doc_id = FixturesManager.getRandomId())
  30. })
  31. after(function () {
  32. logger.error.restore() // remove the spy
  33. logger.warn.restore()
  34. return logger.debug.restore()
  35. })
  36. return describe('when joined to a doc', function () {
  37. beforeEach(function (done) {
  38. return async.series(
  39. [
  40. cb => {
  41. return FixturesManager.setUpProject(
  42. {
  43. privilegeLevel: 'readAndWrite',
  44. },
  45. (e, { project_id: projectId, user_id: userId }) => {
  46. this.project_id = projectId
  47. this.user_id = userId
  48. return cb(e)
  49. }
  50. )
  51. },
  52. cb => {
  53. return FixturesManager.setUpDoc(
  54. this.project_id,
  55. { lines: this.lines, version: this.version, ops: this.ops },
  56. (e, { doc_id: docId }) => {
  57. this.doc_id = docId
  58. return cb(e)
  59. }
  60. )
  61. },
  62. cb => {
  63. this.client = RealTimeClient.connect()
  64. return this.client.on('connectionAccepted', cb)
  65. },
  66. cb => {
  67. return this.client.emit(
  68. 'joinProject',
  69. { project_id: this.project_id },
  70. cb
  71. )
  72. },
  73. cb => {
  74. return this.client.emit(
  75. 'joinDoc',
  76. this.doc_id,
  77. (error, ...rest) => {
  78. ;[...this.returnedArgs] = Array.from(rest)
  79. return cb(error)
  80. }
  81. )
  82. },
  83. ],
  84. done
  85. )
  86. })
  87. describe('then leaving the doc', function () {
  88. beforeEach(function (done) {
  89. return this.client.emit('leaveDoc', this.doc_id, error => {
  90. if (error != null) {
  91. throw error
  92. }
  93. return done()
  94. })
  95. })
  96. return it('should have left the doc room', function (done) {
  97. return RealTimeClient.getConnectedClient(
  98. this.client.socket.sessionid,
  99. (error, client) => {
  100. if (error) return done(error)
  101. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
  102. false
  103. )
  104. return done()
  105. }
  106. )
  107. })
  108. })
  109. describe('then leaving an invalid doc', function () {
  110. beforeEach(function (done) {
  111. return this.client.emit('leaveDoc', 'bad-id', error => {
  112. this.error = error
  113. return done()
  114. })
  115. })
  116. return it('should return an error', function () {
  117. return expect(this.error).to.exist
  118. })
  119. })
  120. describe('when sending a leaveDoc request before the previous joinDoc request has completed', function () {
  121. beforeEach(function (done) {
  122. this.client.emit('leaveDoc', this.doc_id, () => {})
  123. this.client.emit('joinDoc', this.doc_id, () => {})
  124. return this.client.emit('leaveDoc', this.doc_id, error => {
  125. if (error != null) {
  126. throw error
  127. }
  128. return done()
  129. })
  130. })
  131. it('should not trigger an error', function () {
  132. return sinon.assert.neverCalledWith(
  133. logger.error,
  134. sinon.match.any,
  135. "not subscribed - shouldn't happen"
  136. )
  137. })
  138. return it('should have left the doc room', function (done) {
  139. return RealTimeClient.getConnectedClient(
  140. this.client.socket.sessionid,
  141. (error, client) => {
  142. if (error) return done(error)
  143. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
  144. false
  145. )
  146. return done()
  147. }
  148. )
  149. })
  150. })
  151. return describe('when sending a leaveDoc for a room the client has not joined ', function () {
  152. beforeEach(function (done) {
  153. return this.client.emit('leaveDoc', this.other_doc_id, error => {
  154. if (error != null) {
  155. throw error
  156. }
  157. return done()
  158. })
  159. })
  160. return it('should trigger a low level message only', function () {
  161. return sinon.assert.calledWith(
  162. logger.debug,
  163. sinon.match.any,
  164. 'ignoring request from client to leave room it is not in'
  165. )
  166. })
  167. })
  168. })
  169. })