2018-10-07 11:40:26 -04:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = path.join __dirname, "../../../../app/js/Features/Institutions/InstitutionsController"
|
|
|
|
expect = require("chai").expect
|
|
|
|
|
|
|
|
describe "InstitutionsController", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@logger = err: sinon.stub(), log: ->
|
2018-10-08 07:08:29 -04:00
|
|
|
@host = "mit.edu".split('').reverse().join('')
|
2018-10-07 11:40:26 -04:00
|
|
|
@stubbedUser1 =
|
|
|
|
_id: "3131231"
|
|
|
|
name:"bob"
|
|
|
|
email:"hello@world.com"
|
|
|
|
emails: [
|
2018-10-08 08:37:12 -04:00
|
|
|
{"email":"stubb1@mit.edu","reversedHostname":@host},
|
|
|
|
{"email":"test@test.com","reversedHostname":"test.com"},
|
|
|
|
{"email":"another@mit.edu","reversedHostname":@host}
|
2018-10-07 11:40:26 -04:00
|
|
|
]
|
|
|
|
@stubbedUser2 =
|
|
|
|
_id: "3131232"
|
|
|
|
name:"test"
|
|
|
|
email:"hello2@world.com"
|
|
|
|
emails: [
|
2018-10-08 08:37:12 -04:00
|
|
|
{"email":"subb2@mit.edu","reversedHostname":@host}
|
2018-10-07 11:40:26 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
@getUsersByHostname = sinon.stub().callsArgWith(2, null, [ @stubbedUser1, @stubbedUser2 ])
|
|
|
|
@addAffiliation = sinon.stub().callsArgWith(3, null)
|
2019-02-05 10:39:28 -05:00
|
|
|
@refreshFeatures = sinon.stub().callsArgWith(2, null)
|
2018-10-07 11:40:26 -04:00
|
|
|
@InstitutionsController = SandboxedModule.require modulePath, requires:
|
|
|
|
'logger-sharelatex': @logger
|
|
|
|
'../User/UserGetter':
|
|
|
|
getUsersByHostname: @getUsersByHostname
|
|
|
|
'../Institutions/InstitutionsAPI':
|
|
|
|
addAffiliation: @addAffiliation
|
2018-10-30 10:18:29 -04:00
|
|
|
'../../models/Institution': Institution: @Institution =
|
|
|
|
findOneAndUpdate: sinon.stub().yields()
|
2019-02-05 10:39:28 -05:00
|
|
|
'../Subscription/FeaturesUpdater':
|
|
|
|
refreshFeatures: @refreshFeatures
|
2018-10-07 11:40:26 -04:00
|
|
|
|
|
|
|
@req =
|
2018-10-30 10:18:29 -04:00
|
|
|
body: hostname: 'mit.edu'
|
2018-10-07 11:40:26 -04:00
|
|
|
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
|
|
|
json: sinon.stub()
|
|
|
|
@next = sinon.stub()
|
|
|
|
|
2018-11-13 05:32:29 -05:00
|
|
|
describe 'affiliateUsers', ->
|
2018-10-07 11:40:26 -04:00
|
|
|
it 'should add affiliations for matching users', (done)->
|
|
|
|
@res.sendStatus = (code) =>
|
2018-11-13 05:32:29 -05:00
|
|
|
code.should.equal 200
|
2018-10-07 11:40:26 -04:00
|
|
|
@getUsersByHostname.calledOnce.should.equal true
|
2018-10-08 07:08:29 -04:00
|
|
|
@addAffiliation.calledThrice.should.equal true
|
2018-10-07 11:40:26 -04:00
|
|
|
@addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[0].email).should.equal true
|
2018-10-08 07:08:29 -04:00
|
|
|
@addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[2].email).should.equal true
|
2018-11-13 05:32:29 -05:00
|
|
|
@addAffiliation.calledWith(@stubbedUser2._id, @stubbedUser2.emails[0].email).should.equal true
|
2019-02-05 10:39:28 -05:00
|
|
|
@refreshFeatures.calledWith(@stubbedUser1._id, true).should.equal true
|
|
|
|
@refreshFeatures.calledWith(@stubbedUser2._id, true).should.equal true
|
2018-10-07 11:40:26 -04:00
|
|
|
done()
|
|
|
|
@InstitutionsController.confirmDomain @req, @res, @next
|
2018-10-30 10:18:29 -04:00
|
|
|
|
2018-11-13 05:32:29 -05:00
|
|
|
it 'should return errors if last affiliation cannot be added', (done)->
|
|
|
|
@addAffiliation.onCall(2).callsArgWith(3, new Error("error"))
|
|
|
|
@next = (error) =>
|
|
|
|
expect(error).to.exist
|
|
|
|
@getUsersByHostname.calledOnce.should.equal true
|
|
|
|
done()
|
|
|
|
@InstitutionsController.confirmDomain @req, @res, @next
|
|
|
|
|
|
|
|
describe 'createInstitution', ->
|
2018-10-30 10:18:29 -04:00
|
|
|
it 'should create new institution', (done)->
|
|
|
|
@req.body.institution_id = 123
|
|
|
|
expectedData = v1Id: 123
|
|
|
|
@res.sendStatus = (code) =>
|
|
|
|
sinon.assert.calledWith(
|
|
|
|
@Institution.findOneAndUpdate,
|
|
|
|
expectedData,
|
|
|
|
expectedData,
|
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
@InstitutionsController.confirmDomain @req, @res, @next
|