InstitutionsFeaturesTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 = { getUserFullEmails: sinon.stub() }
  24. this.PlansLocator = { findLocalPlanInSettings: sinon.stub() }
  25. this.institutionPlanCode = 'institution_plan_code'
  26. this.InstitutionsFeatures = SandboxedModule.require(modulePath, {
  27. requires: {
  28. '../User/UserGetter': this.UserGetter,
  29. '../Subscription/PlansLocator': this.PlansLocator,
  30. '@overleaf/settings': {
  31. institutionPlanCode: this.institutionPlanCode,
  32. },
  33. },
  34. })
  35. return (this.userId = '12345abcde')
  36. })
  37. describe('hasLicence', function () {
  38. it('should handle error', function (done) {
  39. this.UserGetter.getUserFullEmails.yields(new Error('Nope'))
  40. return this.InstitutionsFeatures.hasLicence(
  41. this.userId,
  42. (error, hasLicence) => {
  43. expect(error).to.exist
  44. return done()
  45. }
  46. )
  47. })
  48. it('should return false if user has no paid affiliations', function (done) {
  49. const emailData = [{ emailHasInstitutionLicence: false }]
  50. this.UserGetter.getUserFullEmails.yields(null, emailData)
  51. return this.InstitutionsFeatures.hasLicence(
  52. this.userId,
  53. (error, hasLicence) => {
  54. expect(error).to.not.exist
  55. expect(hasLicence).to.be.false
  56. return done()
  57. }
  58. )
  59. })
  60. it('should return true if user has confirmed paid affiliation', function (done) {
  61. const emailData = [
  62. { emailHasInstitutionLicence: true },
  63. { emailHasInstitutionLicence: false },
  64. ]
  65. this.UserGetter.getUserFullEmails.yields(null, emailData)
  66. return this.InstitutionsFeatures.hasLicence(
  67. this.userId,
  68. (error, hasLicence) => {
  69. expect(error).to.not.exist
  70. expect(hasLicence).to.be.true
  71. return done()
  72. }
  73. )
  74. })
  75. })
  76. describe('getInstitutionsFeatures', function () {
  77. beforeEach(function () {
  78. this.InstitutionsFeatures.getInstitutionsPlan = sinon.stub()
  79. this.testFeatures = { features: { institution: 'all' } }
  80. return this.PlansLocator.findLocalPlanInSettings
  81. .withArgs(this.institutionPlanCode)
  82. .returns(this.testFeatures)
  83. })
  84. it('should handle error', function (done) {
  85. this.InstitutionsFeatures.getInstitutionsPlan.yields(new Error('Nope'))
  86. return this.InstitutionsFeatures.getInstitutionsFeatures(
  87. this.userId,
  88. (error, features) => {
  89. expect(error).to.exist
  90. return done()
  91. }
  92. )
  93. })
  94. it('should return no feaures if user has no plan code', function (done) {
  95. this.InstitutionsFeatures.getInstitutionsPlan.yields(null, null)
  96. return this.InstitutionsFeatures.getInstitutionsFeatures(
  97. this.userId,
  98. (error, features) => {
  99. expect(error).to.not.exist
  100. expect(features).to.deep.equal({})
  101. return done()
  102. }
  103. )
  104. })
  105. it('should return feaures if user has affiliations plan code', function (done) {
  106. this.InstitutionsFeatures.getInstitutionsPlan.yields(
  107. null,
  108. this.institutionPlanCode
  109. )
  110. return this.InstitutionsFeatures.getInstitutionsFeatures(
  111. this.userId,
  112. (error, features) => {
  113. expect(error).to.not.exist
  114. expect(features).to.deep.equal(this.testFeatures.features)
  115. return done()
  116. }
  117. )
  118. })
  119. })
  120. describe('getInstitutionsPlan', function () {
  121. beforeEach(function () {
  122. return (this.InstitutionsFeatures.hasLicence = sinon.stub())
  123. })
  124. it('should handle error', function (done) {
  125. this.InstitutionsFeatures.hasLicence.yields(new Error('Nope'))
  126. return this.InstitutionsFeatures.getInstitutionsPlan(
  127. this.userId,
  128. error => {
  129. expect(error).to.exist
  130. return done()
  131. }
  132. )
  133. })
  134. it('should return no plan if user has no licence', function (done) {
  135. this.InstitutionsFeatures.hasLicence.yields(null, false)
  136. return this.InstitutionsFeatures.getInstitutionsPlan(
  137. this.userId,
  138. (error, plan) => {
  139. expect(error).to.not.exist
  140. expect(plan).to.equal(null)
  141. return done()
  142. }
  143. )
  144. })
  145. it('should return plan if user has licence', function (done) {
  146. this.InstitutionsFeatures.hasLicence.yields(null, true)
  147. return this.InstitutionsFeatures.getInstitutionsPlan(
  148. this.userId,
  149. (error, plan) => {
  150. expect(error).to.not.exist
  151. expect(plan).to.equal(this.institutionPlanCode)
  152. return done()
  153. }
  154. )
  155. })
  156. })
  157. })