mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-11 01:57:23 +00:00
Unit test SubscriptionGroupHandler.replaceUserReferencesInGroups
This commit is contained in:
parent
259d690f7c
commit
b52fbdbfa4
2 changed files with 48 additions and 15 deletions
|
@ -2,6 +2,7 @@ async = require("async")
|
|||
_ = require("underscore")
|
||||
SubscriptionUpdater = require("./SubscriptionUpdater")
|
||||
SubscriptionLocator = require("./SubscriptionLocator")
|
||||
Subscription = require("../../models/Subscription").Subscription
|
||||
UserLocator = require("../User/UserLocator")
|
||||
LimitationsManager = require("./LimitationsManager")
|
||||
logger = require("logger-sharelatex")
|
||||
|
@ -45,14 +46,14 @@ module.exports = SubscriptionGroupHandler =
|
|||
|
||||
|
||||
replaceUserReferencesInGroups: (oldId, newId, callback) ->
|
||||
Subscription.update {admin_id: userStubId}, {admin_id: slUserId}, (error) ->
|
||||
Subscription.update {admin_id: oldId}, {admin_id: newId}, (error) ->
|
||||
callback(error) if error?
|
||||
|
||||
# Mongo won't let us pull and addToSet in the same query, so do it in
|
||||
# two. Note we need to add first, since the query is based on the old user.
|
||||
query = {member_ids: userStubId}
|
||||
addNewUserUpdate = $addToSet: {member_ids: slUserId}
|
||||
removeOldUserUpdate = $pull: {member_ids: userStubId}
|
||||
query = { member_ids: oldId }
|
||||
addNewUserUpdate = $addToSet: { member_ids: newId }
|
||||
removeOldUserUpdate = $pull: { member_ids: oldId }
|
||||
|
||||
Subscription.update query, addNewUserUpdate, { multi: true }, (error) ->
|
||||
return callback(error) if error?
|
||||
|
|
|
@ -19,12 +19,12 @@ describe "SubscriptionGroupHandler", ->
|
|||
admin_id:@adminUser_id
|
||||
_id:@subscription_id
|
||||
|
||||
@SubscriptionLocator =
|
||||
@SubscriptionLocator =
|
||||
getUsersSubscription: sinon.stub()
|
||||
getSubscriptionByMemberIdAndId: sinon.stub()
|
||||
getSubscription: sinon.stub()
|
||||
|
||||
@UserCreator =
|
||||
@UserCreator =
|
||||
getUserOrCreateHoldingAccount: sinon.stub().callsArgWith(1, null, @user)
|
||||
|
||||
@SubscriptionUpdater =
|
||||
|
@ -47,7 +47,10 @@ describe "SubscriptionGroupHandler", ->
|
|||
@EmailHandler =
|
||||
sendEmail:sinon.stub()
|
||||
|
||||
@settings =
|
||||
@Subscription =
|
||||
update: sinon.stub().yields()
|
||||
|
||||
@settings =
|
||||
siteUrl:"http://www.sharelatex.com"
|
||||
|
||||
@readStub = sinon.stub()
|
||||
|
@ -59,13 +62,14 @@ describe "SubscriptionGroupHandler", ->
|
|||
"../User/UserCreator": @UserCreator
|
||||
"./SubscriptionUpdater": @SubscriptionUpdater
|
||||
"./SubscriptionLocator": @SubscriptionLocator
|
||||
"../../models/Subscription": Subscription: @Subscription
|
||||
"../User/UserLocator": @UserLocator
|
||||
"./LimitationsManager": @LimitationsManager
|
||||
"../Security/OneTimeTokenHandler":@OneTimeTokenHandler
|
||||
"../Email/EmailHandler":@EmailHandler
|
||||
"settings-sharelatex":@settings
|
||||
"../Notifications/NotificationsBuilder": @NotificationsBuilder
|
||||
"logger-sharelatex":
|
||||
"logger-sharelatex":
|
||||
err:->
|
||||
log:->
|
||||
warn:->
|
||||
|
@ -75,7 +79,7 @@ describe "SubscriptionGroupHandler", ->
|
|||
beforeEach ->
|
||||
@LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, false, @subscription)
|
||||
@UserLocator.findByEmail.callsArgWith(1, null, @user)
|
||||
|
||||
|
||||
it "should find the user", (done)->
|
||||
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
|
||||
@UserLocator.findByEmail.calledWith(@newEmail).should.equal true
|
||||
|
@ -85,7 +89,7 @@ describe "SubscriptionGroupHandler", ->
|
|||
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
|
||||
@SubscriptionUpdater.addUserToGroup.calledWith(@adminUser_id, @user._id).should.equal true
|
||||
done()
|
||||
|
||||
|
||||
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)=>
|
||||
|
@ -103,7 +107,7 @@ describe "SubscriptionGroupHandler", ->
|
|||
@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) ->
|
||||
@UserLocator.findByEmail.callsArgWith(1, null, null)
|
||||
@Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
|
||||
|
@ -117,6 +121,35 @@ describe "SubscriptionGroupHandler", ->
|
|||
@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()
|
||||
|
||||
describe "getPopulatedListOfMembers", ->
|
||||
beforeEach ->
|
||||
|
@ -148,7 +181,7 @@ describe "SubscriptionGroupHandler", ->
|
|||
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) ->
|
||||
@subscription.invited_emails = ["jo@example.com", "charlie@example.com"]
|
||||
@Handler.getPopulatedListOfMembers @adminUser_id, (err, users)=>
|
||||
|
@ -219,7 +252,7 @@ describe "SubscriptionGroupHandler", ->
|
|||
@Handler.convertEmailInvitesToMemberships @email, @user_id, (err) =>
|
||||
@SubscriptionLocator.getGroupsWithEmailInvite.calledWith(@email).should.equal true
|
||||
done()
|
||||
|
||||
|
||||
it "should remove the email from each group", (done) ->
|
||||
@Handler.convertEmailInvitesToMemberships @email, @user_id, (err) =>
|
||||
for group in @groups
|
||||
|
@ -227,7 +260,7 @@ describe "SubscriptionGroupHandler", ->
|
|||
.calledWith(group.admin_id, @email)
|
||||
.should.equal true
|
||||
done()
|
||||
|
||||
|
||||
it "should add the user to each group", (done) ->
|
||||
@Handler.convertEmailInvitesToMemberships @email, @user_id, (err) =>
|
||||
for group in @groups
|
||||
|
@ -235,4 +268,3 @@ describe "SubscriptionGroupHandler", ->
|
|||
.calledWith(group.admin_id, @user_id)
|
||||
.should.equal true
|
||||
done()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue