SubscriptionControllerTests.js 28 KB

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