mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-15 06:33:55 +01:00
Clean up dead code and add acceptance tests
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
{ObjectId} = require "../../../app/js/mongojs"
|
||||
expect = require("chai").expect
|
||||
async = require "async"
|
||||
crypto = require "crypto"
|
||||
|
||||
MockWebApi = require "./helpers/MockWebApi"
|
||||
ChatClient = require "./helpers/ChatClient"
|
||||
|
||||
describe "Getting messages", ->
|
||||
before ->
|
||||
@user_id1 = ObjectId().toString()
|
||||
@user_id2 = ObjectId().toString()
|
||||
@content1 = "foo bar"
|
||||
@content2 = "hello world"
|
||||
MockWebApi.addUser @user_id1, @user1 = {
|
||||
id: @user_id1
|
||||
first_name: "Jane"
|
||||
last_name: "Smith"
|
||||
email: "jane@example.com"
|
||||
}
|
||||
MockWebApi.addUser @user_id2, @user2 = {
|
||||
id: @user_id2
|
||||
first_name: "John"
|
||||
last_name: "Doe"
|
||||
email: "john@example.com"
|
||||
}
|
||||
|
||||
describe "normally", ->
|
||||
before (done) ->
|
||||
@project_id = ObjectId().toString()
|
||||
async.series [
|
||||
(cb) => ChatClient.sendMessage @project_id, @user_id1, @content1, cb
|
||||
(cb) => ChatClient.sendMessage @project_id, @user_id2, @content2, cb
|
||||
], done
|
||||
|
||||
it "should contain the messages and populated users when getting the messages", (done) ->
|
||||
ChatClient.getMessages @project_id, (error, response, messages) =>
|
||||
expect(messages.length).to.equal 2
|
||||
messages.reverse()
|
||||
expect(messages[0].content).to.equal @content1
|
||||
expect(messages[0].user).to.deep.equal {
|
||||
id: @user_id1
|
||||
first_name: "Jane"
|
||||
last_name: "Smith"
|
||||
email: "jane@example.com"
|
||||
gravatar_url: "//www.gravatar.com/avatar/#{crypto.createHash("md5").update("jane@example.com").digest("hex")}"
|
||||
}
|
||||
expect(messages[1].content).to.equal @content2
|
||||
expect(messages[1].user).to.deep.equal {
|
||||
id: @user_id2
|
||||
first_name: "John"
|
||||
last_name: "Doe"
|
||||
email: "john@example.com"
|
||||
gravatar_url: "//www.gravatar.com/avatar/#{crypto.createHash("md5").update("john@example.com").digest("hex")}"
|
||||
}
|
||||
done()
|
||||
|
||||
describe "when a user doesn't exit", ->
|
||||
before (done) ->
|
||||
@project_id = ObjectId().toString()
|
||||
@user_id3 = ObjectId().toString()
|
||||
async.series [
|
||||
(cb) => ChatClient.sendMessage @project_id, @user_id3, @content1, cb
|
||||
(cb) => ChatClient.sendMessage @project_id, @user_id2, @content2, cb
|
||||
], done
|
||||
|
||||
it "should just return null for the user", (done) ->
|
||||
ChatClient.getMessages @project_id, (error, response, messages) =>
|
||||
expect(messages.length).to.equal 2
|
||||
messages.reverse()
|
||||
expect(messages[0].content).to.equal @content1
|
||||
expect(messages[0].user).to.equal null
|
||||
done()
|
||||
@@ -0,0 +1,23 @@
|
||||
{ObjectId} = require "../../../app/js/mongojs"
|
||||
expect = require("chai").expect
|
||||
|
||||
MockWebApi = require "./helpers/MockWebApi"
|
||||
ChatClient = require "./helpers/ChatClient"
|
||||
|
||||
describe "Sending a message", ->
|
||||
before (done) ->
|
||||
@project_id = ObjectId().toString()
|
||||
@user_id = ObjectId().toString()
|
||||
@content = "foo bar"
|
||||
ChatClient.sendMessage @project_id, @user_id, @content, (error, response, body) ->
|
||||
expect(error).to.be.null
|
||||
expect(response.statusCode).to.equal 201
|
||||
done()
|
||||
|
||||
it "should then list the message in project messages", (done) ->
|
||||
ChatClient.getMessages @project_id, (error, response, messages) =>
|
||||
expect(error).to.be.null
|
||||
expect(response.statusCode).to.equal 200
|
||||
expect(messages.length).to.equal 1
|
||||
expect(messages[0].content).to.equal @content
|
||||
done()
|
||||
@@ -0,0 +1,16 @@
|
||||
request = require("request").defaults({baseUrl: "http://localhost:3010"})
|
||||
|
||||
module.exports =
|
||||
sendMessage: (project_id, user_id, content, callback) ->
|
||||
request.post {
|
||||
url: "/room/#{project_id}/messages"
|
||||
json:
|
||||
user_id: user_id
|
||||
content: content
|
||||
}, callback
|
||||
|
||||
getMessages: (project_id, callback) ->
|
||||
request.get {
|
||||
url: "/room/#{project_id}/messages",
|
||||
json: true
|
||||
}, callback
|
||||
@@ -0,0 +1,27 @@
|
||||
express = require("express")
|
||||
app = express()
|
||||
|
||||
module.exports = MockWebApi =
|
||||
users: {}
|
||||
|
||||
addUser: (user_id, user) ->
|
||||
@users[user_id] = user
|
||||
|
||||
getUser: (user_id, callback = (error, user) ->) ->
|
||||
return callback null, @users[user_id]
|
||||
|
||||
run: () ->
|
||||
app.get "/user/:user_id/personal_info", (req, res, next) =>
|
||||
@getUser req.params.user_id, (error, user) ->
|
||||
if error?
|
||||
res.send 500
|
||||
else if user?
|
||||
res.send JSON.stringify user
|
||||
else
|
||||
res.send 404
|
||||
|
||||
app.listen 3000, (error) ->
|
||||
throw error if error?
|
||||
|
||||
MockWebApi.run()
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
sinon = require('sinon')
|
||||
chai = require('chai')
|
||||
should = chai.should()
|
||||
expect = chai.expect
|
||||
modulePath = "../../../../app/js/Features/Authentication/AuthenticationController.js"
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
|
||||
describe "AuthenticationController", ->
|
||||
beforeEach ->
|
||||
@AuthenticationController = SandboxedModule.require modulePath, requires:
|
||||
"../WebApi/WebApiManager": @WebApiManager = {}
|
||||
"../Users/UserFormatter": @UserFormatter = {}
|
||||
"logger-sharelatex": @logger = { log: sinon.stub() }
|
||||
@callback = sinon.stub()
|
||||
|
||||
describe "authClient", ->
|
||||
beforeEach ->
|
||||
@auth_token = "super-secret-auth-token"
|
||||
@client =
|
||||
params: {}
|
||||
set: (key, value, callback) ->
|
||||
@params[key] = value
|
||||
callback()
|
||||
@user =
|
||||
id: "user-id-123"
|
||||
email: "doug@sharelatex.com"
|
||||
first_name: "Douglas"
|
||||
last_name: "Adams"
|
||||
@WebApiManager.getUserDetailsFromAuthToken = sinon.stub().callsArgWith(1, null, @user)
|
||||
@UserFormatter.formatUserForClientSide = sinon.stub().returns({
|
||||
id: @user.id
|
||||
first_name: @user.first_name
|
||||
last_name: @user.last_name
|
||||
email: @user.email
|
||||
gravatar_url: "//gravatar/url"
|
||||
})
|
||||
@AuthenticationController.authClient(@client, auth_token: @auth_token, @callback)
|
||||
|
||||
it "should get the user's data from the web api", ->
|
||||
@WebApiManager.getUserDetailsFromAuthToken
|
||||
.calledWith(@auth_token)
|
||||
.should.equal true
|
||||
|
||||
it "should set the user's data and auth_token on the client object", ->
|
||||
@client.params.should.deep.equal {
|
||||
id: @user.id
|
||||
first_name: @user.first_name
|
||||
last_name: @user.last_name
|
||||
email: @user.email
|
||||
gravatar_url: "//gravatar/url"
|
||||
auth_token: @auth_token
|
||||
}
|
||||
|
||||
it "should call the callback with the user details (including the gravatar URL, but not the auth_token)", ->
|
||||
@callback
|
||||
.calledWith(null, {
|
||||
id: @user.id
|
||||
email: @user.email
|
||||
first_name: @user.first_name
|
||||
last_name: @user.last_name
|
||||
gravatar_url: "//gravatar/url"
|
||||
}).should.equal true
|
||||
|
||||
it "should log the request", ->
|
||||
@logger.log
|
||||
.calledWith(auth_token: @auth_token, "authenticating user")
|
||||
.should.equal true
|
||||
@logger.log
|
||||
.calledWith(user: @user, auth_token: @auth_token, "authenticated user")
|
||||
.should.equal true
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
sinon = require('sinon')
|
||||
chai = require('chai')
|
||||
should = chai.should()
|
||||
expect = chai.expect
|
||||
modulePath = "../../../../app/js/Features/Authorization/AuthorizationManager.js"
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
events = require "events"
|
||||
|
||||
describe "AuthorizationManager", ->
|
||||
beforeEach ->
|
||||
@SocketManager = {}
|
||||
@AuthorizationManager = SandboxedModule.require modulePath, requires:
|
||||
"../WebApi/WebApiManager": @WebApiManager = {}
|
||||
"../Sockets/SocketManager": @SocketManager
|
||||
@callback = sinon.stub()
|
||||
@user_id = "user-id-123"
|
||||
@project_id = "project-id-456"
|
||||
@auth_token = "auth-token-789"
|
||||
@client =
|
||||
params: {}
|
||||
get: (key, callback = (error, value) ->) ->
|
||||
callback null, @params[key]
|
||||
|
||||
describe "canClientJoinProjectRoom", ->
|
||||
beforeEach ->
|
||||
@client.params.auth_token = @auth_token
|
||||
@client.params.id = @user_id
|
||||
|
||||
describe "when the client is a collaborator", ->
|
||||
beforeEach ->
|
||||
@collaborators = [
|
||||
id: @user_id
|
||||
]
|
||||
@WebApiManager.getProjectCollaborators = sinon.stub().callsArgWith(2, null, @collaborators)
|
||||
@AuthorizationManager.canClientJoinProjectRoom(@client, @project_id, @callback)
|
||||
|
||||
it "should get the list of collaborators from the web api", ->
|
||||
@WebApiManager.getProjectCollaborators
|
||||
.calledWith(@project_id, @auth_token)
|
||||
.should.equal true
|
||||
|
||||
it "should return true", ->
|
||||
@callback.calledWith(null, true).should.equal true
|
||||
|
||||
describe "when the client is not a collaborator", ->
|
||||
beforeEach ->
|
||||
@collaborators = [
|
||||
id: "not the user id"
|
||||
]
|
||||
@WebApiManager.getProjectCollaborators = sinon.stub().callsArgWith(2, null, @collaborators)
|
||||
@AuthorizationManager.canClientJoinProjectRoom(@client, @project_id, @callback)
|
||||
|
||||
it "should return false", ->
|
||||
@callback.calledWith(null, false).should.equal true
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
sinon = require('sinon')
|
||||
chai = require('chai')
|
||||
should = chai.should()
|
||||
expect = chai.expect
|
||||
modulePath = "../../../../app/js/Features/Messages/MessageController.js"
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
events = require "events"
|
||||
tk = require "timekeeper"
|
||||
ObjectId = require("mongojs").ObjectId
|
||||
|
||||
describe "MessageController", ->
|
||||
beforeEach ->
|
||||
tk.freeze(new Date())
|
||||
@MessageController = SandboxedModule.require modulePath, requires:
|
||||
"./MessageManager": @MessageManager = {}
|
||||
"./MessageFormatter": @MessageFormatter = {}
|
||||
"../Sockets/SocketManager": @SocketManager = {}
|
||||
"../Authorization/AuthorizationManager": @AuthorizationManager = {}
|
||||
"logger-sharelatex": @logger = { log: sinon.stub() }
|
||||
"metrics-sharelatex": @metrics = {inc: sinon.stub()}
|
||||
@callback = sinon.stub()
|
||||
@client =
|
||||
params:
|
||||
id: @user_id = "user-id-123"
|
||||
first_name: @first_name = "Douglas"
|
||||
last_name: @last_name = "Adams"
|
||||
email: @email = "doug@sharelatex.com"
|
||||
gravatar_url: @gravatar_url = "//gravatar/url"
|
||||
get: (key, callback = (error, value) ->) -> callback null, @params[key]
|
||||
|
||||
afterEach ->
|
||||
tk.reset()
|
||||
|
||||
describe "sendMessage", ->
|
||||
beforeEach ->
|
||||
@MessageManager.createMessage = sinon.stub().callsArg(1)
|
||||
@SocketManager.emitToRoom = sinon.stub()
|
||||
@singlePopulatedMessage = {data:"here"}
|
||||
@formattedMessage = {formatted:true}
|
||||
@MessageFormatter.formatMessageForClientSide = sinon.stub().returns(@formattedMessage)
|
||||
@MessageManager.populateMessagesWithUsers = sinon.stub().callsArgWith(1, null, [@singlePopulatedMessage])
|
||||
@SocketManager.getClientAttributes = (client, attributes, callback) ->
|
||||
values = (client.params[key] for key in attributes)
|
||||
callback null, values
|
||||
|
||||
describe "when the client is authorized to send a message to the room", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.canClientSendMessageToRoom = sinon.stub().callsArgWith(2, null, true)
|
||||
@MessageController.sendMessage(@client, {
|
||||
message:
|
||||
content: @content = "Hello world"
|
||||
room:
|
||||
id: @room_id = "room-id-123"
|
||||
}, @callback)
|
||||
|
||||
it "should check that the client can send a message to the room", ->
|
||||
@AuthorizationManager.canClientSendMessageToRoom
|
||||
.calledWith(@client, @room_id)
|
||||
.should.equal true
|
||||
|
||||
it "should insert the message into the database", ->
|
||||
@MessageManager.createMessage
|
||||
.calledWith({
|
||||
content: @content
|
||||
user_id: @user_id
|
||||
room_id: @room_id
|
||||
timestamp: Date.now()
|
||||
})
|
||||
.should.equal true
|
||||
|
||||
|
||||
it "should format the message for the client", ->
|
||||
@MessageFormatter.formatMessageForClientSide.calledWith(@singlePopulatedMessage).should.equal true
|
||||
|
||||
it "should send the formatted message out to the other clients in the room", ->
|
||||
@SocketManager.emitToRoom.calledWith(@room_id, "messageReceived", message:@formattedMessage).should.equal true
|
||||
|
||||
it "should record the message as a metric", ->
|
||||
@metrics.inc
|
||||
.calledWith("editor.instant-message")
|
||||
.should.equal true
|
||||
|
||||
it "should call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
|
||||
describe "when the client is not authorized", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.canClientSendMessageToRoom = sinon.stub().callsArgWith(2, null, false)
|
||||
@MessageController.sendMessage(@client, {
|
||||
message:
|
||||
content: @content = "Hello world"
|
||||
room:
|
||||
id: @room_id = "room-id-123"
|
||||
}, @callback)
|
||||
|
||||
it "should not insert the message into the database", ->
|
||||
@MessageManager.createMessage.called.should.equal false
|
||||
|
||||
it "should not send the message out to the other clients in the room", ->
|
||||
@SocketManager.emitToRoom.called.should.equal false
|
||||
|
||||
it "should call the callback with an error that doesn't give anything away", ->
|
||||
@callback.calledWith("unknown room").should.equal true
|
||||
|
||||
describe "getMessage", ->
|
||||
beforeEach ->
|
||||
@room_id = "room-id-123"
|
||||
@timestamp = Date.now()
|
||||
@limit = 42
|
||||
|
||||
describe "when the client is authorized", ->
|
||||
beforeEach ->
|
||||
@messages = "messages without users stub"
|
||||
@messagesWithUsers = "messages with users stub"
|
||||
@formattedMessages = "formatted messages stub"
|
||||
@MessageManager.getMessages = sinon.stub().callsArgWith(2, null, @messages)
|
||||
@MessageManager.populateMessagesWithUsers = sinon.stub().callsArgWith(1, null, @messagesWithUsers)
|
||||
@AuthorizationManager.canClientReadMessagesInRoom = sinon.stub().callsArgWith(2, null, true)
|
||||
@MessageFormatter.formatMessagesForClientSide = sinon.stub().returns @formattedMessages
|
||||
|
||||
describe "with a timestamp and limit", ->
|
||||
beforeEach ->
|
||||
@MessageController.getMessages(@client, {
|
||||
room:
|
||||
id: @room_id,
|
||||
before: @timestamp,
|
||||
limit: @limit
|
||||
}, @callback)
|
||||
|
||||
it "should get the requested messages", ->
|
||||
@MessageManager.getMessages
|
||||
.calledWith({
|
||||
timestamp: $lt: @timestamp
|
||||
room_id: @room_id
|
||||
}, {
|
||||
limit: @limit
|
||||
order_by: "timestamp"
|
||||
sort_order: -1
|
||||
})
|
||||
.should.equal true
|
||||
|
||||
it "should populate the messages with the users", ->
|
||||
@MessageManager.populateMessagesWithUsers
|
||||
.calledWith(@messages)
|
||||
.should.equal true
|
||||
|
||||
it "should return the formatted messages", ->
|
||||
@MessageFormatter.formatMessagesForClientSide
|
||||
.calledWith(@messagesWithUsers)
|
||||
.should.equal true
|
||||
|
||||
it "should call the callback with the formatted messages", ->
|
||||
@callback
|
||||
.calledWith(null, @formattedMessages)
|
||||
.should.equal true
|
||||
|
||||
describe "without a timestamp or limit", ->
|
||||
beforeEach ->
|
||||
@MessageController.getMessages(@client, {
|
||||
room:
|
||||
id: @room_id,
|
||||
}, @callback)
|
||||
|
||||
it "should get a default number of messages from the beginning", ->
|
||||
@MessageManager.getMessages
|
||||
.calledWith({
|
||||
room_id: @room_id
|
||||
}, {
|
||||
limit: @MessageController.DEFAULT_MESSAGE_LIMIT
|
||||
order_by: "timestamp"
|
||||
sort_order: -1
|
||||
})
|
||||
.should.equal true
|
||||
|
||||
describe "when the client is not authorized", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.canClientReadMessagesInRoom = sinon.stub().callsArgWith(2, null, false)
|
||||
@MessageController.getMessages(@client, {
|
||||
room:
|
||||
id: @room_id,
|
||||
before: @timestamp,
|
||||
limit: @limit
|
||||
}, @callback)
|
||||
|
||||
it "should call the callback with an error", ->
|
||||
@callback.calledWith("unknown room").should.equal true
|
||||
|
||||
@@ -1,240 +0,0 @@
|
||||
sinon = require('sinon')
|
||||
chai = require('chai')
|
||||
should = chai.should()
|
||||
expect = chai.expect
|
||||
modulePath = "../../../../app/js/Features/Rooms/RoomController.js"
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
events = require "events"
|
||||
ObjectId = require("mongojs").ObjectId
|
||||
|
||||
class MockClient
|
||||
params: {}
|
||||
get: (key, callback = (error, value) ->) ->
|
||||
callback null, @params[key]
|
||||
|
||||
describe "RoomController", ->
|
||||
beforeEach ->
|
||||
@SocketManager =
|
||||
getClientAttributes: sinon.stub()
|
||||
|
||||
|
||||
@RoomController = SandboxedModule.require modulePath, requires:
|
||||
"../Authorization/AuthorizationManager": @AuthorizationManager = {}
|
||||
"../Sockets/SocketManager": @SocketManager
|
||||
"../Rooms/RoomManager": @RoomManager = {}
|
||||
"logger-sharelatex": @logger = { log: sinon.stub() }
|
||||
|
||||
|
||||
@project_id = ObjectId().toString()
|
||||
@room_id = ObjectId().toString()
|
||||
@room =
|
||||
_id: ObjectId(@room_id)
|
||||
project_id: ObjectId(@project_id)
|
||||
@callback = sinon.stub()
|
||||
@client =
|
||||
params: {}
|
||||
get: (key, callback = (error, value) ->) -> callback null, @params[key]
|
||||
|
||||
describe "joinRoom", ->
|
||||
describe "when the client is authorized", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.canClientJoinProjectRoom = sinon.stub().callsArgWith(2, null, true)
|
||||
@RoomManager.findOrCreateRoom = sinon.stub().callsArgWith(1, null, @room)
|
||||
@RoomController._addClientToRoom = sinon.stub().callsArg(2)
|
||||
@RoomController._getClientsInRoom = sinon.stub().callsArgWith(1, null, @clients = ["client1", "client2"])
|
||||
@RoomController.joinRoom @client, { room: project_id: @project_id }, @callback
|
||||
|
||||
it "should check that the client can join the room", ->
|
||||
@AuthorizationManager.canClientJoinProjectRoom
|
||||
.calledWith(@client, @project_id)
|
||||
.should.equal true
|
||||
|
||||
it "should ensure that the room exists", ->
|
||||
@RoomManager.findOrCreateRoom
|
||||
.calledWith({ project_id: @project_id })
|
||||
.should.equal true
|
||||
|
||||
it "should put the client into the room", ->
|
||||
@RoomController._addClientToRoom
|
||||
.calledWith(@client, @room_id)
|
||||
.should.equal true
|
||||
|
||||
it "should get the clients already in the room", ->
|
||||
@RoomController._getClientsInRoom
|
||||
.calledWith(@room_id)
|
||||
.should.equal true
|
||||
|
||||
it "should call the callback with the room id", ->
|
||||
@callback.calledWith(null, {
|
||||
room:
|
||||
id: @room_id
|
||||
connectedUsers: @clients
|
||||
}).should.equal true
|
||||
|
||||
describe "when the client is not authorized", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.canClientJoinProjectRoom = sinon.stub().callsArgWith(2, null, false)
|
||||
@RoomController._addClientToRoom = sinon.stub().callsArg(2)
|
||||
@RoomController.joinRoom @client, { room: project_id: @project_id }, @callback
|
||||
|
||||
it "should not put the client into the room", ->
|
||||
@RoomController._addClientToRoom.called.should.equal false
|
||||
|
||||
it "should call the callback with an error that gives nothing away", ->
|
||||
@callback.calledWith("unknown room").should.equal true
|
||||
|
||||
describe "leaveAllRooms", ->
|
||||
|
||||
beforeEach ->
|
||||
@client = new MockClient()
|
||||
@client.params =
|
||||
id: "client-1-id"
|
||||
first_name: "Douglas"
|
||||
last_name: "Adams"
|
||||
email: "doug@sharelatex.com"
|
||||
gravatar_url: "//gravatar/url/1"
|
||||
@room_ids = ["room-id-1", "room-id-2"]
|
||||
@SocketManager.getRoomIdsClientHasJoined = sinon.stub().callsArgWith(1, null, @room_ids)
|
||||
@RoomController.leaveRoom = sinon.stub().callsArg(2)
|
||||
@RoomController.leaveAllRooms @client, @callback
|
||||
|
||||
it "should get the rooms the client has joined", ->
|
||||
@SocketManager.getRoomIdsClientHasJoined
|
||||
.calledWith(@client)
|
||||
.should.equal true
|
||||
|
||||
it "should leave each room", ->
|
||||
for room_id in @room_ids
|
||||
@RoomController.leaveRoom
|
||||
.calledWith(@client, room_id)
|
||||
.should.equal true
|
||||
|
||||
it "should call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
|
||||
describe "leaveRoom", ->
|
||||
beforeEach ->
|
||||
@client = new MockClient()
|
||||
@client.params =
|
||||
id: "client-1-id"
|
||||
first_name: "Douglas"
|
||||
last_name: "Adams"
|
||||
email: "doug@sharelatex.com"
|
||||
gravatar_url: "//gravatar/url/1"
|
||||
@RoomController._getClientAttributes = sinon.stub().callsArgWith(1, null, @client.params)
|
||||
|
||||
@SocketManager.removeClientFromRoom = sinon.stub().callsArg(2)
|
||||
@SocketManager.emitToRoom = sinon.stub()
|
||||
@RoomController.leaveRoom @client, @room_id, @callback
|
||||
|
||||
it "should leave the room", ->
|
||||
@SocketManager.removeClientFromRoom
|
||||
.calledWith(@client, @room_id)
|
||||
.should.equal true
|
||||
|
||||
it "should tell the other clients in the room that we have left", ->
|
||||
@SocketManager.emitToRoom
|
||||
.calledWith(@room_id, "userLeft", {
|
||||
room:
|
||||
id: @room_id
|
||||
user:
|
||||
id : @client.params["id"]
|
||||
first_name : @client.params["first_name"]
|
||||
last_name : @client.params["last_name"]
|
||||
email : @client.params["email"]
|
||||
gravatar_url : @client.params["gravatar_url"]
|
||||
})
|
||||
.should.equal true
|
||||
|
||||
it "should call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
|
||||
describe "_getClientsInRoom", ->
|
||||
beforeEach ->
|
||||
@client1 = new MockClient()
|
||||
@client1.params =
|
||||
id: "client-1-id"
|
||||
first_name: "Douglas"
|
||||
last_name: "Adams"
|
||||
email: "doug@sharelatex.com"
|
||||
gravatar_url: "//gravatar/url/1"
|
||||
@client2 = new MockClient()
|
||||
@client2.params =
|
||||
id: "client-2-id"
|
||||
first_name: "James"
|
||||
last_name: "Allen"
|
||||
email: "james@sharelatex.com"
|
||||
gravatar_url: "//gravatar/url/2"
|
||||
@clients = [ @client1, @client2 ]
|
||||
callCount = 0
|
||||
|
||||
@RoomController._getClientAttributes = (ignore, cb)=>
|
||||
if callCount == 0
|
||||
callCount++
|
||||
cb(null, @client1.params)
|
||||
else
|
||||
cb null, @client2.params
|
||||
|
||||
|
||||
@SocketManager.getClientsInRoom = sinon.stub().callsArgWith(1, null, @clients)
|
||||
@RoomController._getClientsInRoom(@room_id, @callback)
|
||||
|
||||
it "should get the socket.io clients in the room", ->
|
||||
@SocketManager.getClientsInRoom
|
||||
.calledWith(@room_id)
|
||||
.should.equal true
|
||||
|
||||
it "should return a formatted array of clients", ->
|
||||
@callback
|
||||
.calledWith(null, [{
|
||||
id : @client1.params["id"]
|
||||
first_name : @client1.params["first_name"]
|
||||
last_name : @client1.params["last_name"]
|
||||
email : @client1.params["email"]
|
||||
gravatar_url : @client1.params["gravatar_url"]
|
||||
}, {
|
||||
id : @client2.params["id"]
|
||||
first_name : @client2.params["first_name"]
|
||||
last_name : @client2.params["last_name"]
|
||||
email : @client2.params["email"]
|
||||
gravatar_url : @client2.params["gravatar_url"]
|
||||
}])
|
||||
.should.equal true
|
||||
|
||||
describe "_addClientToRoom", ->
|
||||
beforeEach ->
|
||||
@client = new MockClient()
|
||||
@client.params =
|
||||
id: "client-1-id"
|
||||
first_name: "Douglas"
|
||||
last_name: "Adams"
|
||||
email: "doug@sharelatex.com"
|
||||
gravatar_url: "//gravatar/url/1"
|
||||
@RoomController._getClientAttributes = sinon.stub().callsArgWith(1, null, @client.params)
|
||||
@SocketManager.addClientToRoom = sinon.stub().callsArg(2)
|
||||
@SocketManager.emitToRoom = sinon.stub()
|
||||
@RoomController._addClientToRoom(@client, @room_id, @callback)
|
||||
|
||||
it "should add the client to the room", ->
|
||||
@SocketManager.addClientToRoom
|
||||
.calledWith(@client, @room_id)
|
||||
.should.equal true
|
||||
|
||||
it "should tell the room that the client has been added", ->
|
||||
@SocketManager.emitToRoom
|
||||
.calledWith(@room_id, "userJoined", {
|
||||
room:
|
||||
id: @room_id
|
||||
user:
|
||||
id : @client.params["id"]
|
||||
first_name : @client.params["first_name"]
|
||||
last_name : @client.params["last_name"]
|
||||
email : @client.params["email"]
|
||||
gravatar_url : @client.params["gravatar_url"]
|
||||
})
|
||||
.should.equal true
|
||||
|
||||
it "should call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
sinon = require('sinon')
|
||||
require('chai').should()
|
||||
modulePath = "../../../../app/js/Features/Sockets/RealTimeEventManager.js"
|
||||
|
||||
describe "RealTimeEventManager", ->
|
||||
|
||||
beforeEach ->
|
||||
@settings =
|
||||
redis:
|
||||
web:
|
||||
host: "host here"
|
||||
port: "port here"
|
||||
password: "password here"
|
||||
@RealTimeEventManager = SandboxedModule.require modulePath, requires:
|
||||
"redis":
|
||||
createClient: () ->
|
||||
auth:->
|
||||
"../../server" : io: @io = {}
|
||||
"settings-sharelatex":@settings
|
||||
@RealTimeEventManager.rclientPub = publish: sinon.stub()
|
||||
@RealTimeEventManager.rclientSub =
|
||||
subscribe: sinon.stub()
|
||||
on: sinon.stub()
|
||||
|
||||
@room_id = "room-id-here"
|
||||
@message = "message-to-chat-here"
|
||||
@payload = ["argument one", 42]
|
||||
|
||||
describe "emitToRoom", ->
|
||||
beforeEach ->
|
||||
@RealTimeEventManager.emitToRoom(@room_id, @message, @payload...)
|
||||
|
||||
it "should publish the message to redis", ->
|
||||
@RealTimeEventManager.rclientPub.publish
|
||||
.calledWith("chat-events", JSON.stringify(
|
||||
room_id: @room_id,
|
||||
message: @message
|
||||
payload: @payload
|
||||
))
|
||||
.should.equal true
|
||||
|
||||
describe "listenForChatEvents", ->
|
||||
beforeEach ->
|
||||
@RealTimeEventManager._processEditorEvent = sinon.stub()
|
||||
@RealTimeEventManager.listenForChatEvents()
|
||||
|
||||
it "should subscribe to the chat-events channel", ->
|
||||
@RealTimeEventManager.rclientSub.subscribe
|
||||
.calledWith("chat-events")
|
||||
.should.equal true
|
||||
|
||||
it "should process the events with _processEditorEvent", ->
|
||||
@RealTimeEventManager.rclientSub.on
|
||||
.calledWith("message", sinon.match.func)
|
||||
.should.equal true
|
||||
|
||||
describe "_processEditorEvent", ->
|
||||
describe "with a designated room", ->
|
||||
beforeEach ->
|
||||
@io.sockets =
|
||||
in: sinon.stub().returns(emit: @emit = sinon.stub())
|
||||
data = JSON.stringify
|
||||
room_id: @room_id
|
||||
message: @message
|
||||
payload: @payload
|
||||
@RealTimeEventManager._processEditorEvent("chat-events", data)
|
||||
|
||||
it "should send the message to all clients in the room", ->
|
||||
@io.sockets.in
|
||||
.calledWith(@room_id)
|
||||
.should.equal true
|
||||
@emit.calledWith(@message, @payload...).should.equal true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user