InstitutionsFeaturesTests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const assert = require('assert')
  15. const { expect } = require('chai')
  16. const sinon = require('sinon')
  17. const modulePath = require('path').join(
  18. __dirname,
  19. '../../../../app/src/Features/Institutions/InstitutionsFeatures.js'
  20. )
  21. describe('InstitutionsFeatures', function () {
  22. beforeEach(function () {
  23. this.UserGetter = {
  24. promises: { getUserFullEmails: sinon.stub().resolves([]) },
  25. }
  26. this.PlansLocator = { findLocalPlanInSettings: sinon.stub() }
  27. this.institutionPlanCode = 'institution_plan_code'
  28. this.InstitutionsFeatures = SandboxedModule.require(modulePath, {
  29. requires: {
  30. '../User/UserGetter': this.UserGetter,
  31. '../Subscription/PlansLocator': this.PlansLocator,
  32. '@overleaf/settings': {
  33. institutionPlanCode: this.institutionPlanCode,
  34. },
  35. },
  36. })
  37. this.emailDataWithLicense = [{ emailHasInstitutionLicence: true }]
  38. this.emailDataWithoutLicense = [{ emailHasInstitutionLicence: false }]
  39. return (this.userId = '12345abcde')
  40. })
  41. describe('hasLicence', function () {
  42. it('should handle error', function (done) {
  43. this.UserGetter.promises.getUserFullEmails.rejects(new Error('Nope'))
  44. return this.InstitutionsFeatures.hasLicence(
  45. this.userId,
  46. (error, hasLicence) => {
  47. expect(error).to.exist
  48. return done()
  49. }
  50. )
  51. })
  52. it('should return false if user has no paid affiliations', function (done) {
  53. this.UserGetter.promises.getUserFullEmails.resolves(
  54. this.emailDataWithoutLicense
  55. )
  56. return this.InstitutionsFeatures.hasLicence(
  57. this.userId,
  58. (error, hasLicence) => {
  59. expect(error).to.not.exist
  60. expect(hasLicence).to.be.false
  61. return done()
  62. }
  63. )
  64. })
  65. it('should return true if user has confirmed paid affiliation', function (done) {
  66. const emailData = [
  67. { emailHasInstitutionLicence: true },
  68. { emailHasInstitutionLicence: false },
  69. ]
  70. this.UserGetter.promises.getUserFullEmails.resolves(emailData)
  71. return this.InstitutionsFeatures.hasLicence(
  72. this.userId,
  73. (error, hasLicence) => {
  74. expect(error).to.not.exist
  75. expect(hasLicence).to.be.true
  76. return done()
  77. }
  78. )
  79. })
  80. })
  81. describe('getInstitutionsFeatures', function () {
  82. beforeEach(function () {
  83. this.testFeatures = { features: { institution: 'all' } }
  84. return this.PlansLocator.findLocalPlanInSettings
  85. .withArgs(this.institutionPlanCode)
  86. .returns(this.testFeatures)
  87. })
  88. it('should handle error', function (done) {
  89. this.UserGetter.promises.getUserFullEmails.rejects(new Error('Nope'))
  90. return this.InstitutionsFeatures.getInstitutionsFeatures(
  91. this.userId,
  92. (error, features) => {
  93. expect(error).to.exist
  94. return done()
  95. }
  96. )
  97. })
  98. it('should return no feaures if user has no plan code', function (done) {
  99. this.UserGetter.promises.getUserFullEmails.resolves(
  100. this.emailDataWithoutLicense
  101. )
  102. return this.InstitutionsFeatures.getInstitutionsFeatures(
  103. this.userId,
  104. (error, features) => {
  105. expect(error).to.not.exist
  106. expect(features).to.deep.equal({})
  107. return done()
  108. }
  109. )
  110. })
  111. it('should return feaures if user has affiliations plan code', function (done) {
  112. this.UserGetter.promises.getUserFullEmails.resolves(
  113. this.emailDataWithLicense
  114. )
  115. return this.InstitutionsFeatures.getInstitutionsFeatures(
  116. this.userId,
  117. (error, features) => {
  118. expect(error).to.not.exist
  119. expect(features).to.deep.equal(this.testFeatures.features)
  120. return done()
  121. }
  122. )
  123. })
  124. })
  125. describe('getInstitutionsPlan', function () {
  126. it('should handle error', function (done) {
  127. this.UserGetter.promises.getUserFullEmails.rejects(new Error('Nope'))
  128. return this.InstitutionsFeatures.getInstitutionsPlan(
  129. this.userId,
  130. error => {
  131. expect(error).to.exist
  132. return done()
  133. }
  134. )
  135. })
  136. it('should return no plan if user has no licence', function (done) {
  137. this.UserGetter.promises.getUserFullEmails.resolves(
  138. this.emailDataWithoutLicense
  139. )
  140. return this.InstitutionsFeatures.getInstitutionsPlan(
  141. this.userId,
  142. (error, plan) => {
  143. expect(error).to.not.exist
  144. expect(plan).to.equal(null)
  145. return done()
  146. }
  147. )
  148. })
  149. it('should return plan if user has licence', function (done) {
  150. this.UserGetter.promises.getUserFullEmails.resolves(
  151. this.emailDataWithLicense
  152. )
  153. return this.InstitutionsFeatures.getInstitutionsPlan(
  154. this.userId,
  155. (error, plan) => {
  156. expect(error).to.not.exist
  157. expect(plan).to.equal(this.institutionPlanCode)
  158. return done()
  159. }
  160. )
  161. })
  162. })
  163. })