PaymentServiceTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const SandboxedModule = require('sandboxed-module')
  4. const {
  5. PaymentProviderSubscription,
  6. PaymentProviderAccount,
  7. PaymentProviderCoupon,
  8. } = require('../../../../app/src/Features/Subscription/PaymentProviderEntities')
  9. const MODULE_PATH = '../../../../app/src/Features/Subscription/PaymentService'
  10. describe('PaymentService', function () {
  11. beforeEach(function () {
  12. this.user = {
  13. _id: '123456',
  14. }
  15. this.recurlySubscription = new PaymentProviderSubscription({
  16. id: 'subscription-id',
  17. userId: this.user._id,
  18. currency: 'EUR',
  19. planCode: 'plan-code',
  20. planName: 'plan-name',
  21. planPrice: 13,
  22. addOns: [],
  23. subtotal: 15,
  24. taxRate: 0.1,
  25. taxAmount: 1.5,
  26. total: 14.5,
  27. periodStart: new Date(),
  28. periodEnd: new Date(),
  29. collectionMethod: 'automatic',
  30. })
  31. this.recurlyAccount = new PaymentProviderAccount({
  32. code: this.user._id,
  33. email: 'example@example.com',
  34. hasPastDueInvoice: true,
  35. })
  36. this.recurlyCoupons = [
  37. new PaymentProviderCoupon({
  38. code: 'coupon-code',
  39. name: 'coupon name',
  40. description: 'coupon description',
  41. }),
  42. ]
  43. this.mongoSubscription = {
  44. admin_id: this.user,
  45. recurlySubscription_id: this.recurlySubscription.id,
  46. }
  47. this.RecurlyClient = {
  48. promises: {
  49. getSubscription: sinon.stub(),
  50. getAccountForUserId: sinon.stub(),
  51. getActiveCouponsForUserId: sinon.stub(),
  52. },
  53. }
  54. return (this.PaymentService = SandboxedModule.require(MODULE_PATH, {
  55. requires: {
  56. './RecurlyClient': this.RecurlyClient,
  57. },
  58. }))
  59. })
  60. describe('getPaymentFromRecord', function () {
  61. it('should return null for a missing subscription', async function () {
  62. const response =
  63. await this.PaymentService.promises.getPaymentFromRecord(null)
  64. expect(response).to.equal(null)
  65. })
  66. it('should return null if payment service subscription id is missing', async function () {
  67. this.mongoSubscription.recurlySubscription_id = null
  68. const response = await this.PaymentService.promises.getPaymentFromRecord(
  69. this.mongoSubscription
  70. )
  71. expect(response).to.equal(null)
  72. })
  73. it('should return the subscription', async function () {
  74. this.RecurlyClient.promises.getSubscription.returns(
  75. this.recurlySubscription
  76. )
  77. const response = await this.PaymentService.promises.getPaymentFromRecord(
  78. this.mongoSubscription
  79. )
  80. expect(response.subscription).to.deep.equal(this.recurlySubscription)
  81. })
  82. it('should return account information if found', async function () {
  83. this.RecurlyClient.promises.getSubscription.returns(
  84. this.recurlySubscription
  85. )
  86. this.RecurlyClient.promises.getAccountForUserId.returns(
  87. this.recurlyAccount
  88. )
  89. const response = await this.PaymentService.promises.getPaymentFromRecord(
  90. this.mongoSubscription
  91. )
  92. expect(response.account.email).to.equal(this.recurlyAccount.email)
  93. expect(response.account.hasPastDueInvoice).to.equal(
  94. this.recurlyAccount.hasPastDueInvoice
  95. )
  96. })
  97. it('should include coupons if found', async function () {
  98. this.RecurlyClient.promises.getSubscription.returns(
  99. this.recurlySubscription
  100. )
  101. this.RecurlyClient.promises.getActiveCouponsForUserId.returns(
  102. this.recurlyCoupons
  103. )
  104. const response = await this.PaymentService.promises.getPaymentFromRecord(
  105. this.mongoSubscription
  106. )
  107. expect(response.coupons).to.deep.equal(this.recurlyCoupons)
  108. })
  109. })
  110. })