store reversedHostname property and update test accordingly

This commit is contained in:
hugh-obrien 2018-10-08 13:37:12 +01:00
parent 3919acad46
commit f6307f9086
7 changed files with 37 additions and 20 deletions

View file

@ -5,13 +5,14 @@ async = require('async')
module.exports = InstitutionsController =
confirmDomain: (req, res, next) ->
hostname = req.body.hostname.split('').reverse().join('')
hostname = req.body.hostname
reversedHostname = hostname.trim().split('').reverse().join('')
UserGetter.getUsersByHostname hostname, {_id:1, emails:1}, (error, users) ->
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
matchingEmails = user.emails.filter (email) -> email.reversedHostname == reversedHostname
for email in matchingEmails
addAffiliation user._id, email.email, {confirmedAt: email.confirmedAt}, (error) =>
if error?

View file

@ -59,8 +59,8 @@ module.exports = UserGetter =
@getUserByMainEmail email, projection, callback
getUsersByHostname: (hostname, projection, callback = (error, users) ->) ->
hostname = hostname.trim()
query = emails: { $exists: true }, 'emails.hostname': hostname
reversedHostname = hostname.trim().split('').reverse().join('')
query = emails: { $exists: true }, 'emails.reversedHostname': reversedHostname
db.users.find query, projection, callback
getUsers: (user_ids, projection, callback = (error, users) ->) ->

View file

@ -66,8 +66,8 @@ module.exports = UserUpdater =
logger.err error: error, 'problem adding affiliation while adding email'
return callback(error)
hostname = newEmail.split('@')[1].split('').reverse().join('')
update = $push: emails: email: newEmail, createdAt: new Date(), hostname: hostname
reversedHostname = newEmail.split('@')[1].split('').reverse().join('')
update = $push: emails: email: newEmail, createdAt: new Date(), reversedHostname: reversedHostname
@updateUser userId, update, (error) ->
if error?
logger.err error: error, 'problem updating users emails'

View file

@ -10,7 +10,7 @@ UserSchema = new Schema
email : {type : String, default : ''}
emails: [{
email: { type : String, default : '' },
hostname: { type : String, default : '' },
reversedHostname: { type : String, default : '' },
createdAt: { type : Date, default: () -> new Date() },
confirmedAt: { type: Date }
}],

View file

@ -16,16 +16,16 @@ describe "InstitutionsController", ->
name:"bob"
email:"hello@world.com"
emails: [
{"email":"stubb1@mit.edu","hostname":@host},
{"email":"test@test.com","hostname":"test.com"},
{"email":"another@mit.edu","hostname":@host}
{"email":"stubb1@mit.edu","reversedHostname":@host},
{"email":"test@test.com","reversedHostname":"test.com"},
{"email":"another@mit.edu","reversedHostname":@host}
]
@stubbedUser2 =
_id: "3131232"
name:"test"
email:"hello2@world.com"
emails: [
{"email":"subb2@mit.edu","hostname":@host}
{"email":"subb2@mit.edu","reversedHostname":@host}
]
@getUsersByHostname = sinon.stub().callsArgWith(2, null, [ @stubbedUser1, @stubbedUser2 ])

View file

@ -14,12 +14,15 @@ describe "UserGetter", ->
_id: '12390i'
email: 'email2@foo.bar'
emails: [
{ email: 'email1@foo.bar' }
{ email: 'email2@foo.bar' }
{ email: 'email1@foo.bar', reversedHostname: 'rab.oof' }
{ email: 'email2@foo.bar', reversedHostname: 'rab.oof' }
]
@findOne = sinon.stub().callsArgWith(2, null, @fakeUser)
@find = sinon.stub().callsArgWith(2, null, [ @fakeUser ])
@Mongo =
db: users: findOne: @findOne
db: users:
findOne: @findOne
find: @find
ObjectId: (id) -> return id
settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
@getUserAffiliations = sinon.stub().callsArgWith(1, null, [])
@ -66,8 +69,8 @@ describe "UserGetter", ->
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @fakeUser)
@UserGetter.getUserFullEmails @fakeUser._id, (error, fullEmails) =>
assert.deepEqual fullEmails, [
{ email: 'email1@foo.bar', default: false }
{ email: 'email2@foo.bar', default: true }
{ email: 'email1@foo.bar', reversedHostname: 'rab.oof', default: false }
{ email: 'email2@foo.bar', reversedHostname: 'rab.oof', default: true }
]
done()
@ -86,7 +89,8 @@ describe "UserGetter", ->
@UserGetter.getUserFullEmails @fakeUser._id, (error, fullEmails) =>
assert.deepEqual fullEmails, [
{
email: 'email1@foo.bar'
email: 'email1@foo.bar',
reversedHostname: 'rab.oof'
default: false
affiliation:
institution: affiliationsData[0].institution
@ -94,7 +98,7 @@ describe "UserGetter", ->
department: affiliationsData[0].department
role: affiliationsData[0].role
}
{ email: 'email2@foo.bar', default: true }
{ email: 'email2@foo.bar', reversedHostname: 'rab.oof', default: true }
]
done()
@ -161,6 +165,18 @@ describe "UserGetter", ->
@findOne.calledWith(email: email, projection).should.equal true
done()
describe "getUsersByHostname", ->
it "should find user by hostname", (done)->
hostname = "bar.foo"
expectedQuery =
emails: {$exists: true },
'emails.reversedHostname': hostname.split('').reverse().join('')
projection = emails: 1
@UserGetter.getUsersByHostname hostname, projection, (error, users) =>
@find.calledOnce.should.equal true
@find.calledWith(expectedQuery, projection).should.equal true
done()
describe 'ensureUniqueEmailAddress', ->
beforeEach ->
@UserGetter.getUserByAnyEmail = sinon.stub()

View file

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