Get user info via web, not chat

This commit is contained in:
James Allen
2017-01-06 13:41:58 +01:00
parent 5155ebaeec
commit 3a5d45fa32
13 changed files with 384 additions and 69 deletions
@@ -21,6 +21,9 @@ describe "ChatController", ->
"./ChatApiHandler": @ChatApiHandler
"../Editor/EditorRealTimeController": @EditorRealTimeController
'../Authentication/AuthenticationController': @AuthenticationController
'../User/UserInfoManager': @UserInfoManager = {}
'../User/UserInfoController': @UserInfoController = {}
'../Comments/CommentsController': @CommentsController = {}
@req =
params:
project_id: @project_id
@@ -32,9 +35,22 @@ describe "ChatController", ->
beforeEach ->
@req.body =
content: @content = "message-content"
@ChatApiHandler.sendGlobalMessage = sinon.stub().yields(null, @message = {"mock": "message"})
@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})
@ChatController.sendMessage @req, @res
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
it "should tell the chat handler about the message", ->
@ChatApiHandler.sendGlobalMessage
.calledWith(@project_id, @user_id, @content)
@@ -53,6 +69,7 @@ describe "ChatController", ->
@req.query =
limit: @limit = "30"
before: @before = "12345"
@CommentsController._injectUserInfoIntoThreads = sinon.stub().yields()
@ChatApiHandler.getGlobalMessages = sinon.stub().yields(null, @messages = ["mock", "messages"])
@ChatController.getMessages @req, @res
@@ -21,6 +21,8 @@ describe "CommentsController", ->
"../Chat/ChatApiHandler": @ChatApiHandler
"../Editor/EditorRealTimeController": @EditorRealTimeController
'../Authentication/AuthenticationController': @AuthenticationController
'../User/UserInfoManager': @UserInfoManager = {}
'../User/UserInfoController': @UserInfoController = {}
@req = {}
@res =
json: sinon.stub()
@@ -29,12 +31,25 @@ describe "CommentsController", ->
describe "sendComment", ->
beforeEach ->
@req.params =
project_id: @project_id
thread_id: @thread_id
project_id: @project_id = "mock-project-id"
thread_id: @thread_id = "mock-thread-id"
@req.body =
content: @content = "message-content"
@ChatApiHandler.sendComment = sinon.stub().yields(null, @message = {"mock": "message"})
@UserInfoManager.getPersonalInfo = sinon.stub().yields(null, @user = {"unformatted": "user"})
@UserInfoController.formatPersonalInfo = sinon.stub().returns(@formatted_user = {"formatted": "user"})
@ChatApiHandler.sendComment = sinon.stub().yields(null, @message = {"mock": "message", user_id: @user_id})
@CommentsController.sendComment @req, @res
it "should look up the user", ->
@UserInfoManager.getPersonalInfo
.calledWith(@user_id)
.should.equal true
it "should format and inject the user into the comment", ->
@UserInfoController.formatPersonalInfo
.calledWith(@user)
.should.equal true
@message.user.should.deep.equal @formatted_user
it "should tell the chat handler about the message", ->
@ChatApiHandler.sendComment
@@ -52,14 +67,143 @@ describe "CommentsController", ->
describe "getThreads", ->
beforeEach ->
@req.params =
project_id: @project_id
project_id: @project_id = "mock-project-id"
@ChatApiHandler.getThreads = sinon.stub().yields(null, @threads = {"mock", "threads"})
@CommentsController._injectUserInfoIntoThreads = sinon.stub().yields(null, @threads)
@CommentsController.getThreads @req, @res
it "should ask the chat handler about the request", ->
@ChatApiHandler.getThreads
.calledWith(@project_id)
.should.equal true
it "should inject the user details into the threads", ->
@CommentsController._injectUserInfoIntoThreads
.calledWith(@threads)
.should.equal true
it "should return the messages", ->
@res.json.calledWith(@threads).should.equal true
@res.json.calledWith(@threads).should.equal true
describe "resolveThread", ->
beforeEach ->
@req.params =
project_id: @project_id = "mock-project-id"
thread_id: @thread_id = "mock-thread-id"
@ChatApiHandler.resolveThread = sinon.stub().yields()
@UserInfoManager.getPersonalInfo = sinon.stub().yields(null, @user = {"unformatted": "user"})
@UserInfoController.formatPersonalInfo = sinon.stub().returns(@formatted_user = {"formatted": "user"})
@CommentsController.resolveThread @req, @res
it "should ask the chat handler to resolve the thread", ->
@ChatApiHandler.resolveThread
.calledWith(@project_id, @thread_id)
.should.equal true
it "should look up the user", ->
@UserInfoManager.getPersonalInfo
.calledWith(@user_id)
.should.equal true
it "should tell the client the comment was resolved", ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "resolve-thread", @thread_id, @formatted_user)
.should.equal true
it "should return a success code", ->
@res.send.calledWith(204).should.equal
describe "reopenThread", ->
beforeEach ->
@req.params =
project_id: @project_id = "mock-project-id"
thread_id: @thread_id = "mock-thread-id"
@ChatApiHandler.reopenThread = sinon.stub().yields()
@CommentsController.reopenThread @req, @res
it "should ask the chat handler to reopen the thread", ->
@ChatApiHandler.reopenThread
.calledWith(@project_id, @thread_id)
.should.equal true
it "should tell the client the comment was resolved", ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reopen-thread", @thread_id)
.should.equal true
it "should return a success code", ->
@res.send.calledWith(204).should.equal
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) ->
@CommentsController._injectUserInfoIntoThreads [
{
resolved: true
resolved_by_user_id: "user_id_1"
messages: [{
user_id: "user_id_1"
content: "foo"
}, {
user_id: "user_id_2"
content: "bar"
}]
},
{
messages: [{
user_id: "user_id_1"
content: "baz"
}]
}
], (error, threads) ->
expect(threads).to.deep.equal [
{
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"
}]
},
{
messages: [{
user_id: "user_id_1"
user: { "formatted": "user_1" }
content: "baz"
}]
}
]
done()
it "should only need to look up each user once", (done) ->
@CommentsController._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()
@@ -0,0 +1,55 @@
should = require('chai').should()
SandboxedModule = require('sandboxed-module')
assert = require('assert')
sinon = require('sinon')
path = require "path"
modulePath = path.join __dirname, "../../../../app/js/Features/Ranges/RangesManager"
expect = require("chai").expect
describe "RangesManager", ->
beforeEach ->
@RangesManager = SandboxedModule.require modulePath, requires:
"../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler = {}
"../Docstore/DocstoreManager": @DocstoreManager = {}
"../User/UserInfoManager": @UserInfoManager = {}
describe "getAllRangesUsers", ->
beforeEach ->
@project_id = "mock-project-id"
@user_id1 = "mock-user-id-1"
@user_id1 = "mock-user-id-2"
@docs = [{
ranges:
changes: [{
op: { i: "foo", p: 42 }
metadata:
user_id: @user_id1
}, {
op: { i: "bar", p: 102 }
metadata:
user_id: @user_id2
}]
}, {
ranges:
changes: [{
op: { i: "baz", p: 3 }
metadata:
user_id: @user_id1
}]
}]
@users = {}
@users[@user_id1] = {"mock": "user-1"}
@users[@user_id2] = {"mock": "user-2"}
@UserInfoManager.getPersonalInfo = (user_id, callback) => callback null, @users[user_id]
sinon.spy @UserInfoManager, "getPersonalInfo"
@RangesManager.getAllRanges = sinon.stub().yields(null, @docs)
it "should return an array of unique users", (done) ->
@RangesManager.getAllRangesUsers @project_id, (error, users) =>
users.should.deep.equal [{"mock": "user-1"}, {"mock": "user-2"}]
done()
it "should only call getPersonalInfo once for each user", (done) ->
@RangesManager.getAllRangesUsers @project_id, (error, users) =>
@UserInfoManager.getPersonalInfo.calledTwice.should.equal true
done()
@@ -93,18 +93,18 @@ describe "UserInfoController", ->
first_name: @user.first_name
last_name: @user.last_name
email: @user.email
@UserInfoController._formatPersonalInfo = sinon.stub().callsArgWith(1, null, @formattedInfo)
@UserInfoController.formatPersonalInfo = sinon.stub().returns(@formattedInfo)
@UserInfoController.sendFormattedPersonalInfo @user, @res
it "should format the user details for the response", ->
@UserInfoController._formatPersonalInfo
@UserInfoController.formatPersonalInfo
.calledWith(@user)
.should.equal true
it "should send the formatted details back to the client", ->
@res.body.should.equal JSON.stringify(@formattedInfo)
describe "_formatPersonalInfo", ->
describe "formatPersonalInfo", ->
it "should return the correctly formatted data", ->
@user =
_id: ObjectId()
@@ -115,14 +115,13 @@ describe "UserInfoController", ->
signUpDate: new Date()
role:"student"
institution:"sheffield"
@UserInfoController._formatPersonalInfo @user, (error, info) =>
expect(info).to.deep.equal {
id: @user._id.toString()
first_name: @user.first_name
last_name: @user.last_name
email: @user.email
signUpDate: @user.signUpDate
role: @user.role
institution: @user.institution
}
expect(@UserInfoController.formatPersonalInfo(@user)).to.deep.equal {
id: @user._id.toString()
first_name: @user.first_name
last_name: @user.last_name
email: @user.email
signUpDate: @user.signUpDate
role: @user.role
institution: @user.institution
}