SubscriptionControllerTests.js 29 KB

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