SubscriptionGroupController.test.mjs 28 KB

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