Merge pull request #2992 from overleaf/msm-add-user-projection-referal

Added projection to User.find() queries in Referal feature

GitOrigin-RevId: 4929bcd9c1b242b7e35cc2632bbd8da3f378cd1d
This commit is contained in:
Miguel Serrano
2020-07-17 02:06:31 +00:00
committed by Copybot
parent e692802690
commit 5b40eca697
10 changed files with 129 additions and 246 deletions
@@ -1,17 +1,4 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
@@ -44,16 +31,16 @@ describe('ReferalAllocator', function() {
this.new_user_id = 'new-user-id-123'
this.FeaturesUpdater.refreshFeatures = sinon.stub().yields()
this.User.update = sinon.stub().callsArgWith(3, null)
return (this.User.findOne = sinon
this.User.findOne = sinon
.stub()
.callsArgWith(1, null, { _id: this.user_id }))
.callsArgWith(2, null, { _id: this.user_id })
})
describe('allocate', function() {
describe('when the referal was a bonus referal', function() {
beforeEach(function() {
this.referal_source = 'bonus'
return this.ReferalAllocator.allocate(
this.ReferalAllocator.allocate(
this.referal_id,
this.new_user_id,
this.referal_source,
@@ -63,7 +50,7 @@ describe('ReferalAllocator', function() {
})
it('should update the referring user with the refered users id', function() {
return this.User.update
this.User.update
.calledWith(
{
referal_id: this.referal_id
@@ -81,19 +68,19 @@ describe('ReferalAllocator', function() {
})
it('find the referring users id', function() {
return this.User.findOne
this.User.findOne
.calledWith({ referal_id: this.referal_id })
.should.equal(true)
})
it("should refresh the user's subscription", function() {
return this.FeaturesUpdater.refreshFeatures
this.FeaturesUpdater.refreshFeatures
.calledWith(this.user_id)
.should.equal(true)
})
it('should call the callback', function() {
return this.callback.called.should.equal(true)
this.callback.called.should.equal(true)
})
})
@@ -101,8 +88,8 @@ describe('ReferalAllocator', function() {
beforeEach(function() {
this.referal_source = 'bonus'
this.referal_id = 'wombat'
this.User.findOne = sinon.stub().callsArgWith(1, null, null)
return this.ReferalAllocator.allocate(
this.User.findOne = sinon.stub().callsArgWith(2, null, null)
this.ReferalAllocator.allocate(
this.referal_id,
this.new_user_id,
this.referal_source,
@@ -112,28 +99,28 @@ describe('ReferalAllocator', function() {
})
it('should find the referring users id', function() {
return this.User.findOne
this.User.findOne
.calledWith({ referal_id: this.referal_id })
.should.equal(true)
})
it('should not update the referring user with the refered users id', function() {
return this.User.update.called.should.equal(false)
this.User.update.called.should.equal(false)
})
it('should not assign the user a bonus', function() {
return this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
})
it('should call the callback', function() {
return this.callback.called.should.equal(true)
this.callback.called.should.equal(true)
})
})
describe('when the referal is not a bonus referal', function() {
beforeEach(function() {
this.referal_source = 'public_share'
return this.ReferalAllocator.allocate(
this.ReferalAllocator.allocate(
this.referal_id,
this.new_user_id,
this.referal_source,
@@ -143,21 +130,21 @@ describe('ReferalAllocator', function() {
})
it('should not update the referring user with the refered users id', function() {
return this.User.update.called.should.equal(false)
this.User.update.called.should.equal(false)
})
it('find the referring users id', function() {
return this.User.findOne
this.User.findOne
.calledWith({ referal_id: this.referal_id })
.should.equal(true)
})
it('should not assign the user a bonus', function() {
return this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
})
it('should call the callback', function() {
return this.callback.called.should.equal(true)
this.callback.called.should.equal(true)
})
})
})
@@ -1,19 +1,5 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
__dirname,
'../../../../app/src/Features/Referal/ReferalConnect.js'
@@ -21,7 +7,7 @@ const modulePath = require('path').join(
describe('Referal connect middle wear', function() {
beforeEach(function() {
return (this.connect = SandboxedModule.require(modulePath, {
this.connect = SandboxedModule.require(modulePath, {
globals: {
console: console
},
@@ -31,7 +17,7 @@ describe('Referal connect middle wear', function() {
err() {}
}
}
}))
})
})
it('should take a referal query string and put it on the session if it exists', function(done) {
@@ -39,9 +25,9 @@ describe('Referal connect middle wear', function() {
query: { referal: '12345' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_id.should.equal(req.query.referal)
return done()
done()
})
})
@@ -50,9 +36,9 @@ describe('Referal connect middle wear', function() {
query: {},
session: { referal_id: 'same' }
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_id.should.equal('same')
return done()
done()
})
})
@@ -61,9 +47,9 @@ describe('Referal connect middle wear', function() {
query: { fb_ref: '12345' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_id.should.equal(req.query.fb_ref)
return done()
done()
})
})
@@ -72,9 +58,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 'fb' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('facebook')
return done()
done()
})
})
@@ -83,9 +69,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 't' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('twitter')
return done()
done()
})
})
@@ -94,9 +80,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 'gp' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('google_plus')
return done()
done()
})
})
@@ -105,9 +91,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 'e' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('email')
return done()
done()
})
})
@@ -116,9 +102,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 'd' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('direct')
return done()
done()
})
})
@@ -127,9 +113,9 @@ describe('Referal connect middle wear', function() {
query: { rs: 'b' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_source.should.equal('bonus')
return done()
done()
})
})
@@ -138,9 +124,9 @@ describe('Referal connect middle wear', function() {
query: { rs: 'ps' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_source.should.equal('public_share')
return done()
done()
})
})
@@ -149,9 +135,9 @@ describe('Referal connect middle wear', function() {
query: { rs: 'ci' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_source.should.equal('collaborator_invite')
return done()
done()
})
})
})
@@ -1,19 +1,5 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
__dirname,
'../../../../app/src/Features/Referal/ReferalController.js'
@@ -21,7 +7,7 @@ const modulePath = require('path').join(
describe('Referal controller', function() {
beforeEach(function() {
return (this.controller = SandboxedModule.require(modulePath, {
this.controller = SandboxedModule.require(modulePath, {
globals: {
console: console
},
@@ -31,6 +17,6 @@ describe('Referal controller', function() {
err() {}
}
}
}))
})
})
})
@@ -1,17 +1,4 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
@@ -40,7 +27,7 @@ describe('ReferalFeatures', function() {
this.referal_id = 'referal-id-123'
this.referal_medium = 'twitter'
this.user_id = 'user-id-123'
return (this.new_user_id = 'new-user-id-123')
this.new_user_id = 'new-user-id-123'
})
describe('getBonusFeatures', function() {
@@ -58,18 +45,16 @@ describe('ReferalFeatures', function() {
features: { collaborators: 1, dropbox: false, versioning: false }
}
this.User.findOne = sinon.stub().callsArgWith(1, null, stubbedUser)
return this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
this.User.findOne = sinon.stub().callsArgWith(2, null, stubbedUser)
this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
})
it('should get the users number of refered user', function() {
return this.User.findOne
.calledWith({ _id: this.user_id })
.should.equal(true)
this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
})
it('should call the callback with the features', function() {
return this.callback
this.callback
.calledWith(null, this.Settings.bonus_features[3])
.should.equal(true)
})
@@ -87,18 +72,16 @@ describe('ReferalFeatures', function() {
}
this.User.findOne = sinon
.stub()
.callsArgWith(1, null, { refered_user_count: this.refered_user_count })
return this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
.callsArgWith(2, null, { refered_user_count: this.refered_user_count })
this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
})
it('should get the users number of refered user', function() {
return this.User.findOne
.calledWith({ _id: this.user_id })
.should.equal(true)
this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
})
it('should call the callback with no features', function() {
return this.callback.calledWith(null, {}).should.equal(true)
this.callback.calledWith(null, {}).should.equal(true)
})
})
})
@@ -1,19 +1,5 @@
/* eslint-disable
handle-callback-err,
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const should = require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
__dirname,
@@ -37,7 +23,7 @@ describe('Referal handler', function() {
}
}
})
return (this.user_id = '12313')
this.user_id = '12313'
})
describe('getting refered user_ids', function() {
@@ -46,53 +32,57 @@ describe('Referal handler', function() {
refered_users: ['1234', '312312', '3213129'],
refered_user_count: 3
}
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserIds.should.deep.equal(user.refered_users)
passedReferedUserCount.should.equal(3)
return done()
done()
}
)
})
it('should return an empty array if it is not set', function(done) {
const user = {}
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserIds.length.should.equal(0)
return done()
done()
}
)
})
it('should return a zero count if netither it or the array are set', function(done) {
it('should return a zero count if neither it or the array are set', function(done) {
const user = {}
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserCount.should.equal(0)
return done()
done()
}
)
})
it('should return the array length if count is not set', function(done) {
const user = { refered_users: ['1234', '312312', '3213129'] }
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserCount.should.equal(3)
return done()
done()
}
)
})
@@ -102,15 +92,25 @@ describe('Referal handler', function() {
refered_users: ['1234', '312312', '3213129'],
refered_user_count: 5
}
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserCount.should.equal(5)
return done()
done()
}
)
})
it('should error if finding the user fails', function(done) {
this.User.findById.callsArgWith(2, new Error('user not found'))
this.handler.getReferedUsers(this.user_id, err => {
err.should.match(/user not found/)
done()
})
})
})
})