SubscriptionGroupHandlerTests.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const modulePath =
  5. '../../../../app/src/Features/Subscription/SubscriptionGroupHandler'
  6. describe('SubscriptionGroupHandler', function () {
  7. beforeEach(function () {
  8. this.adminUser_id = '12321'
  9. this.newEmail = 'bob@smith.com'
  10. this.user_id = '3121321'
  11. this.email = 'jim@example.com'
  12. this.user = { _id: this.user_id, email: this.newEmail }
  13. this.subscription_id = '31DSd1123D'
  14. this.subscription = {
  15. admin_id: this.adminUser_id,
  16. manager_ids: [this.adminUser_id],
  17. _id: this.subscription_id,
  18. }
  19. this.SubscriptionLocator = {
  20. promises: {
  21. getUsersSubscription: sinon.stub(),
  22. getSubscriptionByMemberIdAndId: sinon.stub(),
  23. getSubscription: sinon.stub().resolves(this.subscription),
  24. },
  25. }
  26. this.SubscriptionUpdater = {
  27. promises: {
  28. removeUserFromGroup: sinon.stub().resolves(),
  29. getSubscription: sinon.stub().resolves(),
  30. },
  31. }
  32. this.Subscription = {
  33. updateOne: sinon.stub().returns({ exec: sinon.stub().resolves }),
  34. updateMany: sinon.stub().returns({ exec: sinon.stub().resolves }),
  35. findOne: sinon.stub().returns({ exec: sinon.stub().resolves }),
  36. }
  37. this.Handler = SandboxedModule.require(modulePath, {
  38. requires: {
  39. './SubscriptionUpdater': this.SubscriptionUpdater,
  40. './SubscriptionLocator': this.SubscriptionLocator,
  41. '../../models/Subscription': {
  42. Subscription: this.Subscription,
  43. },
  44. },
  45. })
  46. })
  47. describe('removeUserFromGroup', function () {
  48. it('should call the subscription updater to remove the user', async function () {
  49. await this.Handler.promises.removeUserFromGroup(
  50. this.adminUser_id,
  51. this.user._id
  52. )
  53. this.SubscriptionUpdater.promises.removeUserFromGroup
  54. .calledWith(this.adminUser_id, this.user._id)
  55. .should.equal(true)
  56. })
  57. })
  58. describe('replaceUserReferencesInGroups', function () {
  59. beforeEach(async function () {
  60. this.oldId = 'ba5eba11'
  61. this.newId = '5ca1ab1e'
  62. await this.Handler.promises.replaceUserReferencesInGroups(
  63. this.oldId,
  64. this.newId
  65. )
  66. })
  67. it('replaces the admin_id', function () {
  68. this.Subscription.updateOne
  69. .calledWith({ admin_id: this.oldId }, { admin_id: this.newId })
  70. .should.equal(true)
  71. })
  72. it('replaces the manager_ids', function () {
  73. this.Subscription.updateMany
  74. .calledWith(
  75. { manager_ids: 'ba5eba11' },
  76. { $addToSet: { manager_ids: '5ca1ab1e' } }
  77. )
  78. .should.equal(true)
  79. this.Subscription.updateMany
  80. .calledWith(
  81. { manager_ids: 'ba5eba11' },
  82. { $pull: { manager_ids: 'ba5eba11' } }
  83. )
  84. .should.equal(true)
  85. })
  86. it('replaces the member ids', function () {
  87. this.Subscription.updateMany
  88. .calledWith(
  89. { member_ids: this.oldId },
  90. { $addToSet: { member_ids: this.newId } }
  91. )
  92. .should.equal(true)
  93. this.Subscription.updateMany
  94. .calledWith(
  95. { member_ids: this.oldId },
  96. { $pull: { member_ids: this.oldId } }
  97. )
  98. .should.equal(true)
  99. })
  100. })
  101. describe('isUserPartOfGroup', function () {
  102. beforeEach(function () {
  103. this.subscription_id = '123ed13123'
  104. })
  105. it('should return true when user is part of subscription', async function () {
  106. this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
  107. {
  108. _id: this.subscription_id,
  109. }
  110. )
  111. const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
  112. this.user_id,
  113. this.subscription_id
  114. )
  115. partOfGroup.should.equal(true)
  116. })
  117. it('should return false when no subscription is found', async function () {
  118. this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
  119. null
  120. )
  121. const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
  122. this.user_id,
  123. this.subscription_id
  124. )
  125. partOfGroup.should.equal(false)
  126. })
  127. })
  128. describe('getTotalConfirmedUsersInGroup', function () {
  129. describe('for existing subscriptions', function () {
  130. beforeEach(function () {
  131. this.subscription.member_ids = ['12321', '3121321']
  132. })
  133. it('should call the subscription locator and return 2 users', async function () {
  134. const count = await this.Handler.promises.getTotalConfirmedUsersInGroup(
  135. this.subscription_id
  136. )
  137. this.SubscriptionLocator.promises.getSubscription
  138. .calledWith(this.subscription_id)
  139. .should.equal(true)
  140. count.should.equal(2)
  141. })
  142. })
  143. describe('for nonexistent subscriptions', function () {
  144. it('should return undefined', async function () {
  145. const count =
  146. await this.Handler.promises.getTotalConfirmedUsersInGroup('fake-id')
  147. expect(count).not.to.exist
  148. })
  149. })
  150. })
  151. })