SubscriptionHandlerTests.js 19 KB

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