2014-02-12 05:23:40 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
modulePath = require('path').join __dirname, '../../../../app/js/Features/Subscription/LimitationsManager'
|
|
|
|
Settings = require("settings-sharelatex")
|
|
|
|
|
|
|
|
describe "LimitationsManager", ->
|
|
|
|
beforeEach ->
|
2017-01-27 10:26:58 -05:00
|
|
|
@project = { _id: @project_id = "project-id" }
|
|
|
|
@user = { _id: @user_id = "user-id", features:{} }
|
2018-02-15 07:18:43 -05:00
|
|
|
@ProjectGetter =
|
|
|
|
getProject: (project_id, fields, callback) =>
|
2014-02-12 05:23:40 -05:00
|
|
|
if project_id == @project_id
|
|
|
|
callback null, @project
|
|
|
|
else
|
|
|
|
callback null, null
|
2017-01-27 10:26:58 -05:00
|
|
|
@UserGetter =
|
|
|
|
getUser: (user_id, filter, callback) =>
|
|
|
|
if user_id == @user_id
|
2014-02-12 05:23:40 -05:00
|
|
|
callback null, @user
|
|
|
|
else
|
|
|
|
callback null, null
|
|
|
|
|
|
|
|
@SubscriptionLocator =
|
|
|
|
getUsersSubscription: sinon.stub()
|
|
|
|
|
|
|
|
@LimitationsManager = SandboxedModule.require modulePath, requires:
|
2018-02-15 07:18:43 -05:00
|
|
|
'../Project/ProjectGetter': @ProjectGetter
|
2017-01-27 10:26:58 -05:00
|
|
|
'../User/UserGetter' : @UserGetter
|
2014-02-12 05:23:40 -05:00
|
|
|
'./SubscriptionLocator':@SubscriptionLocator
|
|
|
|
'settings-sharelatex' : @Settings = {}
|
2016-03-07 10:32:04 -05:00
|
|
|
"../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
|
2016-08-02 10:42:50 -04:00
|
|
|
"../Collaborators/CollaboratorsInviteHandler": @CollaboratorsInviteHandler = {}
|
2014-02-12 05:23:40 -05:00
|
|
|
'logger-sharelatex':log:->
|
|
|
|
|
|
|
|
describe "allowedNumberOfCollaboratorsInProject", ->
|
|
|
|
describe "when the project is owned by a user without a subscription", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Settings.defaultPlanCode = collaborators: 23
|
2017-01-27 10:26:58 -05:00
|
|
|
@project.owner_ref = @user_id
|
2014-02-12 05:23:40 -05:00
|
|
|
delete @user.features
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@LimitationsManager.allowedNumberOfCollaboratorsInProject(@project_id, @callback)
|
|
|
|
|
|
|
|
it "should return the default number", ->
|
|
|
|
@callback.calledWith(null, @Settings.defaultPlanCode.collaborators).should.equal true
|
|
|
|
|
|
|
|
describe "when the project is owned by a user with a subscription", ->
|
|
|
|
beforeEach ->
|
2017-01-27 10:26:58 -05:00
|
|
|
@project.owner_ref = @user_id
|
2014-02-12 05:23:40 -05:00
|
|
|
@user.features =
|
|
|
|
collaborators: 21
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@LimitationsManager.allowedNumberOfCollaboratorsInProject(@project_id, @callback)
|
|
|
|
|
|
|
|
it "should return the number of collaborators the user is allowed", ->
|
2017-01-27 10:26:58 -05:00
|
|
|
@callback.calledWith(null, @user.features.collaborators).should.equal true
|
|
|
|
|
|
|
|
describe "allowedNumberOfCollaboratorsForUser", ->
|
|
|
|
describe "when the user has no features", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Settings.defaultPlanCode = collaborators: 23
|
|
|
|
delete @user.features
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@LimitationsManager.allowedNumberOfCollaboratorsForUser(@user_id, @callback)
|
|
|
|
|
|
|
|
it "should return the default number", ->
|
|
|
|
@callback.calledWith(null, @Settings.defaultPlanCode.collaborators).should.equal true
|
|
|
|
|
|
|
|
describe "when the user has features", ->
|
|
|
|
beforeEach ->
|
|
|
|
@user.features =
|
|
|
|
collaborators: 21
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@LimitationsManager.allowedNumberOfCollaboratorsForUser(@user_id, @callback)
|
|
|
|
|
|
|
|
it "should return the number of collaborators the user is allowed", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
@callback.calledWith(null, @user.features.collaborators).should.equal true
|
|
|
|
|
2015-10-14 12:29:58 -04:00
|
|
|
describe "canAddXCollaborators", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
describe "when the project has fewer collaborators than allowed", ->
|
|
|
|
beforeEach ->
|
|
|
|
@current_number = 1
|
|
|
|
@allowed_number = 2
|
2016-08-02 10:42:50 -04:00
|
|
|
@invite_count = 0
|
2018-02-26 09:21:14 -05:00
|
|
|
@CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
|
|
|
|
@CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
|
|
|
|
sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
|
|
|
|
callback(null, @allowed_number)
|
|
|
|
@callback = sinon.stub()
|
2016-08-02 10:42:50 -04:00
|
|
|
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
|
|
|
|
|
|
|
|
it "should return true", ->
|
|
|
|
@callback.calledWith(null, true).should.equal true
|
|
|
|
|
|
|
|
describe "when the project has fewer collaborators and invites than allowed", ->
|
|
|
|
beforeEach ->
|
|
|
|
@current_number = 1
|
|
|
|
@allowed_number = 4
|
|
|
|
@invite_count = 1
|
2018-02-26 09:21:14 -05:00
|
|
|
@CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
|
|
|
|
@CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
|
|
|
|
sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
|
|
|
|
callback(null, @allowed_number)
|
|
|
|
@callback = sinon.stub()
|
2015-10-14 12:29:58 -04:00
|
|
|
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
|
|
|
|
|
|
|
|
it "should return true", ->
|
|
|
|
@callback.calledWith(null, true).should.equal true
|
|
|
|
|
|
|
|
describe "when the project has fewer collaborators than allowed but I want to add more than allowed", ->
|
|
|
|
beforeEach ->
|
|
|
|
@current_number = 1
|
|
|
|
@allowed_number = 2
|
2016-08-02 10:42:50 -04:00
|
|
|
@invite_count = 0
|
2018-02-26 09:21:14 -05:00
|
|
|
@CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
|
|
|
|
@CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
|
|
|
|
sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
|
|
|
|
callback(null, @allowed_number)
|
|
|
|
@callback = sinon.stub()
|
2015-10-14 12:29:58 -04:00
|
|
|
@LimitationsManager.canAddXCollaborators(@project_id, 2, @callback)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it "should return false", ->
|
|
|
|
@callback.calledWith(null, false).should.equal true
|
|
|
|
|
|
|
|
describe "when the project has more collaborators than allowed", ->
|
|
|
|
beforeEach ->
|
|
|
|
@current_number = 3
|
|
|
|
@allowed_number = 2
|
2016-08-02 10:42:50 -04:00
|
|
|
@invite_count = 0
|
2018-02-26 09:21:14 -05:00
|
|
|
@CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
|
|
|
|
@CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
|
|
|
|
sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
|
|
|
|
callback(null, @allowed_number)
|
|
|
|
@callback = sinon.stub()
|
2015-10-14 12:29:58 -04:00
|
|
|
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-10-14 12:29:58 -04:00
|
|
|
it "should return false", ->
|
|
|
|
@callback.calledWith(null, false).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "when the project has infinite collaborators", ->
|
|
|
|
beforeEach ->
|
|
|
|
@current_number = 100
|
|
|
|
@allowed_number = -1
|
2016-08-02 10:42:50 -04:00
|
|
|
@invite_count = 0
|
2018-02-26 09:21:14 -05:00
|
|
|
@CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
|
|
|
|
@CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
|
|
|
|
sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
|
|
|
|
callback(null, @allowed_number)
|
|
|
|
@callback = sinon.stub()
|
2015-10-14 12:29:58 -04:00
|
|
|
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-10-14 12:29:58 -04:00
|
|
|
it "should return true", ->
|
|
|
|
@callback.calledWith(null, true).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-08-02 10:42:50 -04:00
|
|
|
describe 'when the project has more invites than allowed', ->
|
|
|
|
beforeEach ->
|
|
|
|
@current_number = 0
|
|
|
|
@allowed_number = 2
|
|
|
|
@invite_count = 2
|
2018-02-26 09:21:14 -05:00
|
|
|
@CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
|
|
|
|
@CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
|
|
|
|
sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
|
|
|
|
callback(null, @allowed_number)
|
|
|
|
@callback = sinon.stub()
|
2016-08-02 10:42:50 -04:00
|
|
|
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
|
|
|
|
|
|
|
|
it "should return false", ->
|
|
|
|
@callback.calledWith(null, false).should.equal true
|
|
|
|
|
|
|
|
describe 'when the project has more invites and collaborators than allowed', ->
|
|
|
|
beforeEach ->
|
|
|
|
@current_number = 1
|
|
|
|
@allowed_number = 2
|
|
|
|
@invite_count = 1
|
2018-02-26 09:21:14 -05:00
|
|
|
@CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
|
|
|
|
@CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
|
|
|
|
sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
|
|
|
|
callback(null, @allowed_number)
|
|
|
|
@callback = sinon.stub()
|
2016-08-02 10:42:50 -04:00
|
|
|
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
|
|
|
|
|
|
|
|
it "should return false", ->
|
|
|
|
@callback.calledWith(null, false).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "userHasSubscription", ->
|
|
|
|
beforeEach ->
|
|
|
|
@SubscriptionLocator.getUsersSubscription = sinon.stub()
|
|
|
|
|
|
|
|
it "should return true if the recurly token is set", (done)->
|
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, recurlySubscription_id : "1234")
|
|
|
|
@LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
|
|
|
|
hasSubscription.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return false if the recurly token is not set", (done)->
|
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
|
|
|
|
@subscription = {}
|
|
|
|
@LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
|
|
|
|
hasSubscription.should.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return false if the subscription is undefined", (done)->
|
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1)
|
|
|
|
@LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
|
|
|
|
hasSubscription.should.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the subscription", (done)->
|
|
|
|
stubbedSubscription = {freeTrial:{}, token:""}
|
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, stubbedSubscription)
|
2014-08-07 10:29:06 -04:00
|
|
|
@LimitationsManager.userHasSubscription @user, (err, hasSubOrIsGroupMember, subscription)->
|
2014-02-12 05:23:40 -05:00
|
|
|
subscription.should.deep.equal stubbedSubscription
|
|
|
|
done()
|
2016-07-19 05:15:45 -04:00
|
|
|
|
|
|
|
describe "when user has a custom account", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@fakeSubscription = {customAccount: true}
|
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @fakeSubscription)
|
|
|
|
|
|
|
|
it 'should return true', (done) ->
|
|
|
|
@LimitationsManager.userHasSubscription @user, (err, hasSubscription, subscription)->
|
|
|
|
hasSubscription.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return the subscription', (done) ->
|
|
|
|
@LimitationsManager.userHasSubscription @user, (err, hasSubscription, subscription)=>
|
|
|
|
subscription.should.deep.equal @fakeSubscription
|
|
|
|
done()
|
|
|
|
|
2014-08-07 10:29:06 -04:00
|
|
|
describe "userIsMemberOfGroupSubscription", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2014-08-07 10:29:06 -04:00
|
|
|
@SubscriptionLocator.getMemberSubscriptions = sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-08-07 10:29:06 -04:00
|
|
|
it "should return false if there are no groups subcriptions", (done)->
|
|
|
|
@SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, [])
|
|
|
|
@LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember)->
|
|
|
|
isMember.should.equal false
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
2014-08-07 10:29:06 -04:00
|
|
|
it "should return true if there are no groups subcriptions", (done)->
|
|
|
|
@SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, subscriptions = ["mock-subscription"])
|
|
|
|
@LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember, retSubscriptions)->
|
|
|
|
isMember.should.equal true
|
|
|
|
retSubscriptions.should.equal subscriptions
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
2014-08-07 10:29:06 -04:00
|
|
|
describe "userHasSubscriptionOrIsGroupMember", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2014-08-07 10:29:06 -04:00
|
|
|
@LimitationsManager.userIsMemberOfGroupSubscription = sinon.stub()
|
|
|
|
@LimitationsManager.userHasSubscription = sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it "should return true if both are true", (done)->
|
2014-08-07 10:29:06 -04:00
|
|
|
@LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, true)
|
|
|
|
@LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
|
|
|
|
@LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
|
|
|
|
hasSubOrIsGroupMember.should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return true if one is true", (done)->
|
2014-08-07 10:29:06 -04:00
|
|
|
@LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, true)
|
|
|
|
@LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
|
|
|
|
@LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
|
|
|
|
hasSubOrIsGroupMember.should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return true if other is true", (done)->
|
2014-08-07 10:29:06 -04:00
|
|
|
@LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, false)
|
|
|
|
@LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
|
|
|
|
@LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
|
|
|
|
hasSubOrIsGroupMember.should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return false if both are false", (done)->
|
2014-08-07 10:29:06 -04:00
|
|
|
@LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, false)
|
|
|
|
@LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
|
|
|
|
@LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
|
|
|
|
hasSubOrIsGroupMember.should.equal false
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
2016-08-02 10:42:50 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe "hasGroupMembersLimitReached", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@user_id = "12312"
|
|
|
|
@subscription =
|
2017-06-08 07:12:08 -04:00
|
|
|
membersLimit: 3
|
2014-02-12 05:23:40 -05:00
|
|
|
member_ids: ["", ""]
|
2017-06-08 07:12:08 -04:00
|
|
|
invited_emails: ["bob@example.com"]
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
it "should return true if the limit is hit (including members and invites)", (done)->
|
2014-02-12 05:23:40 -05:00
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
|
|
|
|
@LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
|
|
|
|
limitReached.should.equal true
|
|
|
|
done()
|
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
it "should return false if the limit is not hit (including members and invites)", (done)->
|
|
|
|
@subscription.membersLimit = 4
|
2014-02-12 05:23:40 -05:00
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
|
|
|
|
@LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
|
|
|
|
limitReached.should.equal false
|
|
|
|
done()
|
2016-08-02 10:42:50 -04:00
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
it "should return true if the limit has been exceded (including members and invites)", (done)->
|
|
|
|
@subscription.membersLimit = 2
|
2014-02-12 05:23:40 -05:00
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
|
|
|
|
@LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
|
|
|
|
limitReached.should.equal true
|
2016-08-02 10:42:50 -04:00
|
|
|
done()
|