2014-07-04 08:05:55 -04:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = path.join __dirname, "../../../../app/js/Features/Chat/ChatController"
|
|
|
|
expect = require("chai").expect
|
|
|
|
|
|
|
|
describe "ChatController", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
|
2016-09-07 11:40:49 -04:00
|
|
|
@user_id = 'ier_'
|
2014-07-04 08:05:55 -04:00
|
|
|
@settings = {}
|
2016-09-07 11:40:49 -04:00
|
|
|
@ChatHandler =
|
2014-07-04 08:05:55 -04:00
|
|
|
sendMessage:sinon.stub()
|
|
|
|
getMessages:sinon.stub()
|
|
|
|
|
2014-07-04 08:20:30 -04:00
|
|
|
@EditorRealTimeController =
|
|
|
|
emitToRoom:sinon.stub().callsArgWith(3)
|
2016-09-07 11:40:49 -04:00
|
|
|
|
|
|
|
@AuthenticationController =
|
|
|
|
getLoggedInUserId: sinon.stub().returns(@user_id)
|
2014-07-04 08:05:55 -04:00
|
|
|
@ChatController = SandboxedModule.require modulePath, requires:
|
|
|
|
"settings-sharelatex":@settings
|
|
|
|
"logger-sharelatex": log:->
|
|
|
|
"./ChatHandler":@ChatHandler
|
2014-07-04 08:20:30 -04:00
|
|
|
"../Editor/EditorRealTimeController":@EditorRealTimeController
|
2016-09-07 11:40:49 -04:00
|
|
|
'../Authentication/AuthenticationController': @AuthenticationController
|
|
|
|
@query =
|
2014-07-04 08:05:55 -04:00
|
|
|
before:"some time"
|
|
|
|
|
2014-07-10 10:12:28 -04:00
|
|
|
@req =
|
|
|
|
params:
|
2014-07-14 17:11:41 -04:00
|
|
|
Project_id:@project_id
|
2014-07-10 10:12:28 -04:00
|
|
|
session:
|
|
|
|
user:
|
|
|
|
_id:@user_id
|
|
|
|
body:
|
|
|
|
content:@messageContent
|
2015-05-19 06:04:52 -04:00
|
|
|
@res =
|
|
|
|
set:sinon.stub()
|
2014-07-04 08:05:55 -04:00
|
|
|
|
|
|
|
describe "sendMessage", ->
|
|
|
|
|
|
|
|
it "should tell the chat handler about the message", (done)->
|
|
|
|
@ChatHandler.sendMessage.callsArgWith(3)
|
2014-07-10 10:12:28 -04:00
|
|
|
@res.send = =>
|
2014-07-04 08:05:55 -04:00
|
|
|
@ChatHandler.sendMessage.calledWith(@project_id, @user_id, @messageContent).should.equal true
|
|
|
|
done()
|
2014-07-10 10:12:28 -04:00
|
|
|
@ChatController.sendMessage @req, @res
|
2014-07-04 08:05:55 -04:00
|
|
|
|
2014-07-04 08:20:30 -04:00
|
|
|
it "should tell the editor real time controller about the update with the data from the chat handler", (done)->
|
|
|
|
@chatMessage =
|
|
|
|
content:"hello world"
|
|
|
|
@ChatHandler.sendMessage.callsArgWith(3, null, @chatMessage)
|
2014-07-10 10:12:28 -04:00
|
|
|
@res.send = =>
|
2014-07-04 08:20:30 -04:00
|
|
|
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "new-chat-message", @chatMessage).should.equal true
|
|
|
|
done()
|
2014-07-10 10:12:28 -04:00
|
|
|
@ChatController.sendMessage @req, @res
|
2014-07-04 08:20:30 -04:00
|
|
|
|
2014-07-04 08:05:55 -04:00
|
|
|
describe "getMessages", ->
|
2014-07-10 10:12:28 -04:00
|
|
|
beforeEach ->
|
2014-07-15 13:33:59 -04:00
|
|
|
@req.query = @query
|
2014-07-04 08:05:55 -04:00
|
|
|
|
2014-07-10 10:12:28 -04:00
|
|
|
it "should ask the chat handler about the request", (done)->
|
2014-07-04 08:05:55 -04:00
|
|
|
|
|
|
|
@ChatHandler.getMessages.callsArgWith(2)
|
2014-07-10 10:12:28 -04:00
|
|
|
@res.send = =>
|
2014-07-04 08:05:55 -04:00
|
|
|
@ChatHandler.getMessages.calledWith(@project_id, @query).should.equal true
|
|
|
|
done()
|
2014-07-10 10:12:28 -04:00
|
|
|
@ChatController.getMessages @req, @res
|
2014-07-04 08:05:55 -04:00
|
|
|
|
2014-07-08 05:08:38 -04:00
|
|
|
it "should return the messages", (done)->
|
|
|
|
messages = [{content:"hello"}]
|
|
|
|
@ChatHandler.getMessages.callsArgWith(2, null, messages)
|
2014-07-15 09:28:39 -04:00
|
|
|
@res.send = (sentMessages)=>
|
2015-05-19 06:04:52 -04:00
|
|
|
@res.set.calledWith('Content-Type', 'application/json').should.equal true
|
2014-07-15 09:28:39 -04:00
|
|
|
sentMessages.should.deep.equal messages
|
2014-07-08 05:08:38 -04:00
|
|
|
done()
|
2014-07-15 09:28:39 -04:00
|
|
|
@ChatController.getMessages @req, @res
|