| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- /* 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
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const { expect } = require('chai')
- const async = require('async')
- const UserClient = require('./helpers/User')
- const request = require('./helpers/request')
- const settings = require('settings-sharelatex')
- const { ObjectId } = require('../../../app/src/infrastructure/mongojs')
- const { Subscription } = require('../../../app/src/models/Subscription')
- const { User } = require('../../../app/src/models/User')
- const FeaturesUpdater = require('../../../app/src/Features/Subscription/FeaturesUpdater')
- const MockV1Api = require('./helpers/MockV1Api')
- const logger = require('logger-sharelatex')
- logger.logger.level('error')
- const syncUserAndGetFeatures = function(user, callback) {
- if (callback == null) {
- callback = function(error, features) {}
- }
- return FeaturesUpdater.refreshFeatures(user._id, error => {
- if (error != null) {
- return callback(error)
- }
- return User.findById(user._id, (error, user) => {
- if (error != null) {
- return callback(error)
- }
- const { features } = user.toObject()
- delete features.$init // mongoose internals
- return callback(null, features)
- })
- })
- }
- describe('FeatureUpdater.refreshFeatures', function() {
- beforeEach(function(done) {
- this.user = new UserClient()
- return this.user.ensureUserExists(error => {
- if (error != null) {
- throw error
- }
- return done()
- })
- })
- describe('when user has no subscriptions', function() {
- it('should set their features to the basic set', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- expect(features).to.deep.equal(settings.defaultFeatures)
- return done()
- })
- })
- })
- describe('when the user has an individual subscription', function() {
- beforeEach(function() {
- return Subscription.create({
- admin_id: this.user._id,
- manager_ids: [this.user._id],
- planCode: 'collaborator',
- customAccount: true
- })
- }) // returns a promise
- it('should set their features to the upgraded set', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- const plan = settings.plans.find(
- plan => plan.planCode === 'collaborator'
- )
- expect(features).to.deep.equal(plan.features)
- return done()
- })
- })
- })
- describe('when the user is in a group subscription', function() {
- beforeEach(function() {
- const groupAdminId = ObjectId()
- return Subscription.create({
- admin_id: groupAdminId,
- manager_ids: [groupAdminId],
- member_ids: [this.user._id],
- groupAccount: true,
- planCode: 'collaborator',
- customAccount: true
- })
- }) // returns a promise
- it('should set their features to the upgraded set', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- const plan = settings.plans.find(
- plan => plan.planCode === 'collaborator'
- )
- expect(features).to.deep.equal(plan.features)
- return done()
- })
- })
- })
- describe('when the user has bonus features', function() {
- beforeEach(function() {
- return User.update(
- {
- _id: this.user._id
- },
- {
- refered_user_count: 10
- }
- )
- }) // returns a promise
- it('should set their features to the bonus set', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- expect(features).to.deep.equal(
- Object.assign(
- {},
- settings.defaultFeatures,
- settings.bonus_features[9]
- )
- )
- return done()
- })
- })
- })
- describe('when the user has affiliations', function() {
- beforeEach(function() {
- this.institutionPlan = settings.plans.find(
- plan => plan.planCode === settings.institutionPlanCode
- )
- this.email = this.user.emails[0].email
- return (this.affiliationData = {
- email: this.email,
- institution: { licence: 'pro_plus', confirmed: true }
- })
- })
- it('should not set their features if email is not confirmed', function(done) {
- MockV1Api.setAffiliations([this.affiliationData])
- return syncUserAndGetFeatures(this.user, (error, features) => {
- expect(features).to.deep.equal(settings.defaultFeatures)
- return done()
- })
- })
- it('should set their features if email is confirmed', function(done) {
- MockV1Api.setAffiliations([this.affiliationData])
- return this.user.confirmEmail(this.email, error => {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- expect(features).to.deep.equal(this.institutionPlan.features)
- return done()
- })
- })
- })
- it('should not set their features if institution is not confirmed', function(done) {
- this.affiliationData.institution.confirmed = false
- MockV1Api.setAffiliations([this.affiliationData])
- return this.user.confirmEmail(this.email, error => {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- expect(features).to.deep.equal(settings.defaultFeatures)
- return done()
- })
- })
- })
- })
- describe('when the user is due bonus features and has extra features that no longer apply', function() {
- beforeEach(function() {
- return User.update(
- {
- _id: this.user._id
- },
- {
- refered_user_count: 10,
- 'features.github': true
- }
- )
- }) // returns a promise
- it('should set their features to the bonus set and downgrade the extras', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- expect(features).to.deep.equal(
- Object.assign(
- {},
- settings.defaultFeatures,
- settings.bonus_features[9]
- )
- )
- return done()
- })
- })
- })
- describe('when the user has a v1 plan', function() {
- beforeEach(function() {
- MockV1Api.setUser(42, { plan_name: 'free' })
- return User.update(
- {
- _id: this.user._id
- },
- {
- overleaf: {
- id: 42
- }
- }
- )
- }) // returns a promise
- it('should set their features to the v1 plan', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- const plan = settings.plans.find(plan => plan.planCode === 'v1_free')
- expect(features).to.deep.equal(plan.features)
- return done()
- })
- })
- })
- describe('when the user has a v1 plan and bonus features', function() {
- beforeEach(function() {
- MockV1Api.setUser(42, { plan_name: 'free' })
- return User.update(
- {
- _id: this.user._id
- },
- {
- overleaf: {
- id: 42
- },
- refered_user_count: 10
- }
- )
- }) // returns a promise
- it('should set their features to the best of the v1 plan and bonus features', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- const v1plan = settings.plans.find(plan => plan.planCode === 'v1_free')
- const expectedFeatures = Object.assign(
- {},
- v1plan.features,
- settings.bonus_features[9]
- )
- expect(features).to.deep.equal(expectedFeatures)
- return done()
- })
- })
- })
- describe('when the user has a group and personal subscription', function() {
- beforeEach(function(done) {
- const groupAdminId = ObjectId()
- Subscription.create(
- {
- admin_id: this.user._id,
- manager_ids: [this.user._id],
- planCode: 'professional',
- customAccount: true
- },
- error => {
- if (error != null) {
- throw error
- }
- return Subscription.create(
- {
- admin_id: groupAdminId,
- manager_ids: [groupAdminId],
- member_ids: [this.user._id],
- groupAccount: true,
- planCode: 'collaborator',
- customAccount: true
- },
- done
- )
- }
- )
- })
- it('should set their features to the best set', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- const plan = settings.plans.find(
- plan => plan.planCode === 'professional'
- )
- expect(features).to.deep.equal(plan.features)
- return done()
- })
- })
- })
- describe('when the notifyV1Flag is passed', function() {
- beforeEach(function() {
- return User.update(
- {
- _id: this.user._id
- },
- {
- overleaf: {
- id: 42
- }
- }
- )
- }) // returns a promise
- })
- describe('when the user has features overrides', function() {
- beforeEach(function() {
- const futureDate = new Date()
- futureDate.setDate(futureDate.getDate() + 1)
- return User.update(
- {
- _id: this.user._id
- },
- {
- featuresOverrides: [
- {
- features: {
- github: true
- }
- },
- {
- features: {
- dropbox: true
- },
- expiresAt: new Date(1990, 12, 25)
- },
- {
- features: {
- trackChanges: true
- },
- expiresAt: futureDate
- }
- ]
- }
- )
- }) // returns a promise
- it('should set their features to the overridden set', function(done) {
- return syncUserAndGetFeatures(this.user, (error, features) => {
- if (error != null) {
- throw error
- }
- let expectedFeatures = Object.assign(settings.defaultFeatures, {
- github: true,
- trackChanges: true
- })
- expect(features).to.deep.equal(expectedFeatures)
- return done()
- })
- })
- })
- })
|