overleaf/services/web/test/unit/coffee/Subscription/SubscriptionGroupHandlerTests.coffee

217 lines
7.5 KiB
CoffeeScript
Raw Normal View History

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"
describe "SubscriptionGroupHandler", ->
2014-02-12 05:23:40 -05:00
beforeEach ->
@adminUser_id = "12321"
@newEmail = "bob@smith.com"
@user_id = "3121321"
@email = "jim@example.com"
@user = {_id:@user_id, email:@newEmail}
@subscription_id = "31DSd1123D"
@subscription =
admin_id:@adminUser_id
_id:@subscription_id
2014-02-12 05:23:40 -05:00
@SubscriptionLocator =
2014-02-12 05:23:40 -05:00
getUsersSubscription: sinon.stub()
getSubscriptionByMemberIdAndId: sinon.stub()
getSubscription: sinon.stub()
2014-02-12 05:23:40 -05: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
@UserGetter =
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()
@OneTimeTokenHandler =
getValueFromTokenAndExpire:sinon.stub()
getNewToken:sinon.stub()
@EmailHandler =
sendEmail:sinon.stub()
@Subscription =
update: sinon.stub().yields()
@settings =
siteUrl:"http://www.sharelatex.com"
@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
"../../models/Subscription": Subscription: @Subscription
"../User/UserGetter": @UserGetter
2014-02-12 05:23:40 -05:00
"./LimitationsManager": @LimitationsManager
"../Security/OneTimeTokenHandler":@OneTimeTokenHandler
"../Email/EmailHandler":@EmailHandler
"settings-sharelatex":@settings
"../Notifications/NotificationsBuilder": @NotificationsBuilder
"logger-sharelatex":
err:->
log:->
warn:->
2014-02-12 05:23:40 -05:00
describe "addUserToGroup", ->
beforeEach ->
@LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, false, @subscription)
2018-05-28 10:09:22 -04:00
@UserGetter.getUserByAnyEmail.callsArgWith(1, null, @user)
it "should find the user", (done)->
@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)->
@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()
2014-02-12 05:23:40 -05:00
it "should not add the user to the group if the limit has been reached", (done)->
@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)->
@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()
it "should mark any notification as read if it is part of a licence", (done)->
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
@NotificationsBuilder.groupPlan.calledWith(@user, {subscription_id:@subscription._id}).should.equal true
@readStub.called.should.equal true
done()
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)
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
2018-06-07 10:35:18 -04:00
@TeamInvitesHandler.createInvite.calledWith(@adminUser_id, @newEmail).should.equal true
done()
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()
describe "replaceUserReferencesInGroups", ->
beforeEach ->
@oldId = "ba5eba11"
@newId = "5ca1ab1e"
it "replaces the admin_id", (done) ->
@Handler.replaceUserReferencesInGroups @oldId, @newId, (err) =>
@Subscription.update.calledWith(
{ admin_id: @oldId },
{ admin_id: @newId }
).should.equal true
done()
it "replaces the member ids", (done) ->
@Handler.replaceUserReferencesInGroups @oldId, @newId, (err) =>
@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
done()
2014-02-12 05:23:40 -05:00
describe "getPopulatedListOfMembers", ->
beforeEach ->
@subscription = {}
@SubscriptionLocator.getSubscription.callsArgWith(1, null, @subscription)
@UserGetter.getUser.callsArgWith(1, null, {_id:"31232"})
2014-02-12 05:23:40 -05:00
it "should locate the subscription", (done)->
@UserGetter.getUser.callsArgWith(1, null, {_id:"31232"})
@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)->
@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)=>
@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()
it "should just return the id if the user can not be found as they may have deleted their account", (done)->
@UserGetter.getUser.callsArgWith(1)
@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()
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
@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
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
done()
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()