ChatControllerTests.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/Chat/ChatController"
  7. expect = require("chai").expect
  8. describe "ChatController", ->
  9. beforeEach ->
  10. @user_id = 'mock-user-id'
  11. @settings = {}
  12. @ChatApiHandler = {}
  13. @EditorRealTimeController =
  14. emitToRoom:sinon.stub().callsArgWith(3)
  15. @AuthenticationController =
  16. getLoggedInUserId: sinon.stub().returns(@user_id)
  17. @ChatController = SandboxedModule.require modulePath, requires:
  18. "settings-sharelatex": @settings
  19. "logger-sharelatex": log: ->
  20. "./ChatApiHandler": @ChatApiHandler
  21. "../Editor/EditorRealTimeController": @EditorRealTimeController
  22. '../Authentication/AuthenticationController': @AuthenticationController
  23. '../User/UserInfoManager': @UserInfoManager = {}
  24. '../User/UserInfoController': @UserInfoController = {}
  25. '../Comments/CommentsController': @CommentsController = {}
  26. @req =
  27. params:
  28. project_id: @project_id
  29. @res =
  30. json: sinon.stub()
  31. send: sinon.stub()
  32. describe "sendMessage", ->
  33. beforeEach ->
  34. @req.body =
  35. content: @content = "message-content"
  36. @UserInfoManager.getPersonalInfo = sinon.stub().yields(null, @user = {"unformatted": "user"})
  37. @UserInfoController.formatPersonalInfo = sinon.stub().returns(@formatted_user = {"formatted": "user"})
  38. @ChatApiHandler.sendGlobalMessage = sinon.stub().yields(null, @message = {"mock": "message", user_id: @user_id})
  39. @ChatController.sendMessage @req, @res
  40. it "should look up the user", ->
  41. @UserInfoManager.getPersonalInfo
  42. .calledWith(@user_id)
  43. .should.equal true
  44. it "should format and inject the user into the message", ->
  45. @UserInfoController.formatPersonalInfo
  46. .calledWith(@user)
  47. .should.equal true
  48. @message.user.should.deep.equal @formatted_user
  49. it "should tell the chat handler about the message", ->
  50. @ChatApiHandler.sendGlobalMessage
  51. .calledWith(@project_id, @user_id, @content)
  52. .should.equal true
  53. it "should tell the editor real time controller about the update with the data from the chat handler", ->
  54. @EditorRealTimeController.emitToRoom
  55. .calledWith(@project_id, "new-chat-message", @message)
  56. .should.equal true
  57. it "should return a 204 status code", ->
  58. @res.send.calledWith(204).should.equal true
  59. describe "getMessages", ->
  60. beforeEach ->
  61. @req.query =
  62. limit: @limit = "30"
  63. before: @before = "12345"
  64. @CommentsController._injectUserInfoIntoThreads = sinon.stub().yields()
  65. @ChatApiHandler.getGlobalMessages = sinon.stub().yields(null, @messages = ["mock", "messages"])
  66. @ChatController.getMessages @req, @res
  67. it "should ask the chat handler about the request", ->
  68. @ChatApiHandler.getGlobalMessages
  69. .calledWith(@project_id, @limit, @before)
  70. .should.equal true
  71. it "should return the messages", ->
  72. @res.json.calledWith(@messages).should.equal true