SubscriptionHandlerTests.js 18 KB

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