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-12-16 11:42:41 -05:00
|
|
|
@user_id = 'mock-user-id'
|
2014-07-04 08:05:55 -04:00
|
|
|
@settings = {}
|
2016-12-16 11:42:41 -05:00
|
|
|
@ChatApiHandler = {}
|
2014-07-04 08:20:30 -04:00
|
|
|
@EditorRealTimeController =
|
2019-05-01 05:23:48 -04:00
|
|
|
emitToRoom:sinon.stub()
|
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:
|
2016-12-16 11:42:41 -05:00
|
|
|
"settings-sharelatex": @settings
|
|
|
|
"logger-sharelatex": log: ->
|
|
|
|
"./ChatApiHandler": @ChatApiHandler
|
|
|
|
"../Editor/EditorRealTimeController": @EditorRealTimeController
|
2016-09-07 11:40:49 -04:00
|
|
|
'../Authentication/AuthenticationController': @AuthenticationController
|
2017-01-06 07:41:58 -05:00
|
|
|
'../User/UserInfoManager': @UserInfoManager = {}
|
|
|
|
'../User/UserInfoController': @UserInfoController = {}
|
|
|
|
'../Comments/CommentsController': @CommentsController = {}
|
2014-07-10 10:12:28 -04:00
|
|
|
@req =
|
|
|
|
params:
|
2016-12-16 11:42:41 -05:00
|
|
|
project_id: @project_id
|
2015-05-19 06:04:52 -04:00
|
|
|
@res =
|
2016-12-16 11:42:41 -05:00
|
|
|
json: sinon.stub()
|
|
|
|
send: sinon.stub()
|
2014-07-04 08:05:55 -04:00
|
|
|
|
|
|
|
describe "sendMessage", ->
|
2016-12-16 11:42:41 -05:00
|
|
|
beforeEach ->
|
|
|
|
@req.body =
|
|
|
|
content: @content = "message-content"
|
2017-01-06 07:41:58 -05:00
|
|
|
@UserInfoManager.getPersonalInfo = sinon.stub().yields(null, @user = {"unformatted": "user"})
|
|
|
|
@UserInfoController.formatPersonalInfo = sinon.stub().returns(@formatted_user = {"formatted": "user"})
|
|
|
|
@ChatApiHandler.sendGlobalMessage = sinon.stub().yields(null, @message = {"mock": "message", user_id: @user_id})
|
2014-07-10 10:12:28 -04:00
|
|
|
@ChatController.sendMessage @req, @res
|
2014-07-04 08:05:55 -04:00
|
|
|
|
2017-01-06 07:41:58 -05:00
|
|
|
it "should look up the user", ->
|
|
|
|
@UserInfoManager.getPersonalInfo
|
|
|
|
.calledWith(@user_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should format and inject the user into the message", ->
|
|
|
|
@UserInfoController.formatPersonalInfo
|
|
|
|
.calledWith(@user)
|
|
|
|
.should.equal true
|
|
|
|
@message.user.should.deep.equal @formatted_user
|
|
|
|
|
2016-12-16 11:42:41 -05:00
|
|
|
it "should tell the chat handler about the message", ->
|
|
|
|
@ChatApiHandler.sendGlobalMessage
|
|
|
|
.calledWith(@project_id, @user_id, @content)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should tell the editor real time controller about the update with the data from the chat handler", ->
|
|
|
|
@EditorRealTimeController.emitToRoom
|
|
|
|
.calledWith(@project_id, "new-chat-message", @message)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a 204 status code", ->
|
|
|
|
@res.send.calledWith(204).should.equal true
|
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 ->
|
2016-12-16 11:42:41 -05:00
|
|
|
@req.query =
|
|
|
|
limit: @limit = "30"
|
|
|
|
before: @before = "12345"
|
2017-03-02 11:45:24 -05:00
|
|
|
@ChatController._injectUserInfoIntoThreads = sinon.stub().yields()
|
2016-12-16 11:42:41 -05:00
|
|
|
@ChatApiHandler.getGlobalMessages = sinon.stub().yields(null, @messages = ["mock", "messages"])
|
2014-07-10 10:12:28 -04:00
|
|
|
@ChatController.getMessages @req, @res
|
2014-07-04 08:05:55 -04:00
|
|
|
|
2016-12-16 11:42:41 -05:00
|
|
|
it "should ask the chat handler about the request", ->
|
|
|
|
@ChatApiHandler.getGlobalMessages
|
|
|
|
.calledWith(@project_id, @limit, @before)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the messages", ->
|
2017-03-02 11:45:24 -05:00
|
|
|
@res.json.calledWith(@messages).should.equal true
|
|
|
|
|
|
|
|
describe "_injectUserInfoIntoThreads", ->
|
|
|
|
beforeEach ->
|
|
|
|
@users = {
|
|
|
|
"user_id_1": {
|
|
|
|
"mock": "user_1"
|
|
|
|
}
|
|
|
|
"user_id_2": {
|
|
|
|
"mock": "user_2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@UserInfoManager.getPersonalInfo = (user_id, callback) =>
|
|
|
|
return callback(null, @users[user_id])
|
|
|
|
sinon.spy @UserInfoManager, "getPersonalInfo"
|
|
|
|
@UserInfoController.formatPersonalInfo = (user) ->
|
|
|
|
return { "formatted": user["mock"] }
|
|
|
|
|
|
|
|
it "should inject a user object into messaged and resolved data", (done) ->
|
|
|
|
@ChatController._injectUserInfoIntoThreads {
|
|
|
|
thread1: {
|
|
|
|
resolved: true
|
|
|
|
resolved_by_user_id: "user_id_1"
|
|
|
|
messages: [{
|
|
|
|
user_id: "user_id_1"
|
|
|
|
content: "foo"
|
|
|
|
}, {
|
|
|
|
user_id: "user_id_2"
|
|
|
|
content: "bar"
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
thread2: {
|
|
|
|
messages: [{
|
|
|
|
user_id: "user_id_1"
|
|
|
|
content: "baz"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}, (error, threads) ->
|
|
|
|
expect(threads).to.deep.equal {
|
|
|
|
thread1: {
|
|
|
|
resolved: true
|
|
|
|
resolved_by_user_id: "user_id_1"
|
|
|
|
resolved_by_user: { "formatted": "user_1" }
|
|
|
|
messages: [{
|
|
|
|
user_id: "user_id_1"
|
|
|
|
user: { "formatted": "user_1" }
|
|
|
|
content: "foo"
|
|
|
|
}, {
|
|
|
|
user_id: "user_id_2"
|
|
|
|
user: { "formatted": "user_2" }
|
|
|
|
content: "bar"
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
thread2: {
|
|
|
|
messages: [{
|
|
|
|
user_id: "user_id_1"
|
|
|
|
user: { "formatted": "user_1" }
|
|
|
|
content: "baz"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should only need to look up each user once", (done) ->
|
|
|
|
@ChatController._injectUserInfoIntoThreads [{
|
|
|
|
messages: [{
|
|
|
|
user_id: "user_id_1"
|
|
|
|
content: "foo"
|
|
|
|
}, {
|
|
|
|
user_id: "user_id_1"
|
|
|
|
content: "bar"
|
|
|
|
}]
|
|
|
|
}], (error, threads) =>
|
|
|
|
@UserInfoManager.getPersonalInfo.calledOnce.should.equal true
|
|
|
|
done()
|