SubscriptionHandlerTests.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const chai = require('chai')
  4. const { expect } = chai
  5. const {
  6. PaymentProviderSubscription,
  7. } = require('../../../../app/src/Features/Subscription/PaymentProviderEntities')
  8. const MODULE_PATH =
  9. '../../../../app/src/Features/Subscription/SubscriptionHandler'
  10. const mockRecurlySubscriptions = {
  11. 'subscription-123-active': {
  12. uuid: 'subscription-123-active',
  13. plan: {
  14. name: 'Gold',
  15. plan_code: 'gold',
  16. },
  17. current_period_ends_at: new Date(),
  18. state: 'active',
  19. unit_amount_in_cents: 999,
  20. account: {
  21. account_code: 'user-123',
  22. },
  23. },
  24. }
  25. const mockRecurlyClientSubscriptions = {
  26. 'subscription-123-active': new PaymentProviderSubscription({
  27. id: 'subscription-123-active',
  28. userId: 'user-id',
  29. planCode: 'collaborator',
  30. planName: 'Collaborator',
  31. planPrice: 10,
  32. subtotal: 10,
  33. currency: 'USD',
  34. total: 10,
  35. }),
  36. }
  37. const mockSubscriptionChanges = {
  38. 'subscription-123-active': {
  39. id: 'subscription-change-id',
  40. subscriptionId: 'subscription-123-recurly-id', // not the UUID
  41. },
  42. }
  43. describe('SubscriptionHandler', function () {
  44. beforeEach(function () {
  45. this.Settings = {
  46. plans: [
  47. {
  48. planCode: 'collaborator',
  49. name: 'Collaborator',
  50. price_in_cents: 1000,
  51. features: {
  52. collaborators: -1,
  53. versioning: true,
  54. },
  55. },
  56. {
  57. planCode: 'professional',
  58. price_in_cents: 1500,
  59. },
  60. ],
  61. defaultPlanCode: {
  62. collaborators: 0,
  63. versioning: false,
  64. },
  65. }
  66. this.activeRecurlySubscription =
  67. mockRecurlySubscriptions['subscription-123-active']
  68. this.activeRecurlyClientSubscription =
  69. mockRecurlyClientSubscriptions['subscription-123-active']
  70. this.activeRecurlySubscriptionChange =
  71. mockSubscriptionChanges['subscription-123-active']
  72. this.User = {}
  73. this.user = { _id: (this.user_id = 'user_id_here_') }
  74. this.subscription = {
  75. recurlySubscription_id: this.activeRecurlySubscription.uuid,
  76. }
  77. this.RecurlyWrapper = {
  78. promises: {
  79. getSubscription: sinon.stub().resolves(this.activeRecurlySubscription),
  80. redeemCoupon: sinon.stub().resolves(),
  81. createSubscription: sinon
  82. .stub()
  83. .resolves(this.activeRecurlySubscription),
  84. getBillingInfo: sinon.stub().resolves(),
  85. getAccountPastDueInvoices: sinon.stub().resolves(),
  86. attemptInvoiceCollection: sinon.stub().resolves(),
  87. listAccountActiveSubscriptions: sinon.stub().resolves([]),
  88. },
  89. }
  90. this.RecurlyClient = {
  91. promises: {
  92. reactivateSubscriptionByUuid: sinon
  93. .stub()
  94. .resolves(this.activeRecurlyClientSubscription),
  95. cancelSubscriptionByUuid: sinon.stub().resolves(),
  96. applySubscriptionChangeRequest: sinon
  97. .stub()
  98. .resolves(this.activeRecurlySubscriptionChange),
  99. getSubscription: sinon
  100. .stub()
  101. .resolves(this.activeRecurlyClientSubscription),
  102. pauseSubscriptionByUuid: sinon.stub().resolves(),
  103. resumeSubscriptionByUuid: sinon.stub().resolves(),
  104. },
  105. }
  106. this.SubscriptionUpdater = {
  107. promises: {
  108. updateSubscriptionFromRecurly: sinon.stub().resolves(),
  109. syncSubscription: sinon.stub().resolves(),
  110. syncStripeSubscription: sinon.stub().resolves(),
  111. startFreeTrial: sinon.stub().resolves(),
  112. },
  113. }
  114. this.LimitationsManager = {
  115. promises: {
  116. userHasSubscription: sinon.stub().resolves(),
  117. },
  118. }
  119. this.SubscriptionLocator = {
  120. promises: {
  121. getUsersSubscription: sinon.stub().resolves(this.subscription),
  122. },
  123. }
  124. this.EmailHandler = {
  125. sendEmail: sinon.stub(),
  126. sendDeferredEmail: sinon.stub(),
  127. }
  128. this.UserUpdater = {
  129. promises: {
  130. updateUser: sinon.stub().resolves(),
  131. },
  132. }
  133. this.SubscriptionHandler = SandboxedModule.require(MODULE_PATH, {
  134. requires: {
  135. './RecurlyWrapper': this.RecurlyWrapper,
  136. './RecurlyClient': this.RecurlyClient,
  137. '@overleaf/settings': this.Settings,
  138. '../../models/User': {
  139. User: this.User,
  140. },
  141. './SubscriptionUpdater': this.SubscriptionUpdater,
  142. './SubscriptionLocator': this.SubscriptionLocator,
  143. './LimitationsManager': this.LimitationsManager,
  144. '../Email/EmailHandler': this.EmailHandler,
  145. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  146. '../User/UserUpdater': this.UserUpdater,
  147. '../../infrastructure/Modules': (this.Modules = {
  148. promises: {
  149. hooks: {
  150. fire: sinon.stub(),
  151. },
  152. },
  153. }),
  154. },
  155. })
  156. })
  157. describe('createSubscription', function () {
  158. beforeEach(function () {
  159. this.subscriptionDetails = {
  160. cvv: '123',
  161. number: '12345',
  162. }
  163. this.recurlyTokenIds = { billing: '45555666' }
  164. })
  165. describe('successfully', function () {
  166. beforeEach(async function () {
  167. await this.SubscriptionHandler.promises.createSubscription(
  168. this.user,
  169. this.subscriptionDetails,
  170. this.recurlyTokenIds
  171. )
  172. })
  173. it('should create the subscription with the wrapper', function () {
  174. this.RecurlyWrapper.promises.createSubscription
  175. .calledWith(this.user, this.subscriptionDetails, this.recurlyTokenIds)
  176. .should.equal(true)
  177. })
  178. it('should sync the subscription to the user', function () {
  179. this.SubscriptionUpdater.promises.syncSubscription.calledOnce.should.equal(
  180. true
  181. )
  182. this.SubscriptionUpdater.promises.syncSubscription.args[0][0].should.deep.equal(
  183. this.activeRecurlySubscription
  184. )
  185. this.SubscriptionUpdater.promises.syncSubscription.args[0][1].should.deep.equal(
  186. this.user._id
  187. )
  188. })
  189. it('should not set last trial date if not a trial/the trial_started_at is not set', function () {
  190. this.UserUpdater.promises.updateUser.should.not.have.been.called
  191. })
  192. })
  193. describe('when the subscription is a trial and has a trial_started_at date', function () {
  194. beforeEach(async function () {
  195. this.activeRecurlySubscription.trial_started_at =
  196. '2024-01-01T09:58:35.531+00:00'
  197. await this.SubscriptionHandler.promises.createSubscription(
  198. this.user,
  199. this.subscriptionDetails,
  200. this.recurlyTokenIds
  201. )
  202. })
  203. it('should set the users lastTrial date', function () {
  204. this.UserUpdater.promises.updateUser.should.have.been.calledOnce
  205. expect(this.UserUpdater.promises.updateUser.args[0][0]).to.deep.equal({
  206. _id: this.user_id,
  207. lastTrial: {
  208. $not: {
  209. $gt: new Date(this.activeRecurlySubscription.trial_started_at),
  210. },
  211. },
  212. })
  213. expect(this.UserUpdater.promises.updateUser.args[0][1]).to.deep.equal({
  214. $set: {
  215. lastTrial: new Date(
  216. this.activeRecurlySubscription.trial_started_at
  217. ),
  218. },
  219. })
  220. })
  221. })
  222. describe('when there is already a subscription in Recurly', function () {
  223. beforeEach(function () {
  224. this.RecurlyWrapper.promises.listAccountActiveSubscriptions.resolves([
  225. this.subscription,
  226. ])
  227. })
  228. it('should an error', function () {
  229. expect(
  230. this.SubscriptionHandler.promises.createSubscription(
  231. this.user,
  232. this.subscriptionDetails,
  233. this.recurlyTokenIds
  234. )
  235. ).to.be.rejectedWith('user already has subscription in recurly')
  236. })
  237. })
  238. })
  239. describe('updateSubscription', function () {
  240. beforeEach(function () {
  241. this.user.id = this.activeRecurlySubscription.account.account_code
  242. this.User.findById = (userId, projection) => ({
  243. exec: () => {
  244. userId.should.equal(this.user.id)
  245. return Promise.resolve(this.user)
  246. },
  247. })
  248. })
  249. it('should not fire updatePaidSubscription hook if user has no subscription', async function () {
  250. this.LimitationsManager.promises.userHasSubscription.resolves({
  251. hasSubscription: false,
  252. subscription: null,
  253. })
  254. await this.SubscriptionHandler.promises.updateSubscription(
  255. this.user,
  256. this.plan_code
  257. )
  258. expect(this.Modules.promises.hooks.fire).to.not.have.been.calledWith(
  259. 'updatePaidSubscription',
  260. sinon.match.any,
  261. sinon.match.any,
  262. sinon.match.any
  263. )
  264. })
  265. it('should not fire updatePaidSubscription hook if user has custom subscription', async function () {
  266. this.LimitationsManager.promises.userHasSubscription.resolves({
  267. hasSubscription: true,
  268. subscription: { customAccount: true },
  269. })
  270. await this.SubscriptionHandler.promises.updateSubscription(
  271. this.user,
  272. this.plan_code
  273. )
  274. expect(this.Modules.promises.hooks.fire).to.not.have.been.calledWith(
  275. 'updatePaidSubscription',
  276. sinon.match.any,
  277. sinon.match.any,
  278. sinon.match.any
  279. )
  280. })
  281. it('should fire updatePaidSubscription to update a valid subscription', async function () {
  282. this.LimitationsManager.promises.userHasSubscription.resolves({
  283. hasSubscription: true,
  284. subscription: this.subscription,
  285. })
  286. await this.SubscriptionHandler.promises.updateSubscription(
  287. this.user,
  288. this.plan_code
  289. )
  290. expect(this.Modules.promises.hooks.fire).to.have.been.calledWith(
  291. 'updatePaidSubscription',
  292. this.subscription,
  293. this.plan_code,
  294. this.user._id
  295. )
  296. })
  297. })
  298. describe('cancelPendingSubscriptionChange', function () {
  299. beforeEach(function () {
  300. this.user.id = this.activeRecurlySubscription.account.account_code
  301. this.User.findById = (userId, projection) => ({
  302. exec: () => {
  303. userId.should.equal(this.user.id)
  304. return Promise.resolve(this.user)
  305. },
  306. })
  307. })
  308. it('should not fire cancelPendingPaidSubscriptionChange hook if user has no subscription', async function () {
  309. this.LimitationsManager.promises.userHasSubscription.resolves({
  310. hasSubscription: false,
  311. subscription: null,
  312. })
  313. await this.SubscriptionHandler.promises.cancelPendingSubscriptionChange(
  314. this.user,
  315. this.plan_code
  316. )
  317. expect(this.Modules.promises.hooks.fire).to.not.have.been.calledWith(
  318. 'cancelPendingPaidSubscriptionChange',
  319. sinon.match.any
  320. )
  321. })
  322. it('should fire cancelPendingPaidSubscriptionChange to update a valid subscription', async function () {
  323. this.LimitationsManager.promises.userHasSubscription.resolves({
  324. hasSubscription: true,
  325. subscription: this.subscription,
  326. })
  327. await this.SubscriptionHandler.promises.cancelPendingSubscriptionChange(
  328. this.user,
  329. this.plan_code
  330. )
  331. expect(this.Modules.promises.hooks.fire).to.have.been.calledWith(
  332. 'cancelPendingPaidSubscriptionChange',
  333. this.subscription
  334. )
  335. })
  336. })
  337. describe('cancelSubscription', function () {
  338. describe('with a user without a subscription', function () {
  339. beforeEach(async function () {
  340. this.LimitationsManager.promises.userHasSubscription.resolves({
  341. hasSubscription: false,
  342. subscription: this.subscription,
  343. })
  344. await this.SubscriptionHandler.promises.cancelSubscription(this.user)
  345. })
  346. it('should redirect to the subscription dashboard', function () {
  347. this.RecurlyClient.promises.cancelSubscriptionByUuid.called.should.equal(
  348. false
  349. )
  350. })
  351. })
  352. describe('with a user with a subscription', function () {
  353. beforeEach(async function () {
  354. this.LimitationsManager.promises.userHasSubscription.resolves({
  355. hasSubscription: true,
  356. subscription: this.subscription,
  357. })
  358. await this.SubscriptionHandler.promises.cancelSubscription(this.user)
  359. })
  360. it('should cancel the subscription', function () {
  361. expect(this.Modules.promises.hooks.fire).to.have.been.calledWith(
  362. 'cancelPaidSubscription',
  363. this.subscription
  364. )
  365. })
  366. it('should send the email after 1 hour', function () {
  367. const ONE_HOUR_IN_MS = 1000 * 60 * 60
  368. expect(this.EmailHandler.sendDeferredEmail).to.have.been.calledWith(
  369. 'canceledSubscription',
  370. { to: this.user.email, first_name: this.user.first_name },
  371. ONE_HOUR_IN_MS
  372. )
  373. })
  374. })
  375. })
  376. describe('resumeSubscription', function () {
  377. describe('for a user without a subscription', function () {
  378. beforeEach(async function () {
  379. this.LimitationsManager.promises.userHasSubscription.resolves({
  380. hasSubscription: false,
  381. subscription: this.subscription,
  382. })
  383. })
  384. it('should not make a resume call to recurly', async function () {
  385. expect(
  386. this.SubscriptionHandler.promises.resumeSubscription(this.user)
  387. ).to.be.rejectedWith('No active subscription to resume')
  388. this.RecurlyClient.promises.resumeSubscriptionByUuid.called.should.equal(
  389. false
  390. )
  391. })
  392. })
  393. describe('for a user with a subscription', function () {
  394. beforeEach(async function () {
  395. this.LimitationsManager.promises.userHasSubscription.resolves({
  396. hasSubscription: true,
  397. subscription: {
  398. recurlySubscription_id: this.activeRecurlySubscription.uuid,
  399. recurlyStatus: { state: 'non-trial' },
  400. planCode: 'collaborator',
  401. },
  402. })
  403. })
  404. it('should make a resume call to recurly', async function () {
  405. await this.SubscriptionHandler.promises.resumeSubscription(this.user)
  406. this.RecurlyClient.promises.resumeSubscriptionByUuid.called.should.equal(
  407. true
  408. )
  409. })
  410. })
  411. })
  412. describe('pauseSubscription', function () {
  413. describe('for a user without a subscription', function () {
  414. beforeEach(async function () {
  415. this.LimitationsManager.promises.userHasSubscription.resolves({
  416. hasSubscription: false,
  417. subscription: this.subscription,
  418. })
  419. })
  420. it('should not make a pause call to recurly', async function () {
  421. expect(
  422. this.SubscriptionHandler.promises.pauseSubscription(this.user, 3)
  423. ).to.be.rejectedWith('No active subscription to pause')
  424. this.RecurlyClient.promises.pauseSubscriptionByUuid.called.should.equal(
  425. false
  426. )
  427. })
  428. })
  429. describe('for a user with an annual subscription', function () {
  430. beforeEach(async function () {
  431. this.LimitationsManager.promises.userHasSubscription.resolves({
  432. hasSubscription: false,
  433. subscription: {
  434. recurlySubscription_id: this.activeRecurlySubscription.uuid,
  435. recurlyStatus: { state: 'non-trial' },
  436. planCode: 'collaborator-annual',
  437. },
  438. })
  439. })
  440. it('should not make a pause call to recurly', async function () {
  441. expect(
  442. this.SubscriptionHandler.promises.pauseSubscription(this.user, 3)
  443. ).to.be.rejectedWith('Can only pause monthly individual plans')
  444. this.RecurlyClient.promises.pauseSubscriptionByUuid.called.should.equal(
  445. false
  446. )
  447. })
  448. })
  449. describe('for a user with a subscription', function () {
  450. beforeEach(async function () {
  451. this.LimitationsManager.promises.userHasSubscription.resolves({
  452. hasSubscription: true,
  453. subscription: {
  454. recurlySubscription_id: this.activeRecurlySubscription.uuid,
  455. recurlyStatus: { state: 'non-trial' },
  456. planCode: 'collaborator',
  457. addOns: [],
  458. },
  459. })
  460. })
  461. it('should make a pause call to recurly', async function () {
  462. await this.SubscriptionHandler.promises.pauseSubscription(this.user, 3)
  463. this.RecurlyClient.promises.pauseSubscriptionByUuid.called.should.equal(
  464. true
  465. )
  466. })
  467. })
  468. describe('for a user in a trial', function () {
  469. beforeEach(async function () {
  470. this.LimitationsManager.promises.userHasSubscription.resolves({
  471. hasSubscription: true,
  472. subscription: {
  473. recurlySubscription_id: this.activeRecurlySubscription.uuid,
  474. recurlyStatus: {
  475. state: 'trial',
  476. trialEndsAt: Date.now() + 1000000,
  477. },
  478. planCode: 'collaborator',
  479. },
  480. })
  481. })
  482. it('should not make a pause call to recurly', async function () {
  483. expect(
  484. this.SubscriptionHandler.promises.pauseSubscription(this.user, 3)
  485. ).to.be.rejectedWith('Cannot pause a subscription in a trial')
  486. this.RecurlyClient.promises.pauseSubscriptionByUuid.called.should.equal(
  487. false
  488. )
  489. })
  490. })
  491. describe('for a user with addons', function () {
  492. beforeEach(async function () {
  493. this.LimitationsManager.promises.userHasSubscription.resolves({
  494. hasSubscription: true,
  495. subscription: {
  496. recurlySubscription_id: this.activeRecurlySubscription.uuid,
  497. recurlyStatus: { state: 'non-trial' },
  498. planCode: 'collaborator',
  499. addOns: ['mock-addon'],
  500. },
  501. })
  502. })
  503. it('should not make a pause call to recurly', async function () {
  504. expect(
  505. this.SubscriptionHandler.promises.pauseSubscription(this.user, 3)
  506. ).to.be.rejectedWith('Cannot pause a subscription with addons')
  507. this.RecurlyClient.promises.pauseSubscriptionByUuid.called.should.equal(
  508. false
  509. )
  510. })
  511. })
  512. })
  513. describe('reactivateSubscription', function () {
  514. describe('with a user without a subscription', function () {
  515. beforeEach(async function () {
  516. this.LimitationsManager.promises.userHasSubscription.resolves({
  517. hasSubscription: false,
  518. subscription: this.subscription,
  519. })
  520. await this.SubscriptionHandler.promises.reactivateSubscription(
  521. this.user
  522. )
  523. })
  524. it('should redirect to the subscription dashboard', function () {
  525. this.RecurlyClient.promises.reactivateSubscriptionByUuid.called.should.equal(
  526. false
  527. )
  528. })
  529. it('should not send a notification email', function () {
  530. sinon.assert.notCalled(this.EmailHandler.sendEmail)
  531. })
  532. })
  533. describe('with a user with a subscription', function () {
  534. beforeEach(async function () {
  535. this.LimitationsManager.promises.userHasSubscription.resolves({
  536. hasSubscription: true,
  537. subscription: this.subscription,
  538. })
  539. await this.SubscriptionHandler.promises.reactivateSubscription(
  540. this.user
  541. )
  542. })
  543. it('should reactivate the subscription', function () {
  544. expect(this.Modules.promises.hooks.fire).to.have.been.calledWith(
  545. 'reactivatePaidSubscription',
  546. this.subscription
  547. )
  548. })
  549. it('should send a notification email', function () {
  550. sinon.assert.calledWith(
  551. this.EmailHandler.sendEmail,
  552. 'reactivatedSubscription'
  553. )
  554. })
  555. })
  556. })
  557. describe('syncSubscription', function () {
  558. describe('with an actionable request', function () {
  559. beforeEach(async function () {
  560. this.user.id = this.activeRecurlySubscription.account.account_code
  561. this.User.findById = (userId, projection) => ({
  562. exec: () => {
  563. userId.should.equal(this.user.id)
  564. return Promise.resolve(this.user)
  565. },
  566. })
  567. await this.SubscriptionHandler.promises.syncSubscription(
  568. this.activeRecurlySubscription,
  569. {}
  570. )
  571. })
  572. it('should request the affected subscription from the API', function () {
  573. this.RecurlyWrapper.promises.getSubscription
  574. .calledWith(this.activeRecurlySubscription.uuid)
  575. .should.equal(true)
  576. })
  577. it('should request the account details of the subscription', function () {
  578. const options = this.RecurlyWrapper.promises.getSubscription.args[0][1]
  579. options.includeAccount.should.equal(true)
  580. })
  581. it('should sync the subscription to the user', function () {
  582. this.SubscriptionUpdater.promises.syncSubscription.calledOnce.should.equal(
  583. true
  584. )
  585. this.SubscriptionUpdater.promises.syncSubscription.args[0][0].should.deep.equal(
  586. this.activeRecurlySubscription
  587. )
  588. this.SubscriptionUpdater.promises.syncSubscription.args[0][1].should.deep.equal(
  589. this.user._id
  590. )
  591. })
  592. })
  593. })
  594. describe('attemptPaypalInvoiceCollection', function () {
  595. describe('for credit card users', function () {
  596. beforeEach(async function () {
  597. this.RecurlyWrapper.promises.getBillingInfo.resolves({
  598. paypal_billing_agreement_id: null,
  599. })
  600. await this.SubscriptionHandler.promises.attemptPaypalInvoiceCollection(
  601. this.activeRecurlySubscription.account.account_code
  602. )
  603. })
  604. it('gets billing infos', function () {
  605. sinon.assert.calledWith(
  606. this.RecurlyWrapper.promises.getBillingInfo,
  607. this.activeRecurlySubscription.account.account_code
  608. )
  609. })
  610. it('skips user', function () {
  611. sinon.assert.notCalled(
  612. this.RecurlyWrapper.promises.getAccountPastDueInvoices
  613. )
  614. })
  615. })
  616. describe('for paypal users', function () {
  617. beforeEach(async function () {
  618. this.RecurlyWrapper.promises.getBillingInfo.resolves({
  619. paypal_billing_agreement_id: 'mock-billing-agreement',
  620. })
  621. this.RecurlyWrapper.promises.getAccountPastDueInvoices.resolves([
  622. { invoice_number: 'mock-invoice-number' },
  623. ])
  624. await this.SubscriptionHandler.promises.attemptPaypalInvoiceCollection(
  625. this.activeRecurlySubscription.account.account_code
  626. )
  627. })
  628. it('gets past due invoices', function () {
  629. sinon.assert.calledWith(
  630. this.RecurlyWrapper.promises.getAccountPastDueInvoices,
  631. this.activeRecurlySubscription.account.account_code
  632. )
  633. })
  634. it('calls attemptInvoiceCollection', function () {
  635. sinon.assert.calledWith(
  636. this.RecurlyWrapper.promises.attemptInvoiceCollection,
  637. 'mock-invoice-number'
  638. )
  639. })
  640. })
  641. })
  642. describe('validateNoSubscriptionInRecurly', function () {
  643. describe('with a subscription in recurly', function () {
  644. beforeEach(async function () {
  645. this.RecurlyWrapper.promises.listAccountActiveSubscriptions.resolves([
  646. this.subscription,
  647. ])
  648. this.isValid =
  649. await this.SubscriptionHandler.promises.validateNoSubscriptionInRecurly(
  650. this.user_id
  651. )
  652. })
  653. it('should call RecurlyWrapper.promises.listAccountActiveSubscriptions with the user id', function () {
  654. this.RecurlyWrapper.promises.listAccountActiveSubscriptions
  655. .calledWith(this.user_id)
  656. .should.equal(true)
  657. })
  658. it('should sync the subscription', function () {
  659. this.SubscriptionUpdater.promises.syncSubscription
  660. .calledWith(this.subscription, this.user_id)
  661. .should.equal(true)
  662. })
  663. it('should return false', function () {
  664. expect(this.isValid).to.equal(false)
  665. })
  666. })
  667. describe('with no subscription in recurly', function () {
  668. beforeEach(async function () {
  669. this.isValid =
  670. await this.SubscriptionHandler.promises.validateNoSubscriptionInRecurly(
  671. this.user_id
  672. )
  673. })
  674. it('should be rejected and not sync the subscription', function () {
  675. this.SubscriptionUpdater.promises.syncSubscription.called.should.equal(
  676. false
  677. )
  678. })
  679. it('should return true', function () {
  680. expect(this.isValid).to.equal(true)
  681. })
  682. })
  683. })
  684. })