SubscriptionHandlerTests.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const chai = require('chai')
  4. const { expect } = chai
  5. const MODULE_PATH =
  6. '../../../../app/src/Features/Subscription/SubscriptionHandler'
  7. const mockRecurlySubscriptions = {
  8. 'subscription-123-active': {
  9. uuid: 'subscription-123-active',
  10. plan: {
  11. name: 'Gold',
  12. plan_code: 'gold',
  13. },
  14. current_period_ends_at: new Date(),
  15. state: 'active',
  16. unit_amount_in_cents: 999,
  17. account: {
  18. account_code: 'user-123',
  19. },
  20. },
  21. }
  22. const mockRecurlyClientSubscriptions = {
  23. 'subscription-123-active': {
  24. id: 'subscription-123-recurly-id',
  25. uuid: 'subscription-123-active',
  26. plan: {
  27. name: 'Gold',
  28. code: 'gold',
  29. },
  30. currentPeriodEndsAt: new Date(),
  31. state: 'active',
  32. unitAmount: 10,
  33. account: {
  34. code: 'user-123',
  35. },
  36. },
  37. }
  38. const mockSubscriptionChanges = {
  39. 'subscription-123-active': {
  40. id: 'subscription-change-id',
  41. subscriptionId: 'subscription-123-recurly-id', // not the UUID
  42. },
  43. }
  44. describe('SubscriptionHandler', function () {
  45. beforeEach(function () {
  46. this.callback = sinon.stub()
  47. this.Settings = {
  48. plans: [
  49. {
  50. planCode: 'collaborator',
  51. name: 'Collaborator',
  52. features: {
  53. collaborators: -1,
  54. versioning: true,
  55. },
  56. },
  57. ],
  58. defaultPlanCode: {
  59. collaborators: 0,
  60. versioning: false,
  61. },
  62. }
  63. this.activeRecurlySubscription =
  64. mockRecurlySubscriptions['subscription-123-active']
  65. this.activeRecurlyClientSubscription =
  66. mockRecurlyClientSubscriptions['subscription-123-active']
  67. this.activeRecurlySubscriptionChange =
  68. mockSubscriptionChanges['subscription-123-active']
  69. this.User = {}
  70. this.user = { _id: (this.user_id = 'user_id_here_') }
  71. this.subscription = {
  72. recurlySubscription_id: this.activeRecurlySubscription.uuid,
  73. }
  74. this.RecurlyWrapper = {
  75. getSubscription: sinon
  76. .stub()
  77. .callsArgWith(2, null, this.activeRecurlySubscription),
  78. redeemCoupon: sinon.stub().callsArgWith(2),
  79. createSubscription: sinon
  80. .stub()
  81. .callsArgWith(3, null, this.activeRecurlySubscription),
  82. getBillingInfo: sinon.stub().yields(),
  83. getAccountPastDueInvoices: sinon.stub().yields(),
  84. attemptInvoiceCollection: sinon.stub().yields(),
  85. listAccountActiveSubscriptions: sinon.stub().yields(null, []),
  86. promises: {
  87. getSubscription: sinon.stub().resolves(this.activeRecurlySubscription),
  88. },
  89. }
  90. this.RecurlyClient = {
  91. changeSubscriptionByUuid: sinon
  92. .stub()
  93. .yields(null, this.activeRecurlySubscriptionChange),
  94. getSubscription: sinon
  95. .stub()
  96. .yields(null, this.activeRecurlyClientSubscription),
  97. reactivateSubscriptionByUuid: sinon
  98. .stub()
  99. .yields(null, this.activeRecurlyClientSubscription),
  100. cancelSubscriptionByUuid: sinon.stub().yields(),
  101. promises: {
  102. reactivateSubscriptionByUuid: sinon
  103. .stub()
  104. .resolves(this.activeRecurlyClientSubscription),
  105. cancelSubscriptionByUuid: sinon.stub().resolves(),
  106. },
  107. }
  108. this.SubscriptionUpdater = {
  109. syncSubscription: sinon.stub().yields(),
  110. startFreeTrial: sinon.stub().callsArgWith(1),
  111. promises: {
  112. updateSubscriptionFromRecurly: sinon.stub().resolves(),
  113. },
  114. }
  115. this.LimitationsManager = {
  116. userHasV2Subscription: sinon.stub(),
  117. promises: {
  118. userHasV2Subscription: sinon.stub().resolves(),
  119. },
  120. }
  121. this.EmailHandler = {
  122. sendEmail: sinon.stub(),
  123. sendDeferredEmail: sinon.stub(),
  124. }
  125. this.AnalyticsManager = { recordEventForUser: sinon.stub() }
  126. this.PlansLocator = {
  127. findLocalPlanInSettings: sinon.stub().returns({ planCode: 'plan' }),
  128. }
  129. this.SubscriptionHelper = {
  130. shouldPlanChangeAtTermEnd: sinon.stub(),
  131. }
  132. this.SubscriptionHandler = SandboxedModule.require(MODULE_PATH, {
  133. requires: {
  134. './RecurlyWrapper': this.RecurlyWrapper,
  135. './RecurlyClient': this.RecurlyClient,
  136. '@overleaf/settings': this.Settings,
  137. '../../models/User': {
  138. User: this.User,
  139. },
  140. './SubscriptionUpdater': this.SubscriptionUpdater,
  141. './LimitationsManager': this.LimitationsManager,
  142. '../Email/EmailHandler': this.EmailHandler,
  143. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  144. './PlansLocator': this.PlansLocator,
  145. './SubscriptionHelper': this.SubscriptionHelper,
  146. },
  147. })
  148. })
  149. describe('createSubscription', function () {
  150. beforeEach(function () {
  151. this.subscriptionDetails = {
  152. cvv: '123',
  153. number: '12345',
  154. }
  155. this.recurlyTokenIds = { billing: '45555666' }
  156. })
  157. describe('successfully', function () {
  158. beforeEach(function () {
  159. this.SubscriptionHandler.createSubscription(
  160. this.user,
  161. this.subscriptionDetails,
  162. this.recurlyTokenIds,
  163. this.callback
  164. )
  165. })
  166. it('should create the subscription with the wrapper', function () {
  167. this.RecurlyWrapper.createSubscription
  168. .calledWith(this.user, this.subscriptionDetails, this.recurlyTokenIds)
  169. .should.equal(true)
  170. })
  171. it('should sync the subscription to the user', function () {
  172. this.SubscriptionUpdater.syncSubscription.calledOnce.should.equal(true)
  173. this.SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal(
  174. this.activeRecurlySubscription
  175. )
  176. this.SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal(
  177. this.user._id
  178. )
  179. })
  180. })
  181. describe('when there is already a subscription in Recurly', function () {
  182. beforeEach(function () {
  183. this.RecurlyWrapper.listAccountActiveSubscriptions.yields(null, [
  184. this.subscription,
  185. ])
  186. this.SubscriptionHandler.createSubscription(
  187. this.user,
  188. this.subscriptionDetails,
  189. this.recurlyTokenIds,
  190. this.callback
  191. )
  192. })
  193. it('should an error', function () {
  194. this.callback.calledWith(
  195. new Error('user already has subscription in recurly')
  196. )
  197. })
  198. })
  199. })
  200. function shouldUpdateSubscription() {
  201. it('should update the subscription', function () {
  202. expect(
  203. this.RecurlyClient.changeSubscriptionByUuid
  204. ).to.have.been.calledWith(this.subscription.recurlySubscription_id)
  205. const updateOptions =
  206. this.RecurlyClient.changeSubscriptionByUuid.args[0][1]
  207. updateOptions.planCode.should.equal(this.plan_code)
  208. })
  209. }
  210. function shouldSyncSubscription() {
  211. it('should sync the new subscription to the user', function () {
  212. expect(this.SubscriptionUpdater.syncSubscription).to.have.been.called
  213. this.SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal(
  214. this.activeRecurlySubscription
  215. )
  216. this.SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal(
  217. this.user._id
  218. )
  219. })
  220. }
  221. function testUserWithASubscription(shouldPlanChangeAtTermEnd, timeframe) {
  222. describe(
  223. 'when change should happen with timeframe ' + timeframe,
  224. function () {
  225. beforeEach(function (done) {
  226. this.user.id = this.activeRecurlySubscription.account.account_code
  227. this.User.findById = (userId, projection, callback) => {
  228. userId.should.equal(this.user.id)
  229. callback(null, this.user)
  230. }
  231. this.plan_code = 'collaborator'
  232. this.SubscriptionHelper.shouldPlanChangeAtTermEnd.returns(
  233. shouldPlanChangeAtTermEnd
  234. )
  235. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  236. 1,
  237. null,
  238. true,
  239. this.subscription
  240. )
  241. this.SubscriptionHandler.updateSubscription(
  242. this.user,
  243. this.plan_code,
  244. null,
  245. done
  246. )
  247. })
  248. shouldUpdateSubscription()
  249. shouldSyncSubscription()
  250. it('should update with timeframe ' + timeframe, function () {
  251. const updateOptions =
  252. this.RecurlyClient.changeSubscriptionByUuid.args[0][1]
  253. updateOptions.timeframe.should.equal(timeframe)
  254. })
  255. }
  256. )
  257. }
  258. describe('updateSubscription', function () {
  259. describe('with a user with a subscription', function () {
  260. testUserWithASubscription(false, 'now')
  261. testUserWithASubscription(true, 'term_end')
  262. describe('when plan(s) could not be located in settings', function () {
  263. beforeEach(function () {
  264. this.user.id = this.activeRecurlySubscription.account.account_code
  265. this.User.findById = (userId, projection, callback) => {
  266. userId.should.equal(this.user.id)
  267. callback(null, this.user)
  268. }
  269. this.plan_code = 'collaborator'
  270. this.PlansLocator.findLocalPlanInSettings.returns(null)
  271. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  272. 1,
  273. null,
  274. true,
  275. this.subscription
  276. )
  277. this.SubscriptionHandler.updateSubscription(
  278. this.user,
  279. this.plan_code,
  280. null,
  281. this.callback
  282. )
  283. })
  284. it('should not update the subscription', function () {
  285. this.RecurlyClient.changeSubscriptionByUuid.called.should.equal(false)
  286. })
  287. it('should return an error to the callback', function () {
  288. this.callback
  289. .calledWith(sinon.match.instanceOf(Error))
  290. .should.equal(true)
  291. })
  292. })
  293. })
  294. describe('with a user without a subscription', function () {
  295. beforeEach(function (done) {
  296. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  297. 1,
  298. null,
  299. false
  300. )
  301. this.SubscriptionHandler.updateSubscription(
  302. this.user,
  303. this.plan_code,
  304. null,
  305. done
  306. )
  307. })
  308. it('should redirect to the subscription dashboard', function () {
  309. this.RecurlyClient.changeSubscriptionByUuid.called.should.equal(false)
  310. this.SubscriptionUpdater.syncSubscription.called.should.equal(false)
  311. })
  312. })
  313. describe('with a coupon code', function () {
  314. beforeEach(function (done) {
  315. this.user.id = this.activeRecurlySubscription.account.account_code
  316. this.User.findById = (userId, projection, callback) => {
  317. userId.should.equal(this.user.id)
  318. callback(null, this.user)
  319. }
  320. this.plan_code = 'collaborator'
  321. this.coupon_code = '1231312'
  322. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  323. 1,
  324. null,
  325. true,
  326. this.subscription
  327. )
  328. this.SubscriptionHandler.updateSubscription(
  329. this.user,
  330. this.plan_code,
  331. this.coupon_code,
  332. done
  333. )
  334. })
  335. it('should get the users account', function () {
  336. this.RecurlyWrapper.getSubscription
  337. .calledWith(this.activeRecurlySubscription.uuid)
  338. .should.equal(true)
  339. })
  340. it('should redeem the coupon', function (done) {
  341. this.RecurlyWrapper.redeemCoupon
  342. .calledWith(
  343. this.activeRecurlySubscription.account.account_code,
  344. this.coupon_code
  345. )
  346. .should.equal(true)
  347. done()
  348. })
  349. it('should update the subscription', function () {
  350. expect(this.RecurlyClient.changeSubscriptionByUuid).to.be.calledWith(
  351. this.subscription.recurlySubscription_id
  352. )
  353. const updateOptions =
  354. this.RecurlyClient.changeSubscriptionByUuid.args[0][1]
  355. updateOptions.planCode.should.equal(this.plan_code)
  356. })
  357. })
  358. })
  359. describe('cancelSubscription', function () {
  360. describe('with a user without a subscription', function () {
  361. beforeEach(function (done) {
  362. this.LimitationsManager.promises.userHasV2Subscription.callsArgWith(
  363. 1,
  364. null,
  365. false,
  366. this.subscription
  367. )
  368. this.SubscriptionHandler.cancelSubscription(this.user, done)
  369. })
  370. it('should redirect to the subscription dashboard', function () {
  371. this.RecurlyClient.cancelSubscriptionByUuid.called.should.equal(false)
  372. })
  373. })
  374. describe('with a user with a subscription', function () {
  375. beforeEach(function (done) {
  376. this.LimitationsManager.promises.userHasV2Subscription.resolves({
  377. hasSubscription: true,
  378. subscription: this.subscription,
  379. })
  380. this.SubscriptionHandler.cancelSubscription(this.user, done)
  381. })
  382. it('should cancel the subscription', function () {
  383. this.RecurlyClient.promises.cancelSubscriptionByUuid.called.should.equal(
  384. true
  385. )
  386. this.RecurlyClient.promises.cancelSubscriptionByUuid
  387. .calledWith(this.subscription.recurlySubscription_id)
  388. .should.equal(true)
  389. })
  390. it('should send the email after 1 hour', function () {
  391. const ONE_HOUR_IN_MS = 1000 * 60 * 60
  392. expect(this.EmailHandler.sendDeferredEmail).to.have.been.calledWith(
  393. 'canceledSubscription',
  394. { to: this.user.email, first_name: this.user.first_name },
  395. ONE_HOUR_IN_MS
  396. )
  397. })
  398. })
  399. })
  400. describe('reactivateSubscription', function () {
  401. describe('with a user without a subscription', function () {
  402. beforeEach(function (done) {
  403. this.LimitationsManager.promises.userHasV2Subscription.resolves({
  404. hasSubscription: false,
  405. subscription: this.subscription,
  406. })
  407. this.SubscriptionHandler.reactivateSubscription(this.user, done)
  408. })
  409. it('should redirect to the subscription dashboard', function () {
  410. this.RecurlyClient.reactivateSubscriptionByUuid.called.should.equal(
  411. false
  412. )
  413. })
  414. it('should not send a notification email', function () {
  415. sinon.assert.notCalled(this.EmailHandler.sendEmail)
  416. })
  417. })
  418. describe('with a user with a subscription', function () {
  419. beforeEach(function (done) {
  420. this.LimitationsManager.promises.userHasV2Subscription.resolves({
  421. hasSubscription: true,
  422. subscription: this.subscription,
  423. })
  424. this.SubscriptionHandler.reactivateSubscription(this.user, done)
  425. })
  426. it('should reactivate the subscription', function () {
  427. this.RecurlyClient.promises.reactivateSubscriptionByUuid.called.should.equal(
  428. true
  429. )
  430. this.RecurlyClient.promises.reactivateSubscriptionByUuid
  431. .calledWith(this.subscription.recurlySubscription_id)
  432. .should.equal(true)
  433. })
  434. it('should send a notification email', function () {
  435. sinon.assert.calledWith(
  436. this.EmailHandler.sendEmail,
  437. 'reactivatedSubscription'
  438. )
  439. })
  440. })
  441. })
  442. describe('syncSubscription', function () {
  443. describe('with an actionable request', function () {
  444. beforeEach(function (done) {
  445. this.user.id = this.activeRecurlySubscription.account.account_code
  446. this.User.findById = (userId, projection, callback) => {
  447. userId.should.equal(this.user.id)
  448. callback(null, this.user)
  449. }
  450. this.SubscriptionHandler.syncSubscription(
  451. this.activeRecurlySubscription,
  452. {},
  453. done
  454. )
  455. })
  456. it('should request the affected subscription from the API', function () {
  457. this.RecurlyWrapper.getSubscription
  458. .calledWith(this.activeRecurlySubscription.uuid)
  459. .should.equal(true)
  460. })
  461. it('should request the account details of the subscription', function () {
  462. const options = this.RecurlyWrapper.getSubscription.args[0][1]
  463. options.includeAccount.should.equal(true)
  464. })
  465. it('should sync the subscription to the user', function () {
  466. this.SubscriptionUpdater.syncSubscription.calledOnce.should.equal(true)
  467. this.SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal(
  468. this.activeRecurlySubscription
  469. )
  470. this.SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal(
  471. this.user._id
  472. )
  473. })
  474. })
  475. })
  476. describe('attemptPaypalInvoiceCollection', function () {
  477. describe('for credit card users', function () {
  478. beforeEach(function (done) {
  479. this.RecurlyWrapper.getBillingInfo.yields(null, {
  480. paypal_billing_agreement_id: null,
  481. })
  482. this.SubscriptionHandler.attemptPaypalInvoiceCollection(
  483. this.activeRecurlySubscription.account.account_code,
  484. done
  485. )
  486. })
  487. it('gets billing infos', function () {
  488. sinon.assert.calledWith(
  489. this.RecurlyWrapper.getBillingInfo,
  490. this.activeRecurlySubscription.account.account_code
  491. )
  492. })
  493. it('skips user', function () {
  494. sinon.assert.notCalled(this.RecurlyWrapper.getAccountPastDueInvoices)
  495. })
  496. })
  497. describe('for paypal users', function () {
  498. beforeEach(function (done) {
  499. this.RecurlyWrapper.getBillingInfo.yields(null, {
  500. paypal_billing_agreement_id: 'mock-billing-agreement',
  501. })
  502. this.RecurlyWrapper.getAccountPastDueInvoices.yields(null, [
  503. { invoice_number: 'mock-invoice-number' },
  504. ])
  505. this.SubscriptionHandler.attemptPaypalInvoiceCollection(
  506. this.activeRecurlySubscription.account.account_code,
  507. done
  508. )
  509. })
  510. it('gets past due invoices', function () {
  511. sinon.assert.calledWith(
  512. this.RecurlyWrapper.getAccountPastDueInvoices,
  513. this.activeRecurlySubscription.account.account_code
  514. )
  515. })
  516. it('calls attemptInvoiceCollection', function () {
  517. sinon.assert.calledWith(
  518. this.RecurlyWrapper.attemptInvoiceCollection,
  519. 'mock-invoice-number'
  520. )
  521. })
  522. })
  523. })
  524. describe('validateNoSubscriptionInRecurly', function () {
  525. describe('with a subscription in recurly', function () {
  526. beforeEach(function () {
  527. this.RecurlyWrapper.listAccountActiveSubscriptions.yields(null, [
  528. this.subscription,
  529. ])
  530. this.SubscriptionHandler.validateNoSubscriptionInRecurly(
  531. this.user_id,
  532. this.callback
  533. )
  534. })
  535. it('should call RecurlyWrapper.listAccountActiveSubscriptions with the user id', function () {
  536. this.RecurlyWrapper.listAccountActiveSubscriptions
  537. .calledWith(this.user_id)
  538. .should.equal(true)
  539. })
  540. it('should sync the subscription', function () {
  541. this.SubscriptionUpdater.syncSubscription
  542. .calledWith(this.subscription, this.user_id)
  543. .should.equal(true)
  544. })
  545. it('should call the callback with valid == false', function () {
  546. this.callback.calledWith(null, false).should.equal(true)
  547. })
  548. })
  549. describe('with no subscription in recurly', function () {
  550. beforeEach(function () {
  551. this.SubscriptionHandler.validateNoSubscriptionInRecurly(
  552. this.user_id,
  553. this.callback
  554. )
  555. })
  556. it('should not sync the subscription', function () {
  557. this.SubscriptionUpdater.syncSubscription.called.should.equal(false)
  558. })
  559. it('should call the callback with valid == true', function () {
  560. this.callback.calledWith(null, true).should.equal(true)
  561. })
  562. })
  563. })
  564. })