InstitutionsFeatures.test.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import path from 'node:path'
  4. const modulePath = path.join(
  5. import.meta.dirname,
  6. '../../../../app/src/Features/Institutions/InstitutionsFeatures.mjs'
  7. )
  8. describe('InstitutionsFeatures', function () {
  9. beforeEach(async function (ctx) {
  10. ctx.UserGetter = {
  11. promises: { getUserFullEmails: sinon.stub().resolves([]) },
  12. }
  13. ctx.PlansLocator = { findLocalPlanInSettings: sinon.stub() }
  14. ctx.institutionPlanCode = 'institution_plan_code'
  15. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  16. default: ctx.UserGetter,
  17. }))
  18. vi.doMock('../../../../app/src/Features/Subscription/PlansLocator', () => ({
  19. default: ctx.PlansLocator,
  20. }))
  21. vi.doMock('@overleaf/settings', () => ({
  22. default: {
  23. institutionPlanCode: ctx.institutionPlanCode,
  24. },
  25. }))
  26. ctx.InstitutionsFeatures = (await import(modulePath)).default
  27. ctx.emailDataWithLicense = [{ emailHasInstitutionLicence: true }]
  28. ctx.emailDataWithoutLicense = [{ emailHasInstitutionLicence: false }]
  29. ctx.userId = '12345abcde'
  30. })
  31. describe('hasLicence', function () {
  32. it('should handle error', async function (ctx) {
  33. ctx.UserGetter.promises.getUserFullEmails.rejects(new Error('Nope'))
  34. let error
  35. try {
  36. await ctx.InstitutionsFeatures.promises.hasLicence(ctx.userId)
  37. } catch (err) {
  38. error = err
  39. }
  40. expect(error).to.exist
  41. })
  42. it('should return false if user has no paid affiliations', async function (ctx) {
  43. ctx.UserGetter.promises.getUserFullEmails.resolves(
  44. ctx.emailDataWithoutLicense
  45. )
  46. const hasLicence = await ctx.InstitutionsFeatures.promises.hasLicence(
  47. ctx.userId
  48. )
  49. expect(hasLicence).to.be.false
  50. })
  51. it('should return true if user has confirmed paid affiliation', async function (ctx) {
  52. const emailData = [
  53. { emailHasInstitutionLicence: true },
  54. { emailHasInstitutionLicence: false },
  55. ]
  56. ctx.UserGetter.promises.getUserFullEmails.resolves(emailData)
  57. const hasLicence = await ctx.InstitutionsFeatures.promises.hasLicence(
  58. ctx.userId
  59. )
  60. expect(hasLicence).to.be.true
  61. })
  62. })
  63. describe('getInstitutionsFeatures', function () {
  64. beforeEach(function (ctx) {
  65. ctx.testFeatures = { features: { institution: 'all' } }
  66. return ctx.PlansLocator.findLocalPlanInSettings
  67. .withArgs(ctx.institutionPlanCode)
  68. .returns(ctx.testFeatures)
  69. })
  70. it('should handle error', async function (ctx) {
  71. ctx.UserGetter.promises.getUserFullEmails.rejects(new Error('Nope'))
  72. await expect(
  73. ctx.InstitutionsFeatures.promises.getInstitutionsFeatures(ctx.userId)
  74. ).to.be.rejected
  75. })
  76. it('should return no feaures if user has no plan code', async function (ctx) {
  77. ctx.UserGetter.promises.getUserFullEmails.resolves(
  78. ctx.emailDataWithoutLicense
  79. )
  80. const features =
  81. await ctx.InstitutionsFeatures.promises.getInstitutionsFeatures(
  82. ctx.userId
  83. )
  84. expect(features).to.deep.equal({})
  85. })
  86. it('should return feaures if user has affiliations plan code', async function (ctx) {
  87. ctx.UserGetter.promises.getUserFullEmails.resolves(
  88. ctx.emailDataWithLicense
  89. )
  90. const features =
  91. await ctx.InstitutionsFeatures.promises.getInstitutionsFeatures(
  92. ctx.userId
  93. )
  94. expect(features).to.deep.equal(ctx.testFeatures.features)
  95. })
  96. })
  97. describe('getInstitutionsPlan', function () {
  98. it('should handle error', async function (ctx) {
  99. ctx.UserGetter.promises.getUserFullEmails.rejects(new Error('Nope'))
  100. await expect(
  101. ctx.InstitutionsFeatures.promises.getInstitutionsPlan(ctx.userId)
  102. ).to.be.rejected
  103. })
  104. it('should return no plan if user has no licence', async function (ctx) {
  105. ctx.UserGetter.promises.getUserFullEmails.resolves(
  106. ctx.emailDataWithoutLicense
  107. )
  108. const plan = await ctx.InstitutionsFeatures.promises.getInstitutionsPlan(
  109. ctx.userId
  110. )
  111. expect(plan).to.equal(null)
  112. })
  113. it('should return plan if user has licence', async function (ctx) {
  114. ctx.UserGetter.promises.getUserFullEmails.resolves(
  115. ctx.emailDataWithLicense
  116. )
  117. const plan = await ctx.InstitutionsFeatures.promises.getInstitutionsPlan(
  118. ctx.userId
  119. )
  120. expect(plan).to.equal(ctx.institutionPlanCode)
  121. })
  122. })
  123. })