| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739 |
- /* eslint-disable
- camelcase,
- 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 sinon = require('sinon')
- require('chai').should()
- const modulePath = require('path').join(
- __dirname,
- '../../../../app/src/Features/Subscription/LimitationsManager'
- )
- const Settings = require('settings-sharelatex')
- describe('LimitationsManager', function() {
- beforeEach(function() {
- this.project = { _id: (this.project_id = 'project-id') }
- this.user = { _id: (this.user_id = 'user-id'), features: {} }
- this.ProjectGetter = {
- getProject: (project_id, fields, callback) => {
- if (project_id === this.project_id) {
- return callback(null, this.project)
- } else {
- return callback(null, null)
- }
- }
- }
- this.UserGetter = {
- getUser: (user_id, filter, callback) => {
- if (user_id === this.user_id) {
- return callback(null, this.user)
- } else {
- return callback(null, null)
- }
- }
- }
- this.SubscriptionLocator = {
- getUsersSubscription: sinon.stub(),
- getSubscription: sinon.stub()
- }
- return (this.LimitationsManager = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- '../Project/ProjectGetter': this.ProjectGetter,
- '../User/UserGetter': this.UserGetter,
- './SubscriptionLocator': this.SubscriptionLocator,
- 'settings-sharelatex': (this.Settings = {}),
- '../Collaborators/CollaboratorsGetter': (this.CollaboratorsGetter = {}),
- '../Collaborators/CollaboratorsInviteHandler': (this.CollaboratorsInviteHandler = {}),
- './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
- 'logger-sharelatex': {
- log() {}
- }
- }
- }))
- })
- describe('allowedNumberOfCollaboratorsInProject', function() {
- describe('when the project is owned by a user without a subscription', function() {
- beforeEach(function() {
- this.Settings.defaultFeatures = { collaborators: 23 }
- this.project.owner_ref = this.user_id
- delete this.user.features
- this.callback = sinon.stub()
- return this.LimitationsManager.allowedNumberOfCollaboratorsInProject(
- this.project_id,
- this.callback
- )
- })
- it('should return the default number', function() {
- return this.callback
- .calledWith(null, this.Settings.defaultFeatures.collaborators)
- .should.equal(true)
- })
- })
- describe('when the project is owned by a user with a subscription', function() {
- beforeEach(function() {
- this.project.owner_ref = this.user_id
- this.user.features = { collaborators: 21 }
- this.callback = sinon.stub()
- return this.LimitationsManager.allowedNumberOfCollaboratorsInProject(
- this.project_id,
- this.callback
- )
- })
- it('should return the number of collaborators the user is allowed', function() {
- return this.callback
- .calledWith(null, this.user.features.collaborators)
- .should.equal(true)
- })
- })
- })
- describe('allowedNumberOfCollaboratorsForUser', function() {
- describe('when the user has no features', function() {
- beforeEach(function() {
- this.Settings.defaultFeatures = { collaborators: 23 }
- delete this.user.features
- this.callback = sinon.stub()
- return this.LimitationsManager.allowedNumberOfCollaboratorsForUser(
- this.user_id,
- this.callback
- )
- })
- it('should return the default number', function() {
- return this.callback
- .calledWith(null, this.Settings.defaultFeatures.collaborators)
- .should.equal(true)
- })
- })
- describe('when the user has features', function() {
- beforeEach(function() {
- this.user.features = { collaborators: 21 }
- this.callback = sinon.stub()
- return this.LimitationsManager.allowedNumberOfCollaboratorsForUser(
- this.user_id,
- this.callback
- )
- })
- it('should return the number of collaborators the user is allowed', function() {
- return this.callback
- .calledWith(null, this.user.features.collaborators)
- .should.equal(true)
- })
- })
- })
- describe('canAddXCollaborators', function() {
- describe('when the project has fewer collaborators than allowed', function() {
- beforeEach(function() {
- this.current_number = 1
- this.allowed_number = 2
- this.invite_count = 0
- this.CollaboratorsGetter.getInvitedCollaboratorCount = (
- project_id,
- callback
- ) => callback(null, this.current_number)
- this.CollaboratorsInviteHandler.getInviteCount = (
- project_id,
- callback
- ) => callback(null, this.invite_count)
- sinon
- .stub(
- this.LimitationsManager,
- 'allowedNumberOfCollaboratorsInProject'
- )
- .callsFake((project_id, callback) => {
- return callback(null, this.allowed_number)
- })
- this.callback = sinon.stub()
- return this.LimitationsManager.canAddXCollaborators(
- this.project_id,
- 1,
- this.callback
- )
- })
- it('should return true', function() {
- return this.callback.calledWith(null, true).should.equal(true)
- })
- })
- describe('when the project has fewer collaborators and invites than allowed', function() {
- beforeEach(function() {
- this.current_number = 1
- this.allowed_number = 4
- this.invite_count = 1
- this.CollaboratorsGetter.getInvitedCollaboratorCount = (
- project_id,
- callback
- ) => callback(null, this.current_number)
- this.CollaboratorsInviteHandler.getInviteCount = (
- project_id,
- callback
- ) => callback(null, this.invite_count)
- sinon
- .stub(
- this.LimitationsManager,
- 'allowedNumberOfCollaboratorsInProject'
- )
- .callsFake((project_id, callback) => {
- return callback(null, this.allowed_number)
- })
- this.callback = sinon.stub()
- return this.LimitationsManager.canAddXCollaborators(
- this.project_id,
- 1,
- this.callback
- )
- })
- it('should return true', function() {
- return this.callback.calledWith(null, true).should.equal(true)
- })
- })
- describe('when the project has fewer collaborators than allowed but I want to add more than allowed', function() {
- beforeEach(function() {
- this.current_number = 1
- this.allowed_number = 2
- this.invite_count = 0
- this.CollaboratorsGetter.getInvitedCollaboratorCount = (
- project_id,
- callback
- ) => callback(null, this.current_number)
- this.CollaboratorsInviteHandler.getInviteCount = (
- project_id,
- callback
- ) => callback(null, this.invite_count)
- sinon
- .stub(
- this.LimitationsManager,
- 'allowedNumberOfCollaboratorsInProject'
- )
- .callsFake((project_id, callback) => {
- return callback(null, this.allowed_number)
- })
- this.callback = sinon.stub()
- return this.LimitationsManager.canAddXCollaborators(
- this.project_id,
- 2,
- this.callback
- )
- })
- it('should return false', function() {
- return this.callback.calledWith(null, false).should.equal(true)
- })
- })
- describe('when the project has more collaborators than allowed', function() {
- beforeEach(function() {
- this.current_number = 3
- this.allowed_number = 2
- this.invite_count = 0
- this.CollaboratorsGetter.getInvitedCollaboratorCount = (
- project_id,
- callback
- ) => callback(null, this.current_number)
- this.CollaboratorsInviteHandler.getInviteCount = (
- project_id,
- callback
- ) => callback(null, this.invite_count)
- sinon
- .stub(
- this.LimitationsManager,
- 'allowedNumberOfCollaboratorsInProject'
- )
- .callsFake((project_id, callback) => {
- return callback(null, this.allowed_number)
- })
- this.callback = sinon.stub()
- return this.LimitationsManager.canAddXCollaborators(
- this.project_id,
- 1,
- this.callback
- )
- })
- it('should return false', function() {
- return this.callback.calledWith(null, false).should.equal(true)
- })
- })
- describe('when the project has infinite collaborators', function() {
- beforeEach(function() {
- this.current_number = 100
- this.allowed_number = -1
- this.invite_count = 0
- this.CollaboratorsGetter.getInvitedCollaboratorCount = (
- project_id,
- callback
- ) => callback(null, this.current_number)
- this.CollaboratorsInviteHandler.getInviteCount = (
- project_id,
- callback
- ) => callback(null, this.invite_count)
- sinon
- .stub(
- this.LimitationsManager,
- 'allowedNumberOfCollaboratorsInProject'
- )
- .callsFake((project_id, callback) => {
- return callback(null, this.allowed_number)
- })
- this.callback = sinon.stub()
- return this.LimitationsManager.canAddXCollaborators(
- this.project_id,
- 1,
- this.callback
- )
- })
- it('should return true', function() {
- return this.callback.calledWith(null, true).should.equal(true)
- })
- })
- describe('when the project has more invites than allowed', function() {
- beforeEach(function() {
- this.current_number = 0
- this.allowed_number = 2
- this.invite_count = 2
- this.CollaboratorsGetter.getInvitedCollaboratorCount = (
- project_id,
- callback
- ) => callback(null, this.current_number)
- this.CollaboratorsInviteHandler.getInviteCount = (
- project_id,
- callback
- ) => callback(null, this.invite_count)
- sinon
- .stub(
- this.LimitationsManager,
- 'allowedNumberOfCollaboratorsInProject'
- )
- .callsFake((project_id, callback) => {
- return callback(null, this.allowed_number)
- })
- this.callback = sinon.stub()
- return this.LimitationsManager.canAddXCollaborators(
- this.project_id,
- 1,
- this.callback
- )
- })
- it('should return false', function() {
- return this.callback.calledWith(null, false).should.equal(true)
- })
- })
- describe('when the project has more invites and collaborators than allowed', function() {
- beforeEach(function() {
- this.current_number = 1
- this.allowed_number = 2
- this.invite_count = 1
- this.CollaboratorsGetter.getInvitedCollaboratorCount = (
- project_id,
- callback
- ) => callback(null, this.current_number)
- this.CollaboratorsInviteHandler.getInviteCount = (
- project_id,
- callback
- ) => callback(null, this.invite_count)
- sinon
- .stub(
- this.LimitationsManager,
- 'allowedNumberOfCollaboratorsInProject'
- )
- .callsFake((project_id, callback) => {
- return callback(null, this.allowed_number)
- })
- this.callback = sinon.stub()
- return this.LimitationsManager.canAddXCollaborators(
- this.project_id,
- 1,
- this.callback
- )
- })
- it('should return false', function() {
- return this.callback.calledWith(null, false).should.equal(true)
- })
- })
- })
- describe('userHasV2Subscription', function() {
- beforeEach(function() {
- return (this.SubscriptionLocator.getUsersSubscription = sinon.stub())
- })
- it('should return true if the recurly token is set', function(done) {
- this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {
- recurlySubscription_id: '1234'
- })
- return this.LimitationsManager.userHasV2Subscription(
- this.user,
- (err, hasSubscription) => {
- hasSubscription.should.equal(true)
- return done()
- }
- )
- })
- it('should return false if the recurly token is not set', function(done) {
- this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
- this.subscription = {}
- return this.LimitationsManager.userHasV2Subscription(
- this.user,
- (err, hasSubscription) => {
- hasSubscription.should.equal(false)
- return done()
- }
- )
- })
- it('should return false if the subscription is undefined', function(done) {
- this.SubscriptionLocator.getUsersSubscription.callsArgWith(1)
- return this.LimitationsManager.userHasV2Subscription(
- this.user,
- (err, hasSubscription) => {
- hasSubscription.should.equal(false)
- return done()
- }
- )
- })
- it('should return the subscription', function(done) {
- const stubbedSubscription = { freeTrial: {}, token: '' }
- this.SubscriptionLocator.getUsersSubscription.callsArgWith(
- 1,
- null,
- stubbedSubscription
- )
- return this.LimitationsManager.userHasV2Subscription(
- this.user,
- (err, hasSubOrIsGroupMember, subscription) => {
- subscription.should.deep.equal(stubbedSubscription)
- return done()
- }
- )
- })
- describe('when user has a custom account', function() {
- beforeEach(function() {
- this.fakeSubscription = { customAccount: true }
- return this.SubscriptionLocator.getUsersSubscription.callsArgWith(
- 1,
- null,
- this.fakeSubscription
- )
- })
- it('should return true', function(done) {
- return this.LimitationsManager.userHasV2Subscription(
- this.user,
- (err, hasSubscription, subscription) => {
- hasSubscription.should.equal(true)
- return done()
- }
- )
- })
- it('should return the subscription', function(done) {
- return this.LimitationsManager.userHasV2Subscription(
- this.user,
- (err, hasSubscription, subscription) => {
- subscription.should.deep.equal(this.fakeSubscription)
- return done()
- }
- )
- })
- })
- })
- describe('userIsMemberOfGroupSubscription', function() {
- beforeEach(function() {
- return (this.SubscriptionLocator.getMemberSubscriptions = sinon.stub())
- })
- it('should return false if there are no groups subcriptions', function(done) {
- this.SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, [])
- return this.LimitationsManager.userIsMemberOfGroupSubscription(
- this.user,
- (err, isMember) => {
- isMember.should.equal(false)
- return done()
- }
- )
- })
- it('should return true if there are no groups subcriptions', function(done) {
- let subscriptions
- this.SubscriptionLocator.getMemberSubscriptions.callsArgWith(
- 1,
- null,
- (subscriptions = ['mock-subscription'])
- )
- return this.LimitationsManager.userIsMemberOfGroupSubscription(
- this.user,
- (err, isMember, retSubscriptions) => {
- isMember.should.equal(true)
- retSubscriptions.should.equal(subscriptions)
- return done()
- }
- )
- })
- })
- describe('hasPaidSubscription', function() {
- beforeEach(function() {
- this.LimitationsManager.userIsMemberOfGroupSubscription = sinon
- .stub()
- .yields(null, false)
- this.LimitationsManager.userHasV2Subscription = sinon
- .stub()
- .yields(null, false)
- return (this.LimitationsManager.userHasV1Subscription = sinon
- .stub()
- .yields(null, false))
- })
- it('should return true if userIsMemberOfGroupSubscription', function(done) {
- this.LimitationsManager.userIsMemberOfGroupSubscription = sinon
- .stub()
- .yields(null, true)
- return this.LimitationsManager.hasPaidSubscription(
- this.user,
- (err, hasSubOrIsGroupMember) => {
- hasSubOrIsGroupMember.should.equal(true)
- return done()
- }
- )
- })
- it('should return true if userHasV2Subscription', function(done) {
- this.LimitationsManager.userHasV2Subscription = sinon
- .stub()
- .yields(null, true)
- return this.LimitationsManager.hasPaidSubscription(
- this.user,
- (err, hasSubOrIsGroupMember) => {
- hasSubOrIsGroupMember.should.equal(true)
- return done()
- }
- )
- })
- it('should return true if userHasV1Subscription', function(done) {
- this.LimitationsManager.userHasV1Subscription = sinon
- .stub()
- .yields(null, true)
- return this.LimitationsManager.hasPaidSubscription(
- this.user,
- (err, hasSubOrIsGroupMember) => {
- hasSubOrIsGroupMember.should.equal(true)
- return done()
- }
- )
- })
- it('should return false if none are true', function(done) {
- return this.LimitationsManager.hasPaidSubscription(
- this.user,
- (err, hasSubOrIsGroupMember) => {
- hasSubOrIsGroupMember.should.equal(false)
- return done()
- }
- )
- })
- it('should have userHasSubscriptionOrIsGroupMember alias', function(done) {
- return this.LimitationsManager.userHasSubscriptionOrIsGroupMember(
- this.user,
- (err, hasSubOrIsGroupMember) => {
- hasSubOrIsGroupMember.should.equal(false)
- return done()
- }
- )
- })
- })
- describe('userHasV1OrV2Subscription', function() {
- beforeEach(function() {
- this.LimitationsManager.userHasV2Subscription = sinon
- .stub()
- .yields(null, false)
- return (this.LimitationsManager.userHasV1Subscription = sinon
- .stub()
- .yields(null, false))
- })
- it('should return true if userHasV2Subscription', function(done) {
- this.LimitationsManager.userHasV2Subscription = sinon
- .stub()
- .yields(null, true)
- return this.LimitationsManager.userHasV1OrV2Subscription(
- this.user,
- (err, hasSub) => {
- hasSub.should.equal(true)
- return done()
- }
- )
- })
- it('should return true if userHasV1Subscription', function(done) {
- this.LimitationsManager.userHasV1Subscription = sinon
- .stub()
- .yields(null, true)
- return this.LimitationsManager.userHasV1OrV2Subscription(
- this.user,
- (err, hasSub) => {
- hasSub.should.equal(true)
- return done()
- }
- )
- })
- it('should return false if none are true', function(done) {
- return this.LimitationsManager.userHasV1OrV2Subscription(
- this.user,
- (err, hasSub) => {
- hasSub.should.equal(false)
- return done()
- }
- )
- })
- })
- describe('hasGroupMembersLimitReached', function() {
- beforeEach(function() {
- this.subscriptionId = '12312'
- return (this.subscription = {
- membersLimit: 3,
- member_ids: ['', ''],
- teamInvites: [
- { email: 'bob@example.com', sentAt: new Date(), token: 'hey' }
- ]
- })
- })
- it('should return true if the limit is hit (including members and invites)', function(done) {
- this.SubscriptionLocator.getSubscription.callsArgWith(
- 1,
- null,
- this.subscription
- )
- return this.LimitationsManager.hasGroupMembersLimitReached(
- this.subscriptionId,
- (err, limitReached) => {
- limitReached.should.equal(true)
- return done()
- }
- )
- })
- it('should return false if the limit is not hit (including members and invites)', function(done) {
- this.subscription.membersLimit = 4
- this.SubscriptionLocator.getSubscription.callsArgWith(
- 1,
- null,
- this.subscription
- )
- return this.LimitationsManager.hasGroupMembersLimitReached(
- this.subscriptionId,
- (err, limitReached) => {
- limitReached.should.equal(false)
- return done()
- }
- )
- })
- it('should return true if the limit has been exceded (including members and invites)', function(done) {
- this.subscription.membersLimit = 2
- this.SubscriptionLocator.getSubscription.callsArgWith(
- 1,
- null,
- this.subscription
- )
- return this.LimitationsManager.hasGroupMembersLimitReached(
- this.subscriptionId,
- (err, limitReached) => {
- limitReached.should.equal(true)
- return done()
- }
- )
- })
- })
- describe('userHasV1Subscription', function() {
- it('should return true if v1 returns has_subscription = true', function(done) {
- this.V1SubscriptionManager.getSubscriptionsFromV1 = sinon
- .stub()
- .yields(null, { has_subscription: true })
- return this.LimitationsManager.userHasV1Subscription(
- this.user,
- (error, result) => {
- this.V1SubscriptionManager.getSubscriptionsFromV1
- .calledWith(this.user_id)
- .should.equal(true)
- result.should.equal(true)
- return done()
- }
- )
- })
- it('should return false if v1 returns has_subscription = false', function(done) {
- this.V1SubscriptionManager.getSubscriptionsFromV1 = sinon
- .stub()
- .yields(null, { has_subscription: false })
- return this.LimitationsManager.userHasV1Subscription(
- this.user,
- (error, result) => {
- this.V1SubscriptionManager.getSubscriptionsFromV1
- .calledWith(this.user_id)
- .should.equal(true)
- result.should.equal(false)
- return done()
- }
- )
- })
- it('should return false if v1 returns nothing', function(done) {
- this.V1SubscriptionManager.getSubscriptionsFromV1 = sinon
- .stub()
- .yields(null, null)
- return this.LimitationsManager.userHasV1Subscription(
- this.user,
- (error, result) => {
- this.V1SubscriptionManager.getSubscriptionsFromV1
- .calledWith(this.user_id)
- .should.equal(true)
- result.should.equal(false)
- return done()
- }
- )
- })
- })
- })
|