| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /* eslint-disable
- max-len,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const SandboxedModule = require('sandboxed-module')
- const assert = require('assert')
- const { expect } = require('chai')
- const path = require('path')
- const sinon = require('sinon')
- const modulePath = path.join(
- __dirname,
- '../../../../app/src/Features/Institutions/InstitutionsController'
- )
- describe('InstitutionsController', function () {
- beforeEach(function () {
- this.host = 'mit.edu'.split('').reverse().join('')
- this.stubbedUser1 = {
- _id: '3131231',
- name: 'bob',
- email: 'hello@world.com',
- emails: [
- { email: 'stubb1@mit.edu', reversedHostname: this.host },
- { email: 'test@test.com', reversedHostname: 'test.com' },
- { email: 'another@mit.edu', reversedHostname: this.host },
- ],
- }
- this.stubbedUser1DecoratedEmails = [
- {
- email: 'stubb1@mit.edu',
- reversedHostname: this.host,
- samlIdentifier: { hasEntitlement: false },
- },
- { email: 'test@test.com', reversedHostname: 'test.com' },
- {
- email: 'another@mit.edu',
- reversedHostname: this.host,
- samlIdentifier: { hasEntitlement: true },
- },
- ]
- this.stubbedUser2 = {
- _id: '3131232',
- name: 'test',
- email: 'hello2@world.com',
- emails: [{ email: 'subb2@mit.edu', reversedHostname: this.host }],
- }
- this.stubbedUser2DecoratedEmails = [
- {
- email: 'subb2@mit.edu',
- reversedHostname: this.host,
- },
- ]
- this.getUsersByHostname = sinon.stub().callsArgWith(
- 2,
- null,
- [this.stubbedUser1, this.stubbedUser2].map(user => {
- return { _id: user._id }
- })
- )
- this.addAffiliation = sinon.stub().callsArgWith(3, null)
- this.refreshFeatures = sinon.stub().yields(null)
- this.getUserFullEmails = sinon.stub()
- this.getUserFullEmails
- .withArgs(this.stubbedUser1._id)
- .yields(null, this.stubbedUser1DecoratedEmails)
- this.getUserFullEmails
- .withArgs(this.stubbedUser2._id)
- .yields(null, this.stubbedUser2DecoratedEmails)
- this.InstitutionsController = SandboxedModule.require(modulePath, {
- requires: {
- '../User/UserGetter': {
- getUsersByHostname: this.getUsersByHostname,
- getUserFullEmails: this.getUserFullEmails,
- },
- '../Institutions/InstitutionsAPI': {
- addAffiliation: this.addAffiliation,
- },
- '../Subscription/FeaturesUpdater': {
- refreshFeatures: this.refreshFeatures,
- },
- },
- })
- this.req = { body: { hostname: 'mit.edu' } }
- this.res = {
- send: sinon.stub(),
- json: sinon.stub(),
- }
- return (this.next = sinon.stub())
- })
- describe('affiliateUsers', function () {
- it('should add affiliations for matching users', function (done) {
- this.res.sendStatus = code => {
- code.should.equal(200)
- this.getUsersByHostname.calledOnce.should.equal(true)
- this.addAffiliation.calledThrice.should.equal(true)
- this.addAffiliation
- .calledWithMatch(
- this.stubbedUser1._id,
- this.stubbedUser1.emails[0].email,
- { entitlement: false }
- )
- .should.equal(true)
- this.addAffiliation
- .calledWithMatch(
- this.stubbedUser1._id,
- this.stubbedUser1.emails[2].email,
- { entitlement: true }
- )
- .should.equal(true)
- this.addAffiliation
- .calledWithMatch(
- this.stubbedUser2._id,
- this.stubbedUser2.emails[0].email,
- { entitlement: undefined }
- )
- .should.equal(true)
- this.refreshFeatures
- .calledWith(this.stubbedUser1._id)
- .should.equal(true)
- this.refreshFeatures
- .calledWith(this.stubbedUser2._id)
- .should.equal(true)
- return done()
- }
- this.next.callsFake(done)
- return this.InstitutionsController.confirmDomain(
- this.req,
- this.res,
- this.next
- )
- })
- it('should return errors if last affiliation cannot be added', function (done) {
- this.addAffiliation.onCall(2).callsArgWith(3, new Error('error'))
- this.next = error => {
- expect(error).to.exist
- this.getUsersByHostname.calledOnce.should.equal(true)
- return done()
- }
- return this.InstitutionsController.confirmDomain(
- this.req,
- this.res,
- this.next
- )
- })
- })
- })
|