return an error if expires is not a valid date.

This commit is contained in:
Shane Kilkelly 2016-08-15 08:54:45 +01:00
parent 88f5f29982
commit bea4197732
2 changed files with 37 additions and 2 deletions

View file

@ -32,7 +32,12 @@ module.exports =
# 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
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)
removeNotificationId: (user_id, notification_id, callback)->

View file

@ -28,7 +28,10 @@ describe 'Notifications Tests', ->
@mongojs.ObjectId = ObjectId
@notifications = SandboxedModule.require modulePath, requires:
'logger-sharelatex': log:->
'logger-sharelatex': {
log:()->
error:()->
}
'settings-sharelatex': {}
'mongojs':@mongojs
@ -100,6 +103,33 @@ describe 'Notifications Tests', ->
@insertStub.calledWith(@expectedDocument).should.equal true
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', ->
it 'should mark the notification id as read', (done)->
@updateStub.callsArgWith(2, null)