overleaf/services/web/test/UnitTests/coffee/Chat/ChatHandlerTests.coffee

90 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2014-07-02 16:28:05 +00: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/ChatHandler"
expect = require("chai").expect
describe "ChatHandler", ->
beforeEach ->
@settings =
apis:
chat:
2014-07-17 10:43:12 +00:00
internal_url:"chat.sharelatex.env"
2014-07-02 16:28:05 +00:00
@request = sinon.stub()
@ChatHandler = SandboxedModule.require modulePath, requires:
"settings-sharelatex":@settings
"logger-sharelatex": log:->
"request": @request
@project_id = "3213213kl12j"
@user_id = "2k3jlkjs9"
@messageContent = "my message here"
describe "sending message", ->
beforeEach ->
@messageResponse =
message:"Details"
@request.callsArgWith(1, null, null, @messageResponse)
it "should post the data to the chat api", (done)->
@ChatHandler.sendMessage @project_id, @user_id, @messageContent, (err)=>
@opts =
method:"post"
json:
content:@messageContent
user_id:@user_id
2014-07-17 10:43:12 +00:00
uri:"#{@settings.apis.chat.internal_url}/room/#{@project_id}/messages"
2014-07-02 16:28:05 +00:00
@request.calledWith(@opts).should.equal true
done()
it "should return the message from the post", (done)->
@ChatHandler.sendMessage @project_id, @user_id, @messageContent, (err, returnedMessage)=>
returnedMessage.should.equal @messageResponse
done()
describe "get messages", ->
beforeEach ->
@returnedMessages = [{content:"hello world"}]
@request.callsArgWith(1, null, null, @returnedMessages)
@query = {}
2014-07-02 16:28:05 +00:00
it "should make get request for room to chat api", (done)->
@ChatHandler.getMessages @project_id, @query, (err)=>
2014-07-02 16:28:05 +00:00
@opts =
method:"get"
2014-07-17 10:43:12 +00:00
uri:"#{@settings.apis.chat.internal_url}/room/#{@project_id}/messages"
qs:{}
@request.calledWith(@opts).should.equal true
done()
it "should make get request for room to chat api with query string", (done)->
@query = {limit:5, before:12345, ignore:"this"}
@ChatHandler.getMessages @project_id, @query, (err)=>
@opts =
method:"get"
2014-07-17 10:43:12 +00:00
uri:"#{@settings.apis.chat.internal_url}/room/#{@project_id}/messages"
qs:
limit:5
before:12345
2014-07-02 16:28:05 +00:00
@request.calledWith(@opts).should.equal true
done()
it "should return the messages from the request", (done)->
@ChatHandler.getMessages @project_id, @query, (err, returnedMessages)=>
2014-07-02 16:28:05 +00:00
returnedMessages.should.equal @returnedMessages
done()