InstitutionsControllerTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 should = require('chai').should()
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  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. const { expect } = require('chai')
  23. describe('InstitutionsController', function() {
  24. beforeEach(function() {
  25. this.logger = { err: sinon.stub(), warn: sinon.stub(), log() {} }
  26. this.host = 'mit.edu'
  27. .split('')
  28. .reverse()
  29. .join('')
  30. this.stubbedUser1 = {
  31. _id: '3131231',
  32. name: 'bob',
  33. email: 'hello@world.com',
  34. emails: [
  35. { email: 'stubb1@mit.edu', reversedHostname: this.host },
  36. { email: 'test@test.com', reversedHostname: 'test.com' },
  37. { email: 'another@mit.edu', reversedHostname: this.host }
  38. ]
  39. }
  40. this.stubbedUser2 = {
  41. _id: '3131232',
  42. name: 'test',
  43. email: 'hello2@world.com',
  44. emails: [{ email: 'subb2@mit.edu', reversedHostname: this.host }]
  45. }
  46. this.getUsersByHostname = sinon
  47. .stub()
  48. .callsArgWith(2, null, [this.stubbedUser1, this.stubbedUser2])
  49. this.addAffiliation = sinon.stub().callsArgWith(3, null)
  50. this.refreshFeatures = sinon.stub().yields(null)
  51. this.InstitutionsController = SandboxedModule.require(modulePath, {
  52. globals: {
  53. console: console
  54. },
  55. requires: {
  56. 'logger-sharelatex': this.logger,
  57. '../User/UserGetter': {
  58. getUsersByHostname: this.getUsersByHostname
  59. },
  60. '../Institutions/InstitutionsAPI': {
  61. addAffiliation: this.addAffiliation
  62. },
  63. '../Subscription/FeaturesUpdater': {
  64. refreshFeatures: this.refreshFeatures
  65. }
  66. }
  67. })
  68. this.req = { body: { hostname: 'mit.edu' } }
  69. this.res = {
  70. send: sinon.stub(),
  71. json: sinon.stub()
  72. }
  73. return (this.next = sinon.stub())
  74. })
  75. describe('affiliateUsers', function() {
  76. it('should add affiliations for matching users', function(done) {
  77. this.res.sendStatus = code => {
  78. code.should.equal(200)
  79. this.getUsersByHostname.calledOnce.should.equal(true)
  80. this.addAffiliation.calledThrice.should.equal(true)
  81. this.addAffiliation
  82. .calledWith(this.stubbedUser1._id, this.stubbedUser1.emails[0].email)
  83. .should.equal(true)
  84. this.addAffiliation
  85. .calledWith(this.stubbedUser1._id, this.stubbedUser1.emails[2].email)
  86. .should.equal(true)
  87. this.addAffiliation
  88. .calledWith(this.stubbedUser2._id, this.stubbedUser2.emails[0].email)
  89. .should.equal(true)
  90. this.refreshFeatures
  91. .calledWith(this.stubbedUser1._id)
  92. .should.equal(true)
  93. this.refreshFeatures
  94. .calledWith(this.stubbedUser2._id)
  95. .should.equal(true)
  96. return done()
  97. }
  98. return this.InstitutionsController.confirmDomain(
  99. this.req,
  100. this.res,
  101. this.next
  102. )
  103. })
  104. it('should return errors if last affiliation cannot be added', function(done) {
  105. this.addAffiliation.onCall(2).callsArgWith(3, new Error('error'))
  106. this.next = error => {
  107. expect(error).to.exist
  108. this.getUsersByHostname.calledOnce.should.equal(true)
  109. return done()
  110. }
  111. return this.InstitutionsController.confirmDomain(
  112. this.req,
  113. this.res,
  114. this.next
  115. )
  116. })
  117. })
  118. })