Don't try to fetch user when id doesn't exist

This commit is contained in:
James Allen 2014-03-11 11:45:25 +00:00
parent 83b2aa3082
commit 3d1d962501
3 changed files with 94 additions and 59 deletions

View file

@ -79,6 +79,7 @@ module.exports = UpdatesManager =
fillUserInfo: (updates, callback = (error, updates) ->) ->
users = {}
for update in updates
if UpdatesManager._validUserId(update.meta.user_id)
users[update.meta.user_id] = true
jobs = []
@ -95,5 +96,12 @@ module.exports = UpdatesManager =
for update in updates
user_id = update.meta.user_id
delete update.meta.user_id
if UpdatesManager._validUserId(user_id)
update.meta.user = users[user_id]
callback null, updates
_validUserId: (user_id) ->
if !user_id?
return false
else
return !!user_id.match(/^[a-f0-9]{24}$/)

View file

@ -33,7 +33,7 @@ describe "Getting updates", ->
v: 3
}, {
op: [{ i: "two ", p: 4 }]
meta: { ts: @to - 2 * @minutes, user_id: @user_id }
meta: { ts: @to - 2 * @minutes }
v: 4
}, {
op: [{ i: "three ", p: 8 }]
@ -71,6 +71,5 @@ describe "Getting updates", ->
meta:
start_ts: @to - 2 * @minutes
end_ts: @to - 2 * @minutes
user: @user
v: 4
}]

View file

@ -243,9 +243,11 @@ describe "UpdatesManager", ->
@callback.calledWith(null, @updatesWithUserInfo).should.equal true
describe "fillUserInfo", ->
describe "with valid users", ->
beforeEach (done) ->
@user_id_1 = "user-id-1"
@user_id_2 = "user-id-2"
{ObjectId} = require "mongojs"
@user_id_1 = ObjectId().toString()
@user_id_2 = ObjectId().toString()
@updates = [{
meta:
user_id: @user_id_1
@ -259,13 +261,10 @@ describe "UpdatesManager", ->
user_id: @user_id_2
op: "mock-op-3"
}]
@user_info =
"user-id-1": {
email: "user1@sharelatex.com"
}
"user-id-2": {
email: "user2@sharelatex.com"
}
@user_info = {}
@user_info[@user_id_1] = email: "user1@sharelatex.com"
@user_info[@user_id_2] = email: "user2@sharelatex.com"
@WebApiManager.getUserInfo = (user_id, callback = (error, userInfo) ->) =>
callback null, @user_info[user_id]
sinon.spy @WebApiManager, "getUserInfo"
@ -301,6 +300,35 @@ describe "UpdatesManager", ->
}]
describe "with invalid user ids", ->
beforeEach (done) ->
@updates = [{
meta:
user_id: null
op: "mock-op-1"
}, {
meta:
user_id: "anonymous-user"
op: "mock-op-2"
}]
@WebApiManager.getUserInfo = (user_id, callback = (error, userInfo) ->) =>
callback null, @user_info[user_id]
sinon.spy @WebApiManager, "getUserInfo"
@UpdatesManager.fillUserInfo @updates, (error, @results) =>
done()
it "should not call getUserInfo", ->
@WebApiManager.getUserInfo.called.should.equal false
it "should return the updates without the user info filled", ->
expect(@results).to.deep.equal [{
meta: {}
op: "mock-op-1"
}, {
meta: {}
op: "mock-op-2"
}]