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

return an error if `expires` is not a valid date.

Shane Kilkelly 10 лет назад
Родитель
Сommit
bea4197732

+ 6 - 1
services/notifications/app/coffee/Notifications.coffee

@@ -32,7 +32,12 @@ module.exports =
 				# in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
 				# in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
 				# see `README.md` for instruction on creating TTL index
 				# see `README.md` for instruction on creating TTL index
 				if notification.expires?
 				if notification.expires?
-					doc.expires =  new Date(notification.expires)
+					try
+						doc.expires =  new Date(notification.expires)
+						_testValue = doc.expires.toISOString()
+					catch err
+						logger.error {user_id, expires: notification.expires}, "error converting `expires` field to Date"
+						return callback(err)
 				db.notifications.insert(doc, callback)
 				db.notifications.insert(doc, callback)
 
 
 	removeNotificationId: (user_id, notification_id, callback)->
 	removeNotificationId: (user_id, notification_id, callback)->

+ 31 - 1
services/notifications/test/unit/coffee/NotificationsTests.coffee

@@ -28,7 +28,10 @@ describe 'Notifications Tests', ->
 		@mongojs.ObjectId = ObjectId
 		@mongojs.ObjectId = ObjectId
 
 
 		@notifications = SandboxedModule.require modulePath, requires:
 		@notifications = SandboxedModule.require modulePath, requires:
-			'logger-sharelatex': log:->
+			'logger-sharelatex': {
+				log:()->
+				error:()->
+			}
 			'settings-sharelatex': {}
 			'settings-sharelatex': {}
 			'mongojs':@mongojs
 			'mongojs':@mongojs
 
 
@@ -100,6 +103,33 @@ describe 'Notifications Tests', ->
 					@insertStub.calledWith(@expectedDocument).should.equal true
 					@insertStub.calledWith(@expectedDocument).should.equal true
 					done()
 					done()
 
 
+		describe 'when the notification has a nonsensical expires field', () ->
+			beforeEach ->
+				@stubbedNotification = {
+					user_id: ObjectId(user_id),
+					key:"notification-key",
+					messageOpts:"some info",
+					templateKey:"template-key",
+					expires: 'WAT'
+				}
+				@expectedDocument = {
+					user_id: @stubbedNotification.user_id,
+					key:"notification-key",
+					messageOpts:"some info",
+					templateKey:"template-key",
+					expires: new Date(@stubbedNotification.expires),
+				}
+
+			it 'should produce an error', (done)->
+				@insertStub.callsArgWith(1, null)
+				@countStub.callsArgWith(1, null, 0)
+
+				@notifications.addNotification user_id, @stubbedNotification, (err)=>
+					(err instanceof Error).should.equal true
+					@insertStub.callCount.should.equal 0
+					@insertStub.calledWith(@expectedDocument).should.equal false
+					done()
+
 	describe 'removeNotificationId', ->
 	describe 'removeNotificationId', ->
 		it 'should mark the notification id as read', (done)->
 		it 'should mark the notification id as read', (done)->
 			@updateStub.callsArgWith(2, null)
 			@updateStub.callsArgWith(2, null)