cleanup and tests for v2 affiliations confirmation

This commit is contained in:
hugh-obrien 2018-10-07 16:40:26 +01:00
parent af3cc01496
commit 65ecdf84f4
3 changed files with 66 additions and 2 deletions

View file

@ -1,7 +1,16 @@
logger = require("logger-sharelatex")
UserGetter = require("../User/UserGetter")
{ addAffiliation } = require("../Institutions/InstitutionsAPI")
async = require('async')
module.exports = InstitutionsController =
confirmDomain: (req, res, next) ->
hostname = req.body.hostname
UserGetter.getUsersByHostname hostname, {_id:1, emails:1}, (error, users) ->
res.json {hostname: hostname, wub: users}
async.map users, (user, error) ->
email = user.emails.filter (email) -> email.hostname == hostname
addAffiliation user._id, email[0].email, {}, (error) =>
if error?
logger.err error: error, 'problem adding affiliation while confirming hostname'
return next(error)
res.sendStatus 200

View file

@ -0,0 +1,54 @@
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: ->
@stubbedUser1 =
_id: "3131231"
name:"bob"
email:"hello@world.com"
emails: [
{"email":"stubb1@mit.edu","hostname":"mit.edu"},
{"email":"test@test.com","hostname":"test.com"}
]
@stubbedUser2 =
_id: "3131232"
name:"test"
email:"hello2@world.com"
emails: [
{"email":"subb2@mit.edu","hostname":"mit.edu"}
]
@getUsersByHostname = sinon.stub().callsArgWith(2, null, [ @stubbedUser1, @stubbedUser2 ])
@addAffiliation = sinon.stub().callsArgWith(3, null)
@InstitutionsController = SandboxedModule.require modulePath, requires:
'logger-sharelatex': @logger
'../User/UserGetter':
getUsersByHostname: @getUsersByHostname
'../Institutions/InstitutionsAPI':
addAffiliation: @addAffiliation
@req =
body:{}
@res =
send: sinon.stub()
json: sinon.stub()
@next = sinon.stub()
describe 'confirmDomain', ->
it 'should add affiliations for matching users', (done)->
@req.body.hostname = "mit.edu"
@res.sendStatus = (code) =>
@getUsersByHostname.calledOnce.should.equal true
@addAffiliation.calledTwice.should.equal true
@addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[0].email).should.equal true
done()
@InstitutionsController.confirmDomain @req, @res, @next

View file

@ -90,9 +90,10 @@ describe "UserUpdater", ->
@UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
@UserGetter.ensureUniqueEmailAddress.called.should.equal true
should.not.exist(err)
hostname = @newEmail.split('@')[1]
@UserUpdater.updateUser.calledWith(
@stubbedUser._id,
$push: { emails: { email: @newEmail, createdAt: sinon.match.date } }
$push: { emails: { email: @newEmail, createdAt: sinon.match.date, hostname: hostname } }
).should.equal true
done()