InstitutionsFeaturesTests.js 4.3 KB

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