LeaveProjectTests.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-throw-literal,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const RealTimeClient = require('./helpers/RealTimeClient')
  15. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  16. const FixturesManager = require('./helpers/FixturesManager')
  17. const async = require('async')
  18. const settings = require('settings-sharelatex')
  19. const redis = require('redis-sharelatex')
  20. const rclient = redis.createClient(settings.redis.pubsub)
  21. describe('leaveProject', function () {
  22. before(function (done) {
  23. return MockDocUpdaterServer.run(done)
  24. })
  25. describe('with other clients in the project', function () {
  26. before(function (done) {
  27. return async.series(
  28. [
  29. (cb) => {
  30. return FixturesManager.setUpProject(
  31. {
  32. privilegeLevel: 'owner',
  33. project: {
  34. name: 'Test Project'
  35. }
  36. },
  37. (e, { project_id, user_id }) => {
  38. this.project_id = project_id
  39. this.user_id = user_id
  40. return cb()
  41. }
  42. )
  43. },
  44. (cb) => {
  45. this.clientA = RealTimeClient.connect()
  46. return this.clientA.on('connectionAccepted', cb)
  47. },
  48. (cb) => {
  49. this.clientB = RealTimeClient.connect()
  50. this.clientB.on('connectionAccepted', cb)
  51. this.clientBDisconnectMessages = []
  52. return this.clientB.on(
  53. 'clientTracking.clientDisconnected',
  54. (data) => {
  55. return this.clientBDisconnectMessages.push(data)
  56. }
  57. )
  58. },
  59. (cb) => {
  60. return this.clientA.emit(
  61. 'joinProject',
  62. { project_id: this.project_id },
  63. (error, project, privilegeLevel, protocolVersion) => {
  64. this.project = project
  65. this.privilegeLevel = privilegeLevel
  66. this.protocolVersion = protocolVersion
  67. return cb(error)
  68. }
  69. )
  70. },
  71. (cb) => {
  72. return this.clientB.emit(
  73. 'joinProject',
  74. { project_id: this.project_id },
  75. (error, project, privilegeLevel, protocolVersion) => {
  76. this.project = project
  77. this.privilegeLevel = privilegeLevel
  78. this.protocolVersion = protocolVersion
  79. return cb(error)
  80. }
  81. )
  82. },
  83. (cb) => {
  84. return FixturesManager.setUpDoc(
  85. this.project_id,
  86. { lines: this.lines, version: this.version, ops: this.ops },
  87. (e, { doc_id }) => {
  88. this.doc_id = doc_id
  89. return cb(e)
  90. }
  91. )
  92. },
  93. (cb) => {
  94. return this.clientA.emit('joinDoc', this.doc_id, cb)
  95. },
  96. (cb) => {
  97. return this.clientB.emit('joinDoc', this.doc_id, cb)
  98. },
  99. (cb) => {
  100. // leaveProject is called when the client disconnects
  101. this.clientA.on('disconnect', () => cb())
  102. return this.clientA.disconnect()
  103. },
  104. (cb) => {
  105. // The API waits a little while before flushing changes
  106. return setTimeout(done, 1000)
  107. }
  108. ],
  109. done
  110. )
  111. })
  112. it('should emit a disconnect message to the room', function () {
  113. return this.clientBDisconnectMessages.should.deep.equal([
  114. this.clientA.publicId
  115. ])
  116. })
  117. it('should no longer list the client in connected users', function (done) {
  118. return this.clientB.emit(
  119. 'clientTracking.getConnectedUsers',
  120. (error, users) => {
  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. })