WebsocketControllerTests.coffee 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. chai = require('chai')
  2. should = chai.should()
  3. sinon = require("sinon")
  4. expect = chai.expect
  5. modulePath = "../../../app/js/WebsocketController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. tk = require "timekeeper"
  8. describe 'WebsocketController', ->
  9. beforeEach ->
  10. tk.freeze(new Date())
  11. @project_id = "project-id-123"
  12. @user = {
  13. _id: @user_id = "user-id-123"
  14. first_name: "James"
  15. last_name: "Allen"
  16. email: "james@example.com"
  17. signUpDate: new Date("2014-01-01")
  18. loginCount: 42
  19. }
  20. @callback = sinon.stub()
  21. @client =
  22. id: @client_id = "mock-client-id-123"
  23. params: {}
  24. set: sinon.stub()
  25. get: (param, cb) -> cb null, @params[param]
  26. join: sinon.stub()
  27. leave: sinon.stub()
  28. @WebsocketController = SandboxedModule.require modulePath, requires:
  29. "./WebApiManager": @WebApiManager = {}
  30. "./AuthorizationManager": @AuthorizationManager = {}
  31. "./DocumentUpdaterManager": @DocumentUpdaterManager = {}
  32. "./ConnectedUsersManager": @ConnectedUsersManager = {}
  33. "./WebsocketLoadBalancer": @WebsocketLoadBalancer = {}
  34. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  35. "metrics-sharelatex": @metrics =
  36. inc: sinon.stub()
  37. set: sinon.stub()
  38. "./RoomManager": @RoomManager = {}
  39. afterEach ->
  40. tk.reset()
  41. describe "joinProject", ->
  42. describe "when authorised", ->
  43. beforeEach ->
  44. @client.id = "mock-client-id"
  45. @project = {
  46. name: "Test Project"
  47. owner: {
  48. _id: @owner_id = "mock-owner-id-123"
  49. }
  50. }
  51. @privilegeLevel = "owner"
  52. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArg(4)
  53. @isRestrictedUser = true
  54. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel, @isRestrictedUser)
  55. @RoomManager.joinProject = sinon.stub().callsArg(2)
  56. @WebsocketController.joinProject @client, @user, @project_id, @callback
  57. it "should load the project from web", ->
  58. @WebApiManager.joinProject
  59. .calledWith(@project_id, @user)
  60. .should.equal true
  61. it "should join the project room", ->
  62. @RoomManager.joinProject.calledWith(@client, @project_id).should.equal true
  63. it "should set the privilege level on the client", ->
  64. @client.set.calledWith("privilege_level", @privilegeLevel).should.equal true
  65. it "should set the user's id on the client", ->
  66. @client.set.calledWith("user_id", @user._id).should.equal true
  67. it "should set the user's email on the client", ->
  68. @client.set.calledWith("email", @user.email).should.equal true
  69. it "should set the user's first_name on the client", ->
  70. @client.set.calledWith("first_name", @user.first_name).should.equal true
  71. it "should set the user's last_name on the client", ->
  72. @client.set.calledWith("last_name", @user.last_name).should.equal true
  73. it "should set the user's sign up date on the client", ->
  74. @client.set.calledWith("signup_date", @user.signUpDate).should.equal true
  75. it "should set the user's login_count on the client", ->
  76. @client.set.calledWith("login_count", @user.loginCount).should.equal true
  77. it "should set the connected time on the client", ->
  78. @client.set.calledWith("connected_time", new Date()).should.equal true
  79. it "should set the project_id on the client", ->
  80. @client.set.calledWith("project_id", @project_id).should.equal true
  81. it "should set the project owner id on the client", ->
  82. @client.set.calledWith("owner_id", @owner_id).should.equal true
  83. it "should set the is_restricted_user flag on the client", ->
  84. @client.set.calledWith("is_restricted_user", @isRestrictedUser).should.equal true
  85. it "should call the callback with the project, privilegeLevel and protocolVersion", ->
  86. @callback
  87. .calledWith(null, @project, @privilegeLevel, @WebsocketController.PROTOCOL_VERSION)
  88. .should.equal true
  89. it "should mark the user as connected in ConnectedUsersManager", ->
  90. @ConnectedUsersManager.updateUserPosition
  91. .calledWith(@project_id, @client.id, @user, null)
  92. .should.equal true
  93. it "should increment the join-project metric", ->
  94. @metrics.inc.calledWith("editor.join-project").should.equal true
  95. describe "when not authorized", ->
  96. beforeEach ->
  97. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, null, null)
  98. @WebsocketController.joinProject @client, @user, @project_id, @callback
  99. it "should return an error", ->
  100. @callback
  101. .calledWith(new Error("not authorized"))
  102. .should.equal true
  103. it "should not log an error", ->
  104. @logger.error.called.should.equal false
  105. describe "leaveProject", ->
  106. beforeEach ->
  107. @DocumentUpdaterManager.flushProjectToMongoAndDelete = sinon.stub().callsArg(1)
  108. @ConnectedUsersManager.markUserAsDisconnected = sinon.stub().callsArg(2)
  109. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  110. @RoomManager.leaveProjectAndDocs = sinon.stub()
  111. @clientsInRoom = []
  112. @io =
  113. sockets:
  114. clients: (room_id) =>
  115. if room_id != @project_id
  116. throw "expected room_id to be project_id"
  117. return @clientsInRoom
  118. @client.params.project_id = @project_id
  119. @client.params.user_id = @user_id
  120. @WebsocketController.FLUSH_IF_EMPTY_DELAY = 0
  121. tk.reset() # Allow setTimeout to work.
  122. describe "when the project is empty", ->
  123. beforeEach (done) ->
  124. @clientsInRoom = []
  125. @WebsocketController.leaveProject @io, @client, done
  126. it "should end clientTracking.clientDisconnected to the project room", ->
  127. @WebsocketLoadBalancer.emitToRoom
  128. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
  129. .should.equal true
  130. it "should mark the user as disconnected", ->
  131. @ConnectedUsersManager.markUserAsDisconnected
  132. .calledWith(@project_id, @client.id)
  133. .should.equal true
  134. it "should flush the project in the document updater", ->
  135. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  136. .calledWith(@project_id)
  137. .should.equal true
  138. it "should increment the leave-project metric", ->
  139. @metrics.inc.calledWith("editor.leave-project").should.equal true
  140. it "should track the disconnection in RoomManager", ->
  141. @RoomManager.leaveProjectAndDocs
  142. .calledWith(@client)
  143. .should.equal true
  144. describe "when the project is not empty", ->
  145. beforeEach ->
  146. @clientsInRoom = ["mock-remaining-client"]
  147. @WebsocketController.leaveProject @io, @client
  148. it "should not flush the project in the document updater", ->
  149. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  150. .called.should.equal false
  151. describe "when client has not authenticated", ->
  152. beforeEach (done) ->
  153. @client.params.user_id = null
  154. @client.params.project_id = null
  155. @WebsocketController.leaveProject @io, @client, done
  156. it "should not end clientTracking.clientDisconnected to the project room", ->
  157. @WebsocketLoadBalancer.emitToRoom
  158. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
  159. .should.equal false
  160. it "should not mark the user as disconnected", ->
  161. @ConnectedUsersManager.markUserAsDisconnected
  162. .calledWith(@project_id, @client.id)
  163. .should.equal false
  164. it "should not flush the project in the document updater", ->
  165. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  166. .calledWith(@project_id)
  167. .should.equal false
  168. it "should increment the leave-project metric", ->
  169. @metrics.inc.calledWith("editor.leave-project").should.equal true
  170. describe "when client has not joined a project", ->
  171. beforeEach (done) ->
  172. @client.params.user_id = @user_id
  173. @client.params.project_id = null
  174. @WebsocketController.leaveProject @io, @client, done
  175. it "should not end clientTracking.clientDisconnected to the project room", ->
  176. @WebsocketLoadBalancer.emitToRoom
  177. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
  178. .should.equal false
  179. it "should not mark the user as disconnected", ->
  180. @ConnectedUsersManager.markUserAsDisconnected
  181. .calledWith(@project_id, @client.id)
  182. .should.equal false
  183. it "should not flush the project in the document updater", ->
  184. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  185. .calledWith(@project_id)
  186. .should.equal false
  187. it "should increment the leave-project metric", ->
  188. @metrics.inc.calledWith("editor.leave-project").should.equal true
  189. describe "joinDoc", ->
  190. beforeEach ->
  191. @doc_id = "doc-id-123"
  192. @doc_lines = ["doc", "lines"]
  193. @version = 42
  194. @ops = ["mock", "ops"]
  195. @ranges = { "mock": "ranges" }
  196. @options = {}
  197. @client.params.project_id = @project_id
  198. @AuthorizationManager.addAccessToDoc = sinon.stub()
  199. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  200. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(3, null, @doc_lines, @version, @ranges, @ops)
  201. @RoomManager.joinDoc = sinon.stub().callsArg(2)
  202. describe "works", ->
  203. beforeEach ->
  204. @WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
  205. it "should check that the client is authorized to view the project", ->
  206. @AuthorizationManager.assertClientCanViewProject
  207. .calledWith(@client)
  208. .should.equal true
  209. it "should get the document from the DocumentUpdaterManager with fromVersion", ->
  210. @DocumentUpdaterManager.getDocument
  211. .calledWith(@project_id, @doc_id, -1)
  212. .should.equal true
  213. it "should add permissions for the client to access the doc", ->
  214. @AuthorizationManager.addAccessToDoc
  215. .calledWith(@client, @doc_id)
  216. .should.equal true
  217. it "should join the client to room for the doc_id", ->
  218. @RoomManager.joinDoc
  219. .calledWith(@client, @doc_id)
  220. .should.equal true
  221. it "should call the callback with the lines, version, ranges and ops", ->
  222. @callback
  223. .calledWith(null, @doc_lines, @version, @ops, @ranges)
  224. .should.equal true
  225. it "should increment the join-doc metric", ->
  226. @metrics.inc.calledWith("editor.join-doc").should.equal true
  227. describe "with a fromVersion", ->
  228. beforeEach ->
  229. @fromVersion = 40
  230. @WebsocketController.joinDoc @client, @doc_id, @fromVersion, @options, @callback
  231. it "should get the document from the DocumentUpdaterManager with fromVersion", ->
  232. @DocumentUpdaterManager.getDocument
  233. .calledWith(@project_id, @doc_id, @fromVersion)
  234. .should.equal true
  235. describe "with doclines that need escaping", ->
  236. beforeEach ->
  237. @doc_lines.push ["räksmörgås"]
  238. @WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
  239. it "should call the callback with the escaped lines", ->
  240. escaped_lines = @callback.args[0][1]
  241. escaped_word = escaped_lines.pop()
  242. escaped_word.should.equal 'räksmörgås'
  243. # Check that unescaping works
  244. decodeURIComponent(escape(escaped_word)).should.equal "räksmörgås"
  245. describe "with comments that need encoding", ->
  246. beforeEach ->
  247. @ranges.comments = [{ op: { c: "räksmörgås" } }]
  248. @WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
  249. it "should call the callback with the encoded comment", ->
  250. encoded_comments = @callback.args[0][4]
  251. encoded_comment = encoded_comments.comments.pop()
  252. encoded_comment_text = encoded_comment.op.c
  253. encoded_comment_text.should.equal 'räksmörgås'
  254. describe "with changes that need encoding", ->
  255. it "should call the callback with the encoded insert change", ->
  256. @ranges.changes = [{ op: { i: "räksmörgås" } }]
  257. @WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
  258. encoded_changes = @callback.args[0][4]
  259. encoded_change = encoded_changes.changes.pop()
  260. encoded_change_text = encoded_change.op.i
  261. encoded_change_text.should.equal 'räksmörgås'
  262. it "should call the callback with the encoded delete change", ->
  263. @ranges.changes = [{ op: { d: "räksmörgås" } }]
  264. @WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
  265. encoded_changes = @callback.args[0][4]
  266. encoded_change = encoded_changes.changes.pop()
  267. encoded_change_text = encoded_change.op.d
  268. encoded_change_text.should.equal 'räksmörgås'
  269. describe "when not authorized", ->
  270. beforeEach ->
  271. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  272. @WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
  273. it "should call the callback with an error", ->
  274. @callback.calledWith(@err).should.equal true
  275. it "should not call the DocumentUpdaterManager", ->
  276. @DocumentUpdaterManager.getDocument.called.should.equal false
  277. describe "leaveDoc", ->
  278. beforeEach ->
  279. @doc_id = "doc-id-123"
  280. @client.params.project_id = @project_id
  281. @RoomManager.leaveDoc = sinon.stub()
  282. @WebsocketController.leaveDoc @client, @doc_id, @callback
  283. it "should remove the client from the doc_id room", ->
  284. @RoomManager.leaveDoc
  285. .calledWith(@client, @doc_id).should.equal true
  286. it "should call the callback", ->
  287. @callback.called.should.equal true
  288. it "should increment the leave-doc metric", ->
  289. @metrics.inc.calledWith("editor.leave-doc").should.equal true
  290. describe "getConnectedUsers", ->
  291. beforeEach ->
  292. @client.params.project_id = @project_id
  293. @users = ["mock", "users"]
  294. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  295. @ConnectedUsersManager.getConnectedUsers = sinon.stub().callsArgWith(1, null, @users)
  296. describe "when authorized", ->
  297. beforeEach (done) ->
  298. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  299. @WebsocketController.getConnectedUsers @client, (args...) =>
  300. @callback(args...)
  301. done()
  302. it "should check that the client is authorized to view the project", ->
  303. @AuthorizationManager.assertClientCanViewProject
  304. .calledWith(@client)
  305. .should.equal true
  306. it "should broadcast a request to update the client list", ->
  307. @WebsocketLoadBalancer.emitToRoom
  308. .calledWith(@project_id, "clientTracking.refresh")
  309. .should.equal true
  310. it "should get the connected users for the project", ->
  311. @ConnectedUsersManager.getConnectedUsers
  312. .calledWith(@project_id)
  313. .should.equal true
  314. it "should return the users", ->
  315. @callback.calledWith(null, @users).should.equal true
  316. it "should increment the get-connected-users metric", ->
  317. @metrics.inc.calledWith("editor.get-connected-users").should.equal true
  318. describe "when not authorized", ->
  319. beforeEach ->
  320. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  321. @WebsocketController.getConnectedUsers @client, @callback
  322. it "should not get the connected users for the project", ->
  323. @ConnectedUsersManager.getConnectedUsers
  324. .called
  325. .should.equal false
  326. it "should return an error", ->
  327. @callback.calledWith(@err).should.equal true
  328. describe "when restricted user", ->
  329. beforeEach ->
  330. @client.params.is_restricted_user = true
  331. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  332. @WebsocketController.getConnectedUsers @client, @callback
  333. it "should return an empty array of users", ->
  334. @callback.calledWith(null, []).should.equal true
  335. it "should not get the connected users for the project", ->
  336. @ConnectedUsersManager.getConnectedUsers
  337. .called
  338. .should.equal false
  339. describe "updateClientPosition", ->
  340. beforeEach ->
  341. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  342. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArgWith(4)
  343. @AuthorizationManager.assertClientCanViewProjectAndDoc = sinon.stub().callsArgWith(2, null)
  344. @update = {
  345. doc_id: @doc_id = "doc-id-123"
  346. row: @row = 42
  347. column: @column = 37
  348. }
  349. describe "with a logged in user", ->
  350. beforeEach ->
  351. @clientParams = {
  352. project_id: @project_id
  353. first_name: @first_name = "Douglas"
  354. last_name: @last_name = "Adams"
  355. email: @email = "joe@example.com"
  356. user_id: @user_id = "user-id-123"
  357. }
  358. @client.get = (param, callback) => callback null, @clientParams[param]
  359. @WebsocketController.updateClientPosition @client, @update
  360. @populatedCursorData =
  361. doc_id: @doc_id,
  362. id: @client.id
  363. name: "#{@first_name} #{@last_name}"
  364. row: @row
  365. column: @column
  366. email: @email
  367. user_id: @user_id
  368. it "should send the update to the project room with the user's name", ->
  369. @WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
  370. it "should send the cursor data to the connected user manager", (done)->
  371. @ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
  372. _id: @user_id,
  373. email: @email,
  374. first_name: @first_name,
  375. last_name: @last_name
  376. }, {
  377. row: @row
  378. column: @column
  379. doc_id: @doc_id
  380. }).should.equal true
  381. done()
  382. it "should increment the update-client-position metric at 0.1 frequency", ->
  383. @metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
  384. describe "with a logged in user who has no last_name set", ->
  385. beforeEach ->
  386. @clientParams = {
  387. project_id: @project_id
  388. first_name: @first_name = "Douglas"
  389. last_name: undefined
  390. email: @email = "joe@example.com"
  391. user_id: @user_id = "user-id-123"
  392. }
  393. @client.get = (param, callback) => callback null, @clientParams[param]
  394. @WebsocketController.updateClientPosition @client, @update
  395. @populatedCursorData =
  396. doc_id: @doc_id,
  397. id: @client.id
  398. name: "#{@first_name}"
  399. row: @row
  400. column: @column
  401. email: @email
  402. user_id: @user_id
  403. it "should send the update to the project room with the user's name", ->
  404. @WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
  405. it "should send the cursor data to the connected user manager", (done)->
  406. @ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
  407. _id: @user_id,
  408. email: @email,
  409. first_name: @first_name,
  410. last_name: undefined
  411. }, {
  412. row: @row
  413. column: @column
  414. doc_id: @doc_id
  415. }).should.equal true
  416. done()
  417. it "should increment the update-client-position metric at 0.1 frequency", ->
  418. @metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
  419. describe "with a logged in user who has no first_name set", ->
  420. beforeEach ->
  421. @clientParams = {
  422. project_id: @project_id
  423. first_name: undefined
  424. last_name: @last_name = "Adams"
  425. email: @email = "joe@example.com"
  426. user_id: @user_id = "user-id-123"
  427. }
  428. @client.get = (param, callback) => callback null, @clientParams[param]
  429. @WebsocketController.updateClientPosition @client, @update
  430. @populatedCursorData =
  431. doc_id: @doc_id,
  432. id: @client.id
  433. name: "#{@last_name}"
  434. row: @row
  435. column: @column
  436. email: @email
  437. user_id: @user_id
  438. it "should send the update to the project room with the user's name", ->
  439. @WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
  440. it "should send the cursor data to the connected user manager", (done)->
  441. @ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
  442. _id: @user_id,
  443. email: @email,
  444. first_name: undefined,
  445. last_name: @last_name
  446. }, {
  447. row: @row
  448. column: @column
  449. doc_id: @doc_id
  450. }).should.equal true
  451. done()
  452. it "should increment the update-client-position metric at 0.1 frequency", ->
  453. @metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
  454. describe "with a logged in user who has no names set", ->
  455. beforeEach ->
  456. @clientParams = {
  457. project_id: @project_id
  458. first_name: undefined
  459. last_name: undefined
  460. email: @email = "joe@example.com"
  461. user_id: @user_id = "user-id-123"
  462. }
  463. @client.get = (param, callback) => callback null, @clientParams[param]
  464. @WebsocketController.updateClientPosition @client, @update
  465. it "should send the update to the project name with no name", ->
  466. @WebsocketLoadBalancer.emitToRoom
  467. .calledWith(@project_id, "clientTracking.clientUpdated", {
  468. doc_id: @doc_id,
  469. id: @client.id,
  470. user_id: @user_id,
  471. name: "",
  472. row: @row,
  473. column: @column,
  474. email: @email
  475. })
  476. .should.equal true
  477. describe "with an anonymous user", ->
  478. beforeEach ->
  479. @clientParams = {
  480. project_id: @project_id
  481. }
  482. @client.get = (param, callback) => callback null, @clientParams[param]
  483. @WebsocketController.updateClientPosition @client, @update
  484. it "should send the update to the project room with no name", ->
  485. @WebsocketLoadBalancer.emitToRoom
  486. .calledWith(@project_id, "clientTracking.clientUpdated", {
  487. doc_id: @doc_id,
  488. id: @client.id
  489. name: ""
  490. row: @row
  491. column: @column
  492. })
  493. .should.equal true
  494. it "should not send cursor data to the connected user manager", (done)->
  495. @ConnectedUsersManager.updateUserPosition.called.should.equal false
  496. done()
  497. describe "applyOtUpdate", ->
  498. beforeEach ->
  499. @update = {op: {p: 12, t: "foo"}}
  500. @client.params.user_id = @user_id
  501. @client.params.project_id = @project_id
  502. @WebsocketController._assertClientCanApplyUpdate = sinon.stub().yields()
  503. @DocumentUpdaterManager.queueChange = sinon.stub().callsArg(3)
  504. describe "succesfully", ->
  505. beforeEach ->
  506. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  507. it "should set the source of the update to the client id", ->
  508. @update.meta.source.should.equal @client.id
  509. it "should set the user_id of the update to the user id", ->
  510. @update.meta.user_id.should.equal @user_id
  511. it "should queue the update", ->
  512. @DocumentUpdaterManager.queueChange
  513. .calledWith(@project_id, @doc_id, @update)
  514. .should.equal true
  515. it "should call the callback", ->
  516. @callback.called.should.equal true
  517. it "should increment the doc updates", ->
  518. @metrics.inc.calledWith("editor.doc-update").should.equal true
  519. describe "unsuccessfully", ->
  520. beforeEach ->
  521. @client.disconnect = sinon.stub()
  522. @DocumentUpdaterManager.queueChange = sinon.stub().callsArgWith(3, @error = new Error("Something went wrong"))
  523. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  524. it "should disconnect the client", ->
  525. @client.disconnect.called.should.equal true
  526. it "should log an error", ->
  527. @logger.error.called.should.equal true
  528. it "should call the callback with the error", ->
  529. @callback.calledWith(@error).should.equal true
  530. describe "when not authorized", ->
  531. beforeEach ->
  532. @client.disconnect = sinon.stub()
  533. @WebsocketController._assertClientCanApplyUpdate = sinon.stub().yields(@error = new Error("not authorized"))
  534. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  535. # This happens in a setTimeout to allow the client a chance to receive the error first.
  536. # I'm not sure how to unit test, but it is acceptance tested.
  537. # it "should disconnect the client", ->
  538. # @client.disconnect.called.should.equal true
  539. it "should log a warning", ->
  540. @logger.warn.called.should.equal true
  541. it "should call the callback with the error", ->
  542. @callback.calledWith(@error).should.equal true
  543. describe "_assertClientCanApplyUpdate", ->
  544. beforeEach ->
  545. @edit_update = { op: [{i: "foo", p: 42}, {c: "bar", p: 132}] } # comments may still be in an edit op
  546. @comment_update = { op: [{c: "bar", p: 132}] }
  547. @AuthorizationManager.assertClientCanEditProjectAndDoc = sinon.stub()
  548. @AuthorizationManager.assertClientCanViewProjectAndDoc = sinon.stub()
  549. describe "with a read-write client", ->
  550. it "should return successfully", (done) ->
  551. @AuthorizationManager.assertClientCanEditProjectAndDoc.yields(null)
  552. @WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @edit_update, (error) ->
  553. expect(error).to.be.null
  554. done()
  555. describe "with a read-only client and an edit op", ->
  556. it "should return an error", (done) ->
  557. @AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
  558. @AuthorizationManager.assertClientCanViewProjectAndDoc.yields(null)
  559. @WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @edit_update, (error) ->
  560. expect(error.message).to.equal "not authorized"
  561. done()
  562. describe "with a read-only client and a comment op", ->
  563. it "should return successfully", (done) ->
  564. @AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
  565. @AuthorizationManager.assertClientCanViewProjectAndDoc.yields(null)
  566. @WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @comment_update, (error) ->
  567. expect(error).to.be.null
  568. done()
  569. describe "with a totally unauthorized client", ->
  570. it "should return an error", (done) ->
  571. @AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
  572. @AuthorizationManager.assertClientCanViewProjectAndDoc.yields(new Error("not authorized"))
  573. @WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @comment_update, (error) ->
  574. expect(error.message).to.equal "not authorized"
  575. done()