| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- import { vi, expect } from 'vitest'
- import mongodb from 'mongodb-legacy'
- import EntityConfigs from '../../../../app/src/Features/UserMembership/UserMembershipEntityConfigs.mjs'
- import UserMembershipErrors from '../../../../app/src/Features/UserMembership/UserMembershipErrors.mjs'
- const { ObjectId } = mongodb
- const modulePath =
- '../../../../app/src/Features/UserMembership/UserMembershipHandler'
- const serializeIds = ids =>
- ids.map(id => (id instanceof ObjectId ? `objectId-${id.toString()}` : id))
- vi.mock(
- '../../../../app/src/Features/UserMembership/UserMembershipErrors.mjs',
- () =>
- vi.importActual(
- '../../../../app/src/Features/UserMembership/UserMembershipErrors.mjs'
- )
- )
- describe('UserMembershipHandler', function () {
- beforeEach(async function (ctx) {
- ctx.user = { _id: new ObjectId() }
- ctx.newUser = { _id: new ObjectId(), email: 'new-user-email@foo.bar' }
- ctx.fakeEntityId = new ObjectId()
- ctx.subscription = {
- _id: 'mock-subscription-id',
- groupPlan: true,
- membersLimit: 10,
- member_ids: [new ObjectId(), new ObjectId()],
- manager_ids: [new ObjectId()],
- invited_emails: ['mock-email-1@foo.com'],
- teamInvites: [{ email: 'mock-email-1@bar.com' }],
- update: vi.fn().mockReturnValue({
- exec: vi.fn().mockResolvedValue(),
- }),
- updateOne: vi.fn().mockReturnValue({
- exec: vi.fn().mockResolvedValue(),
- }),
- }
- ctx.institution = {
- _id: 'mock-institution-id',
- v1Id: 123,
- managerIds: [new ObjectId(), new ObjectId(), new ObjectId()],
- updateOne: vi.fn().mockReturnValue({
- exec: vi.fn().mockResolvedValue(),
- }),
- }
- ctx.publisher = {
- _id: 'mock-publisher-id',
- slug: 'slug',
- managerIds: [new ObjectId(), new ObjectId()],
- updateOne: vi.fn().mockReturnValue({
- exec: vi.fn().mockResolvedValue(),
- }),
- }
- ctx.UserMembershipViewModel = {
- promises: {
- buildAsync: vi.fn().mockResolvedValue([{ _id: 'mock-member-id' }]),
- },
- build: vi.fn().mockReturnValue(ctx.newUser),
- }
- ctx.UserGetter = {
- promises: {
- getUserByAnyEmail: vi.fn().mockResolvedValue(ctx.newUser),
- },
- }
- ctx.Institution = {
- findOne: vi.fn().mockReturnValue({
- exec: vi.fn().mockResolvedValue(ctx.institution),
- }),
- }
- ctx.Subscription = {
- findOne: vi.fn().mockReturnValue({
- exec: vi.fn().mockResolvedValue(ctx.subscription),
- }),
- }
- ctx.Publisher = {
- findOne: vi.fn().mockReturnValue({
- exec: vi.fn().mockResolvedValue(ctx.publisher),
- }),
- create: vi.fn().mockReturnValue({
- exec: vi.fn().mockResolvedValue(ctx.publisher),
- }),
- }
- ctx.Modules = {
- promises: {
- hooks: {
- fire: vi.fn().mockResolvedValue(),
- },
- },
- }
- ctx.mongoose = {
- startSession: vi.fn().mockResolvedValue({
- withTransaction: vi.fn(async callback => await callback()),
- endSession: vi.fn().mockResolvedValue(),
- }),
- }
- ctx.SubscriptionUpdater = {
- promises: {
- sendGroupRoleUserProperty: vi.fn().mockResolvedValue(),
- },
- }
- vi.doMock('mongodb-legacy', () => ({
- default: { ObjectId },
- }))
- vi.doMock(
- '../../../../app/src/Features/UserMembership/UserMembershipViewModel',
- () => ({
- default: ctx.UserMembershipViewModel,
- })
- )
- vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
- default: ctx.UserGetter,
- }))
- vi.doMock('../../../../app/src/models/Institution', () => ({
- Institution: ctx.Institution,
- }))
- vi.doMock('../../../../app/src/models/Subscription', () => ({
- Subscription: ctx.Subscription,
- }))
- vi.doMock('../../../../app/src/models/Publisher', () => ({
- Publisher: ctx.Publisher,
- }))
- vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
- default: ctx.Modules,
- }))
- vi.doMock('../../../../app/src/infrastructure/Mongoose', () => ({
- default: ctx.mongoose,
- }))
- vi.doMock(
- '../../../../app/src/Features/Subscription/SubscriptionUpdater',
- () => ({
- default: ctx.SubscriptionUpdater,
- })
- )
- ctx.UserMembershipHandler = (await import(modulePath)).default
- })
- describe('getEntityWithoutAuthorizationCheck', function () {
- it('get publisher', async function (ctx) {
- const subscription =
- await ctx.UserMembershipHandler.promises.getEntityWithoutAuthorizationCheck(
- ctx.fakeEntityId,
- EntityConfigs.publisher
- )
- const expectedQuery = { slug: ctx.fakeEntityId }
- expect(ctx.Publisher.findOne).toHaveBeenCalledWith(expectedQuery)
- expect(subscription).to.equal(ctx.publisher)
- })
- })
- describe('getUsers', function () {
- describe('group', function () {
- it('build view model for all users', async function (ctx) {
- await ctx.UserMembershipHandler.promises.getUsers(
- ctx.subscription,
- EntityConfigs.group
- )
- expect(
- serializeIds(
- ctx.UserMembershipViewModel.promises.buildAsync.mock.calls[0][0]
- )
- ).toEqual(
- serializeIds(
- ctx.subscription.invited_emails.concat(
- ctx.subscription.teamInvites[0].email,
- ctx.subscription.member_ids
- )
- )
- )
- })
- })
- describe('group managers', function () {
- it('build view model for all managers', async function (ctx) {
- await ctx.UserMembershipHandler.promises.getUsers(
- ctx.subscription,
- EntityConfigs.groupManagers
- )
- expect(
- serializeIds(
- ctx.UserMembershipViewModel.promises.buildAsync.mock.calls[0][0]
- )
- ).toEqual(serializeIds(ctx.subscription.manager_ids))
- })
- })
- describe('institution', function () {
- it('build view model for all managers', async function (ctx) {
- await ctx.UserMembershipHandler.promises.getUsers(
- ctx.institution,
- EntityConfigs.institution
- )
- expect(
- serializeIds(
- ctx.UserMembershipViewModel.promises.buildAsync.mock.calls[0][0]
- )
- ).toEqual(serializeIds(ctx.institution.managerIds))
- })
- })
- })
- describe('createEntity', function () {
- it('creates publisher', async function (ctx) {
- await ctx.UserMembershipHandler.promises.createEntity(
- ctx.fakeEntityId,
- EntityConfigs.publisher
- )
- expect(ctx.Publisher.create).toHaveBeenCalledWith({
- slug: ctx.fakeEntityId,
- })
- })
- })
- describe('addUser', function () {
- beforeEach(function (ctx) {
- ctx.email = ctx.newUser.email
- })
- describe('institution', function () {
- it('get user', async function (ctx) {
- await ctx.UserMembershipHandler.promises.addUser(
- ctx.institution,
- EntityConfigs.institution,
- ctx.email
- )
- expect(ctx.UserGetter.promises.getUserByAnyEmail).toHaveBeenCalledWith(
- ctx.email
- )
- })
- it('handle user not found', async function (ctx) {
- ctx.UserGetter.promises.getUserByAnyEmail.mockResolvedValue(null)
- try {
- await ctx.UserMembershipHandler.promises.addUser(
- ctx.institution,
- EntityConfigs.institution,
- ctx.email
- )
- expect.fail('Expected addUser to throw')
- } catch (err) {
- expect(err).toBeInstanceOf(UserMembershipErrors.UserNotFoundError)
- }
- })
- it('handle user already added', async function (ctx) {
- ctx.institution.managerIds.push(ctx.newUser._id)
- try {
- await ctx.UserMembershipHandler.promises.addUser(
- ctx.institution,
- EntityConfigs.institution,
- ctx.email
- )
- expect.fail('Expected addUser to throw')
- } catch (err) {
- expect(err).toBeInstanceOf(UserMembershipErrors.UserAlreadyAddedError)
- }
- })
- it('add user to institution', async function (ctx) {
- await ctx.UserMembershipHandler.promises.addUser(
- ctx.institution,
- EntityConfigs.institution,
- ctx.email
- )
- expect(ctx.institution.updateOne).toHaveBeenCalledWith({
- $addToSet: { managerIds: ctx.newUser._id },
- })
- })
- it('return user view', async function (ctx) {
- const user = await ctx.UserMembershipHandler.promises.addUser(
- ctx.institution,
- EntityConfigs.institution,
- ctx.email
- )
- expect(user).to.equal(ctx.newUser)
- })
- })
- describe('group managers', function () {
- it('add user to group managers', async function (ctx) {
- await ctx.UserMembershipHandler.promises.addUser(
- ctx.subscription,
- EntityConfigs.groupManagers,
- ctx.email
- )
- expect(ctx.subscription.updateOne).toHaveBeenCalledWith({
- $addToSet: { manager_ids: ctx.newUser._id },
- })
- })
- it('should write a group audit log when subscription has managed users enabled', async function (ctx) {
- ctx.subscription.managedUsersEnabled = true
- const auditInfo = {
- initiatorId: new ObjectId(),
- ipAddress: '192.168.1.1',
- }
- await ctx.UserMembershipHandler.promises.addUser(
- ctx.subscription,
- EntityConfigs.groupManagers,
- ctx.email,
- auditInfo
- )
- expect(ctx.Modules.promises.hooks.fire).toHaveBeenCalledWith(
- 'addGroupAuditLogEntry',
- {
- groupId: ctx.subscription._id,
- operation: 'group-role-changed',
- initiatorId: auditInfo.initiatorId,
- ipAddress: auditInfo.ipAddress,
- info: {
- userId: ctx.newUser._id,
- role: 'manager',
- },
- },
- expect.anything() // session object
- )
- })
- it('should not write a group audit log when subscription does not have managed users enabled', async function (ctx) {
- ctx.subscription.managedUsersEnabled = false
- const auditInfo = {
- initiatorId: new ObjectId(),
- ipAddress: '192.168.1.1',
- }
- await ctx.UserMembershipHandler.promises.addUser(
- ctx.subscription,
- EntityConfigs.groupManagers,
- ctx.email,
- auditInfo
- )
- expect(ctx.Modules.promises.hooks.fire).not.toHaveBeenCalled()
- })
- })
- })
- describe('removeUser', function () {
- describe('institution', function () {
- it('remove user from institution', async function (ctx) {
- await ctx.UserMembershipHandler.promises.removeUser(
- ctx.institution,
- EntityConfigs.institution,
- ctx.newUser._id
- )
- expect(ctx.institution.updateOne).toHaveBeenCalledWith({
- $pull: { managerIds: ctx.newUser._id },
- })
- })
- it('handle admin', async function (ctx) {
- ctx.subscription.admin_id = ctx.newUser._id
- try {
- await ctx.UserMembershipHandler.promises.removeUser(
- ctx.subscription,
- EntityConfigs.groupManagers,
- ctx.newUser._id
- )
- expect.fail('Expected removeUser to throw')
- } catch (err) {
- expect(err).toBeInstanceOf(UserMembershipErrors.UserIsManagerError)
- }
- })
- })
- describe('group managers', function () {
- it('remove user from group managers', async function (ctx) {
- await ctx.UserMembershipHandler.promises.removeUser(
- ctx.subscription,
- EntityConfigs.groupManagers,
- ctx.newUser._id
- )
- expect(ctx.subscription.updateOne).toHaveBeenCalledWith({
- $pull: { manager_ids: ctx.newUser._id },
- })
- })
- it('should write a group audit log when subscription has managed users enabled', async function (ctx) {
- ctx.subscription.managedUsersEnabled = true
- const auditInfo = {
- initiatorId: new ObjectId(),
- ipAddress: '192.168.1.1',
- }
- await ctx.UserMembershipHandler.promises.removeUser(
- ctx.subscription,
- EntityConfigs.groupManagers,
- ctx.newUser._id,
- auditInfo
- )
- expect(ctx.Modules.promises.hooks.fire).toHaveBeenCalledWith(
- 'addGroupAuditLogEntry',
- {
- groupId: ctx.subscription._id,
- operation: 'group-role-changed',
- initiatorId: auditInfo.initiatorId,
- ipAddress: auditInfo.ipAddress,
- info: {
- userId: ctx.newUser._id,
- role: 'member',
- },
- }
- )
- })
- it('should not write a group audit log when subscription does not have managed users enabled', async function (ctx) {
- ctx.subscription.managedUsersEnabled = false
- const auditInfo = {
- initiatorId: new ObjectId(),
- ipAddress: '192.168.1.1',
- }
- await ctx.UserMembershipHandler.promises.removeUser(
- ctx.subscription,
- EntityConfigs.groupManagers,
- ctx.newUser._id,
- auditInfo
- )
- expect(ctx.Modules.promises.hooks.fire).not.toHaveBeenCalled()
- })
- })
- })
- })
|