SubscriptionHandlerTests.js 19 KB

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