SubscriptionControllerTests.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { assert, expect } = require('chai')
  4. const MockRequest = require('../helpers/MockRequest')
  5. const MockResponse = require('../helpers/MockResponse')
  6. const modulePath =
  7. '../../../../app/src/Features/Subscription/SubscriptionController'
  8. const Errors = require('../../../../app/src/Features/Errors/Errors')
  9. const SubscriptionErrors = require('../../../../app/src/Features/Subscription/Errors')
  10. const mockSubscriptions = {
  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. describe('SubscriptionController', function () {
  26. beforeEach(function () {
  27. this.user = {
  28. email: 'tom@yahoo.com',
  29. _id: 'one',
  30. signUpDate: new Date('2000-10-01'),
  31. }
  32. this.activeRecurlySubscription =
  33. mockSubscriptions['subscription-123-active']
  34. this.SessionManager = {
  35. getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
  36. getLoggedInUserId: sinon.stub().returns(this.user._id),
  37. getSessionUser: sinon.stub().returns(this.user),
  38. isUserLoggedIn: sinon.stub().returns(true),
  39. }
  40. this.SubscriptionHandler = {
  41. createSubscription: sinon.stub().callsArgWith(3),
  42. updateSubscription: sinon.stub().callsArgWith(3),
  43. reactivateSubscription: sinon.stub().callsArgWith(1),
  44. cancelSubscription: sinon.stub().callsArgWith(1),
  45. syncSubscription: sinon.stub().yields(),
  46. attemptPaypalInvoiceCollection: sinon.stub().yields(),
  47. startFreeTrial: sinon.stub(),
  48. promises: {
  49. createSubscription: sinon.stub().resolves(),
  50. updateSubscription: sinon.stub().resolves(),
  51. reactivateSubscription: sinon.stub().resolves(),
  52. cancelSubscription: sinon.stub().resolves(),
  53. syncSubscription: sinon.stub().resolves(),
  54. attemptPaypalInvoiceCollection: sinon.stub().resolves(),
  55. startFreeTrial: sinon.stub().resolves(),
  56. },
  57. }
  58. this.PlansLocator = { findLocalPlanInSettings: sinon.stub() }
  59. this.LimitationsManager = {
  60. hasPaidSubscription: sinon.stub(),
  61. userHasV1OrV2Subscription: sinon.stub(),
  62. userHasV2Subscription: sinon.stub(),
  63. promises: {
  64. hasPaidSubscription: sinon.stub().resolves(),
  65. userHasV1OrV2Subscription: sinon.stub().resolves(),
  66. userHasV2Subscription: sinon.stub().resolves(),
  67. },
  68. }
  69. this.SubscriptionViewModelBuilder = {
  70. buildUsersSubscriptionViewModel: sinon.stub().callsArgWith(1, null, {}),
  71. buildPlansList: sinon.stub(),
  72. promises: {
  73. buildUsersSubscriptionViewModel: sinon.stub().resolves({}),
  74. },
  75. buildPlansListForSubscriptionDash: sinon
  76. .stub()
  77. .returns({ plans: [], planCodesChangingAtTermEnd: [] }),
  78. }
  79. this.settings = {
  80. coupon_codes: {
  81. upgradeToAnnualPromo: {
  82. student: 'STUDENTCODEHERE',
  83. collaborator: 'COLLABORATORCODEHERE',
  84. },
  85. },
  86. groupPlanModalOptions: {
  87. plan_codes: [],
  88. currencies: [
  89. {
  90. display: 'GBP (£)',
  91. code: 'GBP',
  92. },
  93. ],
  94. sizes: ['42'],
  95. usages: [{ code: 'foo', display: 'Foo' }],
  96. },
  97. apis: {
  98. recurly: {
  99. subdomain: 'sl',
  100. },
  101. },
  102. siteUrl: 'http://de.sharelatex.dev:3000',
  103. }
  104. this.GeoIpLookup = {
  105. isValidCurrencyParam: sinon.stub().returns(true),
  106. getCurrencyCode: sinon.stub(),
  107. promises: {
  108. getCurrencyCode: sinon.stub(),
  109. },
  110. }
  111. this.UserGetter = {
  112. getUser: sinon.stub().callsArgWith(2, null, this.user),
  113. promises: {
  114. getUser: sinon.stub().resolves(this.user),
  115. },
  116. }
  117. this.SplitTestV2Hander = {
  118. promises: {
  119. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  120. },
  121. }
  122. this.SubscriptionHelper = {
  123. generateInitialLocalizedGroupPrice: sinon.stub(),
  124. }
  125. this.SubscriptionController = SandboxedModule.require(modulePath, {
  126. requires: {
  127. '../SplitTests/SplitTestHandler': this.SplitTestV2Hander,
  128. '../Authentication/SessionManager': this.SessionManager,
  129. './SubscriptionHandler': this.SubscriptionHandler,
  130. './SubscriptionHelper': this.SubscriptionHelper,
  131. './PlansLocator': this.PlansLocator,
  132. './SubscriptionViewModelBuilder': this.SubscriptionViewModelBuilder,
  133. './LimitationsManager': this.LimitationsManager,
  134. '../../infrastructure/GeoIpLookup': this.GeoIpLookup,
  135. '@overleaf/settings': this.settings,
  136. '../User/UserGetter': this.UserGetter,
  137. './RecurlyWrapper': (this.RecurlyWrapper = {
  138. updateAccountEmailAddress: sinon.stub().yields(),
  139. }),
  140. './RecurlyEventHandler': {
  141. sendRecurlyAnalyticsEvent: sinon.stub().resolves(),
  142. },
  143. './FeaturesUpdater': (this.FeaturesUpdater = {}),
  144. './GroupPlansData': (this.GroupPlansData = {}),
  145. './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
  146. '../Errors/HttpErrorHandler': (this.HttpErrorHandler = {
  147. unprocessableEntity: sinon.stub(),
  148. }),
  149. './Errors': SubscriptionErrors,
  150. '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
  151. recordEventForUser: sinon.stub(),
  152. recordEventForSession: sinon.stub(),
  153. setUserPropertyForUser: sinon.stub(),
  154. }),
  155. '../../../../modules/managed-users/app/src/ManagedUsersManager':
  156. (this.ManagedUsersManager = {
  157. hasManagedUsersFeature: sinon.stub(),
  158. }),
  159. },
  160. })
  161. this.res = new MockResponse()
  162. this.req = new MockRequest()
  163. this.req.body = {}
  164. this.req.query = { planCode: '123123' }
  165. this.stubbedCurrencyCode = 'GBP'
  166. })
  167. describe('plansPage', function () {
  168. beforeEach(function () {
  169. this.req.ip = '1234.3123.3131.333 313.133.445.666 653.5345.5345.534'
  170. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  171. currencyCode: this.stubbedCurrencyCode,
  172. })
  173. })
  174. describe('groupPlanModal data', function () {
  175. it('should pass local currency if valid', function (done) {
  176. this.res.render = (page, opts) => {
  177. page.should.equal('subscriptions/plans')
  178. opts.groupPlanModalDefaults.currency.should.equal('GBP')
  179. done()
  180. }
  181. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  182. currencyCode: 'GBP',
  183. })
  184. this.SubscriptionController.plansPage(this.req, this.res)
  185. })
  186. it('should fallback to USD when valid', function (done) {
  187. this.res.render = (page, opts) => {
  188. page.should.equal('subscriptions/plans')
  189. opts.groupPlanModalDefaults.currency.should.equal('USD')
  190. done()
  191. }
  192. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  193. currencyCode: 'FOO',
  194. })
  195. this.SubscriptionController.plansPage(this.req, this.res)
  196. })
  197. it('should pass valid options for group plan modal and discard invalid', function (done) {
  198. this.res.render = (page, opts) => {
  199. page.should.equal('subscriptions/plans')
  200. opts.groupPlanModalDefaults.size.should.equal('42')
  201. opts.groupPlanModalDefaults.plan_code.should.equal('collaborator')
  202. opts.groupPlanModalDefaults.currency.should.equal('GBP')
  203. opts.groupPlanModalDefaults.usage.should.equal('foo')
  204. done()
  205. }
  206. this.GeoIpLookup.isValidCurrencyParam.returns(false)
  207. this.req.query = {
  208. number: '42',
  209. currency: 'ABC',
  210. plan: 'does-not-exist',
  211. usage: 'foo',
  212. }
  213. this.SubscriptionController.plansPage(this.req, this.res)
  214. })
  215. })
  216. })
  217. describe('interstitialPaymentPage', function () {
  218. beforeEach(function () {
  219. this.req.ip = '1234.3123.3131.333 313.133.445.666 653.5345.5345.534'
  220. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  221. currencyCode: this.stubbedCurrencyCode,
  222. })
  223. })
  224. describe('with a user without subscription', function () {
  225. it('should render the interstitial payment page', function (done) {
  226. this.res.render = (page, opts) => {
  227. page.should.equal('subscriptions/interstitial-payment')
  228. done()
  229. }
  230. this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
  231. })
  232. })
  233. describe('with a user with subscription', function () {
  234. it('should redirect to the subscription dashboard', function (done) {
  235. this.PlansLocator.findLocalPlanInSettings.returns({})
  236. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(
  237. true
  238. )
  239. this.res.redirect = url => {
  240. url.should.equal('/user/subscription?hasSubscription=true')
  241. done()
  242. }
  243. this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
  244. })
  245. })
  246. })
  247. describe('paymentPage', function () {
  248. beforeEach(function () {
  249. this.req.headers = {}
  250. this.SubscriptionHandler.promises.validateNoSubscriptionInRecurly = sinon
  251. .stub()
  252. .resolves(true)
  253. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  254. currencyCode: this.stubbedCurrencyCode,
  255. })
  256. })
  257. describe('with a user without a subscription', function () {
  258. beforeEach(function () {
  259. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(
  260. false
  261. )
  262. this.PlansLocator.findLocalPlanInSettings.returns({})
  263. })
  264. describe('with a valid plan code', function () {
  265. it('should render the new subscription page', function (done) {
  266. this.res.render = (page, opts) => {
  267. page.should.equal('subscriptions/new-react')
  268. done()
  269. }
  270. this.SubscriptionController.paymentPage(this.req, this.res, done)
  271. })
  272. })
  273. })
  274. describe('with a user with subscription', function () {
  275. it('should redirect to the subscription dashboard', function (done) {
  276. this.PlansLocator.findLocalPlanInSettings.returns({})
  277. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(
  278. true
  279. )
  280. this.res.redirect = url => {
  281. url.should.equal('/user/subscription?hasSubscription=true')
  282. done()
  283. }
  284. this.SubscriptionController.paymentPage(this.req, this.res)
  285. })
  286. })
  287. describe('with an invalid plan code', function () {
  288. it('should return 422 error - Unprocessable Entity', function (done) {
  289. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(
  290. false
  291. )
  292. this.PlansLocator.findLocalPlanInSettings.returns(null)
  293. this.HttpErrorHandler.unprocessableEntity = sinon.spy(
  294. (req, res, message) => {
  295. expect(req).to.exist
  296. expect(res).to.exist
  297. expect(message).to.deep.equal('Plan not found')
  298. done()
  299. }
  300. )
  301. this.SubscriptionController.paymentPage(this.req, this.res)
  302. })
  303. })
  304. describe('which currency to use', function () {
  305. beforeEach(function () {
  306. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(
  307. false
  308. )
  309. this.PlansLocator.findLocalPlanInSettings.returns({})
  310. })
  311. it('should use the set currency from the query string', function (done) {
  312. this.req.query.currency = 'EUR'
  313. this.res.render = (page, opts) => {
  314. opts.currency.should.equal('EUR')
  315. opts.currency.should.not.equal(this.stubbedCurrencyCode)
  316. done()
  317. }
  318. this.SubscriptionController.paymentPage(this.req, this.res)
  319. })
  320. it('should upercase the currency code', function (done) {
  321. this.req.query.currency = 'eur'
  322. this.res.render = (page, opts) => {
  323. opts.currency.should.equal('EUR')
  324. done()
  325. }
  326. this.SubscriptionController.paymentPage(this.req, this.res)
  327. })
  328. it('should use the geo ip currency if non is provided', function (done) {
  329. this.req.query.currency = null
  330. this.res.render = (page, opts) => {
  331. opts.currency.should.equal(this.stubbedCurrencyCode)
  332. done()
  333. }
  334. this.SubscriptionController.paymentPage(this.req, this.res)
  335. })
  336. it('should use the geo ip currency if not valid', function (done) {
  337. this.req.query.currency = 'WAT'
  338. this.GeoIpLookup.isValidCurrencyParam.returns(false)
  339. this.res.render = (page, opts) => {
  340. opts.currency.should.equal(this.stubbedCurrencyCode)
  341. done()
  342. }
  343. this.SubscriptionController.paymentPage(this.req, this.res)
  344. })
  345. })
  346. describe('with a recurly subscription already', function () {
  347. it('should redirect to the subscription dashboard', function (done) {
  348. this.PlansLocator.findLocalPlanInSettings.returns({})
  349. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(
  350. false
  351. )
  352. this.SubscriptionHandler.promises.validateNoSubscriptionInRecurly.resolves(
  353. false
  354. )
  355. this.res.redirect = url => {
  356. url.should.equal('/user/subscription?hasSubscription=true')
  357. done()
  358. }
  359. this.SubscriptionController.paymentPage(this.req, this.res)
  360. })
  361. })
  362. describe('with a user from a restricted country', function () {
  363. beforeEach(function () {
  364. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(
  365. false
  366. )
  367. this.PlansLocator.findLocalPlanInSettings.returns({})
  368. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  369. currencyCode: this.stubbedCurrencyCode,
  370. countryCode: 'KP',
  371. })
  372. })
  373. it('should render the restricted country page', function (done) {
  374. this.res.render = (page, opts) => {
  375. page.should.equal('subscriptions/restricted-country')
  376. done()
  377. }
  378. this.SubscriptionController.paymentPage(this.req, this.res, done)
  379. })
  380. })
  381. })
  382. describe('successfulSubscription', function () {
  383. it('without a personal subscription', function (done) {
  384. this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
  385. {}
  386. )
  387. this.res.redirect = url => {
  388. url.should.equal('/user/subscription/plans')
  389. done()
  390. }
  391. this.SubscriptionController.successfulSubscription(this.req, this.res)
  392. })
  393. it('with a personal subscription', function (done) {
  394. this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
  395. {
  396. personalSubscription: 'foo',
  397. }
  398. )
  399. this.res.render = (url, variables) => {
  400. url.should.equal('subscriptions/successful-subscription-react')
  401. assert.deepEqual(variables, {
  402. title: 'thank_you',
  403. personalSubscription: 'foo',
  404. postCheckoutRedirect: undefined,
  405. })
  406. done()
  407. }
  408. this.SubscriptionController.successfulSubscription(this.req, this.res)
  409. })
  410. it('with an error', function (done) {
  411. this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
  412. undefined
  413. )
  414. this.SubscriptionController.successfulSubscription(
  415. this.req,
  416. this.res,
  417. error => {
  418. assert.isNotNull(error)
  419. done()
  420. }
  421. )
  422. })
  423. })
  424. describe('userSubscriptionPage', function () {
  425. beforeEach(function (done) {
  426. this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
  427. {
  428. personalSubscription: (this.personalSubscription = {
  429. 'personal-subscription': 'mock',
  430. }),
  431. memberGroupSubscriptions: (this.memberGroupSubscriptions = {
  432. 'group-subscriptions': 'mock',
  433. }),
  434. }
  435. )
  436. this.SubscriptionViewModelBuilder.buildPlansList.returns(
  437. (this.plans = { plans: 'mock' })
  438. )
  439. this.SubscriptionViewModelBuilder.buildPlansListForSubscriptionDash.returns(
  440. {
  441. plans: this.plans,
  442. planCodesChangingAtTermEnd: [],
  443. }
  444. )
  445. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
  446. this.res.render = (view, data) => {
  447. this.data = data
  448. expect(view).to.equal('subscriptions/dashboard-react')
  449. done()
  450. }
  451. this.SubscriptionController.userSubscriptionPage(this.req, this.res, done)
  452. })
  453. it('should load the personal, groups and v1 subscriptions', function () {
  454. expect(this.data.personalSubscription).to.deep.equal(
  455. this.personalSubscription
  456. )
  457. expect(this.data.memberGroupSubscriptions).to.deep.equal(
  458. this.memberGroupSubscriptions
  459. )
  460. })
  461. it('should load the user', function () {
  462. expect(this.data.user).to.deep.equal(this.user)
  463. })
  464. it('should load the plans', function () {
  465. expect(this.data.plans).to.deep.equal(this.plans)
  466. })
  467. it('should load an empty list of groups with settings available', function () {
  468. expect(this.data.groupSettingsEnabledFor).to.deep.equal([])
  469. })
  470. })
  471. describe('createSubscription', function () {
  472. beforeEach(function (done) {
  473. this.res = {
  474. sendStatus() {
  475. done()
  476. },
  477. }
  478. sinon.spy(this.res, 'sendStatus')
  479. this.subscriptionDetails = {
  480. card: '1234',
  481. cvv: '123',
  482. }
  483. this.recurlyTokenIds = {
  484. billing: '1234',
  485. threeDSecureActionResult: '5678',
  486. }
  487. this.req.body.recurly_token_id = this.recurlyTokenIds.billing
  488. this.req.body.recurly_three_d_secure_action_result_token_id =
  489. this.recurlyTokenIds.threeDSecureActionResult
  490. this.req.body.subscriptionDetails = this.subscriptionDetails
  491. this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
  492. this.SubscriptionController.createSubscription(this.req, this.res)
  493. })
  494. it('should send the user and subscriptionId to the handler', function (done) {
  495. this.SubscriptionHandler.promises.createSubscription
  496. .calledWithMatch(
  497. this.user,
  498. this.subscriptionDetails,
  499. this.recurlyTokenIds
  500. )
  501. .should.equal(true)
  502. done()
  503. })
  504. it('should redirect to the subscription page', function (done) {
  505. this.res.sendStatus.calledWith(201).should.equal(true)
  506. done()
  507. })
  508. })
  509. describe('createSubscription with errors', function () {
  510. it('should handle users with subscription', function (done) {
  511. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(true)
  512. this.SubscriptionController.createSubscription(this.req, {
  513. sendStatus: status => {
  514. expect(status).to.equal(409)
  515. this.SubscriptionHandler.promises.createSubscription.called.should.equal(
  516. false
  517. )
  518. done()
  519. },
  520. })
  521. })
  522. it('should handle 3DSecure errors/recurly transaction errors', function (done) {
  523. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
  524. this.SubscriptionHandler.promises.createSubscription.rejects(
  525. new SubscriptionErrors.RecurlyTransactionError({})
  526. )
  527. this.HttpErrorHandler.unprocessableEntity = sinon.spy(
  528. (req, res, message) => {
  529. expect(req).to.exist
  530. expect(res).to.exist
  531. expect(message).to.deep.equal('Unknown transaction error')
  532. done()
  533. }
  534. )
  535. this.SubscriptionController.createSubscription(this.req, this.res)
  536. })
  537. it('should handle validation errors', function (done) {
  538. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
  539. this.SubscriptionHandler.promises.createSubscription.rejects(
  540. new Errors.InvalidError('invalid error test')
  541. )
  542. this.HttpErrorHandler.unprocessableEntity = sinon.spy(
  543. (req, res, message) => {
  544. expect(req).to.exist
  545. expect(res).to.exist
  546. expect(message).to.deep.equal('invalid error test')
  547. done()
  548. }
  549. )
  550. this.SubscriptionController.createSubscription(this.req, this.res)
  551. })
  552. it('should throw errors from createSubscription that are not handled', function (done) {
  553. const genericError = new Error('generic error')
  554. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
  555. this.SubscriptionHandler.promises.createSubscription.rejects(genericError)
  556. this.SubscriptionController.createSubscription(
  557. this.req,
  558. this.res,
  559. error => {
  560. expect(error).to.be.instanceof(Error)
  561. expect(error.message).to.equal(genericError.message)
  562. done()
  563. }
  564. )
  565. })
  566. })
  567. describe('updateSubscription via post', function () {
  568. beforeEach(function (done) {
  569. this.res = {
  570. redirect() {
  571. done()
  572. },
  573. }
  574. sinon.spy(this.res, 'redirect')
  575. this.plan_code = '1234'
  576. this.req.body.plan_code = this.plan_code
  577. this.SubscriptionController.updateSubscription(this.req, this.res)
  578. })
  579. it('should send the user and subscriptionId to the handler', function (done) {
  580. this.SubscriptionHandler.updateSubscription
  581. .calledWith(this.user, this.plan_code)
  582. .should.equal(true)
  583. done()
  584. })
  585. it('should redurect to the subscription page', function (done) {
  586. this.res.redirect.calledWith('/user/subscription').should.equal(true)
  587. done()
  588. })
  589. })
  590. describe('updateAccountEmailAddress via put', function () {
  591. it('should send the user and subscriptionId to RecurlyWrapper', function () {
  592. this.res.sendStatus = sinon.spy()
  593. this.SubscriptionController.updateAccountEmailAddress(this.req, this.res)
  594. this.RecurlyWrapper.updateAccountEmailAddress
  595. .calledWith(this.user._id, this.user.email)
  596. .should.equal(true)
  597. })
  598. it('should respond with 200', function () {
  599. this.res.sendStatus = sinon.spy()
  600. this.SubscriptionController.updateAccountEmailAddress(this.req, this.res)
  601. this.res.sendStatus.calledWith(200).should.equal(true)
  602. })
  603. it('should send the error to the next handler when updating recurly account email fails', function (done) {
  604. this.RecurlyWrapper.updateAccountEmailAddress.yields(new Error())
  605. this.next = sinon.spy(error => {
  606. expect(error).instanceOf(Error)
  607. done()
  608. })
  609. this.SubscriptionController.updateAccountEmailAddress(
  610. this.req,
  611. this.res,
  612. this.next
  613. )
  614. })
  615. })
  616. describe('reactivateSubscription', function () {
  617. describe('when the user has permission', function () {
  618. beforeEach(function (done) {
  619. this.res = {
  620. redirect() {
  621. done()
  622. },
  623. }
  624. this.req.assertPermission = sinon.stub()
  625. this.next = sinon.stub().callsFake(error => {
  626. done(error)
  627. })
  628. sinon.spy(this.res, 'redirect')
  629. this.SubscriptionController.reactivateSubscription(
  630. this.req,
  631. this.res,
  632. this.next
  633. )
  634. })
  635. it('should assert the user has permission to reactivate their subscription', function (done) {
  636. this.req.assertPermission
  637. .calledWith('reactivate-subscription')
  638. .should.equal(true)
  639. done()
  640. })
  641. it('should tell the handler to reactivate this user', function (done) {
  642. this.SubscriptionHandler.reactivateSubscription
  643. .calledWith(this.user)
  644. .should.equal(true)
  645. done()
  646. })
  647. it('should redurect to the subscription page', function (done) {
  648. this.res.redirect.calledWith('/user/subscription').should.equal(true)
  649. done()
  650. })
  651. })
  652. describe('when the user does not have permission', function () {
  653. beforeEach(function (done) {
  654. this.res = {
  655. redirect() {
  656. done()
  657. },
  658. }
  659. this.req.assertPermission = sinon.stub().throws()
  660. this.next = sinon.stub().callsFake(() => {
  661. done()
  662. })
  663. sinon.spy(this.res, 'redirect')
  664. this.SubscriptionController.reactivateSubscription(
  665. this.req,
  666. this.res,
  667. this.next
  668. )
  669. })
  670. it('should not reactivate the user', function (done) {
  671. this.req.assertPermission = sinon.stub().throws()
  672. this.SubscriptionHandler.reactivateSubscription.called.should.equal(
  673. false
  674. )
  675. done()
  676. })
  677. it('should call next with an error', function (done) {
  678. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  679. done()
  680. })
  681. })
  682. })
  683. describe('cancelSubscription', function () {
  684. beforeEach(function (done) {
  685. this.res = {
  686. redirect() {
  687. done()
  688. },
  689. }
  690. sinon.spy(this.res, 'redirect')
  691. this.SubscriptionController.cancelSubscription(this.req, this.res)
  692. })
  693. it('should tell the handler to cancel this user', function (done) {
  694. this.SubscriptionHandler.cancelSubscription
  695. .calledWith(this.user)
  696. .should.equal(true)
  697. done()
  698. })
  699. it('should redurect to the subscription page', function (done) {
  700. this.res.redirect
  701. .calledWith('/user/subscription/canceled')
  702. .should.equal(true)
  703. done()
  704. })
  705. })
  706. describe('recurly callback', function () {
  707. describe('with a sync subscription request', function () {
  708. beforeEach(function (done) {
  709. this.req = {
  710. body: {
  711. expired_subscription_notification: {
  712. account: {
  713. account_code: this.user._id,
  714. },
  715. subscription: {
  716. uuid: this.activeRecurlySubscription.uuid,
  717. plan: {
  718. plan_code: 'collaborator',
  719. state: 'active',
  720. },
  721. },
  722. },
  723. },
  724. }
  725. this.res = {
  726. sendStatus() {
  727. done()
  728. },
  729. }
  730. sinon.spy(this.res, 'sendStatus')
  731. this.SubscriptionController.recurlyCallback(this.req, this.res)
  732. })
  733. it('should tell the SubscriptionHandler to process the recurly callback', function (done) {
  734. this.SubscriptionHandler.syncSubscription.called.should.equal(true)
  735. done()
  736. })
  737. it('should send a 200', function (done) {
  738. this.res.sendStatus.calledWith(200)
  739. done()
  740. })
  741. })
  742. describe('with a billing info updated request', function () {
  743. beforeEach(function (done) {
  744. this.req = {
  745. body: {
  746. billing_info_updated_notification: {
  747. account: {
  748. account_code: 'mock-account-code',
  749. },
  750. },
  751. },
  752. }
  753. this.res = {
  754. sendStatus() {
  755. done()
  756. },
  757. }
  758. sinon.spy(this.res, 'sendStatus')
  759. this.SubscriptionController.recurlyCallback(this.req, this.res)
  760. })
  761. it('should call attemptPaypalInvoiceCollection', function (done) {
  762. this.SubscriptionHandler.attemptPaypalInvoiceCollection
  763. .calledWith('mock-account-code')
  764. .should.equal(true)
  765. done()
  766. })
  767. it('should send a 200', function (done) {
  768. this.res.sendStatus.calledWith(200)
  769. done()
  770. })
  771. })
  772. describe('with a non-actionable request', function () {
  773. beforeEach(function (done) {
  774. this.user.id = this.activeRecurlySubscription.account.account_code
  775. this.req = {
  776. body: {
  777. renewed_subscription_notification: {
  778. account: {
  779. account_code: this.user._id,
  780. },
  781. subscription: {
  782. uuid: this.activeRecurlySubscription.uuid,
  783. plan: {
  784. plan_code: 'collaborator',
  785. state: 'active',
  786. },
  787. },
  788. },
  789. },
  790. }
  791. this.res = {
  792. sendStatus() {
  793. done()
  794. },
  795. }
  796. sinon.spy(this.res, 'sendStatus')
  797. this.SubscriptionController.recurlyCallback(this.req, this.res)
  798. })
  799. it('should not call the subscriptionshandler', function () {
  800. this.SubscriptionHandler.syncSubscription.called.should.equal(false)
  801. this.SubscriptionHandler.attemptPaypalInvoiceCollection.called.should.equal(
  802. false
  803. )
  804. })
  805. it('should respond with a 200 status', function () {
  806. this.res.sendStatus.calledWith(200)
  807. })
  808. })
  809. })
  810. describe('renderUpgradeToAnnualPlanPage', function () {
  811. it('should redirect to the plans page if the user does not have a subscription', function (done) {
  812. this.LimitationsManager.userHasV2Subscription.callsArgWith(1, null, false)
  813. this.res.redirect = function (url) {
  814. url.should.equal('/user/subscription/plans')
  815. done()
  816. }
  817. this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  818. this.req,
  819. this.res
  820. )
  821. })
  822. it('should pass the plan code to the view - student', function (done) {
  823. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  824. 1,
  825. null,
  826. true,
  827. { planCode: 'Student free trial 14 days' }
  828. )
  829. this.res.render = function (view, opts) {
  830. view.should.equal('subscriptions/upgradeToAnnual')
  831. opts.planName.should.equal('student')
  832. done()
  833. }
  834. this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  835. this.req,
  836. this.res
  837. )
  838. })
  839. it('should pass the plan code to the view - collaborator', function (done) {
  840. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  841. 1,
  842. null,
  843. true,
  844. { planCode: 'free trial for Collaborator free trial 14 days' }
  845. )
  846. this.res.render = function (view, opts) {
  847. opts.planName.should.equal('collaborator')
  848. done()
  849. }
  850. this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  851. this.req,
  852. this.res
  853. )
  854. })
  855. it('should pass annual as the plan name if the user is already on an annual plan', function (done) {
  856. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  857. 1,
  858. null,
  859. true,
  860. { planCode: 'student annual with free trial' }
  861. )
  862. this.res.render = function (view, opts) {
  863. opts.planName.should.equal('annual')
  864. done()
  865. }
  866. this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  867. this.req,
  868. this.res
  869. )
  870. })
  871. })
  872. describe('processUpgradeToAnnualPlan', function () {
  873. beforeEach(function () {})
  874. it('should tell the subscription handler to update the subscription with the annual plan and apply a coupon code', function (done) {
  875. this.req.body = { planName: 'student' }
  876. this.res.sendStatus = () => {
  877. this.SubscriptionHandler.updateSubscription
  878. .calledWith(this.user, 'student-annual', 'STUDENTCODEHERE')
  879. .should.equal(true)
  880. done()
  881. }
  882. this.SubscriptionController.processUpgradeToAnnualPlan(this.req, this.res)
  883. })
  884. it('should get the collaborator coupon code', function (done) {
  885. this.req.body = { planName: 'collaborator' }
  886. this.res.sendStatus = url => {
  887. this.SubscriptionHandler.updateSubscription
  888. .calledWith(this.user, 'collaborator-annual', 'COLLABORATORCODEHERE')
  889. .should.equal(true)
  890. done()
  891. }
  892. this.SubscriptionController.processUpgradeToAnnualPlan(this.req, this.res)
  893. })
  894. })
  895. })