2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
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 { expect } = require('chai')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const assertCalledWith = sinon.assert.calledWith
|
|
|
|
const assertNotCalled = sinon.assert.notCalled
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
const modulePath =
|
|
|
|
'../../../../app/src/Features/UserMembership/UserMembershipHandler'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
|
|
|
const EntityConfigs = require('../../../../app/src/Features/UserMembership/UserMembershipEntityConfigs')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserMembershipHandler', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user = { _id: ObjectId() }
|
|
|
|
this.newUser = { _id: ObjectId(), email: 'new-user-email@foo.bar' }
|
|
|
|
this.fakeEntityId = ObjectId()
|
|
|
|
this.subscription = {
|
|
|
|
_id: 'mock-subscription-id',
|
|
|
|
groupPlan: true,
|
|
|
|
membersLimit: 10,
|
|
|
|
member_ids: [ObjectId(), ObjectId()],
|
|
|
|
manager_ids: [ObjectId()],
|
|
|
|
invited_emails: ['mock-email-1@foo.com'],
|
|
|
|
teamInvites: [{ email: 'mock-email-1@bar.com' }],
|
2021-04-27 03:52:58 -04:00
|
|
|
update: sinon.stub().yields(null),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.institution = {
|
|
|
|
_id: 'mock-institution-id',
|
|
|
|
v1Id: 123,
|
|
|
|
managerIds: [ObjectId(), ObjectId(), ObjectId()],
|
2021-04-27 03:52:58 -04:00
|
|
|
updateOne: sinon.stub().yields(null),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.publisher = {
|
|
|
|
_id: 'mock-publisher-id',
|
|
|
|
slug: 'slug',
|
|
|
|
managerIds: [ObjectId(), ObjectId()],
|
2021-04-27 03:52:58 -04:00
|
|
|
updateOne: sinon.stub().yields(null),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.UserMembershipViewModel = {
|
|
|
|
buildAsync: sinon.stub().yields(null, { _id: 'mock-member-id' }),
|
2021-04-27 03:52:58 -04:00
|
|
|
build: sinon.stub().returns(this.newUser),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.UserGetter = {
|
2021-04-27 03:52:58 -04:00
|
|
|
getUserByAnyEmail: sinon.stub().yields(null, this.newUser),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.Institution = { findOne: sinon.stub().yields(null, this.institution) }
|
|
|
|
this.Subscription = {
|
2021-04-27 03:52:58 -04:00
|
|
|
findOne: sinon.stub().yields(null, this.subscription),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.Publisher = {
|
|
|
|
findOne: sinon.stub().yields(null, this.publisher),
|
2021-04-27 03:52:58 -04:00
|
|
|
create: sinon.stub().yields(null, this.publisher),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return (this.UserMembershipHandler = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-10-05 04:23:21 -04:00
|
|
|
mongodb: { ObjectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'./UserMembershipViewModel': this.UserMembershipViewModel,
|
|
|
|
'../User/UserGetter': this.UserGetter,
|
|
|
|
'../../models/Institution': {
|
2021-04-27 03:52:58 -04:00
|
|
|
Institution: this.Institution,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
'../../models/Subscription': {
|
2021-04-27 03:52:58 -04:00
|
|
|
Subscription: this.Subscription,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
'../../models/Publisher': {
|
2021-04-27 03:52:58 -04:00
|
|
|
Publisher: this.Publisher,
|
|
|
|
},
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getEntityWithoutAuthorizationCheck', function () {
|
|
|
|
it('get publisher', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.getEntityWithoutAuthorizationCheck(
|
|
|
|
this.fakeEntityId,
|
|
|
|
EntityConfigs.publisher,
|
|
|
|
(error, subscription) => {
|
2021-03-31 08:20:55 -04:00
|
|
|
expect(error).not.to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
const expectedQuery = { slug: this.fakeEntityId }
|
|
|
|
assertCalledWith(this.Publisher.findOne, expectedQuery)
|
|
|
|
expect(subscription).to.equal(this.publisher)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getUsers', function () {
|
|
|
|
describe('group', function () {
|
|
|
|
it('build view model for all users', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.getUsers(
|
|
|
|
this.subscription,
|
|
|
|
EntityConfigs.group,
|
|
|
|
(error, users) => {
|
|
|
|
const expectedCallcount =
|
|
|
|
this.subscription.member_ids.length +
|
|
|
|
this.subscription.invited_emails.length +
|
|
|
|
this.subscription.teamInvites.length
|
|
|
|
expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
|
|
|
|
expectedCallcount
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('group mamagers', function () {
|
|
|
|
it('build view model for all managers', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.getUsers(
|
|
|
|
this.subscription,
|
|
|
|
EntityConfigs.groupManagers,
|
|
|
|
(error, users) => {
|
|
|
|
const expectedCallcount = this.subscription.manager_ids.length
|
|
|
|
expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
|
|
|
|
expectedCallcount
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('institution', function () {
|
|
|
|
it('build view model for all managers', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.getUsers(
|
|
|
|
this.institution,
|
|
|
|
EntityConfigs.institution,
|
|
|
|
(error, users) => {
|
|
|
|
const expectedCallcount = this.institution.managerIds.length
|
|
|
|
expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
|
|
|
|
expectedCallcount
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('createEntity', function () {
|
|
|
|
it('creates publisher', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.createEntity(
|
|
|
|
this.fakeEntityId,
|
|
|
|
EntityConfigs.publisher,
|
|
|
|
(error, publisher) => {
|
2021-03-31 08:20:55 -04:00
|
|
|
expect(error).not.to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
assertCalledWith(this.Publisher.create, { slug: this.fakeEntityId })
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('addUser', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.email = this.newUser.email)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('institution', function () {
|
|
|
|
it('get user', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.addUser(
|
|
|
|
this.institution,
|
|
|
|
EntityConfigs.institution,
|
|
|
|
this.email,
|
|
|
|
(error, user) => {
|
|
|
|
assertCalledWith(this.UserGetter.getUserByAnyEmail, this.email)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handle user not found', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.UserGetter.getUserByAnyEmail.yields(null, null)
|
|
|
|
return this.UserMembershipHandler.addUser(
|
|
|
|
this.institution,
|
|
|
|
EntityConfigs.institution,
|
|
|
|
this.email,
|
|
|
|
error => {
|
|
|
|
expect(error).to.exist
|
|
|
|
expect(error.userNotFound).to.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handle user already added', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.institution.managerIds.push(this.newUser._id)
|
|
|
|
return this.UserMembershipHandler.addUser(
|
|
|
|
this.institution,
|
|
|
|
EntityConfigs.institution,
|
|
|
|
this.email,
|
|
|
|
(error, users) => {
|
|
|
|
expect(error).to.exist
|
|
|
|
expect(error.alreadyAdded).to.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('add user to institution', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.addUser(
|
|
|
|
this.institution,
|
|
|
|
EntityConfigs.institution,
|
|
|
|
this.email,
|
|
|
|
(error, user) => {
|
2020-11-03 04:19:05 -05:00
|
|
|
assertCalledWith(this.institution.updateOne, {
|
2021-04-27 03:52:58 -04:00
|
|
|
$addToSet: { managerIds: this.newUser._id },
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('return user view', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.addUser(
|
|
|
|
this.institution,
|
|
|
|
EntityConfigs.institution,
|
|
|
|
this.email,
|
|
|
|
(error, user) => {
|
|
|
|
user.should.equal(this.newUser)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('removeUser', function () {
|
|
|
|
describe('institution', function () {
|
|
|
|
it('remove user from institution', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipHandler.removeUser(
|
|
|
|
this.institution,
|
|
|
|
EntityConfigs.institution,
|
|
|
|
this.newUser._id,
|
|
|
|
(error, user) => {
|
2020-11-03 04:19:05 -05:00
|
|
|
const { lastCall } = this.institution.updateOne
|
|
|
|
assertCalledWith(this.institution.updateOne, {
|
2021-04-27 03:52:58 -04:00
|
|
|
$pull: { managerIds: this.newUser._id },
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('handle admin', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription.admin_id = this.newUser._id
|
|
|
|
return this.UserMembershipHandler.removeUser(
|
|
|
|
this.subscription,
|
|
|
|
EntityConfigs.groupManagers,
|
|
|
|
this.newUser._id,
|
|
|
|
(error, user) => {
|
|
|
|
expect(error).to.exist
|
|
|
|
expect(error.isAdmin).to.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|