2019-05-29 05:21:06 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const { expect } = require('chai')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/src/Features/Subscription/FeaturesUpdater'
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
describe('FeaturesUpdater', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater = SandboxedModule.require(modulePath, {
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'./UserFeaturesUpdater': (this.UserFeaturesUpdater = {}),
|
|
|
|
'./SubscriptionLocator': (this.SubscriptionLocator = {}),
|
|
|
|
'./PlansLocator': (this.PlansLocator = {}),
|
|
|
|
'settings-sharelatex': (this.Settings = {}),
|
|
|
|
'../Referal/ReferalFeatures': (this.ReferalFeatures = {}),
|
|
|
|
'./V1SubscriptionManager': (this.V1SubscriptionManager = {}),
|
2019-10-05 13:43:21 -04:00
|
|
|
'../Institutions/InstitutionsFeatures': (this.InstitutionsFeatures = {}),
|
2020-11-04 04:53:06 -05:00
|
|
|
'../User/UserGetter': (this.UserGetter = {}),
|
|
|
|
'../../infrastructure/Modules': (this.Modules = {
|
|
|
|
hooks: { fire: sinon.stub() }
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('refreshFeatures', function() {
|
|
|
|
beforeEach(function() {
|
2020-11-04 04:53:06 -05:00
|
|
|
this.user = {
|
|
|
|
_id: this.user_id,
|
|
|
|
features: {}
|
|
|
|
}
|
2021-03-10 04:51:50 -05:00
|
|
|
this.UserFeaturesUpdater.updateFeatures = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { some: 'features' }, true)
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater._getIndividualFeatures = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { individual: 'features' })
|
|
|
|
this.FeaturesUpdater._getGroupFeatureSets = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, [{ group: 'features' }, { group: 'features2' }])
|
|
|
|
this.InstitutionsFeatures.getInstitutionsFeatures = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { institutions: 'features' })
|
|
|
|
this.FeaturesUpdater._getV1Features = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { v1: 'features' })
|
|
|
|
this.ReferalFeatures.getBonusFeatures = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { bonus: 'features' })
|
|
|
|
this.FeaturesUpdater._mergeFeatures = sinon
|
|
|
|
.stub()
|
|
|
|
.returns({ merged: 'features' })
|
2020-11-04 04:53:06 -05:00
|
|
|
this.UserGetter.getUser = sinon.stub().yields(null, this.user)
|
2021-03-04 09:22:43 -05:00
|
|
|
this.callback = sinon.stub()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2021-03-10 04:51:50 -05:00
|
|
|
|
|
|
|
it('should return features and featuresChanged', function() {
|
|
|
|
this.FeaturesUpdater.refreshFeatures(
|
|
|
|
this.user_id,
|
|
|
|
(err, features, featuresChanged) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(features).to.exist
|
|
|
|
expect(featuresChanged).to.exist
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
describe('normally', function() {
|
|
|
|
beforeEach(function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater.refreshFeatures(this.user_id, this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the individual features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._getIndividualFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.user_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the group features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._getGroupFeatureSets
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.user_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the institution features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.InstitutionsFeatures.getInstitutionsFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.user_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the v1 features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._getV1Features
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.user_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the bonus features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.ReferalFeatures.getBonusFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.user_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge from the default features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._mergeFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.Settings.defaultFeatures)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge the individual features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._mergeFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(sinon.match.any, { individual: 'features' })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge the group features', function() {
|
|
|
|
this.FeaturesUpdater._mergeFeatures
|
|
|
|
.calledWith(sinon.match.any, { group: 'features' })
|
|
|
|
.should.equal(true)
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._mergeFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(sinon.match.any, { group: 'features2' })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge the institutions features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._mergeFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(sinon.match.any, { institutions: 'features' })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge the v1 features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._mergeFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(sinon.match.any, { v1: 'features' })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge the bonus features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater._mergeFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(sinon.match.any, { bonus: 'features' })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should update the user with the merged features', function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.UserFeaturesUpdater.updateFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.user_id, { merged: 'features' })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2020-11-04 04:53:06 -05:00
|
|
|
describe('when losing dropbox feature', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.user = {
|
|
|
|
_id: this.user_id,
|
|
|
|
features: { dropbox: true }
|
|
|
|
}
|
|
|
|
this.UserGetter.getUser = sinon.stub().yields(null, this.user)
|
|
|
|
this.FeaturesUpdater._mergeFeatures = sinon
|
|
|
|
.stub()
|
|
|
|
.returns({ dropbox: false })
|
2021-03-04 09:22:43 -05:00
|
|
|
this.FeaturesUpdater.refreshFeatures(this.user_id, this.callback)
|
2020-11-04 04:53:06 -05:00
|
|
|
})
|
|
|
|
it('should fire module hook to unlink dropbox', function() {
|
|
|
|
this.Modules.hooks.fire
|
|
|
|
.calledWith('removeDropbox', this.user._id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('_mergeFeatures', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
it('should prefer priority over standard for compileGroup', function() {
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
compileGroup: 'priority'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
compileGroup: 'standard'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
compileGroup: 'priority'
|
|
|
|
})
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
compileGroup: 'standard'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
compileGroup: 'priority'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
compileGroup: 'priority'
|
|
|
|
})
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
compileGroup: 'priority'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
compileGroup: 'priority'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
compileGroup: 'priority'
|
|
|
|
})
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
compileGroup: 'standard'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
compileGroup: 'standard'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
compileGroup: 'standard'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should prefer -1 over any other for collaborators', function() {
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
collaborators: -1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
collaborators: 10
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
collaborators: -1
|
|
|
|
})
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
collaborators: 10
|
|
|
|
},
|
|
|
|
{
|
|
|
|
collaborators: -1
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
collaborators: -1
|
|
|
|
})
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
collaborators: 4
|
|
|
|
},
|
|
|
|
{
|
|
|
|
collaborators: 10
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
collaborators: 10
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should prefer the higher of compileTimeout', function() {
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
compileTimeout: 20
|
|
|
|
},
|
|
|
|
{
|
|
|
|
compileTimeout: 10
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
compileTimeout: 20
|
|
|
|
})
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
compileTimeout: 10
|
|
|
|
},
|
|
|
|
{
|
|
|
|
compileTimeout: 20
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
compileTimeout: 20
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should prefer the true over false for other keys', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
github: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
github: false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
github: true
|
|
|
|
})
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
github: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
github: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
github: true
|
|
|
|
})
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
github: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
github: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
github: true
|
|
|
|
})
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater._mergeFeatures(
|
|
|
|
{
|
|
|
|
github: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
github: false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
|
|
|
github: false
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-02-03 09:12:34 -05:00
|
|
|
|
|
|
|
describe('doSyncFromV1', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.v1UserId = 1
|
|
|
|
this.user = {
|
|
|
|
_id: this.user_id,
|
|
|
|
email: 'user@example.com',
|
|
|
|
overleaf: {
|
|
|
|
id: this.v1UserId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
|
|
|
|
this.FeaturesUpdater.refreshFeatures = sinon.stub().yields(null)
|
2021-03-04 09:22:43 -05:00
|
|
|
this.call = cb => {
|
|
|
|
this.FeaturesUpdater.doSyncFromV1(this.v1UserId, cb)
|
|
|
|
}
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('when all goes well', function() {
|
|
|
|
it('should call getUser', function(done) {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.call(() => {
|
2020-02-03 09:12:34 -05:00
|
|
|
expect(this.UserGetter.getUser.callCount).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.UserGetter.getUser.calledWith({ 'overleaf.id': this.v1UserId })
|
|
|
|
).to.equal(true)
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call refreshFeatures', function(done) {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.call(() => {
|
2020-02-03 09:12:34 -05:00
|
|
|
expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.FeaturesUpdater.refreshFeatures.calledWith(this.user_id)
|
|
|
|
).to.equal(true)
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not produce an error', function(done) {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.call(err => {
|
2020-02-03 09:12:34 -05:00
|
|
|
expect(err).to.not.exist
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when getUser produces an error', function() {
|
|
|
|
beforeEach(function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.UserGetter.getUser = sinon
|
2020-02-03 09:12:34 -05:00
|
|
|
.stub()
|
2021-03-04 09:22:43 -05:00
|
|
|
.callsArgWith(2, new Error('woops'))
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call refreshFeatures', function() {
|
|
|
|
expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should produce an error', function(done) {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.call(err => {
|
2020-02-03 09:12:34 -05:00
|
|
|
expect(err).to.exist
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when getUser does not find a user', function() {
|
|
|
|
beforeEach(function() {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call refreshFeatures', function(done) {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.call(() => {
|
2020-02-03 09:12:34 -05:00
|
|
|
expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(0)
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not produce an error', function(done) {
|
2021-03-04 09:22:43 -05:00
|
|
|
this.call(err => {
|
2020-02-03 09:12:34 -05:00
|
|
|
expect(err).to.not.exist
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2020-02-03 09:12:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|