Просмотр исходного кода

store reversedHostname property and update test accordingly

hugh-obrien 8 лет назад
Родитель
Сommit
f6307f9086

+ 3 - 2
services/web/app/coffee/Features/Institutions/InstitutionsController.coffee

@@ -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?

+ 2 - 2
services/web/app/coffee/Features/User/UserGetter.coffee

@@ -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) ->) ->

+ 2 - 2
services/web/app/coffee/Features/User/UserUpdater.coffee

@@ -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'

+ 1 - 1
services/web/app/coffee/models/User.coffee

@@ -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 }
 	}],

+ 4 - 4
services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee

@@ -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 ])

+ 23 - 7
services/web/test/unit/coffee/User/UserGetterTests.coffee

@@ -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()

+ 2 - 2
services/web/test/unit/coffee/User/UserUpdaterTests.coffee

@@ -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()