From 3a3688d3d064c50d83e5caf9b7a9156dc989d5f7 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Tue, 2 Aug 2016 15:42:50 +0100 Subject: [PATCH] Include invites count in `canAddXCollaborators` --- .../CollaboratorsInviteHandler.coffee | 8 ++++ .../Subscription/LimitationsManager.coffee | 14 ++++--- .../LimitationsManagerTests.coffee | 41 +++++++++++++++++-- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee index 57d9169b8f..25008cfde7 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee @@ -17,6 +17,14 @@ module.exports = CollaboratorsInviteHandler = return callback(err) callback(null, invites) + getInviteCount: (projectId, callback=(err, count)->) -> + logger.log {projectId}, "counting invites for project" + ProjectInvite.count {projectId: projectId}, (err, count) -> + if err? + logger.err {err, projectId}, "error getting invites from mongo" + return callback(err) + callback(null, count) + inviteToProject: (projectId, sendingUserId, email, privileges, callback=(err,invite)->) -> logger.log {projectId, sendingUserId, email, privileges}, "adding invite" Crypto.randomBytes 24, (err, buffer) -> diff --git a/services/web/app/coffee/Features/Subscription/LimitationsManager.coffee b/services/web/app/coffee/Features/Subscription/LimitationsManager.coffee index 6c402220a9..59a0748f36 100644 --- a/services/web/app/coffee/Features/Subscription/LimitationsManager.coffee +++ b/services/web/app/coffee/Features/Subscription/LimitationsManager.coffee @@ -4,6 +4,7 @@ User = require("../../models/User").User SubscriptionLocator = require("./SubscriptionLocator") Settings = require("settings-sharelatex") CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler") +CollaboratorsInvitesHandler = require("../Collaborators/CollaboratorsInviteHandler") module.exports = @@ -20,10 +21,12 @@ module.exports = return callback(error) if error? CollaboratorsHandler.getCollaboratorCount project_id, (error, current_number) => return callback(error) if error? - if current_number + x_collaborators <= allowed_number or allowed_number < 0 - callback null, true - else - callback null, false + CollaboratorsInvitesHandler.getInviteCount project_id, (error, invite_count) => + return callback(error) if error? + if current_number + invite_count + x_collaborators <= allowed_number or allowed_number < 0 + callback null, true + else + callback null, false userHasSubscriptionOrIsGroupMember: (user, callback = (err, hasSubscriptionOrIsMember)->) -> @userHasSubscription user, (err, hasSubscription, subscription)=> @@ -41,7 +44,7 @@ module.exports = hasValidSubscription = subscription? and (subscription.recurlySubscription_id? or subscription?.customAccount == true) logger.log user:user, hasValidSubscription:hasValidSubscription, subscription:subscription, "checking if user has subscription" callback err, hasValidSubscription, subscription - + userIsMemberOfGroupSubscription: (user, callback = (error, isMember, subscriptions) ->) -> logger.log user_id: user._id, "checking is user is member of subscription groups" SubscriptionLocator.getMemberSubscriptions user._id, (err, subscriptions = []) -> @@ -65,4 +68,3 @@ getOwnerOfProject = (project_id, callback)-> return callback(error) if error? User.findById project.owner_ref, (error, owner) -> callback(error, owner) - diff --git a/services/web/test/UnitTests/coffee/Subscription/LimitationsManagerTests.coffee b/services/web/test/UnitTests/coffee/Subscription/LimitationsManagerTests.coffee index 94a1edb5f2..93f00afad5 100644 --- a/services/web/test/UnitTests/coffee/Subscription/LimitationsManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Subscription/LimitationsManagerTests.coffee @@ -30,6 +30,7 @@ describe "LimitationsManager", -> './SubscriptionLocator':@SubscriptionLocator 'settings-sharelatex' : @Settings = {} "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {} + "../Collaborators/CollaboratorsInviteHandler": @CollaboratorsInviteHandler = {} 'logger-sharelatex':log:-> describe "allowedNumberOfCollaboratorsInProject", -> @@ -56,6 +57,7 @@ describe "LimitationsManager", -> describe "canAddXCollaborators", -> beforeEach -> @CollaboratorsHandler.getCollaboratorCount = (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) @@ -65,6 +67,17 @@ describe "LimitationsManager", -> beforeEach -> @current_number = 1 @allowed_number = 2 + @invite_count = 0 + @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 @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback) it "should return true", -> @@ -74,6 +87,7 @@ describe "LimitationsManager", -> beforeEach -> @current_number = 1 @allowed_number = 2 + @invite_count = 0 @LimitationsManager.canAddXCollaborators(@project_id, 2, @callback) it "should return false", -> @@ -83,6 +97,7 @@ describe "LimitationsManager", -> beforeEach -> @current_number = 3 @allowed_number = 2 + @invite_count = 0 @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback) it "should return false", -> @@ -92,11 +107,31 @@ describe "LimitationsManager", -> beforeEach -> @current_number = 100 @allowed_number = -1 + @invite_count = 0 @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback) it "should return true", -> @callback.calledWith(null, true).should.equal true + describe 'when the project has more invites than allowed', -> + beforeEach -> + @current_number = 0 + @allowed_number = 2 + @invite_count = 2 + @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 + @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback) + + it "should return false", -> + @callback.calledWith(null, false).should.equal true describe "userHasSubscription", -> beforeEach -> @@ -193,7 +228,7 @@ describe "LimitationsManager", -> @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)-> hasSubOrIsGroupMember.should.equal false done() - + describe "hasGroupMembersLimitReached", -> beforeEach -> @@ -214,10 +249,10 @@ describe "LimitationsManager", -> @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)-> limitReached.should.equal false done() - + it "should return true if the limit has been exceded", (done)-> @subscription.membersLimit = 0 @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription) @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)-> limitReached.should.equal true - done() \ No newline at end of file + done()