From 3919acad46564a4bdc8c26cf6b994d4ab911348b Mon Sep 17 00:00:00 2001 From: hugh-obrien Date: Mon, 8 Oct 2018 12:08:29 +0100 Subject: [PATCH] store reversed hostname and review fixes for v2 university domain confirmation --- .../Institutions/InstitutionsController.coffee | 17 ++++++++++++----- .../app/coffee/Features/User/UserUpdater.coffee | 2 +- services/web/app/coffee/router.coffee | 2 +- .../InstitutionsControllerTests.coffee | 11 +++++++---- .../unit/coffee/User/UserUpdaterTests.coffee | 2 +- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee b/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee index de562530b9..6325d9336d 100644 --- a/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee +++ b/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee @@ -5,12 +5,19 @@ async = require('async') module.exports = InstitutionsController = confirmDomain: (req, res, next) -> - hostname = req.body.hostname + hostname = req.body.hostname.split('').reverse().join('') UserGetter.getUsersByHostname hostname, {_id:1, emails:1}, (error, 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 fetching users by hostname' + return next(error) + async.map users, ((user) -> + matchingEmails = user.emails.filter (email) -> email.hostname == hostname + for email in matchingEmails + addAffiliation user._id, email.email, {confirmedAt: email.confirmedAt}, (error) => + if error? + logger.err error: error, 'problem adding affiliation while confirming hostname' + return next(error) + ), (error) -> if error? - logger.err error: error, 'problem adding affiliation while confirming hostname' return next(error) res.sendStatus 200 diff --git a/services/web/app/coffee/Features/User/UserUpdater.coffee b/services/web/app/coffee/Features/User/UserUpdater.coffee index 3d6f8544cf..832943fbf0 100644 --- a/services/web/app/coffee/Features/User/UserUpdater.coffee +++ b/services/web/app/coffee/Features/User/UserUpdater.coffee @@ -66,7 +66,7 @@ module.exports = UserUpdater = logger.err error: error, 'problem adding affiliation while adding email' return callback(error) - hostname = newEmail.split('@')[1] + hostname = newEmail.split('@')[1].split('').reverse().join('') update = $push: emails: email: newEmail, createdAt: new Date(), hostname: hostname @updateUser userId, update, (error) -> if error? diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index 050ab32b92..e072d6d0f9 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -331,7 +331,7 @@ module.exports = class Router ), AuthenticationController.httpAuth, CompileController.getFileFromClsiWithoutUser - publicApiRouter.post '/api/institutions/confirm_affiliation_domain', AuthenticationController.httpAuth, InstitutionsController.confirmDomain + publicApiRouter.post '/api/institutions/confirm_university_domain', AuthenticationController.httpAuth, InstitutionsController.confirmDomain webRouter.get '/teams', (req, res, next) -> # Match v1 behaviour - if the user is signed in, show their teams list diff --git a/services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee b/services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee index 8355dceea4..1d59dc6633 100644 --- a/services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee +++ b/services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee @@ -10,20 +10,22 @@ describe "InstitutionsController", -> beforeEach -> @logger = err: sinon.stub(), log: -> + @host = "mit.edu".split('').reverse().join('') @stubbedUser1 = _id: "3131231" name:"bob" email:"hello@world.com" emails: [ - {"email":"stubb1@mit.edu","hostname":"mit.edu"}, - {"email":"test@test.com","hostname":"test.com"} + {"email":"stubb1@mit.edu","hostname":@host}, + {"email":"test@test.com","hostname":"test.com"}, + {"email":"another@mit.edu","hostname":@host} ] @stubbedUser2 = _id: "3131232" name:"test" email:"hello2@world.com" emails: [ - {"email":"subb2@mit.edu","hostname":"mit.edu"} + {"email":"subb2@mit.edu","hostname":@host} ] @getUsersByHostname = sinon.stub().callsArgWith(2, null, [ @stubbedUser1, @stubbedUser2 ]) @@ -48,7 +50,8 @@ describe "InstitutionsController", -> @req.body.hostname = "mit.edu" @res.sendStatus = (code) => @getUsersByHostname.calledOnce.should.equal true - @addAffiliation.calledTwice.should.equal true + @addAffiliation.calledThrice.should.equal true @addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[0].email).should.equal true + @addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[2].email).should.equal true done() @InstitutionsController.confirmDomain @req, @res, @next diff --git a/services/web/test/unit/coffee/User/UserUpdaterTests.coffee b/services/web/test/unit/coffee/User/UserUpdaterTests.coffee index aaf2c032bc..16d98e1db5 100644 --- a/services/web/test/unit/coffee/User/UserUpdaterTests.coffee +++ b/services/web/test/unit/coffee/User/UserUpdaterTests.coffee @@ -90,7 +90,7 @@ describe "UserUpdater", -> @UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=> @UserGetter.ensureUniqueEmailAddress.called.should.equal true should.not.exist(err) - hostname = @newEmail.split('@')[1] + hostname = @newEmail.split('@')[1].split('').reverse().join('') @UserUpdater.updateUser.calledWith( @stubbedUser._id, $push: { emails: { email: @newEmail, createdAt: sinon.match.date, hostname: hostname } }