LeaveProjectTests.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* eslint-disable
  2. no-throw-literal,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const RealTimeClient = require('./helpers/RealTimeClient')
  13. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  14. const FixturesManager = require('./helpers/FixturesManager')
  15. const async = require('async')
  16. const settings = require('@overleaf/settings')
  17. const redis = require('@overleaf/redis-wrapper')
  18. const rclient = redis.createClient(settings.redis.pubsub)
  19. describe('leaveProject', function () {
  20. before(function (done) {
  21. return MockDocUpdaterServer.run(done)
  22. })
  23. describe('with other clients in the project', function () {
  24. before(function (done) {
  25. return async.series(
  26. [
  27. cb => {
  28. return FixturesManager.setUpProject(
  29. {
  30. privilegeLevel: 'owner',
  31. project: {
  32. name: 'Test Project',
  33. },
  34. },
  35. (e, { project_id: projectId, user_id: userId }) => {
  36. this.project_id = projectId
  37. this.user_id = userId
  38. return cb()
  39. }
  40. )
  41. },
  42. cb => {
  43. this.clientA = RealTimeClient.connect()
  44. return this.clientA.on('connectionAccepted', cb)
  45. },
  46. cb => {
  47. this.clientB = RealTimeClient.connect()
  48. this.clientB.on('connectionAccepted', cb)
  49. this.clientBDisconnectMessages = []
  50. return this.clientB.on(
  51. 'clientTracking.clientDisconnected',
  52. data => {
  53. return this.clientBDisconnectMessages.push(data)
  54. }
  55. )
  56. },
  57. cb => {
  58. return this.clientA.emit(
  59. 'joinProject',
  60. { project_id: this.project_id },
  61. (error, project, privilegeLevel, protocolVersion) => {
  62. this.project = project
  63. this.privilegeLevel = privilegeLevel
  64. this.protocolVersion = protocolVersion
  65. return cb(error)
  66. }
  67. )
  68. },
  69. cb => {
  70. return this.clientB.emit(
  71. 'joinProject',
  72. { project_id: this.project_id },
  73. (error, project, privilegeLevel, protocolVersion) => {
  74. this.project = project
  75. this.privilegeLevel = privilegeLevel
  76. this.protocolVersion = protocolVersion
  77. return cb(error)
  78. }
  79. )
  80. },
  81. cb => {
  82. return FixturesManager.setUpDoc(
  83. this.project_id,
  84. { lines: this.lines, version: this.version, ops: this.ops },
  85. (e, { doc_id: docId }) => {
  86. this.doc_id = docId
  87. return cb(e)
  88. }
  89. )
  90. },
  91. cb => {
  92. return this.clientA.emit('joinDoc', this.doc_id, cb)
  93. },
  94. cb => {
  95. return this.clientB.emit('joinDoc', this.doc_id, cb)
  96. },
  97. cb => {
  98. // leaveProject is called when the client disconnects
  99. this.clientA.on('disconnect', () => cb())
  100. return this.clientA.disconnect()
  101. },
  102. cb => {
  103. // The API waits a little while before flushing changes
  104. return setTimeout(done, 1000)
  105. },
  106. ],
  107. done
  108. )
  109. })
  110. it('should emit a disconnect message to the room', function () {
  111. return this.clientBDisconnectMessages.should.deep.equal([
  112. this.clientA.publicId,
  113. ])
  114. })
  115. it('should no longer list the client in connected users', function (done) {
  116. return this.clientB.emit(
  117. 'clientTracking.getConnectedUsers',
  118. (error, users) => {
  119. if (error) return done(error)
  120. for (const user of Array.from(users)) {
  121. if (user.client_id === this.clientA.publicId) {
  122. throw 'Expected clientA to not be listed in connected users'
  123. }
  124. }
  125. return done()
  126. }
  127. )
  128. })
  129. it('should not flush the project to the document updater', function () {
  130. return MockDocUpdaterServer.deleteProject
  131. .calledWith(this.project_id)
  132. .should.equal(false)
  133. })
  134. it('should remain subscribed to the editor-events channels', function (done) {
  135. rclient.pubsub('CHANNELS', (err, resp) => {
  136. if (err) {
  137. return done(err)
  138. }
  139. resp.should.include(`editor-events:${this.project_id}`)
  140. return done()
  141. })
  142. return null
  143. })
  144. return it('should remain subscribed to the applied-ops channels', function (done) {
  145. rclient.pubsub('CHANNELS', (err, resp) => {
  146. if (err) {
  147. return done(err)
  148. }
  149. resp.should.include(`applied-ops:${this.doc_id}`)
  150. return done()
  151. })
  152. return null
  153. })
  154. })
  155. return describe('with no other clients in the project', function () {
  156. before(function (done) {
  157. return async.series(
  158. [
  159. cb => {
  160. return FixturesManager.setUpProject(
  161. {
  162. privilegeLevel: 'owner',
  163. project: {
  164. name: 'Test Project',
  165. },
  166. },
  167. (e, { project_id: projectId, user_id: userId }) => {
  168. this.project_id = projectId
  169. this.user_id = userId
  170. return cb()
  171. }
  172. )
  173. },
  174. cb => {
  175. this.clientA = RealTimeClient.connect()
  176. return this.clientA.on('connect', cb)
  177. },
  178. cb => {
  179. return this.clientA.emit(
  180. 'joinProject',
  181. { project_id: this.project_id },
  182. (error, project, privilegeLevel, protocolVersion) => {
  183. this.project = project
  184. this.privilegeLevel = privilegeLevel
  185. this.protocolVersion = protocolVersion
  186. return cb(error)
  187. }
  188. )
  189. },
  190. cb => {
  191. return FixturesManager.setUpDoc(
  192. this.project_id,
  193. { lines: this.lines, version: this.version, ops: this.ops },
  194. (e, { doc_id: docId }) => {
  195. this.doc_id = docId
  196. return cb(e)
  197. }
  198. )
  199. },
  200. cb => {
  201. return this.clientA.emit('joinDoc', this.doc_id, cb)
  202. },
  203. cb => {
  204. // leaveProject is called when the client disconnects
  205. this.clientA.on('disconnect', () => cb())
  206. return this.clientA.disconnect()
  207. },
  208. cb => {
  209. // The API waits a little while before flushing changes
  210. return setTimeout(done, 1000)
  211. },
  212. ],
  213. done
  214. )
  215. })
  216. it('should flush the project to the document updater', function () {
  217. return MockDocUpdaterServer.deleteProject
  218. .calledWith(this.project_id)
  219. .should.equal(true)
  220. })
  221. it('should not subscribe to the editor-events channels anymore', function (done) {
  222. rclient.pubsub('CHANNELS', (err, resp) => {
  223. if (err) {
  224. return done(err)
  225. }
  226. resp.should.not.include(`editor-events:${this.project_id}`)
  227. return done()
  228. })
  229. return null
  230. })
  231. return it('should not subscribe to the applied-ops channels anymore', function (done) {
  232. rclient.pubsub('CHANNELS', (err, resp) => {
  233. if (err) {
  234. return done(err)
  235. }
  236. resp.should.not.include(`applied-ops:${this.doc_id}`)
  237. return done()
  238. })
  239. return null
  240. })
  241. })
  242. })