SubscriptionControllerTests.js 29 KB

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