2014-02-12 05:23:40 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
should = require('chai').should()
|
|
|
|
sinon = require 'sinon'
|
|
|
|
assert = require("chai").assert
|
|
|
|
modulePath = "../../../../app/js/Features/Subscription/SubscriptionGroupHandler"
|
|
|
|
|
|
|
|
|
2015-06-01 07:43:42 -04:00
|
|
|
describe "SubscriptionGroupHandler", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@adminUser_id = "12321"
|
|
|
|
@newEmail = "bob@smith.com"
|
2015-05-27 11:33:47 -04:00
|
|
|
@user_id = "3121321"
|
2017-06-08 07:12:08 -04:00
|
|
|
@email = "jim@example.com"
|
2015-05-27 11:33:47 -04:00
|
|
|
@user = {_id:@user_id, email:@newEmail}
|
2016-02-18 06:43:43 -05:00
|
|
|
@subscription_id = "31DSd1123D"
|
|
|
|
|
|
|
|
@subscription =
|
2018-06-29 12:01:34 -04:00
|
|
|
admin_id: @adminUser_id
|
|
|
|
manager_ids: [@adminUser_id]
|
2016-02-18 06:43:43 -05:00
|
|
|
_id:@subscription_id
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-05-29 10:35:46 -04:00
|
|
|
@SubscriptionLocator =
|
2014-02-12 05:23:40 -05:00
|
|
|
getUsersSubscription: sinon.stub()
|
2015-05-27 11:33:47 -04:00
|
|
|
getSubscriptionByMemberIdAndId: sinon.stub()
|
2015-05-27 15:50:16 -04:00
|
|
|
getSubscription: sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-05-29 10:35:46 -04:00
|
|
|
@UserCreator =
|
2014-02-12 05:23:40 -05:00
|
|
|
getUserOrCreateHoldingAccount: sinon.stub().callsArgWith(1, null, @user)
|
|
|
|
|
|
|
|
@SubscriptionUpdater =
|
|
|
|
addUserToGroup: sinon.stub().callsArgWith(2)
|
|
|
|
removeUserFromGroup: sinon.stub().callsArgWith(2)
|
2018-06-01 06:23:25 -04:00
|
|
|
|
|
|
|
@TeamInvitesHandler =
|
2018-06-07 10:35:18 -04:00
|
|
|
createInvite: sinon.stub().callsArgWith(2)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-05-23 10:12:23 -04:00
|
|
|
@UserGetter =
|
2018-05-24 09:55:12 -04:00
|
|
|
getUser: sinon.stub()
|
2018-05-28 10:09:22 -04:00
|
|
|
getUserByAnyEmail: sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
@LimitationsManager =
|
|
|
|
hasGroupMembersLimitReached: sinon.stub()
|
|
|
|
|
2015-05-27 15:50:16 -04:00
|
|
|
@OneTimeTokenHandler =
|
|
|
|
getValueFromTokenAndExpire:sinon.stub()
|
|
|
|
getNewToken:sinon.stub()
|
|
|
|
|
|
|
|
@EmailHandler =
|
|
|
|
sendEmail:sinon.stub()
|
|
|
|
|
2018-05-29 10:35:46 -04:00
|
|
|
@Subscription =
|
|
|
|
update: sinon.stub().yields()
|
|
|
|
|
|
|
|
@settings =
|
2015-05-27 15:50:16 -04:00
|
|
|
siteUrl:"http://www.sharelatex.com"
|
|
|
|
|
2016-02-18 06:43:43 -05:00
|
|
|
@readStub = sinon.stub()
|
|
|
|
@NotificationsBuilder =
|
|
|
|
groupPlan: sinon.stub().returns({read:@readStub})
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
@Handler = SandboxedModule.require modulePath, requires:
|
|
|
|
"logger-sharelatex": log:->
|
|
|
|
"../User/UserCreator": @UserCreator
|
|
|
|
"./SubscriptionUpdater": @SubscriptionUpdater
|
2018-06-01 06:23:25 -04:00
|
|
|
"./TeamInvitesHandler": @TeamInvitesHandler
|
2014-02-12 05:23:40 -05:00
|
|
|
"./SubscriptionLocator": @SubscriptionLocator
|
2018-05-29 10:35:46 -04:00
|
|
|
"../../models/Subscription": Subscription: @Subscription
|
2018-05-23 10:12:23 -04:00
|
|
|
"../User/UserGetter": @UserGetter
|
2014-02-12 05:23:40 -05:00
|
|
|
"./LimitationsManager": @LimitationsManager
|
2015-05-27 15:50:16 -04:00
|
|
|
"../Security/OneTimeTokenHandler":@OneTimeTokenHandler
|
|
|
|
"../Email/EmailHandler":@EmailHandler
|
|
|
|
"settings-sharelatex":@settings
|
2016-02-18 06:43:43 -05:00
|
|
|
"../Notifications/NotificationsBuilder": @NotificationsBuilder
|
2018-05-29 10:35:46 -04:00
|
|
|
"logger-sharelatex":
|
2015-05-27 15:50:16 -04:00
|
|
|
err:->
|
|
|
|
log:->
|
2016-02-22 11:50:41 -05:00
|
|
|
warn:->
|
2015-05-27 15:50:16 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "addUserToGroup", ->
|
2017-06-08 07:12:08 -04:00
|
|
|
beforeEach ->
|
2016-02-22 11:50:41 -05:00
|
|
|
@LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, false, @subscription)
|
2018-05-28 10:09:22 -04:00
|
|
|
@UserGetter.getUserByAnyEmail.callsArgWith(1, null, @user)
|
2018-07-11 04:31:57 -04:00
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
it "should find the user", (done)->
|
2016-02-22 11:50:41 -05:00
|
|
|
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
|
2018-05-28 10:09:22 -04:00
|
|
|
@UserGetter.getUserByAnyEmail.calledWith(@newEmail).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should add the user to the group", (done)->
|
2018-07-16 09:40:53 -04:00
|
|
|
@Handler.addUserToGroup @subscription_id, @newEmail, (err)=>
|
|
|
|
@SubscriptionUpdater.addUserToGroup.calledWith(@subscription_id, @user._id).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
2018-05-29 10:35:46 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
it "should not add the user to the group if the limit has been reached", (done)->
|
2016-02-22 11:50:41 -05:00
|
|
|
@LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, true, @subscription)
|
|
|
|
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
@SubscriptionUpdater.addUserToGroup.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return error that limit has been reached", (done)->
|
2016-02-22 11:50:41 -05:00
|
|
|
@LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, true, @subscription)
|
|
|
|
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
err.limitReached.should.equal true
|
|
|
|
done()
|
|
|
|
|
2016-02-18 06:43:43 -05:00
|
|
|
it "should mark any notification as read if it is part of a licence", (done)->
|
2016-02-22 11:50:41 -05:00
|
|
|
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
|
2016-02-18 06:43:43 -05:00
|
|
|
@NotificationsBuilder.groupPlan.calledWith(@user, {subscription_id:@subscription._id}).should.equal true
|
|
|
|
@readStub.called.should.equal true
|
|
|
|
done()
|
2018-05-29 10:35:46 -04:00
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
it "should add an email invite if no user is found", (done) ->
|
2018-05-28 10:09:22 -04:00
|
|
|
@UserGetter.getUserByAnyEmail.callsArgWith(1, null, null)
|
2017-06-08 07:12:08 -04:00
|
|
|
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
|
2018-06-07 10:35:18 -04:00
|
|
|
@TeamInvitesHandler.createInvite.calledWith(@adminUser_id, @newEmail).should.equal true
|
2017-06-08 07:12:08 -04:00
|
|
|
done()
|
2016-02-18 06:43:43 -05:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe "removeUserFromGroup", ->
|
|
|
|
|
|
|
|
it "should call the subscription updater to remove the user", (done)->
|
|
|
|
@Handler.removeUserFromGroup @adminUser_id, @user._id, (err)=>
|
|
|
|
@SubscriptionUpdater.removeUserFromGroup.calledWith(@adminUser_id, @user._id).should.equal true
|
|
|
|
done()
|
|
|
|
|
2018-05-29 10:35:46 -04:00
|
|
|
describe "replaceUserReferencesInGroups", ->
|
2018-06-29 12:01:34 -04:00
|
|
|
beforeEach (done)->
|
2018-05-29 10:35:46 -04:00
|
|
|
@oldId = "ba5eba11"
|
|
|
|
@newId = "5ca1ab1e"
|
2018-06-29 12:01:34 -04:00
|
|
|
@Handler.replaceUserReferencesInGroups @oldId, @newId, ->
|
|
|
|
done()
|
2018-05-29 10:35:46 -04:00
|
|
|
|
2018-06-29 12:01:34 -04:00
|
|
|
it "replaces the admin_id", ->
|
2018-05-29 10:35:46 -04:00
|
|
|
@Subscription.update.calledWith(
|
|
|
|
{ admin_id: @oldId },
|
|
|
|
{ admin_id: @newId }
|
|
|
|
).should.equal true
|
|
|
|
|
2018-06-29 12:01:34 -04:00
|
|
|
it "replaces the manager_ids", ->
|
2018-05-29 10:35:46 -04:00
|
|
|
@Subscription.update.calledWith(
|
2018-06-29 12:01:34 -04:00
|
|
|
{manager_ids:"ba5eba11"},{$addToSet:{manager_ids:"5ca1ab1e"}},{multi:true}
|
2018-05-29 10:35:46 -04:00
|
|
|
).should.equal true
|
|
|
|
|
|
|
|
@Subscription.update.calledWith(
|
2018-06-29 12:01:34 -04:00
|
|
|
{manager_ids:"ba5eba11"},{$pull:{manager_ids:"ba5eba11"}},{multi:true}
|
2018-05-29 10:35:46 -04:00
|
|
|
).should.equal true
|
|
|
|
|
2018-06-29 12:01:34 -04:00
|
|
|
it "replaces the member ids", ->
|
|
|
|
@Subscription.update.calledWith(
|
|
|
|
{ member_ids: @oldId },
|
|
|
|
{ $addToSet: { member_ids: @newId } }
|
|
|
|
).should.equal true
|
|
|
|
|
|
|
|
@Subscription.update.calledWith(
|
|
|
|
{ member_ids: @oldId },
|
|
|
|
{ $pull: { member_ids: @oldId } }
|
|
|
|
).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "getPopulatedListOfMembers", ->
|
|
|
|
beforeEach ->
|
|
|
|
@subscription = {}
|
2018-07-11 04:31:57 -04:00
|
|
|
@SubscriptionLocator.getSubscription.callsArgWith(1, null, @subscription)
|
2018-05-24 09:55:12 -04:00
|
|
|
@UserGetter.getUser.callsArgWith(1, null, {_id:"31232"})
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it "should locate the subscription", (done)->
|
2018-05-24 09:55:12 -04:00
|
|
|
@UserGetter.getUser.callsArgWith(1, null, {_id:"31232"})
|
2018-07-11 04:31:57 -04:00
|
|
|
@Handler.getPopulatedListOfMembers @subscriptionId, (err, users)=>
|
|
|
|
@SubscriptionLocator.getSubscription.calledWith(@subscriptionId).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should get the users by id", (done)->
|
2018-05-24 09:55:12 -04:00
|
|
|
@UserGetter.getUser.callsArgWith(1, null, {_id:"31232"})
|
2014-02-12 05:23:40 -05:00
|
|
|
@subscription.member_ids = ["1234", "342432", "312312"]
|
|
|
|
@Handler.getPopulatedListOfMembers @adminUser_id, (err, users)=>
|
2018-05-24 09:55:12 -04:00
|
|
|
@UserGetter.getUser.calledWith(@subscription.member_ids[0]).should.equal true
|
|
|
|
@UserGetter.getUser.calledWith(@subscription.member_ids[1]).should.equal true
|
|
|
|
@UserGetter.getUser.calledWith(@subscription.member_ids[2]).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
users.length.should.equal @subscription.member_ids.length
|
|
|
|
done()
|
2014-03-25 12:19:30 -04:00
|
|
|
|
|
|
|
it "should just return the id if the user can not be found as they may have deleted their account", (done)->
|
2018-05-24 09:55:12 -04:00
|
|
|
@UserGetter.getUser.callsArgWith(1)
|
2014-03-25 12:19:30 -04:00
|
|
|
@subscription.member_ids = ["1234", "342432", "312312"]
|
|
|
|
@Handler.getPopulatedListOfMembers @adminUser_id, (err, users)=>
|
|
|
|
assert.deepEqual users[0], {_id:@subscription.member_ids[0]}
|
|
|
|
assert.deepEqual users[1], {_id:@subscription.member_ids[1]}
|
|
|
|
assert.deepEqual users[2], {_id:@subscription.member_ids[2]}
|
|
|
|
done()
|
2018-05-29 10:35:46 -04:00
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
it "should return any invited users", (done) ->
|
2018-06-08 05:05:26 -04:00
|
|
|
@subscription.invited_emails = [ "jo@example.com" ]
|
|
|
|
|
2018-06-01 06:23:25 -04:00
|
|
|
@subscription.teamInvites = [
|
|
|
|
{ email: "charlie@example.com" }
|
|
|
|
]
|
2018-06-08 05:05:26 -04:00
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
@Handler.getPopulatedListOfMembers @adminUser_id, (err, users)=>
|
|
|
|
users[0].email.should.equal "jo@example.com"
|
2018-06-08 05:05:26 -04:00
|
|
|
users[0].invite.should.equal true
|
2017-06-08 07:12:08 -04:00
|
|
|
users[1].email.should.equal "charlie@example.com"
|
2018-06-08 05:05:26 -04:00
|
|
|
users[1].invite.should.equal true
|
|
|
|
users.length.should.equal @subscription.teamInvites.length + @subscription.invited_emails.length
|
2017-06-08 07:12:08 -04:00
|
|
|
done()
|
2014-03-25 12:19:30 -04:00
|
|
|
|
2015-05-27 11:33:47 -04:00
|
|
|
describe "isUserPartOfGroup", ->
|
|
|
|
beforeEach ->
|
|
|
|
@subscription_id = "123ed13123"
|
|
|
|
|
|
|
|
it "should return true when user is part of subscription", (done)->
|
|
|
|
@SubscriptionLocator.getSubscriptionByMemberIdAndId.callsArgWith(2, null, {_id:@subscription_id})
|
|
|
|
@Handler.isUserPartOfGroup @user_id, @subscription_id, (err, partOfGroup)->
|
|
|
|
partOfGroup.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return false when no subscription is found", (done)->
|
|
|
|
@SubscriptionLocator.getSubscriptionByMemberIdAndId.callsArgWith(2, null)
|
|
|
|
@Handler.isUserPartOfGroup @user_id, @subscription_id, (err, partOfGroup)->
|
|
|
|
partOfGroup.should.equal false
|
|
|
|
done()
|