2016-01-21 15:42:50 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('chai').assert
|
|
|
|
require('chai').should()
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = require('path').join __dirname, '../../../../app/js/Features/Notifications/NotificationsHandler.js'
|
|
|
|
_ = require('underscore')
|
|
|
|
|
|
|
|
|
|
|
|
describe 'NotificationsHandler', ->
|
|
|
|
user_id = "123nd3ijdks"
|
|
|
|
notification_id = "123njdskj9jlk"
|
|
|
|
notificationUrl = "notification.sharelatex.testing"
|
|
|
|
|
|
|
|
beforeEach ->
|
2016-06-24 09:06:04 -04:00
|
|
|
@request = sinon.stub().callsArgWith(1)
|
2016-01-21 15:42:50 -05:00
|
|
|
@handler = SandboxedModule.require modulePath, requires:
|
|
|
|
"settings-sharelatex": apis:{notifications:{url:notificationUrl}}
|
|
|
|
"request":@request
|
|
|
|
'logger-sharelatex':
|
|
|
|
log:->
|
|
|
|
err:->
|
|
|
|
|
|
|
|
describe "getUserNotifications", ->
|
|
|
|
it 'should get unread notifications', (done)->
|
|
|
|
stubbedNotifications = [{_id: notification_id, user_id: user_id}]
|
2016-06-24 09:06:04 -04:00
|
|
|
@request.callsArgWith(1, null, {statusCode:200}, stubbedNotifications)
|
2016-01-21 15:42:50 -05:00
|
|
|
@handler.getUserNotifications user_id, (err, unreadNotifications)=>
|
|
|
|
stubbedNotifications.should.deep.equal unreadNotifications
|
|
|
|
getOpts =
|
|
|
|
uri: "#{notificationUrl}/user/#{user_id}"
|
|
|
|
json:true
|
2016-02-18 06:43:43 -05:00
|
|
|
timeout:1000
|
2016-06-24 09:06:04 -04:00
|
|
|
method: "GET"
|
|
|
|
@request.calledWith(getOpts).should.equal true
|
2016-01-21 15:42:50 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return empty arrays if there are no notifications', ->
|
2016-06-24 09:06:04 -04:00
|
|
|
@request.callsArgWith(1, null, {statusCode:200}, null)
|
2016-01-21 15:42:50 -05:00
|
|
|
@handler.getUserNotifications user_id, (err, unreadNotifications)=>
|
|
|
|
unreadNotifications.length.should.equal 0
|
|
|
|
|
|
|
|
describe "markAsRead", ->
|
2016-02-05 09:13:38 -05:00
|
|
|
beforeEach ->
|
|
|
|
@key = "some key here"
|
|
|
|
|
2016-01-21 15:42:50 -05:00
|
|
|
it 'should send a delete request when a delete has been received to mark a notification', (done)->
|
2016-02-05 09:13:38 -05:00
|
|
|
@handler.markAsReadWithKey user_id, @key, =>
|
|
|
|
opts =
|
|
|
|
uri: "#{notificationUrl}/user/#{user_id}"
|
|
|
|
json:
|
|
|
|
key:@key
|
|
|
|
timeout:1000
|
2016-06-24 09:06:04 -04:00
|
|
|
method: "DELETE"
|
|
|
|
@request.calledWith(opts).should.equal true
|
2016-01-21 15:42:50 -05:00
|
|
|
done()
|
2016-02-05 09:13:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
describe "createNotification", ->
|
|
|
|
beforeEach ->
|
|
|
|
@key = "some key here"
|
|
|
|
@messageOpts = {value:12344}
|
|
|
|
@templateKey = "renderThisHtml"
|
|
|
|
|
|
|
|
it "should post the message over", (done)->
|
|
|
|
@handler.createNotification user_id, @key, @templateKey, @messageOpts, =>
|
2016-06-24 09:06:04 -04:00
|
|
|
args = @request.args[0][0]
|
2016-02-05 09:13:38 -05:00
|
|
|
args.uri.should.equal "#{notificationUrl}/user/#{user_id}"
|
|
|
|
args.timeout.should.equal 1000
|
|
|
|
expectedJson = {key:@key, templateKey:@templateKey, messageOpts:@messageOpts}
|
|
|
|
assert.deepEqual(args.json, expectedJson)
|
|
|
|
done()
|