SubscriptionControllerTests.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 SubscriptionErrors = require('../../../../app/src/Features/Subscription/Errors')
  9. const mockSubscriptions = {
  10. 'subscription-123-active': {
  11. uuid: 'subscription-123-active',
  12. plan: {
  13. name: 'Gold',
  14. plan_code: 'gold',
  15. },
  16. current_period_ends_at: new Date(),
  17. state: 'active',
  18. unit_amount_in_cents: 999,
  19. account: {
  20. account_code: 'user-123',
  21. },
  22. },
  23. }
  24. describe('SubscriptionController', function () {
  25. beforeEach(function () {
  26. this.user = {
  27. email: 'tom@yahoo.com',
  28. _id: 'one',
  29. signUpDate: new Date('2000-10-01'),
  30. emails: [{ email: 'tom@yahoo.com', confirmedAt: new Date('2000-10-02') }],
  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. getBestSubscription: sinon.stub().resolves({}),
  75. },
  76. buildPlansListForSubscriptionDash: sinon
  77. .stub()
  78. .returns({ plans: [], planCodesChangingAtTermEnd: [] }),
  79. }
  80. this.settings = {
  81. coupon_codes: {
  82. upgradeToAnnualPromo: {
  83. student: 'STUDENTCODEHERE',
  84. collaborator: 'COLLABORATORCODEHERE',
  85. },
  86. },
  87. groupPlanModalOptions: {
  88. plan_codes: [],
  89. currencies: [
  90. {
  91. display: 'GBP (£)',
  92. code: 'GBP',
  93. },
  94. ],
  95. sizes: ['42'],
  96. usages: [{ code: 'foo', display: 'Foo' }],
  97. },
  98. apis: {
  99. recurly: {
  100. subdomain: 'sl',
  101. },
  102. },
  103. siteUrl: 'http://de.overleaf.dev:3000',
  104. }
  105. this.AuthorizationManager = {
  106. promises: {
  107. isUserSiteAdmin: sinon.stub().resolves(false),
  108. },
  109. }
  110. this.GeoIpLookup = {
  111. isValidCurrencyParam: sinon.stub().returns(true),
  112. getCurrencyCode: sinon.stub().yields('USD', 'US'),
  113. promises: {
  114. getCurrencyCode: sinon.stub().resolves({
  115. countryCode: 'US',
  116. currencyCode: 'USD',
  117. }),
  118. },
  119. }
  120. this.UserGetter = {
  121. getUser: sinon.stub().callsArgWith(2, null, this.user),
  122. promises: {
  123. getUser: sinon.stub().resolves(this.user),
  124. },
  125. }
  126. this.SplitTestV2Hander = {
  127. promises: {
  128. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  129. },
  130. }
  131. this.SubscriptionHelper = {
  132. generateInitialLocalizedGroupPrice: sinon.stub(),
  133. }
  134. this.Features = {
  135. hasFeature: sinon.stub().returns(false),
  136. }
  137. this.SubscriptionController = SandboxedModule.require(modulePath, {
  138. requires: {
  139. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  140. '../SplitTests/SplitTestHandler': this.SplitTestV2Hander,
  141. '../Authentication/SessionManager': this.SessionManager,
  142. './SubscriptionHandler': this.SubscriptionHandler,
  143. './SubscriptionHelper': this.SubscriptionHelper,
  144. './PlansLocator': this.PlansLocator,
  145. './SubscriptionViewModelBuilder': this.SubscriptionViewModelBuilder,
  146. './LimitationsManager': this.LimitationsManager,
  147. '../../infrastructure/GeoIpLookup': this.GeoIpLookup,
  148. '@overleaf/settings': this.settings,
  149. '../User/UserGetter': this.UserGetter,
  150. './RecurlyWrapper': (this.RecurlyWrapper = {
  151. updateAccountEmailAddress: sinon.stub().yields(),
  152. }),
  153. './RecurlyEventHandler': {
  154. sendRecurlyAnalyticsEvent: sinon.stub().resolves(),
  155. },
  156. './FeaturesUpdater': (this.FeaturesUpdater = {}),
  157. './GroupPlansData': (this.GroupPlansData = {}),
  158. './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
  159. '../Errors/HttpErrorHandler': (this.HttpErrorHandler = {
  160. unprocessableEntity: sinon.stub().callsFake((req, res, message) => {
  161. res.status(422)
  162. res.json({ message })
  163. }),
  164. }),
  165. './Errors': SubscriptionErrors,
  166. '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
  167. recordEventForUser: sinon.stub(),
  168. recordEventForSession: sinon.stub(),
  169. setUserPropertyForUser: sinon.stub(),
  170. }),
  171. '../../infrastructure/Features': this.Features,
  172. },
  173. })
  174. this.res = new MockResponse()
  175. this.req = new MockRequest()
  176. this.req.body = {}
  177. this.req.query = { planCode: '123123' }
  178. this.stubbedCurrencyCode = 'GBP'
  179. })
  180. describe('plansPage', function () {
  181. beforeEach(function () {
  182. this.req.ip = '1234.3123.3131.333 313.133.445.666 653.5345.5345.534'
  183. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  184. currencyCode: this.stubbedCurrencyCode,
  185. })
  186. })
  187. describe('ip override', function () {
  188. beforeEach(function () {
  189. this.req.ip = '1.2.3.4'
  190. this.req.query = { ip: '5.6.7.8' }
  191. this.GeoIpLookup.promises.getCurrencyCode.withArgs('1.2.3.4').resolves({
  192. currencyCode: 'GBP',
  193. })
  194. this.GeoIpLookup.promises.getCurrencyCode.withArgs('5.6.7.8').resolves({
  195. currencyCode: 'USD',
  196. })
  197. })
  198. it('should ignore override for non admin', function (done) {
  199. this.res.render = (page, opts) => {
  200. opts.recommendedCurrency.should.equal('GBP')
  201. done()
  202. }
  203. this.AuthorizationManager.promises.isUserSiteAdmin.resolves(false)
  204. this.SubscriptionController.plansPage(this.req, this.res)
  205. })
  206. it('should accept override for admin', function (done) {
  207. this.res.render = (page, opts) => {
  208. opts.recommendedCurrency.should.equal('USD')
  209. done()
  210. }
  211. this.AuthorizationManager.promises.isUserSiteAdmin.resolves(true)
  212. this.SubscriptionController.plansPage(this.req, this.res)
  213. })
  214. })
  215. describe('groupPlanModal data', function () {
  216. it('should pass local currency if valid', function (done) {
  217. this.res.render = (page, opts) => {
  218. page.should.equal('subscriptions/plans')
  219. opts.groupPlanModalDefaults.currency.should.equal('GBP')
  220. done()
  221. }
  222. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  223. currencyCode: 'GBP',
  224. })
  225. this.SubscriptionController.plansPage(this.req, this.res)
  226. })
  227. it('should fallback to USD when valid', function (done) {
  228. this.res.render = (page, opts) => {
  229. page.should.equal('subscriptions/plans')
  230. opts.groupPlanModalDefaults.currency.should.equal('USD')
  231. done()
  232. }
  233. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  234. currencyCode: 'FOO',
  235. })
  236. this.SubscriptionController.plansPage(this.req, this.res)
  237. })
  238. it('should pass valid options for group plan modal and discard invalid', function (done) {
  239. this.res.render = (page, opts) => {
  240. page.should.equal('subscriptions/plans')
  241. opts.groupPlanModalDefaults.size.should.equal('42')
  242. opts.groupPlanModalDefaults.plan_code.should.equal('collaborator')
  243. opts.groupPlanModalDefaults.currency.should.equal('GBP')
  244. opts.groupPlanModalDefaults.usage.should.equal('foo')
  245. done()
  246. }
  247. this.GeoIpLookup.isValidCurrencyParam.returns(false)
  248. this.req.query = {
  249. number: '42',
  250. currency: 'ABC',
  251. plan: 'does-not-exist',
  252. usage: 'foo',
  253. }
  254. this.SubscriptionController.plansPage(this.req, this.res)
  255. })
  256. })
  257. })
  258. describe('interstitialPaymentPage', function () {
  259. beforeEach(function () {
  260. this.req.ip = '1234.3123.3131.333 313.133.445.666 653.5345.5345.534'
  261. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  262. currencyCode: this.stubbedCurrencyCode,
  263. })
  264. })
  265. describe('with a user without subscription', function () {
  266. it('should render the interstitial payment page', function (done) {
  267. this.res.render = (page, opts) => {
  268. page.should.equal('subscriptions/interstitial-payment')
  269. done()
  270. }
  271. this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
  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.interstitialPaymentPage(this.req, this.res)
  285. })
  286. })
  287. })
  288. describe('successfulSubscription', function () {
  289. it('without a personal subscription', function (done) {
  290. this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
  291. {}
  292. )
  293. this.res.redirect = url => {
  294. url.should.equal('/user/subscription/plans')
  295. done()
  296. }
  297. this.SubscriptionController.successfulSubscription(this.req, this.res)
  298. })
  299. it('with a personal subscription', function (done) {
  300. this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
  301. {
  302. personalSubscription: 'foo',
  303. }
  304. )
  305. this.res.render = (url, variables) => {
  306. url.should.equal('subscriptions/successful-subscription-react')
  307. assert.deepEqual(variables, {
  308. title: 'thank_you',
  309. personalSubscription: 'foo',
  310. postCheckoutRedirect: undefined,
  311. })
  312. done()
  313. }
  314. this.SubscriptionController.successfulSubscription(this.req, this.res)
  315. })
  316. it('with an error', function (done) {
  317. this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
  318. undefined
  319. )
  320. this.SubscriptionController.successfulSubscription(
  321. this.req,
  322. this.res,
  323. error => {
  324. assert.isNotNull(error)
  325. done()
  326. }
  327. )
  328. })
  329. })
  330. describe('userSubscriptionPage', function () {
  331. beforeEach(function (done) {
  332. this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
  333. {
  334. personalSubscription: (this.personalSubscription = {
  335. 'personal-subscription': 'mock',
  336. }),
  337. memberGroupSubscriptions: (this.memberGroupSubscriptions = {
  338. 'group-subscriptions': 'mock',
  339. }),
  340. }
  341. )
  342. this.SubscriptionViewModelBuilder.buildPlansList.returns(
  343. (this.plans = { plans: 'mock' })
  344. )
  345. this.SubscriptionViewModelBuilder.buildPlansListForSubscriptionDash.returns(
  346. {
  347. plans: this.plans,
  348. planCodesChangingAtTermEnd: [],
  349. }
  350. )
  351. this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
  352. this.res.render = (view, data) => {
  353. this.data = data
  354. expect(view).to.equal('subscriptions/dashboard-react')
  355. done()
  356. }
  357. this.SubscriptionController.userSubscriptionPage(this.req, this.res, done)
  358. })
  359. it('should load the personal, groups and v1 subscriptions', function () {
  360. expect(this.data.personalSubscription).to.deep.equal(
  361. this.personalSubscription
  362. )
  363. expect(this.data.memberGroupSubscriptions).to.deep.equal(
  364. this.memberGroupSubscriptions
  365. )
  366. })
  367. it('should load the user', function () {
  368. expect(this.data.user).to.deep.equal(this.user)
  369. })
  370. it('should load the plans', function () {
  371. expect(this.data.plans).to.deep.equal(this.plans)
  372. })
  373. it('should load an empty list of groups with settings available', function () {
  374. expect(this.data.groupSettingsEnabledFor).to.deep.equal([])
  375. })
  376. })
  377. describe('updateSubscription via post', function () {
  378. beforeEach(function (done) {
  379. this.res = {
  380. redirect() {
  381. done()
  382. },
  383. }
  384. sinon.spy(this.res, 'redirect')
  385. this.plan_code = '1234'
  386. this.req.body.plan_code = this.plan_code
  387. this.SubscriptionController.updateSubscription(this.req, this.res)
  388. })
  389. it('should send the user and subscriptionId to the handler', function (done) {
  390. this.SubscriptionHandler.updateSubscription
  391. .calledWith(this.user, this.plan_code)
  392. .should.equal(true)
  393. done()
  394. })
  395. it('should redurect to the subscription page', function (done) {
  396. this.res.redirect.calledWith('/user/subscription').should.equal(true)
  397. done()
  398. })
  399. })
  400. describe('updateAccountEmailAddress via put', function () {
  401. it('should send the user and subscriptionId to RecurlyWrapper', function () {
  402. this.res.sendStatus = sinon.spy()
  403. this.SubscriptionController.updateAccountEmailAddress(this.req, this.res)
  404. this.RecurlyWrapper.updateAccountEmailAddress
  405. .calledWith(this.user._id, this.user.email)
  406. .should.equal(true)
  407. })
  408. it('should respond with 200', function () {
  409. this.res.sendStatus = sinon.spy()
  410. this.SubscriptionController.updateAccountEmailAddress(this.req, this.res)
  411. this.res.sendStatus.calledWith(200).should.equal(true)
  412. })
  413. it('should send the error to the next handler when updating recurly account email fails', function (done) {
  414. this.RecurlyWrapper.updateAccountEmailAddress.yields(new Error())
  415. this.next = sinon.spy(error => {
  416. expect(error).instanceOf(Error)
  417. done()
  418. })
  419. this.SubscriptionController.updateAccountEmailAddress(
  420. this.req,
  421. this.res,
  422. this.next
  423. )
  424. })
  425. })
  426. describe('reactivateSubscription', function () {
  427. describe('when the user has permission', function () {
  428. beforeEach(function (done) {
  429. this.res = {
  430. redirect() {
  431. done()
  432. },
  433. }
  434. this.req.assertPermission = sinon.stub()
  435. this.next = sinon.stub().callsFake(error => {
  436. done(error)
  437. })
  438. sinon.spy(this.res, 'redirect')
  439. this.SubscriptionController.reactivateSubscription(
  440. this.req,
  441. this.res,
  442. this.next
  443. )
  444. })
  445. it('should assert the user has permission to reactivate their subscription', function (done) {
  446. this.req.assertPermission
  447. .calledWith('reactivate-subscription')
  448. .should.equal(true)
  449. done()
  450. })
  451. it('should tell the handler to reactivate this user', function (done) {
  452. this.SubscriptionHandler.reactivateSubscription
  453. .calledWith(this.user)
  454. .should.equal(true)
  455. done()
  456. })
  457. it('should redurect to the subscription page', function (done) {
  458. this.res.redirect.calledWith('/user/subscription').should.equal(true)
  459. done()
  460. })
  461. })
  462. describe('when the user does not have permission', function () {
  463. beforeEach(function (done) {
  464. this.res = {
  465. redirect() {
  466. done()
  467. },
  468. }
  469. this.req.assertPermission = sinon.stub().throws()
  470. this.next = sinon.stub().callsFake(() => {
  471. done()
  472. })
  473. sinon.spy(this.res, 'redirect')
  474. this.SubscriptionController.reactivateSubscription(
  475. this.req,
  476. this.res,
  477. this.next
  478. )
  479. })
  480. it('should not reactivate the user', function (done) {
  481. this.req.assertPermission = sinon.stub().throws()
  482. this.SubscriptionHandler.reactivateSubscription.called.should.equal(
  483. false
  484. )
  485. done()
  486. })
  487. it('should call next with an error', function (done) {
  488. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  489. done()
  490. })
  491. })
  492. })
  493. describe('cancelSubscription', function () {
  494. beforeEach(function (done) {
  495. this.res = {
  496. redirect() {
  497. done()
  498. },
  499. }
  500. sinon.spy(this.res, 'redirect')
  501. this.SubscriptionController.cancelSubscription(this.req, this.res)
  502. })
  503. it('should tell the handler to cancel this user', function (done) {
  504. this.SubscriptionHandler.cancelSubscription
  505. .calledWith(this.user)
  506. .should.equal(true)
  507. done()
  508. })
  509. it('should redurect to the subscription page', function (done) {
  510. this.res.redirect
  511. .calledWith('/user/subscription/canceled')
  512. .should.equal(true)
  513. done()
  514. })
  515. })
  516. describe('recurly callback', function () {
  517. describe('with a sync subscription request', function () {
  518. beforeEach(function (done) {
  519. this.req = {
  520. body: {
  521. expired_subscription_notification: {
  522. account: {
  523. account_code: this.user._id,
  524. },
  525. subscription: {
  526. uuid: this.activeRecurlySubscription.uuid,
  527. plan: {
  528. plan_code: 'collaborator',
  529. state: 'active',
  530. },
  531. },
  532. },
  533. },
  534. }
  535. this.res = {
  536. sendStatus() {
  537. done()
  538. },
  539. }
  540. sinon.spy(this.res, 'sendStatus')
  541. this.SubscriptionController.recurlyCallback(this.req, this.res)
  542. })
  543. it('should tell the SubscriptionHandler to process the recurly callback', function (done) {
  544. this.SubscriptionHandler.syncSubscription.called.should.equal(true)
  545. done()
  546. })
  547. it('should send a 200', function (done) {
  548. this.res.sendStatus.calledWith(200)
  549. done()
  550. })
  551. })
  552. describe('with a billing info updated request', function () {
  553. beforeEach(function (done) {
  554. this.req = {
  555. body: {
  556. billing_info_updated_notification: {
  557. account: {
  558. account_code: 'mock-account-code',
  559. },
  560. },
  561. },
  562. }
  563. this.res = {
  564. sendStatus() {
  565. done()
  566. },
  567. }
  568. sinon.spy(this.res, 'sendStatus')
  569. this.SubscriptionController.recurlyCallback(this.req, this.res)
  570. })
  571. it('should call attemptPaypalInvoiceCollection', function (done) {
  572. this.SubscriptionHandler.attemptPaypalInvoiceCollection
  573. .calledWith('mock-account-code')
  574. .should.equal(true)
  575. done()
  576. })
  577. it('should send a 200', function (done) {
  578. this.res.sendStatus.calledWith(200)
  579. done()
  580. })
  581. })
  582. describe('with a non-actionable request', function () {
  583. beforeEach(function (done) {
  584. this.user.id = this.activeRecurlySubscription.account.account_code
  585. this.req = {
  586. body: {
  587. renewed_subscription_notification: {
  588. account: {
  589. account_code: this.user._id,
  590. },
  591. subscription: {
  592. uuid: this.activeRecurlySubscription.uuid,
  593. plan: {
  594. plan_code: 'collaborator',
  595. state: 'active',
  596. },
  597. },
  598. },
  599. },
  600. }
  601. this.res = {
  602. sendStatus() {
  603. done()
  604. },
  605. }
  606. sinon.spy(this.res, 'sendStatus')
  607. this.SubscriptionController.recurlyCallback(this.req, this.res)
  608. })
  609. it('should not call the subscriptionshandler', function () {
  610. this.SubscriptionHandler.syncSubscription.called.should.equal(false)
  611. this.SubscriptionHandler.attemptPaypalInvoiceCollection.called.should.equal(
  612. false
  613. )
  614. })
  615. it('should respond with a 200 status', function () {
  616. this.res.sendStatus.calledWith(200)
  617. })
  618. })
  619. })
  620. describe('renderUpgradeToAnnualPlanPage', function () {
  621. it('should redirect to the plans page if the user does not have a subscription', function (done) {
  622. this.LimitationsManager.userHasV2Subscription.callsArgWith(1, null, false)
  623. this.res.redirect = function (url) {
  624. url.should.equal('/user/subscription/plans')
  625. done()
  626. }
  627. this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  628. this.req,
  629. this.res
  630. )
  631. })
  632. it('should pass the plan code to the view - student', function (done) {
  633. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  634. 1,
  635. null,
  636. true,
  637. { planCode: 'Student free trial 14 days' }
  638. )
  639. this.res.render = function (view, opts) {
  640. view.should.equal('subscriptions/upgradeToAnnual')
  641. opts.planName.should.equal('student')
  642. done()
  643. }
  644. this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  645. this.req,
  646. this.res
  647. )
  648. })
  649. it('should pass the plan code to the view - collaborator', function (done) {
  650. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  651. 1,
  652. null,
  653. true,
  654. { planCode: 'free trial for Collaborator free trial 14 days' }
  655. )
  656. this.res.render = function (view, opts) {
  657. opts.planName.should.equal('collaborator')
  658. done()
  659. }
  660. this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  661. this.req,
  662. this.res
  663. )
  664. })
  665. it('should pass annual as the plan name if the user is already on an annual plan', function (done) {
  666. this.LimitationsManager.userHasV2Subscription.callsArgWith(
  667. 1,
  668. null,
  669. true,
  670. { planCode: 'student annual with free trial' }
  671. )
  672. this.res.render = function (view, opts) {
  673. opts.planName.should.equal('annual')
  674. done()
  675. }
  676. this.SubscriptionController.renderUpgradeToAnnualPlanPage(
  677. this.req,
  678. this.res
  679. )
  680. })
  681. })
  682. describe('processUpgradeToAnnualPlan', function () {
  683. beforeEach(function () {})
  684. it('should tell the subscription handler to update the subscription with the annual plan and apply a coupon code', function (done) {
  685. this.req.body = { planName: 'student' }
  686. this.res.sendStatus = () => {
  687. this.SubscriptionHandler.updateSubscription
  688. .calledWith(this.user, 'student-annual', 'STUDENTCODEHERE')
  689. .should.equal(true)
  690. done()
  691. }
  692. this.SubscriptionController.processUpgradeToAnnualPlan(this.req, this.res)
  693. })
  694. it('should get the collaborator coupon code', function (done) {
  695. this.req.body = { planName: 'collaborator' }
  696. this.res.sendStatus = url => {
  697. this.SubscriptionHandler.updateSubscription
  698. .calledWith(this.user, 'collaborator-annual', 'COLLABORATORCODEHERE')
  699. .should.equal(true)
  700. done()
  701. }
  702. this.SubscriptionController.processUpgradeToAnnualPlan(this.req, this.res)
  703. })
  704. })
  705. })