LeaveProjectTests.js 7.8 KB

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