ReceiveEditorEventTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. import { expect } from 'chai'
  13. import RealTimeClient from './helpers/RealTimeClient.js'
  14. import FixturesManager from './helpers/FixturesManager.js'
  15. import async from 'async'
  16. import settings from '@overleaf/settings'
  17. import redis from '@overleaf/redis-wrapper'
  18. const rclient = redis.createClient(settings.redis.pubsub)
  19. describe('receiveEditorEvent', function () {
  20. beforeEach(function (done) {
  21. this.lines = ['test', 'doc', 'lines']
  22. this.version = 42
  23. this.ops = ['mock', 'doc', 'ops']
  24. /**
  25. * We will set up a project, a doc, and three users: the owner, user 'a' and user 'b'
  26. */
  27. this.project_id = null
  28. this.doc_id = null
  29. this.owner_user_id = null
  30. this.owner_client = null
  31. this.user_a_id = null
  32. this.user_a_client = null
  33. this.user_b_id = null
  34. this.user_b_client = null
  35. this.user_c_id = null
  36. this.user_c_client = null
  37. async.series(
  38. [
  39. /**
  40. * Create the project, doc, and owner
  41. */
  42. cb => {
  43. return FixturesManager.setUpProject(
  44. {
  45. privilegeLevel: 'owner',
  46. project: { name: 'Test Project' },
  47. userMetadata: { isInvitedMember: true },
  48. },
  49. (error, { user_id: userId, project_id: projectId }) => {
  50. if (error) return done(error)
  51. this.owner_user_id = userId
  52. this.project_id = projectId
  53. return cb()
  54. }
  55. )
  56. },
  57. cb => {
  58. return FixturesManager.setUpDoc(
  59. this.project_id,
  60. { lines: this.lines, version: this.version, ops: this.ops },
  61. (e, { doc_id: docId }) => {
  62. this.doc_id = docId
  63. return cb(e)
  64. }
  65. )
  66. },
  67. /**
  68. * Connect owner to project/doc
  69. */
  70. cb => {
  71. this.owner_client = RealTimeClient.connect(this.project_id, cb)
  72. },
  73. cb => {
  74. return this.owner_client.emit('joinDoc', this.doc_id, cb)
  75. },
  76. /**
  77. * add user_a to project, as an invited member
  78. */
  79. cb => {
  80. return FixturesManager.setUpProject(
  81. {
  82. privilegeLevel: 'readAndWrite',
  83. project_id: this.project_id,
  84. userMetadata: { isTokenMember: false, isInvitedMember: true },
  85. },
  86. (error, { user_id: userIdSecond }) => {
  87. if (error) return done(error)
  88. this.user_a_id = userIdSecond
  89. return cb()
  90. }
  91. )
  92. },
  93. /**
  94. * Connect user_a to project/doc
  95. */
  96. cb => {
  97. this.user_a_client = RealTimeClient.connect(this.project_id, cb)
  98. },
  99. cb => {
  100. return this.user_a_client.emit('joinDoc', this.doc_id, cb)
  101. },
  102. /**
  103. * Set up user_b, as a token-access/link-sharing user
  104. */
  105. cb => {
  106. return FixturesManager.setUpProject(
  107. {
  108. privilegeLevel: 'readAndWrite',
  109. project_id: this.project_id,
  110. userMetadata: { isTokenMember: true, isInvitedMember: false },
  111. },
  112. (error, { user_id: userIdThird }) => {
  113. if (error) return done(error)
  114. this.user_b_id = userIdThird
  115. return cb()
  116. }
  117. )
  118. },
  119. /**
  120. * Connect user_b to project/doc
  121. */
  122. cb => {
  123. this.user_b_client = RealTimeClient.connect(this.project_id, cb)
  124. },
  125. cb => {
  126. return this.user_b_client.emit('joinDoc', this.doc_id, cb)
  127. },
  128. /**
  129. * Set up user_c, as a 'restricted' user (anonymous read-only link-sharing)
  130. */
  131. cb => {
  132. return FixturesManager.setUpProject(
  133. {
  134. privilegeLevel: 'readAndWrite',
  135. project_id: this.project_id,
  136. userMetadata: {
  137. isTokenMember: false,
  138. isInvitedMember: false,
  139. isRestrictedUser: true,
  140. },
  141. },
  142. (error, { user_id: userIdFourth }) => {
  143. if (error) return done(error)
  144. this.user_c_id = userIdFourth
  145. return cb()
  146. }
  147. )
  148. },
  149. /**
  150. * Connect user_c to project/doc
  151. */
  152. cb => {
  153. this.user_c_client = RealTimeClient.connect(this.project_id, cb)
  154. },
  155. cb => {
  156. return this.user_c_client.emit('joinDoc', this.doc_id, cb)
  157. },
  158. // --------------
  159. /**
  160. * Listen for updates
  161. */
  162. cb => {
  163. this.owner_updates = []
  164. this.user_a_updates = []
  165. this.user_b_updates = []
  166. this.user_c_updates = []
  167. const eventNames = [
  168. 'userRemovedFromProject',
  169. 'project:publicAccessLevel:changed',
  170. 'project:access:revoked',
  171. ]
  172. for (const eventName of eventNames) {
  173. this.owner_client.on(eventName, update =>
  174. this.owner_updates.push({ [eventName]: update })
  175. )
  176. this.user_a_client.on(eventName, update =>
  177. this.user_a_updates.push({ [eventName]: update })
  178. )
  179. this.user_b_client.on(eventName, update =>
  180. this.user_b_updates.push({ [eventName]: update })
  181. )
  182. this.user_c_client.on(eventName, update =>
  183. this.user_c_updates.push({ [eventName]: update })
  184. )
  185. }
  186. return cb()
  187. },
  188. ],
  189. done
  190. )
  191. })
  192. afterEach(function () {
  193. if (this.owner_client) {
  194. this.owner_client.disconnect()
  195. }
  196. if (this.user_a_client) {
  197. this.user_a_client.disconnect()
  198. }
  199. if (this.user_b_client) {
  200. this.user_b_client.disconnect()
  201. }
  202. if (this.user_c_client) {
  203. this.user_c_client.disconnect()
  204. }
  205. })
  206. describe('event: project:publicAccessLevel:changed, set to private', function () {
  207. beforeEach(function (done) {
  208. /**
  209. * We turn off link sharing
  210. */
  211. rclient.publish(
  212. 'editor-events',
  213. JSON.stringify({
  214. room_id: this.project_id,
  215. message: 'project:publicAccessLevel:changed',
  216. payload: [{ newAccessLevel: 'private' }],
  217. })
  218. )
  219. setTimeout(done, 200)
  220. })
  221. it('should disconnect the token-access user, and restricted users', function () {
  222. expect(this.user_b_client.socket.connected).to.equal(false)
  223. expect(this.user_c_client.socket.connected).to.equal(false)
  224. })
  225. it('should not disconnect the other users', function () {
  226. expect(this.owner_client.socket.connected).to.equal(true)
  227. expect(this.user_a_client.socket.connected).to.equal(true)
  228. })
  229. it('should send the event to the remaining connected clients', function () {
  230. expect(this.owner_updates).to.deep.equal([
  231. { 'project:publicAccessLevel:changed': { newAccessLevel: 'private' } },
  232. ])
  233. expect(this.user_a_updates).to.deep.equal([
  234. { 'project:publicAccessLevel:changed': { newAccessLevel: 'private' } },
  235. ])
  236. })
  237. it('should send a project:access:revoked message to the disconnected clients', function () {
  238. expect(this.user_b_updates).to.deep.equal([
  239. { 'project:access:revoked': undefined },
  240. ])
  241. expect(this.user_c_updates).to.deep.equal([
  242. { 'project:access:revoked': undefined },
  243. ])
  244. })
  245. })
  246. describe('event: project:publicAccessLevel:changed, set to tokenBased', function () {
  247. beforeEach(function (done) {
  248. /**
  249. * We turn on link sharing
  250. */
  251. rclient.publish(
  252. 'editor-events',
  253. JSON.stringify({
  254. room_id: this.project_id,
  255. message: 'project:publicAccessLevel:changed',
  256. payload: [{ newAccessLevel: 'tokenBased' }],
  257. })
  258. )
  259. setTimeout(done, 200)
  260. })
  261. it('should not disconnect anyone', function () {
  262. expect(this.owner_client.socket.connected).to.equal(true)
  263. expect(this.user_a_client.socket.connected).to.equal(true)
  264. expect(this.user_b_client.socket.connected).to.equal(true)
  265. expect(this.user_c_client.socket.connected).to.equal(true)
  266. })
  267. it('should send the event to all non-restricted clients', function () {
  268. expect(this.owner_updates).to.deep.equal([
  269. {
  270. 'project:publicAccessLevel:changed': { newAccessLevel: 'tokenBased' },
  271. },
  272. ])
  273. expect(this.user_a_updates).to.deep.equal([
  274. {
  275. 'project:publicAccessLevel:changed': { newAccessLevel: 'tokenBased' },
  276. },
  277. ])
  278. expect(this.user_b_updates).to.deep.equal([
  279. {
  280. 'project:publicAccessLevel:changed': { newAccessLevel: 'tokenBased' },
  281. },
  282. ])
  283. // restricted users don't receive this type of message
  284. expect(this.user_c_updates.length).to.equal(0)
  285. })
  286. })
  287. describe('event: userRemovedFromProject', function () {
  288. let removedUserId
  289. beforeEach(function (done) {
  290. /**
  291. * We remove user_a from the project
  292. */
  293. removedUserId = `${this.user_a_id}`
  294. rclient.publish(
  295. 'editor-events',
  296. JSON.stringify({
  297. room_id: this.project_id,
  298. message: 'userRemovedFromProject',
  299. payload: [removedUserId],
  300. })
  301. )
  302. setTimeout(done, 200)
  303. })
  304. it('should disconnect the removed user', function () {
  305. expect(this.user_a_client.socket.connected).to.equal(false)
  306. })
  307. it('should not disconnect the other users', function () {
  308. expect(this.owner_client.socket.connected).to.equal(true)
  309. expect(this.user_b_client.socket.connected).to.equal(true)
  310. })
  311. it('should send the event to the remaining connected clients', function () {
  312. expect(this.owner_updates).to.deep.equal([
  313. { userRemovedFromProject: removedUserId },
  314. ])
  315. expect(this.user_b_updates).to.deep.equal([
  316. { userRemovedFromProject: removedUserId },
  317. ])
  318. })
  319. it('should send a project:access:revoked message to the disconnected clients', function () {
  320. expect(this.user_a_updates).to.deep.equal([
  321. { 'project:access:revoked': undefined },
  322. ])
  323. })
  324. })
  325. })