SubscriptionGroupController.test.mjs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. import { beforeEach, describe, it, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath =
  4. '../../../../app/src/Features/Subscription/SubscriptionGroupController'
  5. describe('SubscriptionGroupController', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.user = { _id: '!@312431', email: 'user@email.com' }
  8. ctx.adminUserId = '123jlkj'
  9. ctx.subscriptionId = '123434325412'
  10. ctx.user_email = 'bob@gmail.com'
  11. ctx.req = {
  12. session: {
  13. user: {
  14. _id: ctx.adminUserId,
  15. email: ctx.user_email,
  16. },
  17. },
  18. params: {
  19. subscriptionId: ctx.subscriptionId,
  20. },
  21. query: {},
  22. }
  23. ctx.subscription = {
  24. _id: ctx.subscriptionId,
  25. teamName: 'Cool group',
  26. groupPlan: true,
  27. membersLimit: 5,
  28. }
  29. ctx.plan = {
  30. canUseFlexibleLicensing: true,
  31. }
  32. ctx.recurlySubscription = {
  33. get isCollectionMethodManual() {
  34. return true
  35. },
  36. }
  37. ctx.previewSubscriptionChangeData = {
  38. change: {},
  39. currency: 'USD',
  40. }
  41. ctx.createSubscriptionChangeData = { adding: 1 }
  42. ctx.paymentMethod = { cardType: 'Visa', lastFour: '1111' }
  43. ctx.SubscriptionGroupHandler = {
  44. promises: {
  45. removeUserFromGroup: sinon.stub().resolves(),
  46. getUsersGroupSubscriptionDetails: sinon.stub().resolves({
  47. subscription: ctx.subscription,
  48. plan: ctx.plan,
  49. paymentProviderSubscription: ctx.recurlySubscription,
  50. }),
  51. previewAddSeatsSubscriptionChange: sinon
  52. .stub()
  53. .resolves(ctx.previewSubscriptionChangeData),
  54. createAddSeatsSubscriptionChange: sinon
  55. .stub()
  56. .resolves(ctx.createSubscriptionChangeData),
  57. ensureFlexibleLicensingEnabled: sinon.stub().resolves(),
  58. ensureSubscriptionIsActive: sinon.stub().resolves(),
  59. ensureSubscriptionCollectionMethodIsNotManual: sinon.stub().resolves(),
  60. ensureSubscriptionHasNoPastDueInvoice: sinon.stub().resolves(),
  61. getGroupPlanUpgradePreview: sinon
  62. .stub()
  63. .resolves(ctx.previewSubscriptionChangeData),
  64. checkBillingInfoExistence: sinon.stub().resolves(ctx.paymentMethod),
  65. updateSubscriptionPaymentTerms: sinon.stub().resolves(),
  66. ensureSubscriptionHasAdditionalLicenseAddOnWhenCollectionMethodIsManual:
  67. sinon.stub().resolves(),
  68. },
  69. }
  70. ctx.SubscriptionLocator = {
  71. promises: {
  72. getSubscription: sinon.stub().resolves(ctx.subscription),
  73. },
  74. }
  75. ctx.SessionManager = {
  76. getLoggedInUserId(session) {
  77. return session.user._id
  78. },
  79. getSessionUser(session) {
  80. return session.user
  81. },
  82. }
  83. ctx.UserAuditLogHandler = {
  84. promises: {
  85. addEntry: sinon.stub().resolves(),
  86. },
  87. }
  88. ctx.Modules = {
  89. promises: {
  90. hooks: {
  91. fire: sinon.stub().resolves(),
  92. },
  93. },
  94. }
  95. ctx.UserGetter = {
  96. promises: {
  97. getUserEmail: sinon.stub().resolves(ctx.user.email),
  98. },
  99. }
  100. ctx.paymentMethod = { cardType: 'Visa', lastFour: '1111' }
  101. ctx.RecurlyClient = {
  102. promises: {
  103. getPaymentMethod: sinon.stub().resolves(ctx.paymentMethod),
  104. },
  105. }
  106. ctx.SubscriptionController = {}
  107. ctx.SubscriptionModel = { Subscription: {} }
  108. ctx.PlansHelper = {
  109. isProfessionalGroupPlan: sinon.stub().returns(false),
  110. }
  111. ctx.Errors = {
  112. MissingBillingInfoError: class extends Error {},
  113. ManuallyCollectedError: class extends Error {},
  114. InactiveError: class extends Error {},
  115. SubtotalLimitExceededError: class extends Error {},
  116. HasPastDueInvoiceError: class extends Error {},
  117. HasNoAdditionalLicenseWhenManuallyCollectedError: class extends Error {},
  118. PaymentActionRequiredError: class extends Error {
  119. constructor(info) {
  120. super('Payment action required')
  121. this.info = info
  122. }
  123. },
  124. MultiplePendingChangesError: class extends Error {},
  125. }
  126. vi.doMock(
  127. '../../../../app/src/Features/Subscription/SubscriptionGroupHandler',
  128. () => ({
  129. default: ctx.SubscriptionGroupHandler,
  130. })
  131. )
  132. vi.doMock(
  133. '../../../../app/src/Features/Subscription/SubscriptionLocator',
  134. () => ({
  135. default: ctx.SubscriptionLocator,
  136. })
  137. )
  138. vi.doMock(
  139. '../../../../app/src/Features/Authentication/SessionManager',
  140. () => ({
  141. default: ctx.SessionManager,
  142. })
  143. )
  144. vi.doMock('../../../../app/src/Features/User/UserAuditLogHandler', () => ({
  145. default: ctx.UserAuditLogHandler,
  146. }))
  147. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  148. default: ctx.Modules,
  149. }))
  150. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  151. default: ctx.UserGetter,
  152. }))
  153. vi.doMock('../../../../app/src/Features/Errors/ErrorController', () => ({
  154. default: (ctx.ErrorController = {
  155. notFound: sinon.stub(),
  156. }),
  157. }))
  158. vi.doMock(
  159. '../../../../app/src/Features/Subscription/SubscriptionController',
  160. () => ({
  161. default: ctx.SubscriptionController,
  162. })
  163. )
  164. vi.doMock(
  165. '../../../../app/src/Features/Subscription/RecurlyClient',
  166. () => ({
  167. default: ctx.RecurlyClient,
  168. })
  169. )
  170. vi.doMock(
  171. '../../../../app/src/Features/Subscription/PlansHelper',
  172. () => ctx.PlansHelper
  173. )
  174. vi.doMock(
  175. '../../../../app/src/Features/Subscription/Errors',
  176. () => ctx.Errors
  177. )
  178. vi.doMock(
  179. '../../../../app/src/models/Subscription',
  180. () => ctx.SubscriptionModel
  181. )
  182. vi.doMock('@overleaf/logger', () => ({
  183. default: {
  184. err: sinon.stub(),
  185. error: sinon.stub(),
  186. warn: sinon.stub(),
  187. log: sinon.stub(),
  188. debug: sinon.stub(),
  189. },
  190. }))
  191. ctx.Controller = (await import(modulePath)).default
  192. })
  193. describe('removeUserFromGroup', function () {
  194. it('should use the subscription id for the logged in user and take the user id from the params', async function (ctx) {
  195. await new Promise(resolve => {
  196. const userIdToRemove = '31231'
  197. ctx.req.params = { user_id: userIdToRemove }
  198. ctx.req.entity = ctx.subscription
  199. const res = {
  200. sendStatus: () => {
  201. ctx.SubscriptionGroupHandler.promises.removeUserFromGroup
  202. .calledWith(ctx.subscriptionId, userIdToRemove, {
  203. initiatorId: ctx.req.session.user._id,
  204. ipAddress: ctx.req.ip,
  205. })
  206. .should.equal(true)
  207. resolve()
  208. },
  209. }
  210. ctx.Controller.removeUserFromGroup(ctx.req, res, resolve)
  211. })
  212. })
  213. it('should log that the user has been removed', async function (ctx) {
  214. await new Promise(resolve => {
  215. const userIdToRemove = '31231'
  216. ctx.req.params = { user_id: userIdToRemove }
  217. ctx.req.entity = ctx.subscription
  218. const res = {
  219. sendStatus: () => {
  220. sinon.assert.calledWith(
  221. ctx.UserAuditLogHandler.promises.addEntry,
  222. userIdToRemove,
  223. 'remove-from-group-subscription',
  224. ctx.adminUserId,
  225. ctx.req.ip,
  226. { subscriptionId: ctx.subscriptionId }
  227. )
  228. resolve()
  229. },
  230. }
  231. ctx.Controller.removeUserFromGroup(ctx.req, res, resolve)
  232. })
  233. })
  234. it('should call the group SSO hooks with group SSO enabled', async function (ctx) {
  235. await new Promise(resolve => {
  236. const userIdToRemove = '31231'
  237. ctx.req.params = { user_id: userIdToRemove }
  238. ctx.req.entity = ctx.subscription
  239. ctx.Modules.promises.hooks.fire
  240. .withArgs('hasGroupSSOEnabled', ctx.subscription)
  241. .resolves([true])
  242. const res = {
  243. sendStatus: () => {
  244. ctx.Modules.promises.hooks.fire
  245. .calledWith('hasGroupSSOEnabled', ctx.subscription)
  246. .should.equal(true)
  247. ctx.Modules.promises.hooks.fire
  248. .calledWith(
  249. 'unlinkUserFromGroupSSO',
  250. userIdToRemove,
  251. ctx.subscriptionId
  252. )
  253. .should.equal(true)
  254. sinon.assert.calledTwice(ctx.Modules.promises.hooks.fire)
  255. resolve()
  256. },
  257. }
  258. ctx.Controller.removeUserFromGroup(ctx.req, res, resolve)
  259. })
  260. })
  261. it('should call the group SSO hooks with group SSO disabled', async function (ctx) {
  262. await new Promise(resolve => {
  263. const userIdToRemove = '31231'
  264. ctx.req.params = { user_id: userIdToRemove }
  265. ctx.req.entity = ctx.subscription
  266. ctx.Modules.promises.hooks.fire
  267. .withArgs('hasGroupSSOEnabled', ctx.subscription)
  268. .resolves([false])
  269. const res = {
  270. sendStatus: () => {
  271. ctx.Modules.promises.hooks.fire
  272. .calledWith('hasGroupSSOEnabled', ctx.subscription)
  273. .should.equal(true)
  274. sinon.assert.calledOnce(ctx.Modules.promises.hooks.fire)
  275. resolve()
  276. },
  277. }
  278. ctx.Controller.removeUserFromGroup(ctx.req, res, resolve)
  279. })
  280. })
  281. })
  282. describe('removeSelfFromGroup', function () {
  283. it('gets subscription and remove user', async function (ctx) {
  284. await new Promise(resolve => {
  285. ctx.req.query = { subscriptionId: ctx.subscriptionId }
  286. const memberUserIdToremove = 123456789
  287. ctx.req.session.user._id = memberUserIdToremove
  288. const res = {
  289. sendStatus: () => {
  290. sinon.assert.calledWith(
  291. ctx.SubscriptionLocator.promises.getSubscription,
  292. ctx.subscriptionId
  293. )
  294. sinon.assert.calledWith(
  295. ctx.SubscriptionGroupHandler.promises.removeUserFromGroup,
  296. ctx.subscriptionId,
  297. memberUserIdToremove,
  298. {
  299. initiatorId: ctx.req.session.user._id,
  300. ipAddress: ctx.req.ip,
  301. }
  302. )
  303. resolve()
  304. },
  305. }
  306. ctx.Controller.removeSelfFromGroup(ctx.req, res, resolve)
  307. })
  308. })
  309. it('should log that the user has left the subscription', async function (ctx) {
  310. await new Promise(resolve => {
  311. ctx.req.query = { subscriptionId: ctx.subscriptionId }
  312. const memberUserIdToremove = '123456789'
  313. ctx.req.session.user._id = memberUserIdToremove
  314. const res = {
  315. sendStatus: () => {
  316. sinon.assert.calledWith(
  317. ctx.UserAuditLogHandler.promises.addEntry,
  318. memberUserIdToremove,
  319. 'remove-from-group-subscription',
  320. memberUserIdToremove,
  321. ctx.req.ip,
  322. { subscriptionId: ctx.subscriptionId }
  323. )
  324. resolve()
  325. },
  326. }
  327. ctx.Controller.removeSelfFromGroup(ctx.req, res, resolve)
  328. })
  329. })
  330. it('should call the group SSO hooks with group SSO enabled', async function (ctx) {
  331. await new Promise(resolve => {
  332. ctx.req.query = { subscriptionId: ctx.subscriptionId }
  333. const memberUserIdToremove = '123456789'
  334. ctx.req.session.user._id = memberUserIdToremove
  335. ctx.Modules.promises.hooks.fire
  336. .withArgs('hasGroupSSOEnabled', ctx.subscription)
  337. .resolves([true])
  338. const res = {
  339. sendStatus: () => {
  340. ctx.Modules.promises.hooks.fire
  341. .calledWith('hasGroupSSOEnabled', ctx.subscription)
  342. .should.equal(true)
  343. ctx.Modules.promises.hooks.fire
  344. .calledWith(
  345. 'unlinkUserFromGroupSSO',
  346. memberUserIdToremove,
  347. ctx.subscriptionId
  348. )
  349. .should.equal(true)
  350. sinon.assert.calledTwice(ctx.Modules.promises.hooks.fire)
  351. resolve()
  352. },
  353. }
  354. ctx.Controller.removeSelfFromGroup(ctx.req, res, resolve)
  355. })
  356. })
  357. it('should call the group SSO hooks with group SSO disabled', async function (ctx) {
  358. await new Promise(resolve => {
  359. const userIdToRemove = '31231'
  360. ctx.req.session.user._id = userIdToRemove
  361. ctx.req.params = { user_id: userIdToRemove }
  362. ctx.req.entity = ctx.subscription
  363. ctx.Modules.promises.hooks.fire
  364. .withArgs('hasGroupSSOEnabled', ctx.subscription)
  365. .resolves([false])
  366. const res = {
  367. sendStatus: () => {
  368. ctx.Modules.promises.hooks.fire
  369. .calledWith('hasGroupSSOEnabled', ctx.subscription)
  370. .should.equal(true)
  371. sinon.assert.calledOnce(ctx.Modules.promises.hooks.fire)
  372. resolve()
  373. },
  374. }
  375. ctx.Controller.removeSelfFromGroup(ctx.req, res, resolve)
  376. })
  377. })
  378. })
  379. describe('addSeatsToGroupSubscription', function () {
  380. it('should render the "add seats" page', async function (ctx) {
  381. await new Promise((resolve, reject) => {
  382. const res = {
  383. render: (page, props) => {
  384. ctx.SubscriptionGroupHandler.promises.getUsersGroupSubscriptionDetails
  385. .calledWith(ctx.req.session.user._id)
  386. .should.equal(true)
  387. ctx.SubscriptionGroupHandler.promises.ensureFlexibleLicensingEnabled
  388. .calledWith(ctx.plan)
  389. .should.equal(true)
  390. ctx.SubscriptionGroupHandler.promises.ensureSubscriptionIsActive
  391. .calledWith(ctx.subscription)
  392. .should.equal(true)
  393. ctx.SubscriptionGroupHandler.promises.ensureSubscriptionHasNoPastDueInvoice
  394. .calledWith(ctx.subscription)
  395. .should.equal(true)
  396. ctx.SubscriptionGroupHandler.promises.checkBillingInfoExistence
  397. .calledWith(ctx.recurlySubscription, ctx.adminUserId)
  398. .should.equal(true)
  399. ctx.SubscriptionGroupHandler.promises.ensureSubscriptionHasAdditionalLicenseAddOnWhenCollectionMethodIsManual
  400. .calledWith(ctx.recurlySubscription)
  401. .should.equal(true)
  402. page.should.equal('subscriptions/add-seats')
  403. props.subscriptionId.should.equal(ctx.subscriptionId)
  404. props.groupName.should.equal(ctx.subscription.teamName)
  405. props.totalLicenses.should.equal(ctx.subscription.membersLimit)
  406. props.isProfessional.should.equal(false)
  407. props.isCollectionMethodManual.should.equal(true)
  408. resolve()
  409. },
  410. }
  411. ctx.Controller.addSeatsToGroupSubscription(ctx.req, res)
  412. })
  413. })
  414. it('should redirect to subscription page when getting subscription details fails', async function (ctx) {
  415. await new Promise(resolve => {
  416. ctx.SubscriptionGroupHandler.promises.getUsersGroupSubscriptionDetails =
  417. sinon.stub().rejects()
  418. const res = {
  419. redirect: url => {
  420. url.should.equal('/user/subscription')
  421. resolve()
  422. },
  423. }
  424. ctx.Controller.addSeatsToGroupSubscription(ctx.req, res)
  425. })
  426. })
  427. it('should redirect to subscription page when flexible licensing is not enabled', async function (ctx) {
  428. await new Promise(resolve => {
  429. ctx.SubscriptionGroupHandler.promises.ensureFlexibleLicensingEnabled =
  430. sinon.stub().rejects()
  431. const res = {
  432. redirect: url => {
  433. url.should.equal('/user/subscription')
  434. resolve()
  435. },
  436. }
  437. ctx.Controller.addSeatsToGroupSubscription(ctx.req, res)
  438. })
  439. })
  440. it('should redirect to missing billing information page when billing information is missing', async function (ctx) {
  441. await new Promise(resolve => {
  442. ctx.SubscriptionGroupHandler.promises.checkBillingInfoExistence = sinon
  443. .stub()
  444. .throws(new ctx.Errors.MissingBillingInfoError())
  445. const res = {
  446. redirect: url => {
  447. url.should.equal(
  448. '/user/subscription/group/missing-billing-information'
  449. )
  450. resolve()
  451. },
  452. }
  453. ctx.Controller.addSeatsToGroupSubscription(ctx.req, res)
  454. })
  455. })
  456. it('should redirect to manually collected subscription error page when collection method is manual and has no additional license add-on', async function (ctx) {
  457. await new Promise(resolve => {
  458. ctx.SubscriptionGroupHandler.promises.ensureSubscriptionHasAdditionalLicenseAddOnWhenCollectionMethodIsManual =
  459. sinon
  460. .stub()
  461. .throws(
  462. new ctx.Errors.HasNoAdditionalLicenseWhenManuallyCollectedError()
  463. )
  464. const res = {
  465. redirect: url => {
  466. url.should.equal(
  467. '/user/subscription/group/manually-collected-subscription?error_type=no-additional-license'
  468. )
  469. resolve()
  470. },
  471. }
  472. ctx.Controller.addSeatsToGroupSubscription(ctx.req, res)
  473. })
  474. })
  475. it('should redirect to subscription page when subscription is not active', async function (ctx) {
  476. await new Promise(resolve => {
  477. ctx.SubscriptionGroupHandler.promises.ensureSubscriptionIsActive = sinon
  478. .stub()
  479. .rejects()
  480. const res = {
  481. redirect: url => {
  482. url.should.equal('/user/subscription')
  483. resolve()
  484. },
  485. }
  486. ctx.Controller.addSeatsToGroupSubscription(ctx.req, res)
  487. })
  488. })
  489. it('should redirect to subscription page when subscription has pending invoice', async function (ctx) {
  490. ctx.SubscriptionGroupHandler.promises.ensureSubscriptionHasNoPastDueInvoice =
  491. sinon.stub().rejects()
  492. await new Promise(resolve => {
  493. const res = {
  494. redirect: url => {
  495. url.should.equal('/user/subscription')
  496. resolve()
  497. },
  498. }
  499. ctx.Controller.addSeatsToGroupSubscription(ctx.req, res)
  500. })
  501. })
  502. })
  503. describe('previewAddSeatsSubscriptionChange', function () {
  504. it('should preview "add seats" change', async function (ctx) {
  505. await new Promise(resolve => {
  506. ctx.req.body = { adding: 2 }
  507. const res = {
  508. json: data => {
  509. ctx.SubscriptionGroupHandler.promises.previewAddSeatsSubscriptionChange
  510. .calledWith(ctx.req.session.user._id, ctx.req.body.adding)
  511. .should.equal(true)
  512. data.should.deep.equal(ctx.previewSubscriptionChangeData)
  513. resolve()
  514. },
  515. }
  516. ctx.Controller.previewAddSeatsSubscriptionChange(ctx.req, res)
  517. })
  518. })
  519. it('should fail previewing "add seats" change', async function (ctx) {
  520. ctx.SubscriptionGroupHandler.promises.previewAddSeatsSubscriptionChange =
  521. sinon.stub().rejects()
  522. ctx.req.body = { adding: 2 }
  523. await new Promise(resolve => {
  524. const res = {
  525. status: statusCode => {
  526. statusCode.should.equal(500)
  527. return {
  528. end: () => {
  529. resolve()
  530. },
  531. }
  532. },
  533. }
  534. ctx.Controller.previewAddSeatsSubscriptionChange(ctx.req, res)
  535. })
  536. })
  537. it('should fail previewing "add seats" change with SubtotalLimitExceededError', async function (ctx) {
  538. await new Promise(resolve => {
  539. ctx.req.body = { adding: 2 }
  540. ctx.SubscriptionGroupHandler.promises.previewAddSeatsSubscriptionChange =
  541. sinon.stub().throws(new ctx.Errors.SubtotalLimitExceededError())
  542. const res = {
  543. status: statusCode => {
  544. statusCode.should.equal(422)
  545. return {
  546. json: data => {
  547. data.should.deep.equal({
  548. code: 'subtotal_limit_exceeded',
  549. adding: ctx.req.body.adding,
  550. })
  551. resolve()
  552. },
  553. }
  554. },
  555. }
  556. ctx.Controller.previewAddSeatsSubscriptionChange(ctx.req, res)
  557. })
  558. })
  559. })
  560. describe('createAddSeatsSubscriptionChange', function () {
  561. it('should apply "add seats" change', async function (ctx) {
  562. await new Promise(resolve => {
  563. ctx.req.body = { adding: 2 }
  564. const res = {
  565. json: data => {
  566. ctx.SubscriptionGroupHandler.promises.createAddSeatsSubscriptionChange
  567. .calledWith(ctx.req.session.user._id, ctx.req.body.adding)
  568. .should.equal(true)
  569. data.should.deep.equal(ctx.createSubscriptionChangeData)
  570. resolve()
  571. },
  572. }
  573. ctx.Controller.createAddSeatsSubscriptionChange(ctx.req, res)
  574. })
  575. })
  576. it('should fail applying "add seats" change', async function (ctx) {
  577. await new Promise(resolve => {
  578. ctx.SubscriptionGroupHandler.promises.createAddSeatsSubscriptionChange =
  579. sinon.stub().rejects()
  580. const res = {
  581. status: statusCode => {
  582. statusCode.should.equal(500)
  583. return {
  584. end: () => {
  585. resolve()
  586. },
  587. }
  588. },
  589. }
  590. ctx.Controller.createAddSeatsSubscriptionChange(ctx.req, res)
  591. })
  592. })
  593. it('should fail applying "add seats" change with SubtotalLimitExceededError', async function (ctx) {
  594. await new Promise(resolve => {
  595. ctx.req.body = { adding: 2 }
  596. ctx.SubscriptionGroupHandler.promises.createAddSeatsSubscriptionChange =
  597. sinon.stub().throws(new ctx.Errors.SubtotalLimitExceededError())
  598. const res = {
  599. status: statusCode => {
  600. statusCode.should.equal(422)
  601. return {
  602. json: data => {
  603. data.should.deep.equal({
  604. code: 'subtotal_limit_exceeded',
  605. adding: ctx.req.body.adding,
  606. })
  607. resolve()
  608. },
  609. }
  610. },
  611. }
  612. ctx.Controller.createAddSeatsSubscriptionChange(ctx.req, res)
  613. })
  614. })
  615. it('should send 402 response with PaymentActionRequiredError', async function (ctx) {
  616. await new Promise(resolve => {
  617. const adding = 2
  618. ctx.req.body = { adding }
  619. const error = new ctx.Errors.PaymentActionRequiredError({
  620. clientSecret: 'secret',
  621. publicKey: 'key',
  622. })
  623. ctx.SubscriptionGroupHandler.promises.createAddSeatsSubscriptionChange =
  624. sinon.stub().throws(error)
  625. const res = {
  626. status: statusCode => {
  627. statusCode.should.equal(402)
  628. return {
  629. json: data => {
  630. data.should.deep.equal({
  631. message: 'Payment action required',
  632. clientSecret: error.info.clientSecret,
  633. publicKey: error.info.publicKey,
  634. })
  635. resolve()
  636. },
  637. }
  638. },
  639. }
  640. ctx.Controller.createAddSeatsSubscriptionChange(ctx.req, res)
  641. })
  642. })
  643. it('should return 422 with MultiplePendingChangesError', async function (ctx) {
  644. await new Promise(resolve => {
  645. ctx.req.body = { adding: 2 }
  646. ctx.SubscriptionGroupHandler.promises.createAddSeatsSubscriptionChange =
  647. sinon.stub().throws(new ctx.Errors.MultiplePendingChangesError())
  648. const res = {
  649. status: statusCode => {
  650. statusCode.should.equal(422)
  651. return {
  652. end: () => {
  653. resolve()
  654. },
  655. }
  656. },
  657. }
  658. ctx.Controller.createAddSeatsSubscriptionChange(ctx.req, res)
  659. })
  660. })
  661. })
  662. describe('submitForm', function () {
  663. it('should build and pass the request body to the sales submit handler', async function (ctx) {
  664. await new Promise(resolve => {
  665. const adding = 100
  666. const poNumber = 'PO123456'
  667. ctx.req.body = { adding, poNumber }
  668. const res = {
  669. sendStatus: code => {
  670. ctx.SubscriptionGroupHandler.promises.updateSubscriptionPaymentTerms(
  671. ctx.adminUserId,
  672. ctx.recurlySubscription,
  673. poNumber
  674. )
  675. ctx.Modules.promises.hooks.fire
  676. .calledWith('sendSupportRequest', {
  677. email: ctx.user.email,
  678. subject: 'Sales Contact Form',
  679. message:
  680. '\n' +
  681. '**Overleaf Sales Contact Form:**\n' +
  682. '\n' +
  683. '**Subject:** Self-Serve Group User Increase Request\n' +
  684. '\n' +
  685. `**Estimated Number of Users:** ${adding}\n` +
  686. '\n' +
  687. `**PO Number:** ${poNumber}\n` +
  688. '\n' +
  689. `**Message:** This email has been generated on behalf of user with email **${ctx.user.email}** to request an increase in the total number of users for their subscription.`,
  690. inbox: 'sales',
  691. })
  692. .should.equal(true)
  693. sinon.assert.calledOnce(ctx.Modules.promises.hooks.fire)
  694. code.should.equal(204)
  695. resolve()
  696. },
  697. }
  698. ctx.Controller.submitForm(ctx.req, res, resolve)
  699. })
  700. })
  701. })
  702. describe('subscriptionUpgradePage', function () {
  703. it('should render "subscription upgrade" page', async function (ctx) {
  704. await new Promise(resolve => {
  705. const olSubscription = { membersLimit: 1, teamName: 'test team' }
  706. ctx.SubscriptionModel.Subscription.findOne = () => {
  707. return {
  708. exec: () => olSubscription,
  709. }
  710. }
  711. const res = {
  712. render: (page, data) => {
  713. ctx.SubscriptionGroupHandler.promises.getGroupPlanUpgradePreview
  714. .calledWith(ctx.req.session.user._id)
  715. .should.equal(true)
  716. page.should.equal('subscriptions/upgrade-group-subscription-react')
  717. data.totalLicenses.should.equal(olSubscription.membersLimit)
  718. data.groupName.should.equal(olSubscription.teamName)
  719. data.changePreview.should.equal(ctx.previewSubscriptionChangeData)
  720. resolve()
  721. },
  722. }
  723. ctx.Controller.subscriptionUpgradePage(ctx.req, res)
  724. })
  725. })
  726. it('should redirect if failed to generate preview', async function (ctx) {
  727. await new Promise(resolve => {
  728. ctx.SubscriptionGroupHandler.promises.getGroupPlanUpgradePreview = sinon
  729. .stub()
  730. .rejects()
  731. const res = {
  732. redirect: url => {
  733. url.should.equal('/user/subscription')
  734. resolve()
  735. },
  736. }
  737. ctx.Controller.subscriptionUpgradePage(ctx.req, res)
  738. })
  739. })
  740. it('should redirect to missing billing information page when billing information is missing', async function (ctx) {
  741. await new Promise(resolve => {
  742. ctx.SubscriptionGroupHandler.promises.getGroupPlanUpgradePreview = sinon
  743. .stub()
  744. .throws(new ctx.Errors.MissingBillingInfoError())
  745. const res = {
  746. redirect: url => {
  747. url.should.equal(
  748. '/user/subscription/group/missing-billing-information'
  749. )
  750. resolve()
  751. },
  752. }
  753. ctx.Controller.subscriptionUpgradePage(ctx.req, res)
  754. })
  755. })
  756. it('should redirect to manually collected subscription error page when collection method is manual', async function (ctx) {
  757. await new Promise(resolve => {
  758. ctx.SubscriptionGroupHandler.promises.getGroupPlanUpgradePreview = sinon
  759. .stub()
  760. .throws(new ctx.Errors.ManuallyCollectedError())
  761. const res = {
  762. redirect: url => {
  763. url.should.equal(
  764. '/user/subscription/group/manually-collected-subscription?error_type=plan-upgrade'
  765. )
  766. resolve()
  767. },
  768. }
  769. ctx.Controller.subscriptionUpgradePage(ctx.req, res)
  770. })
  771. })
  772. it('should redirect to subtotal limit exceeded page', async function (ctx) {
  773. await new Promise(resolve => {
  774. ctx.SubscriptionGroupHandler.promises.getGroupPlanUpgradePreview = sinon
  775. .stub()
  776. .throws(new ctx.Errors.SubtotalLimitExceededError())
  777. const res = {
  778. redirect: url => {
  779. url.should.equal('/user/subscription/group/subtotal-limit-exceeded')
  780. resolve()
  781. },
  782. }
  783. ctx.Controller.subscriptionUpgradePage(ctx.req, res)
  784. })
  785. })
  786. })
  787. describe('upgradeSubscription', function () {
  788. it('should send 200 response', async function (ctx) {
  789. await new Promise(resolve => {
  790. ctx.SubscriptionGroupHandler.promises.upgradeGroupPlan = sinon
  791. .stub()
  792. .resolves()
  793. const res = {
  794. sendStatus: code => {
  795. code.should.equal(200)
  796. resolve()
  797. },
  798. }
  799. ctx.Controller.upgradeSubscription(ctx.req, res)
  800. })
  801. })
  802. it('should send 500 response', async function (ctx) {
  803. await new Promise(resolve => {
  804. ctx.SubscriptionGroupHandler.promises.upgradeGroupPlan = sinon
  805. .stub()
  806. .rejects()
  807. const res = {
  808. sendStatus: code => {
  809. code.should.equal(500)
  810. resolve()
  811. },
  812. }
  813. ctx.Controller.upgradeSubscription(ctx.req, res)
  814. })
  815. })
  816. it('should send 402 response with PaymentActionRequiredError', async function (ctx) {
  817. await new Promise(resolve => {
  818. const error = new ctx.Errors.PaymentActionRequiredError({
  819. clientSecret: 'secret',
  820. publicKey: 'public',
  821. })
  822. ctx.SubscriptionGroupHandler.promises.upgradeGroupPlan = sinon
  823. .stub()
  824. .rejects(error)
  825. const res = {
  826. status: code => {
  827. code.should.equal(402)
  828. return {
  829. json: data => {
  830. data.should.deep.equal({
  831. message: 'Payment action required',
  832. clientSecret: error.info.clientSecret,
  833. publicKey: error.info.publicKey,
  834. })
  835. resolve()
  836. },
  837. }
  838. },
  839. }
  840. ctx.Controller.upgradeSubscription(ctx.req, res)
  841. })
  842. })
  843. it('should send 422 response with MultiplePendingChangesError', async function (ctx) {
  844. await new Promise(resolve => {
  845. ctx.SubscriptionGroupHandler.promises.upgradeGroupPlan = sinon
  846. .stub()
  847. .rejects(new ctx.Errors.MultiplePendingChangesError())
  848. const res = {
  849. status: code => {
  850. code.should.equal(422)
  851. return {
  852. json: data => {
  853. data.should.deep.equal({
  854. code: 'multiple_pending_changes',
  855. message:
  856. 'Cannot upgrade subscription while there are multiple pending subscription changes. Please contact support.',
  857. })
  858. resolve()
  859. },
  860. }
  861. },
  862. }
  863. ctx.Controller.upgradeSubscription(ctx.req, res)
  864. })
  865. })
  866. })
  867. })