UserMembershipHandler.test.mjs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import { vi, expect } from 'vitest'
  2. import mongodb from 'mongodb-legacy'
  3. import EntityConfigs from '../../../../app/src/Features/UserMembership/UserMembershipEntityConfigs.js'
  4. import UserMembershipErrors from '../../../../app/src/Features/UserMembership/UserMembershipErrors.mjs'
  5. const { ObjectId } = mongodb
  6. const modulePath =
  7. '../../../../app/src/Features/UserMembership/UserMembershipHandler'
  8. const serializeIds = ids =>
  9. ids.map(id => (id instanceof ObjectId ? `objectId-${id.toString()}` : id))
  10. vi.mock(
  11. '../../../../app/src/Features/UserMembership/UserMembershipErrors.mjs',
  12. () =>
  13. vi.importActual(
  14. '../../../../app/src/Features/UserMembership/UserMembershipErrors.mjs'
  15. )
  16. )
  17. describe('UserMembershipHandler', function () {
  18. beforeEach(async function (ctx) {
  19. ctx.user = { _id: new ObjectId() }
  20. ctx.newUser = { _id: new ObjectId(), email: 'new-user-email@foo.bar' }
  21. ctx.fakeEntityId = new ObjectId()
  22. ctx.subscription = {
  23. _id: 'mock-subscription-id',
  24. groupPlan: true,
  25. membersLimit: 10,
  26. member_ids: [new ObjectId(), new ObjectId()],
  27. manager_ids: [new ObjectId()],
  28. invited_emails: ['mock-email-1@foo.com'],
  29. teamInvites: [{ email: 'mock-email-1@bar.com' }],
  30. update: vi.fn().mockReturnValue({
  31. exec: vi.fn().mockResolvedValue(),
  32. }),
  33. }
  34. ctx.institution = {
  35. _id: 'mock-institution-id',
  36. v1Id: 123,
  37. managerIds: [new ObjectId(), new ObjectId(), new ObjectId()],
  38. updateOne: vi.fn().mockReturnValue({
  39. exec: vi.fn().mockResolvedValue(),
  40. }),
  41. }
  42. ctx.publisher = {
  43. _id: 'mock-publisher-id',
  44. slug: 'slug',
  45. managerIds: [new ObjectId(), new ObjectId()],
  46. updateOne: vi.fn().mockReturnValue({
  47. exec: vi.fn().mockResolvedValue(),
  48. }),
  49. }
  50. ctx.UserMembershipViewModel = {
  51. promises: {
  52. buildAsync: vi.fn().mockResolvedValue([{ _id: 'mock-member-id' }]),
  53. },
  54. build: vi.fn().mockReturnValue(ctx.newUser),
  55. }
  56. ctx.UserGetter = {
  57. promises: {
  58. getUserByAnyEmail: vi.fn().mockResolvedValue(ctx.newUser),
  59. },
  60. }
  61. ctx.Institution = {
  62. findOne: vi.fn().mockReturnValue({
  63. exec: vi.fn().mockResolvedValue(ctx.institution),
  64. }),
  65. }
  66. ctx.Subscription = {
  67. findOne: vi.fn().mockReturnValue({
  68. exec: vi.fn().mockResolvedValue(ctx.subscription),
  69. }),
  70. }
  71. ctx.Publisher = {
  72. findOne: vi.fn().mockReturnValue({
  73. exec: vi.fn().mockResolvedValue(ctx.publisher),
  74. }),
  75. create: vi.fn().mockReturnValue({
  76. exec: vi.fn().mockResolvedValue(ctx.publisher),
  77. }),
  78. }
  79. vi.doMock('mongodb-legacy', () => ({
  80. default: { ObjectId },
  81. }))
  82. vi.doMock(
  83. '../../../../app/src/Features/UserMembership/UserMembershipViewModel',
  84. () => ({
  85. default: ctx.UserMembershipViewModel,
  86. })
  87. )
  88. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  89. default: ctx.UserGetter,
  90. }))
  91. vi.doMock('../../../../app/src/models/Institution', () => ({
  92. Institution: ctx.Institution,
  93. }))
  94. vi.doMock('../../../../app/src/models/Subscription', () => ({
  95. Subscription: ctx.Subscription,
  96. }))
  97. vi.doMock('../../../../app/src/models/Publisher', () => ({
  98. Publisher: ctx.Publisher,
  99. }))
  100. ctx.UserMembershipHandler = (await import(modulePath)).default
  101. })
  102. describe('getEntityWithoutAuthorizationCheck', function () {
  103. it('get publisher', async function (ctx) {
  104. const subscription =
  105. await ctx.UserMembershipHandler.promises.getEntityWithoutAuthorizationCheck(
  106. ctx.fakeEntityId,
  107. EntityConfigs.publisher
  108. )
  109. const expectedQuery = { slug: ctx.fakeEntityId }
  110. expect(ctx.Publisher.findOne).toHaveBeenCalledWith(expectedQuery)
  111. expect(subscription).to.equal(ctx.publisher)
  112. })
  113. })
  114. describe('getUsers', function () {
  115. describe('group', function () {
  116. it('build view model for all users', async function (ctx) {
  117. await ctx.UserMembershipHandler.promises.getUsers(
  118. ctx.subscription,
  119. EntityConfigs.group
  120. )
  121. expect(
  122. serializeIds(
  123. ctx.UserMembershipViewModel.promises.buildAsync.mock.calls[0][0]
  124. )
  125. ).toEqual(
  126. serializeIds(
  127. ctx.subscription.invited_emails.concat(
  128. ctx.subscription.teamInvites[0].email,
  129. ctx.subscription.member_ids
  130. )
  131. )
  132. )
  133. })
  134. })
  135. describe('group managers', function () {
  136. it('build view model for all managers', async function (ctx) {
  137. await ctx.UserMembershipHandler.promises.getUsers(
  138. ctx.subscription,
  139. EntityConfigs.groupManagers
  140. )
  141. expect(
  142. serializeIds(
  143. ctx.UserMembershipViewModel.promises.buildAsync.mock.calls[0][0]
  144. )
  145. ).toEqual(serializeIds(ctx.subscription.manager_ids))
  146. })
  147. })
  148. describe('institution', function () {
  149. it('build view model for all managers', async function (ctx) {
  150. await ctx.UserMembershipHandler.promises.getUsers(
  151. ctx.institution,
  152. EntityConfigs.institution
  153. )
  154. expect(
  155. serializeIds(
  156. ctx.UserMembershipViewModel.promises.buildAsync.mock.calls[0][0]
  157. )
  158. ).toEqual(serializeIds(ctx.institution.managerIds))
  159. })
  160. })
  161. })
  162. describe('createEntity', function () {
  163. it('creates publisher', async function (ctx) {
  164. await ctx.UserMembershipHandler.promises.createEntity(
  165. ctx.fakeEntityId,
  166. EntityConfigs.publisher
  167. )
  168. expect(ctx.Publisher.create).toHaveBeenCalledWith({
  169. slug: ctx.fakeEntityId,
  170. })
  171. })
  172. })
  173. describe('addUser', function () {
  174. beforeEach(function (ctx) {
  175. ctx.email = ctx.newUser.email
  176. })
  177. describe('institution', function () {
  178. it('get user', async function (ctx) {
  179. await ctx.UserMembershipHandler.promises.addUser(
  180. ctx.institution,
  181. EntityConfigs.institution,
  182. ctx.email
  183. )
  184. expect(ctx.UserGetter.promises.getUserByAnyEmail).toHaveBeenCalledWith(
  185. ctx.email
  186. )
  187. })
  188. it('handle user not found', async function (ctx) {
  189. ctx.UserGetter.promises.getUserByAnyEmail.mockResolvedValue(null)
  190. try {
  191. await ctx.UserMembershipHandler.promises.addUser(
  192. ctx.institution,
  193. EntityConfigs.institution,
  194. ctx.email
  195. )
  196. expect.fail('Expected addUser to throw')
  197. } catch (err) {
  198. expect(err).toBeInstanceOf(UserMembershipErrors.UserNotFoundError)
  199. }
  200. })
  201. it('handle user already added', async function (ctx) {
  202. ctx.institution.managerIds.push(ctx.newUser._id)
  203. try {
  204. await ctx.UserMembershipHandler.promises.addUser(
  205. ctx.institution,
  206. EntityConfigs.institution,
  207. ctx.email
  208. )
  209. expect.fail('Expected addUser to throw')
  210. } catch (err) {
  211. expect(err).toBeInstanceOf(UserMembershipErrors.UserAlreadyAddedError)
  212. }
  213. })
  214. it('add user to institution', async function (ctx) {
  215. await ctx.UserMembershipHandler.promises.addUser(
  216. ctx.institution,
  217. EntityConfigs.institution,
  218. ctx.email
  219. )
  220. expect(ctx.institution.updateOne).toHaveBeenCalledWith({
  221. $addToSet: { managerIds: ctx.newUser._id },
  222. })
  223. })
  224. it('return user view', async function (ctx) {
  225. const user = await ctx.UserMembershipHandler.promises.addUser(
  226. ctx.institution,
  227. EntityConfigs.institution,
  228. ctx.email
  229. )
  230. expect(user).to.equal(ctx.newUser)
  231. })
  232. })
  233. })
  234. describe('removeUser', function () {
  235. describe('institution', function () {
  236. it('remove user from institution', async function (ctx) {
  237. await ctx.UserMembershipHandler.promises.removeUser(
  238. ctx.institution,
  239. EntityConfigs.institution,
  240. ctx.newUser._id
  241. )
  242. expect(ctx.institution.updateOne).toHaveBeenCalledWith({
  243. $pull: { managerIds: ctx.newUser._id },
  244. })
  245. })
  246. it('handle admin', async function (ctx) {
  247. ctx.subscription.admin_id = ctx.newUser._id
  248. try {
  249. await ctx.UserMembershipHandler.promises.removeUser(
  250. ctx.subscription,
  251. EntityConfigs.groupManagers,
  252. ctx.newUser._id
  253. )
  254. expect.fail('Expected removeUser to throw')
  255. } catch (err) {
  256. expect(err).toBeInstanceOf(UserMembershipErrors.UserIsManagerError)
  257. }
  258. })
  259. })
  260. })
  261. })