SubscriptionLocatorTests.js 5.9 KB

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