SubscriptionControllerTests.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /* eslint-disable
  2. max-len,
  3. mocha/handle-done-callback,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const sinon = require('sinon')
  16. const should = require('chai').should()
  17. const { expect } = require('chai')
  18. const MockRequest = require('../helpers/MockRequest')
  19. const MockResponse = require('../helpers/MockResponse')
  20. const modulePath =
  21. '../../../../app/src/Features/Subscription/SubscriptionController'
  22. const Errors = require('../../../../app/src/Features/Errors/Errors')
  23. const SubscriptionErrors = require('../../../../app/src/Features/Subscription/Errors')
  24. const mockSubscriptions = {
  25. 'subscription-123-active': {
  26. uuid: 'subscription-123-active',
  27. plan: {
  28. name: 'Gold',
  29. plan_code: 'gold'
  30. },
  31. current_period_ends_at: new Date(),
  32. state: 'active',
  33. unit_amount_in_cents: 999,
  34. account: {
  35. account_code: 'user-123'
  36. }
  37. }
  38. }
  39. describe('SubscriptionController', function() {
  40. beforeEach(function() {
  41. this.user = {
  42. email: 'tom@yahoo.com',
  43. _id: 'one',
  44. signUpDate: new Date('2000-10-01')
  45. }
  46. this.activeRecurlySubscription =
  47. mockSubscriptions['subscription-123-active']
  48. this.AuthenticationController = {
  49. getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
  50. getLoggedInUserId: sinon.stub().returns(this.user._id),
  51. getSessionUser: sinon.stub().returns(this.user),
  52. isUserLoggedIn: sinon.stub().returns(true)
  53. }
  54. this.SubscriptionHandler = {
  55. createSubscription: sinon.stub().callsArgWith(3),
  56. updateSubscription: sinon.stub().callsArgWith(3),
  57. reactivateSubscription: sinon.stub().callsArgWith(1),
  58. cancelSubscription: sinon.stub().callsArgWith(1),
  59. syncSubscription: sinon.stub().yields(),
  60. attemptPaypalInvoiceCollection: sinon.stub().yields(),
  61. startFreeTrial: sinon.stub()
  62. }
  63. this.PlansLocator = { findLocalPlanInSettings: sinon.stub() }
  64. this.LimitationsManager = {
  65. hasPaidSubscription: sinon.stub(),
  66. userHasV1OrV2Subscription: sinon.stub(),
  67. userHasV2Subscription: sinon.stub()
  68. }
  69. this.SubscriptionViewModelBuilder = {
  70. buildUsersSubscriptionViewModel: sinon.stub().callsArgWith(1, null, {}),
  71. buildPlansList: sinon.stub()
  72. }
  73. this.settings = {
  74. coupon_codes: {
  75. upgradeToAnnualPromo: {
  76. student: 'STUDENTCODEHERE',
  77. collaborator: 'COLLABORATORCODEHERE'
  78. }
  79. },
  80. apis: {
  81. recurly: {
  82. subdomain: 'sl'
  83. }
  84. },
  85. siteUrl: 'http://de.sharelatex.dev:3000',
  86. gaExperiments: {}
  87. }
  88. this.GeoIpLookup = { getCurrencyCode: sinon.stub() }
  89. this.UserGetter = {
  90. getUser: sinon.stub().callsArgWith(2, null, this.user)
  91. }
  92. this.SubscriptionController = SandboxedModule.require(modulePath, {
  93. globals: {
  94. console: console
  95. },
  96. requires: {
  97. '../Authentication/AuthenticationController': this
  98. .AuthenticationController,
  99. './SubscriptionHandler': this.SubscriptionHandler,
  100. './PlansLocator': this.PlansLocator,
  101. './SubscriptionViewModelBuilder': this.SubscriptionViewModelBuilder,
  102. './LimitationsManager': this.LimitationsManager,
  103. '../../infrastructure/GeoIpLookup': this.GeoIpLookup,
  104. 'logger-sharelatex': {
  105. log() {},
  106. warn() {}
  107. },
  108. 'settings-sharelatex': this.settings,
  109. '../User/UserGetter': this.UserGetter,
  110. './RecurlyWrapper': (this.RecurlyWrapper = {
  111. updateAccountEmailAddress: sinon.stub().yields()
  112. }),
  113. './FeaturesUpdater': (this.FeaturesUpdater = {}),
  114. './GroupPlansData': (this.GroupPlansData = {}),
  115. './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
  116. '../Errors/HttpErrorHandler': (this.HttpErrorHandler = {
  117. unprocessableEntity: sinon.stub()
  118. }),
  119. '../Errors/Errors': Errors,
  120. './Errors': SubscriptionErrors
  121. }
  122. })
  123. this.res = new MockResponse()
  124. this.req = new MockRequest()
  125. this.req.body = {}
  126. this.req.query = { planCode: '123123' }
  127. return (this.stubbedCurrencyCode = 'GBP')
  128. })
  129. describe('plansPage', function() {
  130. beforeEach(function() {
  131. this.req.ip = '1234.3123.3131.333 313.133.445.666 653.5345.5345.534'
  132. return this.GeoIpLookup.getCurrencyCode.callsArgWith(
  133. 1,
  134. null,
  135. this.stubbedCurrencyCode
  136. )
  137. })
  138. describe('when user is logged in', function(done) {
  139. beforeEach(function(done) {
  140. this.res.callback = done
  141. return this.SubscriptionController.plansPage(this.req, this.res)
  142. })
  143. it('should fetch the current user', function(done) {
  144. this.UserGetter.getUser.callCount.should.equal(1)
  145. return done()
  146. })
  147. describe('not dependant on logged in state', function(done) {
  148. // these could have been put in 'when user is not logged in' too
  149. it('should set the recommended currency from the geoiplookup', function(done) {
  150. this.res.renderedVariables.recomendedCurrency.should.equal(
  151. this.stubbedCurrencyCode
  152. )
  153. this.GeoIpLookup.getCurrencyCode
  154. .calledWith(this.req.ip)
  155. .should.equal(true)
  156. return done()
  157. })
  158. it('should include data for features table', function(done) {
  159. this.res.renderedVariables.planFeatures.length.should.not.equal(0)
  160. return done()
  161. })
  162. })
  163. })
  164. describe('when user is not logged in', function(done) {
  165. beforeEach(function(done) {
  166. this.res.callback = done
  167. this.AuthenticationController.getLoggedInUserId = sinon
  168. .stub()
  169. .returns(null)
  170. return this.SubscriptionController.plansPage(this.req, this.res)
  171. })
  172. it('should not fetch the current user', function(done) {
  173. this.UserGetter.getUser.callCount.should.equal(0)
  174. return done()
  175. })
  176. })
  177. })
  178. describe('paymentPage', function() {
  179. beforeEach(function() {
  180. this.req.headers = {}
  181. this.SubscriptionHandler.validateNoSubscriptionInRecurly = sinon
  182. .stub()
  183. .yields(null, true)
  184. return this.GeoIpLookup.getCurrencyCode.callsArgWith(
  185. 1,
  186. null,
  187. this.stubbedCurrencyCode
  188. )
  189. })
  190. describe('with a user without a subscription', function() {
  191. beforeEach(function() {
  192. this.LimitationsManager.userHasV1OrV2Subscription.callsArgWith(
  193. 1,
  194. null,
  195. false
  196. )
  197. return this.PlansLocator.findLocalPlanInSettings.returns({})
  198. })
  199. describe('with a valid plan code', function() {
  200. it('should render the new subscription page', function(done) {
  201. this.res.render = (page, opts) => {
  202. page.should.equal('subscriptions/new')
  203. return done()
  204. }
  205. return this.SubscriptionController.paymentPage(this.req, this.res)
  206. })
  207. })
  208. })
  209. describe('with a user with subscription', function() {
  210. it('should redirect to the subscription dashboard', function(done) {
  211. this.PlansLocator.findLocalPlanInSettings.returns({})
  212. this.LimitationsManager.userHasV1OrV2Subscription.callsArgWith(
  213. 1,
  214. null,
  215. true
  216. )
  217. this.res.redirect = url => {
  218. url.should.equal('/user/subscription?hasSubscription=true')
  219. return done()
  220. }
  221. return this.SubscriptionController.paymentPage(this.req, this.res)
  222. })
  223. })
  224. describe('with an invalid plan code', function() {
  225. it('should return 422 error - Unprocessable Entity', function(done) {
  226. this.LimitationsManager.userHasV1OrV2Subscription.callsArgWith(
  227. 1,
  228. null,
  229. false
  230. )
  231. this.PlansLocator.findLocalPlanInSettings.returns(null)
  232. this.HttpErrorHandler.unprocessableEntity = sinon.spy(
  233. (req, res, message) => {
  234. expect(req).to.exist
  235. expect(res).to.exist
  236. expect(message).to.deep.equal('Plan not found')
  237. done()
  238. }
  239. )
  240. return this.SubscriptionController.paymentPage(
  241. this.req,
  242. this.res,
  243. this.next
  244. )
  245. })
  246. })
  247. describe('which currency to use', function() {
  248. beforeEach(function() {
  249. this.LimitationsManager.userHasV1OrV2Subscription.callsArgWith(
  250. 1,
  251. null,
  252. false
  253. )
  254. return this.PlansLocator.findLocalPlanInSettings.returns({})
  255. })
  256. it('should use the set currency from the query string', function(done) {
  257. this.req.query.currency = 'EUR'
  258. this.res.render = (page, opts) => {
  259. opts.currency.should.equal('EUR')
  260. opts.currency.should.not.equal(this.stubbedCurrencyCode)
  261. return done()
  262. }
  263. return this.SubscriptionController.paymentPage(this.req, this.res)
  264. })
  265. it('should upercase the currency code', function(done) {
  266. this.req.query.currency = 'eur'
  267. this.res.render = (page, opts) => {
  268. opts.currency.should.equal('EUR')
  269. return done()
  270. }
  271. return this.SubscriptionController.paymentPage(this.req, this.res)
  272. })
  273. it('should use the geo ip currency if non is provided', function(done) {
  274. this.req.query.currency = null
  275. this.res.render = (page, opts) => {
  276. opts.currency.should.equal(this.stubbedCurrencyCode)
  277. return done()
  278. }
  279. return this.SubscriptionController.paymentPage(this.req, this.res)
  280. })
  281. })
  282. describe('with a recurly subscription already', function() {
  283. it('should redirect to the subscription dashboard', function(done) {
  284. this.PlansLocator.findLocalPlanInSettings.returns({})
  285. this.LimitationsManager.userHasV1OrV2Subscription.callsArgWith(
  286. 1,
  287. null,
  288. false
  289. )
  290. this.SubscriptionHandler.validateNoSubscriptionInRecurly = sinon
  291. .stub()
  292. .yields(null, false)
  293. this.res.redirect = url => {
  294. url.should.equal('/user/subscription?hasSubscription=true')
  295. return done()
  296. }
  297. return this.SubscriptionController.paymentPage(this.req, this.res)
  298. })
  299. })
  300. })
  301. describe('successful_subscription', function() {
  302. beforeEach(function(done) {
  303. this.SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(
  304. 1,
  305. null,
  306. {}
  307. )
  308. this.res.callback = done
  309. return this.SubscriptionController.successful_subscription(
  310. this.req,
  311. this.res
  312. )
  313. })
  314. })
  315. describe('userSubscriptionPage', function() {
  316. beforeEach(function(done) {
  317. this.SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(
  318. 1,
  319. null,
  320. {
  321. personalSubscription: (this.personalSubscription = {
  322. 'personal-subscription': 'mock'
  323. }),
  324. memberGroupSubscriptions: (this.memberGroupSubscriptions = {
  325. 'group-subscriptions': 'mock'
  326. })
  327. }
  328. )
  329. this.SubscriptionViewModelBuilder.buildPlansList.returns(
  330. (this.plans = { plans: 'mock' })
  331. )
  332. this.LimitationsManager.userHasV1OrV2Subscription.callsArgWith(
  333. 1,
  334. null,
  335. false
  336. )
  337. this.res.render = (view, data) => {
  338. this.data = data
  339. expect(view).to.equal('subscriptions/dashboard')
  340. return done()
  341. }
  342. return this.SubscriptionController.userSubscriptionPage(
  343. this.req,
  344. this.res
  345. )
  346. })
  347. it('should load the personal, groups and v1 subscriptions', function() {
  348. expect(this.data.personalSubscription).to.deep.equal(
  349. this.personalSubscription
  350. )
  351. return expect(this.data.memberGroupSubscriptions).to.deep.equal(
  352. this.memberGroupSubscriptions
  353. )
  354. })
  355. it('should load the user', function() {
  356. return expect(this.data.user).to.deep.equal(this.user)
  357. })
  358. it('should load the plans', function() {
  359. return expect(this.data.plans).to.deep.equal(this.plans)
  360. })
  361. })
  362. describe('createSubscription', function() {
  363. beforeEach(function(done) {
  364. this.res = {
  365. sendStatus() {
  366. return done()
  367. }
  368. }
  369. sinon.spy(this.res, 'sendStatus')
  370. this.subscriptionDetails = {
  371. card: '1234',
  372. cvv: '123'
  373. }
  374. this.recurlyTokenIds = {
  375. billing: '1234',
  376. threeDSecureActionResult: '5678'
  377. }
  378. this.req.body.recurly_token_id = this.recurlyTokenIds.billing
  379. this.req.body.recurly_three_d_secure_action_result_token_id = this.recurlyTokenIds.threeDSecureActionResult
  380. this.req.body.subscriptionDetails = this.subscriptionDetails
  381. this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
  382. return this.SubscriptionController.createSubscription(this.req, this.res)
  383. })
  384. it('should send the user and subscriptionId to the handler', function(done) {
  385. this.SubscriptionHandler.createSubscription
  386. .calledWithMatch(
  387. this.user,
  388. this.subscriptionDetails,
  389. this.recurlyTokenIds
  390. )
  391. .should.equal(true)
  392. return done()
  393. })
  394. it('should redurect to the subscription page', function(done) {
  395. this.res.sendStatus.calledWith(201).should.equal(true)
  396. return done()
  397. })
  398. })
  399. describe('createSubscription with errors', function() {
  400. it('should handle users with subscription', function(done) {
  401. this.LimitationsManager.userHasV1OrV2Subscription.yields(null, true)
  402. this.SubscriptionController.createSubscription(this.req, {
  403. sendStatus: status => {
  404. expect(status).to.equal(409)
  405. this.SubscriptionHandler.createSubscription.called.should.equal(false)
  406. done()
  407. }
  408. })
  409. })
  410. it('should handle 3DSecure errors', function(done) {
  411. this.next = sinon.stub()
  412. this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
  413. this.SubscriptionHandler.createSubscription.yields(
  414. new SubscriptionErrors.RecurlyTransactionError({})
  415. )
  416. this.HttpErrorHandler.unprocessableEntity = sinon.spy(
  417. (req, res, message) => {
  418. expect(req).to.exist
  419. expect(res).to.exist
  420. expect(message).to.deep.equal('Unknown transaction error')
  421. done()
  422. }
  423. )
  424. this.SubscriptionController.createSubscription(this.req, this.res)
  425. })
  426. it('should handle validation errors', function(done) {
  427. this.next = sinon.stub()
  428. this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
  429. this.SubscriptionHandler.createSubscription.yields(
  430. new Errors.InvalidError('invalid error test')
  431. )
  432. this.HttpErrorHandler.unprocessableEntity = sinon.spy(
  433. (req, res, message) => {
  434. expect(req).to.exist
  435. expect(res).to.exist
  436. expect(message).to.deep.equal('invalid error test')
  437. done()
  438. }
  439. )
  440. this.SubscriptionController.createSubscription(this.req, this.res)
  441. })
  442. it('should handle recurly errors', function(done) {
  443. this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
  444. this.SubscriptionHandler.createSubscription.yields(
  445. new SubscriptionErrors.RecurlyTransactionError({})
  446. )
  447. this.HttpErrorHandler.unprocessableEntity = sinon.spy(
  448. (req, res, info) => {
  449. expect(req).to.exist
  450. expect(res).to.exist
  451. expect(info).to.deep.equal('Unknown transaction error')
  452. done()
  453. }
  454. )
  455. return this.SubscriptionController.createSubscription(this.req, this.res)
  456. })
  457. it('should handle invalid error', function(done) {
  458. this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
  459. this.SubscriptionHandler.createSubscription.yields(
  460. new Errors.InvalidError({})
  461. )
  462. this.HttpErrorHandler.unprocessableEntity = sinon.spy((req, res) => {
  463. expect(req).to.exist
  464. expect(res).to.exist
  465. done()
  466. })
  467. return this.SubscriptionController.createSubscription(this.req, this.res)
  468. })
  469. })
  470. describe('updateSubscription via post', function() {
  471. beforeEach(function(done) {
  472. this.res = {
  473. redirect() {
  474. return done()
  475. }
  476. }
  477. sinon.spy(this.res, 'redirect')
  478. this.plan_code = '1234'
  479. this.req.body.plan_code = this.plan_code
  480. return this.SubscriptionController.updateSubscription(this.req, this.res)
  481. })
  482. it('should send the user and subscriptionId to the handler', function(done) {
  483. this.SubscriptionHandler.updateSubscription
  484. .calledWith(this.user, this.plan_code)
  485. .should.equal(true)
  486. return done()
  487. })
  488. it('should redurect to the subscription page', function(done) {
  489. this.res.redirect.calledWith('/user/subscription').should.equal(true)
  490. return done()
  491. })
  492. })
  493. describe('updateAccountEmailAddress via put', function() {
  494. it('should send the user and subscriptionId to RecurlyWrapper', function() {
  495. this.res.sendStatus = sinon.spy()
  496. this.SubscriptionController.updateAccountEmailAddress(this.req, this.res)
  497. this.RecurlyWrapper.updateAccountEmailAddress
  498. .calledWith(this.user._id, this.user.email)
  499. .should.equal(true)
  500. })
  501. it('should respond with 200', function() {
  502. this.res.sendStatus = sinon.spy()
  503. this.SubscriptionController.updateAccountEmailAddress(this.req, this.res)
  504. this.res.sendStatus.calledWith(200).should.equal(true)
  505. })
  506. it('should send the error to the next handler when updating recurly account email fails', function(done) {
  507. this.RecurlyWrapper.updateAccountEmailAddress.yields(new Error())
  508. this.next = sinon.spy(error => {
  509. expect(error).instanceOf(Error)
  510. done()
  511. })
  512. this.SubscriptionController.updateAccountEmailAddress(
  513. this.req,
  514. this.res,
  515. this.next
  516. )
  517. })
  518. })
  519. describe('reactivateSubscription', function() {
  520. beforeEach(function(done) {
  521. this.res = {
  522. redirect() {
  523. return done()
  524. }
  525. }
  526. sinon.spy(this.res, 'redirect')
  527. return this.SubscriptionController.reactivateSubscription(
  528. this.req,
  529. this.res
  530. )
  531. })
  532. it('should tell the handler to reactivate this user', function(done) {
  533. this.SubscriptionHandler.reactivateSubscription
  534. .calledWith(this.user)
  535. .should.equal(true)
  536. return done()
  537. })
  538. it('should redurect to the subscription page', function(done) {
  539. this.res.redirect.calledWith('/user/subscription').should.equal(true)
  540. return done()
  541. })
  542. })
  543. describe('cancelSubscription', function() {
  544. beforeEach(function(done) {
  545. this.res = {
  546. redirect() {
  547. return done()
  548. }
  549. }
  550. sinon.spy(this.res, 'redirect')
  551. return this.SubscriptionController.cancelSubscription(this.req, this.res)
  552. })
  553. it('should tell the handler to cancel this user', function(done) {
  554. this.SubscriptionHandler.cancelSubscription
  555. .calledWith(this.user)
  556. .should.equal(true)
  557. return done()
  558. })
  559. it('should redurect to the subscription page', function(done) {
  560. this.res.redirect
  561. .calledWith('/user/subscription/canceled')
  562. .should.equal(true)
  563. return done()
  564. })
  565. })
  566. describe('recurly callback', function() {
  567. describe('with a sync subscription request', function() {
  568. beforeEach(function(done) {
  569. this.req = {
  570. body: {
  571. expired_subscription_notification: {
  572. subscription: {
  573. uuid: this.activeRecurlySubscription.uuid
  574. }
  575. }
  576. }
  577. }
  578. this.res = {
  579. sendStatus() {
  580. return done()
  581. }
  582. }
  583. sinon.spy(this.res, 'sendStatus')
  584. return this.SubscriptionController.recurlyCallback(this.req, this.res)
  585. })
  586. it('should tell the SubscriptionHandler to process the recurly callback', function(done) {
  587. this.SubscriptionHandler.syncSubscription.called.should.equal(true)
  588. return done()
  589. })
  590. it('should send a 200', function(done) {
  591. this.res.sendStatus.calledWith(200)
  592. return done()
  593. })
  594. })
  595. describe('with a billing info updated request', function() {
  596. beforeEach(function(done) {
  597. this.req = {
  598. body: {
  599. billing_info_updated_notification: {
  600. account: {
  601. account_code: 'mock-account-code'
  602. }
  603. }
  604. }
  605. }
  606. this.res = {
  607. sendStatus() {
  608. done()
  609. }
  610. }
  611. sinon.spy(this.res, 'sendStatus')
  612. this.SubscriptionController.recurlyCallback(this.req, this.res)
  613. })
  614. it('should call attemptPaypalInvoiceCollection', function(done) {
  615. this.SubscriptionHandler.attemptPaypalInvoiceCollection
  616. .calledWith('mock-account-code')
  617. .should.equal(true)
  618. done()
  619. })
  620. it('should send a 200', function(done) {
  621. this.res.sendStatus.calledWith(200)
  622. done()
  623. })
  624. })
  625. describe('with a non-actionable request', function() {
  626. beforeEach(function(done) {
  627. this.user.id = this.activeRecurlySubscription.account.account_code
  628. this.req = {
  629. body: {
  630. renewed_subscription_notification: {
  631. subscription: {
  632. uuid: this.activeRecurlySubscription.uuid
  633. }
  634. }
  635. }
  636. }
  637. this.res = {
  638. sendStatus() {
  639. return done()
  640. }
  641. }
  642. sinon.spy(this.res, 'sendStatus')
  643. return this.SubscriptionController.recurlyCallback(this.req, this.res)
  644. })
  645. it('should not call the subscriptionshandler', function() {
  646. this.SubscriptionHandler.syncSubscription.called.should.equal(false)
  647. this.SubscriptionHandler.attemptPaypalInvoiceCollection.called.should.equal(
  648. false
  649. )
  650. })
  651. it('should respond with a 200 status', function() {
  652. return this.res.sendStatus.calledWith(200)
  653. })
  654. })
  655. })
  656. describe('renderUpgradeToAnnualPlanPage', function() {
  657. it('should redirect to the plans page if the user does not have a subscription', function(done) {
  658. this.LimitationsManager.userHasV2Subscription.callsArgWith(1, null, false)
  659. this.res.redirect = function(url) {
  660. url.should.equal('/user/subscription/plans')
  661. return done()
  662. }
  663. return this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  664. this.req,
  665. this.res
  666. )
  667. })
  668. it('should pass the plan code to the view - student', function(done) {
  669. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  670. 1,
  671. null,
  672. true,
  673. { planCode: 'Student free trial 14 days' }
  674. )
  675. this.res.render = function(view, opts) {
  676. view.should.equal('subscriptions/upgradeToAnnual')
  677. opts.planName.should.equal('student')
  678. return done()
  679. }
  680. return this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  681. this.req,
  682. this.res
  683. )
  684. })
  685. it('should pass the plan code to the view - collaborator', function(done) {
  686. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  687. 1,
  688. null,
  689. true,
  690. { planCode: 'free trial for Collaborator free trial 14 days' }
  691. )
  692. this.res.render = function(view, opts) {
  693. opts.planName.should.equal('collaborator')
  694. return done()
  695. }
  696. return this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  697. this.req,
  698. this.res
  699. )
  700. })
  701. it('should pass annual as the plan name if the user is already on an annual plan', function(done) {
  702. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  703. 1,
  704. null,
  705. true,
  706. { planCode: 'student annual with free trial' }
  707. )
  708. this.res.render = function(view, opts) {
  709. opts.planName.should.equal('annual')
  710. return done()
  711. }
  712. return this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  713. this.req,
  714. this.res
  715. )
  716. })
  717. })
  718. describe('processUpgradeToAnnualPlan', function() {
  719. beforeEach(function() {})
  720. it('should tell the subscription handler to update the subscription with the annual plan and apply a coupon code', function(done) {
  721. this.req.body = { planName: 'student' }
  722. this.res.sendStatus = () => {
  723. this.SubscriptionHandler.updateSubscription
  724. .calledWith(this.user, 'student-annual', 'STUDENTCODEHERE')
  725. .should.equal(true)
  726. return done()
  727. }
  728. return this.SubscriptionController.processUpgradeToAnnualPlan(
  729. this.req,
  730. this.res
  731. )
  732. })
  733. it('should get the collaborator coupon code', function(done) {
  734. this.req.body = { planName: 'collaborator' }
  735. this.res.sendStatus = url => {
  736. this.SubscriptionHandler.updateSubscription
  737. .calledWith(this.user, 'collaborator-annual', 'COLLABORATORCODEHERE')
  738. .should.equal(true)
  739. return done()
  740. }
  741. return this.SubscriptionController.processUpgradeToAnnualPlan(
  742. this.req,
  743. this.res
  744. )
  745. })
  746. })
  747. })