SubscriptionLocator.test.mjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath =
  4. '../../../../app/src/Features/Subscription/SubscriptionLocator'
  5. describe('Subscription Locator Tests', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.user = { _id: '5208dd34438842e2db333333' }
  8. ctx.subscription = { hello: 'world' }
  9. ctx.Subscription = {
  10. findOne: sinon.stub().returns({
  11. exec: sinon.stub().resolves(),
  12. }),
  13. find: sinon.stub().returns({
  14. populate: sinon.stub().returns({
  15. populate: sinon.stub().returns({
  16. exec: sinon.stub().resolves([]),
  17. }),
  18. }),
  19. exec: sinon.stub().resolves(),
  20. }),
  21. }
  22. ctx.DeletedSubscription = {
  23. findOne: sinon.stub().returns({
  24. exec: sinon.stub().resolves(),
  25. }),
  26. find: sinon.stub().returns({
  27. exec: sinon.stub().resolves(),
  28. }),
  29. }
  30. vi.doMock(
  31. '../../../../app/src/Features/Subscription/GroupPlansData',
  32. () => ({
  33. default: {},
  34. })
  35. )
  36. vi.doMock('../../../../app/src/models/Subscription', () => ({
  37. Subscription: ctx.Subscription,
  38. }))
  39. vi.doMock('../../../../app/src/models/DeletedSubscription', () => ({
  40. DeletedSubscription: ctx.DeletedSubscription,
  41. }))
  42. vi.doMock('../../../../app/src/models/SSOConfig', () => ({
  43. SSOConfig: ctx.SSOConfig,
  44. }))
  45. ctx.SubscriptionLocator = (await import(modulePath)).default
  46. })
  47. describe('finding users subscription', function () {
  48. it('should send the users features', async function (ctx) {
  49. ctx.Subscription.findOne.returns({
  50. exec: sinon.stub().resolves(ctx.subscription),
  51. })
  52. const subscription =
  53. await ctx.SubscriptionLocator.promises.getUsersSubscription(ctx.user)
  54. ctx.Subscription.findOne
  55. .calledWith({ admin_id: ctx.user._id })
  56. .should.equal(true)
  57. subscription.should.equal(ctx.subscription)
  58. })
  59. it('should error if not found', async function (ctx) {
  60. ctx.Subscription.findOne.returns({
  61. exec: sinon.stub().rejects('not found'),
  62. })
  63. await expect(
  64. ctx.SubscriptionLocator.promises.getUsersSubscription(ctx.user)
  65. ).to.be.rejected
  66. })
  67. it('should take a user id rather than the user object', async function (ctx) {
  68. ctx.Subscription.findOne.returns({
  69. exec: sinon.stub().resolves(ctx.subscription),
  70. })
  71. const subscription =
  72. await ctx.SubscriptionLocator.promises.getUsersSubscription(
  73. ctx.user._id
  74. )
  75. ctx.Subscription.findOne
  76. .calledWith({ admin_id: ctx.user._id })
  77. .should.equal(true)
  78. subscription.should.equal(ctx.subscription)
  79. })
  80. })
  81. describe('getUserSubscriptionStatus', function () {
  82. it('should return no active personal or group subscription when no user is passed', async function (ctx) {
  83. const subscriptionStatus =
  84. await ctx.SubscriptionLocator.promises.getUserSubscriptionStatus(
  85. undefined
  86. )
  87. expect(subscriptionStatus).to.deep.equal({
  88. personal: false,
  89. group: false,
  90. })
  91. })
  92. it('should return no active personal or group subscription when the user has no subscription', async function (ctx) {
  93. const subscriptionStatus =
  94. await ctx.SubscriptionLocator.promises.getUserSubscriptionStatus(
  95. ctx.user._id
  96. )
  97. expect(subscriptionStatus).to.deep.equal({
  98. personal: false,
  99. group: false,
  100. })
  101. })
  102. it('should return active personal subscription', async function (ctx) {
  103. ctx.Subscription.findOne.returns({
  104. exec: sinon.stub().resolves({
  105. recurlyStatus: {
  106. state: 'active',
  107. },
  108. }),
  109. })
  110. const subscriptionStatus =
  111. await ctx.SubscriptionLocator.promises.getUserSubscriptionStatus(
  112. ctx.user._id
  113. )
  114. expect(subscriptionStatus).to.deep.equal({ personal: true, group: false })
  115. })
  116. it('should return active group subscription when member of a group plan', async function (ctx) {
  117. ctx.Subscription.find.returns({
  118. populate: sinon.stub().returns({
  119. populate: sinon.stub().returns({
  120. exec: sinon.stub().resolves([
  121. {
  122. recurlyStatus: {
  123. state: 'active',
  124. },
  125. groupPlan: true,
  126. },
  127. ]),
  128. }),
  129. }),
  130. })
  131. const subscriptionStatus =
  132. await ctx.SubscriptionLocator.promises.getUserSubscriptionStatus(
  133. ctx.user._id
  134. )
  135. expect(subscriptionStatus).to.deep.equal({ personal: false, group: true })
  136. })
  137. it('should return active group subscription when owner of a group plan', async function (ctx) {
  138. ctx.Subscription.findOne.returns({
  139. exec: sinon.stub().resolves({
  140. recurlyStatus: {
  141. state: 'active',
  142. },
  143. groupPlan: true,
  144. }),
  145. })
  146. const subscriptionStatus =
  147. await ctx.SubscriptionLocator.promises.getUserSubscriptionStatus(
  148. ctx.user._id
  149. )
  150. expect(subscriptionStatus).to.deep.equal({ personal: false, group: true })
  151. })
  152. it('should return active personal and group subscription when has personal subscription and member of a group', async function (ctx) {
  153. ctx.Subscription.find.returns({
  154. populate: sinon.stub().returns({
  155. populate: sinon.stub().returns({
  156. exec: sinon.stub().resolves([
  157. {
  158. recurlyStatus: {
  159. state: 'active',
  160. },
  161. groupPlan: true,
  162. },
  163. ]),
  164. }),
  165. }),
  166. })
  167. ctx.Subscription.findOne.returns({
  168. exec: sinon.stub().resolves({
  169. recurlyStatus: {
  170. state: 'active',
  171. },
  172. }),
  173. })
  174. const subscriptionStatus =
  175. await ctx.SubscriptionLocator.promises.getUserSubscriptionStatus(
  176. ctx.user._id
  177. )
  178. expect(subscriptionStatus).to.deep.equal({ personal: true, group: true })
  179. })
  180. })
  181. })