InstitutionsControllerTests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/Institutions/InstitutionsController'
  21. )
  22. describe('InstitutionsController', function () {
  23. beforeEach(function () {
  24. this.host = 'mit.edu'.split('').reverse().join('')
  25. this.stubbedUser1 = {
  26. _id: '3131231',
  27. name: 'bob',
  28. email: 'hello@world.com',
  29. emails: [
  30. { email: 'stubb1@mit.edu', reversedHostname: this.host },
  31. { email: 'test@test.com', reversedHostname: 'test.com' },
  32. { email: 'another@mit.edu', reversedHostname: this.host },
  33. ],
  34. }
  35. this.stubbedUser1DecoratedEmails = [
  36. {
  37. email: 'stubb1@mit.edu',
  38. reversedHostname: this.host,
  39. samlIdentifier: { hasEntitlement: false },
  40. },
  41. { email: 'test@test.com', reversedHostname: 'test.com' },
  42. {
  43. email: 'another@mit.edu',
  44. reversedHostname: this.host,
  45. samlIdentifier: { hasEntitlement: true },
  46. },
  47. ]
  48. this.stubbedUser2 = {
  49. _id: '3131232',
  50. name: 'test',
  51. email: 'hello2@world.com',
  52. emails: [{ email: 'subb2@mit.edu', reversedHostname: this.host }],
  53. }
  54. this.stubbedUser2DecoratedEmails = [
  55. {
  56. email: 'subb2@mit.edu',
  57. reversedHostname: this.host,
  58. },
  59. ]
  60. this.getInstitutionUsersByHostname = sinon.stub().yields(null, [
  61. {
  62. _id: this.stubbedUser1._id,
  63. emails: this.stubbedUser1DecoratedEmails,
  64. },
  65. {
  66. _id: this.stubbedUser2._id,
  67. emails: this.stubbedUser2DecoratedEmails,
  68. },
  69. ])
  70. this.addAffiliation = sinon.stub().callsArgWith(3, null)
  71. this.refreshFeatures = sinon.stub().yields(null)
  72. this.InstitutionsController = SandboxedModule.require(modulePath, {
  73. requires: {
  74. '../User/UserGetter': {
  75. getInstitutionUsersByHostname: this.getInstitutionUsersByHostname,
  76. },
  77. '../Institutions/InstitutionsAPI': {
  78. addAffiliation: this.addAffiliation,
  79. },
  80. '../Subscription/FeaturesUpdater': {
  81. refreshFeatures: this.refreshFeatures,
  82. },
  83. },
  84. })
  85. this.req = { body: { hostname: 'mit.edu' } }
  86. this.res = {
  87. send: sinon.stub(),
  88. json: sinon.stub(),
  89. }
  90. return (this.next = sinon.stub())
  91. })
  92. describe('affiliateUsers', function () {
  93. it('should add affiliations for matching users', function (done) {
  94. this.res.sendStatus = code => {
  95. code.should.equal(200)
  96. this.getInstitutionUsersByHostname.calledOnce.should.equal(true)
  97. this.addAffiliation.calledThrice.should.equal(true)
  98. this.addAffiliation
  99. .calledWithMatch(
  100. this.stubbedUser1._id,
  101. this.stubbedUser1.emails[0].email,
  102. { entitlement: false }
  103. )
  104. .should.equal(true)
  105. this.addAffiliation
  106. .calledWithMatch(
  107. this.stubbedUser1._id,
  108. this.stubbedUser1.emails[2].email,
  109. { entitlement: true }
  110. )
  111. .should.equal(true)
  112. this.addAffiliation
  113. .calledWithMatch(
  114. this.stubbedUser2._id,
  115. this.stubbedUser2.emails[0].email,
  116. { entitlement: undefined }
  117. )
  118. .should.equal(true)
  119. this.refreshFeatures
  120. .calledWith(this.stubbedUser1._id)
  121. .should.equal(true)
  122. this.refreshFeatures
  123. .calledWith(this.stubbedUser2._id)
  124. .should.equal(true)
  125. this.refreshFeatures.should.have.been.calledTwice
  126. return done()
  127. }
  128. this.next.callsFake(done)
  129. return this.InstitutionsController.confirmDomain(
  130. this.req,
  131. this.res,
  132. this.next
  133. )
  134. })
  135. it('should return errors if last affiliation cannot be added', function (done) {
  136. this.addAffiliation.onCall(2).callsArgWith(3, new Error('error'))
  137. this.next = error => {
  138. expect(error).to.exist
  139. this.getInstitutionUsersByHostname.calledOnce.should.equal(true)
  140. return done()
  141. }
  142. return this.InstitutionsController.confirmDomain(
  143. this.req,
  144. this.res,
  145. this.next
  146. )
  147. })
  148. })
  149. })