2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
no-dupe-keys,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const should = require('chai').should()
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const { assert } = require('chai')
|
|
|
|
const modulePath =
|
|
|
|
'../../../../app/src/Features/Subscription/SubscriptionGroupHandler'
|
|
|
|
|
|
|
|
describe('SubscriptionGroupHandler', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.adminUser_id = '12321'
|
|
|
|
this.newEmail = 'bob@smith.com'
|
|
|
|
this.user_id = '3121321'
|
|
|
|
this.email = 'jim@example.com'
|
|
|
|
this.user = { _id: this.user_id, email: this.newEmail }
|
|
|
|
this.subscription_id = '31DSd1123D'
|
|
|
|
|
|
|
|
this.subscription = {
|
|
|
|
admin_id: this.adminUser_id,
|
|
|
|
manager_ids: [this.adminUser_id],
|
|
|
|
_id: this.subscription_id
|
|
|
|
}
|
|
|
|
|
|
|
|
this.SubscriptionLocator = {
|
|
|
|
getUsersSubscription: sinon.stub(),
|
|
|
|
getSubscriptionByMemberIdAndId: sinon.stub(),
|
|
|
|
getSubscription: sinon.stub().callsArgWith(1, null, this.subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.UserCreator = {
|
|
|
|
getUserOrCreateHoldingAccount: sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.user)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.SubscriptionUpdater = {
|
|
|
|
removeUserFromGroup: sinon.stub().callsArgWith(2),
|
|
|
|
getSubscription: sinon.stub().callsArgWith(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.TeamInvitesHandler = { createInvite: sinon.stub().callsArgWith(2) }
|
|
|
|
|
|
|
|
this.UserGetter = {
|
|
|
|
getUser: sinon.stub(),
|
|
|
|
getUserByAnyEmail: sinon.stub()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.LimitationsManager = { hasGroupMembersLimitReached: sinon.stub() }
|
|
|
|
|
|
|
|
this.OneTimeTokenHandler = {
|
|
|
|
getValueFromTokenAndExpire: sinon.stub(),
|
|
|
|
getNewToken: sinon.stub()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.EmailHandler = { sendEmail: sinon.stub() }
|
|
|
|
|
|
|
|
this.Subscription = {
|
2020-10-12 08:35:50 -04:00
|
|
|
update: sinon.stub().yields(),
|
2019-05-29 05:21:06 -04:00
|
|
|
findOne: sinon.stub().yields()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.settings = { siteUrl: 'http://www.sharelatex.com' }
|
|
|
|
|
|
|
|
this.readStub = sinon.stub()
|
|
|
|
this.NotificationsBuilder = {
|
|
|
|
groupPlan: sinon.stub().returns({ read: this.readStub })
|
|
|
|
}
|
|
|
|
|
|
|
|
this.UserMembershipViewModel = {
|
|
|
|
build(email) {
|
|
|
|
return { email }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (this.Handler = SandboxedModule.require(modulePath, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'logger-sharelatex': {
|
|
|
|
log() {}
|
|
|
|
},
|
|
|
|
'../User/UserCreator': this.UserCreator,
|
|
|
|
'./SubscriptionUpdater': this.SubscriptionUpdater,
|
|
|
|
'./SubscriptionLocator': this.SubscriptionLocator,
|
|
|
|
'../../models/Subscription': {
|
|
|
|
Subscription: this.Subscription
|
|
|
|
},
|
|
|
|
'../User/UserGetter': this.UserGetter,
|
|
|
|
'./LimitationsManager': this.LimitationsManager,
|
|
|
|
'../Security/OneTimeTokenHandler': this.OneTimeTokenHandler,
|
|
|
|
'../Email/EmailHandler': this.EmailHandler,
|
|
|
|
'settings-sharelatex': this.settings,
|
|
|
|
'../Notifications/NotificationsBuilder': this.NotificationsBuilder,
|
|
|
|
'../UserMembership/UserMembershipViewModel': this
|
|
|
|
.UserMembershipViewModel,
|
|
|
|
'logger-sharelatex': {
|
|
|
|
err() {},
|
|
|
|
log() {},
|
|
|
|
warn() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('removeUserFromGroup', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
it('should call the subscription updater to remove the user', function(done) {
|
|
|
|
return this.Handler.removeUserFromGroup(
|
|
|
|
this.adminUser_id,
|
|
|
|
this.user._id,
|
|
|
|
err => {
|
|
|
|
this.SubscriptionUpdater.removeUserFromGroup
|
|
|
|
.calledWith(this.adminUser_id, this.user._id)
|
|
|
|
.should.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
describe('replaceUserReferencesInGroups', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
this.oldId = 'ba5eba11'
|
|
|
|
this.newId = '5ca1ab1e'
|
|
|
|
return this.Handler.replaceUserReferencesInGroups(
|
|
|
|
this.oldId,
|
|
|
|
this.newId,
|
|
|
|
() => done()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('replaces the admin_id', function() {
|
2020-10-12 08:35:50 -04:00
|
|
|
return this.Subscription.update
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith({ admin_id: this.oldId }, { admin_id: this.newId })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('replaces the manager_ids', function() {
|
2020-10-12 08:35:50 -04:00
|
|
|
this.Subscription.update
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
{ manager_ids: 'ba5eba11' },
|
2020-10-12 08:35:50 -04:00
|
|
|
{ $addToSet: { manager_ids: '5ca1ab1e' } },
|
|
|
|
{ multi: true }
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
|
2020-10-12 08:35:50 -04:00
|
|
|
return this.Subscription.update
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
{ manager_ids: 'ba5eba11' },
|
2020-10-12 08:35:50 -04:00
|
|
|
{ $pull: { manager_ids: 'ba5eba11' } },
|
|
|
|
{ multi: true }
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('replaces the member ids', function() {
|
2020-10-12 08:35:50 -04:00
|
|
|
this.Subscription.update
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
{ member_ids: this.oldId },
|
|
|
|
{ $addToSet: { member_ids: this.newId } }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
|
2020-10-12 08:35:50 -04:00
|
|
|
return this.Subscription.update
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
{ member_ids: this.oldId },
|
|
|
|
{ $pull: { member_ids: this.oldId } }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('isUserPartOfGroup', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return (this.subscription_id = '123ed13123')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return true when user is part of subscription', function(done) {
|
|
|
|
this.SubscriptionLocator.getSubscriptionByMemberIdAndId.callsArgWith(
|
|
|
|
2,
|
|
|
|
null,
|
|
|
|
{ _id: this.subscription_id }
|
|
|
|
)
|
|
|
|
return this.Handler.isUserPartOfGroup(
|
|
|
|
this.user_id,
|
|
|
|
this.subscription_id,
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, partOfGroup) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
partOfGroup.should.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should return false when no subscription is found', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.SubscriptionLocator.getSubscriptionByMemberIdAndId.callsArgWith(
|
|
|
|
2,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
return this.Handler.isUserPartOfGroup(
|
|
|
|
this.user_id,
|
|
|
|
this.subscription_id,
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, partOfGroup) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
partOfGroup.should.equal(false)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('getTotalConfirmedUsersInGroup', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
describe('for existing subscriptions', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return (this.subscription.member_ids = ['12321', '3121321'])
|
|
|
|
})
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should call the subscription locator and return 2 users', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.Handler.getTotalConfirmedUsersInGroup(
|
|
|
|
this.subscription_id,
|
|
|
|
(err, count) => {
|
|
|
|
this.SubscriptionLocator.getSubscription
|
|
|
|
.calledWith(this.subscription_id)
|
|
|
|
.should.equal(true)
|
|
|
|
count.should.equal(2)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('for nonexistent subscriptions', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
it('should return undefined', function(done) {
|
|
|
|
return this.Handler.getTotalConfirmedUsersInGroup(
|
|
|
|
'fake-id',
|
|
|
|
(err, count) => {
|
|
|
|
should.not.exist(count)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|