WebsocketControllerTests.coffee 24 KB

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