InstitutionsControllerTests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.getUsersByHostname = sinon.stub().callsArgWith(
  61. 2,
  62. null,
  63. [this.stubbedUser1, this.stubbedUser2].map(user => {
  64. return { _id: user._id }
  65. })
  66. )
  67. this.addAffiliation = sinon.stub().callsArgWith(3, null)
  68. this.refreshFeatures = sinon.stub().yields(null)
  69. this.getUserFullEmails = sinon.stub()
  70. this.getUserFullEmails
  71. .withArgs(this.stubbedUser1._id)
  72. .yields(null, this.stubbedUser1DecoratedEmails)
  73. this.getUserFullEmails
  74. .withArgs(this.stubbedUser2._id)
  75. .yields(null, this.stubbedUser2DecoratedEmails)
  76. this.InstitutionsController = SandboxedModule.require(modulePath, {
  77. requires: {
  78. '../User/UserGetter': {
  79. getUsersByHostname: this.getUsersByHostname,
  80. getUserFullEmails: this.getUserFullEmails,
  81. },
  82. '../Institutions/InstitutionsAPI': {
  83. addAffiliation: this.addAffiliation,
  84. },
  85. '../Subscription/FeaturesUpdater': {
  86. refreshFeatures: this.refreshFeatures,
  87. },
  88. },
  89. })
  90. this.req = { body: { hostname: 'mit.edu' } }
  91. this.res = {
  92. send: sinon.stub(),
  93. json: sinon.stub(),
  94. }
  95. return (this.next = sinon.stub())
  96. })
  97. describe('affiliateUsers', function () {
  98. it('should add affiliations for matching users', function (done) {
  99. this.res.sendStatus = code => {
  100. code.should.equal(200)
  101. this.getUsersByHostname.calledOnce.should.equal(true)
  102. this.addAffiliation.calledThrice.should.equal(true)
  103. this.addAffiliation
  104. .calledWithMatch(
  105. this.stubbedUser1._id,
  106. this.stubbedUser1.emails[0].email,
  107. { entitlement: false }
  108. )
  109. .should.equal(true)
  110. this.addAffiliation
  111. .calledWithMatch(
  112. this.stubbedUser1._id,
  113. this.stubbedUser1.emails[2].email,
  114. { entitlement: true }
  115. )
  116. .should.equal(true)
  117. this.addAffiliation
  118. .calledWithMatch(
  119. this.stubbedUser2._id,
  120. this.stubbedUser2.emails[0].email,
  121. { entitlement: undefined }
  122. )
  123. .should.equal(true)
  124. this.refreshFeatures
  125. .calledWith(this.stubbedUser1._id)
  126. .should.equal(true)
  127. this.refreshFeatures
  128. .calledWith(this.stubbedUser2._id)
  129. .should.equal(true)
  130. return done()
  131. }
  132. this.next.callsFake(done)
  133. return this.InstitutionsController.confirmDomain(
  134. this.req,
  135. this.res,
  136. this.next
  137. )
  138. })
  139. it('should return errors if last affiliation cannot be added', function (done) {
  140. this.addAffiliation.onCall(2).callsArgWith(3, new Error('error'))
  141. this.next = error => {
  142. expect(error).to.exist
  143. this.getUsersByHostname.calledOnce.should.equal(true)
  144. return done()
  145. }
  146. return this.InstitutionsController.confirmDomain(
  147. this.req,
  148. this.res,
  149. this.next
  150. )
  151. })
  152. })
  153. })