2016-11-11 12:03:01 -05:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
modulePath = path.join __dirname, '../../../../app/js/Features/Announcements/AnnouncementsHandler'
|
|
|
|
sinon = require("sinon")
|
|
|
|
expect = require("chai").expect
|
|
|
|
|
|
|
|
|
|
|
|
describe 'AnnouncementsHandler', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2017-01-24 11:03:05 -05:00
|
|
|
@user =
|
2017-02-14 07:29:48 -05:00
|
|
|
_id:"3c6afe000000000000000000" #2002-02-14T00:00:00.000Z
|
2017-01-24 11:03:05 -05:00
|
|
|
email: "someone@gmail.com"
|
2016-11-11 12:03:01 -05:00
|
|
|
@AnalyticsManager =
|
2018-02-07 04:33:28 -05:00
|
|
|
getLastOccurrence: sinon.stub()
|
2016-11-11 12:03:01 -05:00
|
|
|
@BlogHandler =
|
|
|
|
getLatestAnnouncements:sinon.stub()
|
2017-01-25 04:34:53 -05:00
|
|
|
@settings = {}
|
2016-11-11 12:03:01 -05:00
|
|
|
@handler = SandboxedModule.require modulePath, requires:
|
|
|
|
"../Analytics/AnalyticsManager":@AnalyticsManager
|
|
|
|
"../Blog/BlogHandler":@BlogHandler
|
2017-01-25 04:34:53 -05:00
|
|
|
"settings-sharelatex":@settings
|
2016-11-11 12:03:01 -05:00
|
|
|
"logger-sharelatex":
|
|
|
|
log:->
|
|
|
|
|
|
|
|
describe "getUnreadAnnouncements", ->
|
|
|
|
beforeEach ->
|
|
|
|
@stubbedAnnouncements = [
|
|
|
|
{
|
|
|
|
date: new Date(1478836800000),
|
|
|
|
id: '/2016/11/01/introducting-latex-code-checker'
|
|
|
|
}, {
|
|
|
|
date: new Date(1308369600000),
|
|
|
|
id: '/2013/08/02/thesis-series-pt1'
|
|
|
|
}, {
|
|
|
|
date: new Date(1108369600000),
|
2017-02-14 07:29:48 -05:00
|
|
|
id: '/2005/08/04/somethingelse'
|
2016-11-11 12:03:01 -05:00
|
|
|
}, {
|
|
|
|
date: new Date(1208369600000),
|
2017-02-14 07:29:48 -05:00
|
|
|
id: '/2008/04/12/title-date-irrelivant'
|
2016-11-11 12:03:01 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
@BlogHandler.getLatestAnnouncements.callsArgWith(0, null, @stubbedAnnouncements)
|
|
|
|
|
|
|
|
|
2016-12-07 06:39:22 -05:00
|
|
|
it "should mark all announcements as read is false", (done)->
|
2018-02-07 04:33:28 -05:00
|
|
|
@AnalyticsManager.getLastOccurrence.callsArgWith(2, null, [])
|
2017-01-24 11:03:05 -05:00
|
|
|
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
2016-12-07 06:39:22 -05:00
|
|
|
announcements[0].read.should.equal false
|
|
|
|
announcements[1].read.should.equal false
|
|
|
|
announcements[2].read.should.equal false
|
|
|
|
announcements[3].read.should.equal false
|
2016-11-11 12:03:01 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should should be sorted again to ensure correct order", (done)->
|
2018-02-07 04:33:28 -05:00
|
|
|
@AnalyticsManager.getLastOccurrence.callsArgWith(2, null, [])
|
2017-01-24 11:03:05 -05:00
|
|
|
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
2016-11-11 12:03:01 -05:00
|
|
|
announcements[3].should.equal @stubbedAnnouncements[2]
|
|
|
|
announcements[2].should.equal @stubbedAnnouncements[3]
|
|
|
|
announcements[1].should.equal @stubbedAnnouncements[1]
|
|
|
|
announcements[0].should.equal @stubbedAnnouncements[0]
|
|
|
|
done()
|
|
|
|
|
2016-12-07 06:39:22 -05:00
|
|
|
it "should return older ones marked as read as well", (done)->
|
2018-02-07 04:33:28 -05:00
|
|
|
@AnalyticsManager.getLastOccurrence.callsArgWith(2, null, {segmentation:{blogPostId:"/2008/04/12/title-date-irrelivant"}})
|
2017-01-24 11:03:05 -05:00
|
|
|
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
2016-11-11 12:03:01 -05:00
|
|
|
announcements[0].id.should.equal @stubbedAnnouncements[0].id
|
2016-12-07 06:39:22 -05:00
|
|
|
announcements[0].read.should.equal false
|
|
|
|
|
2016-11-11 12:03:01 -05:00
|
|
|
announcements[1].id.should.equal @stubbedAnnouncements[1].id
|
2016-12-07 06:39:22 -05:00
|
|
|
announcements[1].read.should.equal false
|
|
|
|
|
|
|
|
announcements[2].id.should.equal @stubbedAnnouncements[3].id
|
|
|
|
announcements[2].read.should.equal true
|
|
|
|
|
|
|
|
announcements[3].id.should.equal @stubbedAnnouncements[2].id
|
|
|
|
announcements[3].read.should.equal true
|
|
|
|
|
2016-11-11 12:03:01 -05:00
|
|
|
done()
|
|
|
|
|
2016-12-07 06:39:22 -05:00
|
|
|
it "should return all of them marked as read", (done)->
|
2018-02-07 04:33:28 -05:00
|
|
|
@AnalyticsManager.getLastOccurrence.callsArgWith(2, null, {segmentation:{blogPostId:"/2016/11/01/introducting-latex-code-checker"}})
|
2017-01-24 11:03:05 -05:00
|
|
|
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
2016-12-07 06:39:22 -05:00
|
|
|
announcements[0].read.should.equal true
|
|
|
|
announcements[1].read.should.equal true
|
|
|
|
announcements[2].read.should.equal true
|
|
|
|
announcements[3].read.should.equal true
|
2016-11-11 12:03:01 -05:00
|
|
|
done()
|
2016-12-07 06:39:22 -05:00
|
|
|
|
2017-02-14 07:29:48 -05:00
|
|
|
it "should return posts older than signup date as read", (done)->
|
|
|
|
@stubbedAnnouncements.push({
|
|
|
|
date: new Date(978836800000),
|
|
|
|
id: '/2001/04/12/title-date-irrelivant'
|
|
|
|
})
|
2018-02-07 04:33:28 -05:00
|
|
|
@AnalyticsManager.getLastOccurrence.callsArgWith(2, null, [])
|
2017-02-14 07:29:48 -05:00
|
|
|
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
|
|
|
announcements[0].read.should.equal false
|
|
|
|
announcements[1].read.should.equal false
|
|
|
|
announcements[2].read.should.equal false
|
|
|
|
announcements[3].read.should.equal false
|
|
|
|
announcements[4].read.should.equal true
|
|
|
|
announcements[4].id.should.equal '/2001/04/12/title-date-irrelivant'
|
|
|
|
done()
|
|
|
|
|
2016-12-07 06:39:22 -05:00
|
|
|
|
2017-01-24 11:03:05 -05:00
|
|
|
describe "with custom domain announcements", ->
|
|
|
|
beforeEach ->
|
|
|
|
@stubbedDomainSpecificAnn = [
|
|
|
|
{
|
|
|
|
domains: ["gmail.com", 'yahoo.edu']
|
|
|
|
title: "some message"
|
|
|
|
excerpt: "read this"
|
|
|
|
url:"http://www.sharelatex.com/i/somewhere"
|
|
|
|
id:"iaaa"
|
|
|
|
date: new Date(1308369600000).toString()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
@handler._domainSpecificAnnouncements = sinon.stub().returns(@stubbedDomainSpecificAnn)
|
|
|
|
|
|
|
|
it "should insert the domain specific in the correct place", (done)->
|
2018-02-07 04:33:28 -05:00
|
|
|
@AnalyticsManager.getLastOccurrence.callsArgWith(2, null, [])
|
2017-01-24 11:03:05 -05:00
|
|
|
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
|
|
|
announcements[4].should.equal @stubbedAnnouncements[2]
|
|
|
|
announcements[3].should.equal @stubbedAnnouncements[3]
|
|
|
|
announcements[2].should.equal @stubbedAnnouncements[1]
|
|
|
|
announcements[1].should.equal @stubbedDomainSpecificAnn[0]
|
|
|
|
announcements[0].should.equal @stubbedAnnouncements[0]
|
|
|
|
done()
|
2017-01-25 04:34:53 -05:00
|
|
|
|
|
|
|
describe "_domainSpecificAnnouncements", ->
|
|
|
|
beforeEach ->
|
2017-01-25 04:43:42 -05:00
|
|
|
@settings.domainAnnouncements = [
|
2017-01-25 04:34:53 -05:00
|
|
|
{
|
|
|
|
domains: ["gmail.com", 'yahoo.edu']
|
|
|
|
title: "some message"
|
|
|
|
excerpt: "read this"
|
|
|
|
url:"http://www.sharelatex.com/i/somewhere"
|
|
|
|
id:"id1"
|
|
|
|
date: new Date(1308369600000).toString()
|
|
|
|
}, {
|
|
|
|
domains: ["gmail.com", 'yahoo.edu']
|
|
|
|
title: "some message"
|
|
|
|
excerpt: "read this"
|
|
|
|
url:"http://www.sharelatex.com/i/somewhere"
|
|
|
|
date: new Date(1308369600000).toString()
|
|
|
|
}, {
|
|
|
|
domains: ["gmail.com", 'yahoo.edu']
|
|
|
|
title: "some message"
|
|
|
|
excerpt: "read this"
|
|
|
|
url:"http://www.sharelatex.com/i/somewhere"
|
|
|
|
id:"id3"
|
|
|
|
date: new Date(1308369600000).toString()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
it "should filter announcments which don't have an id", (done) ->
|
|
|
|
result = @handler._domainSpecificAnnouncements "someone@gmail.com"
|
|
|
|
result.length.should.equal 2
|
|
|
|
result[0].id.should.equal "id1"
|
|
|
|
result[1].id.should.equal "id3"
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
it "should match on domain", (done) ->
|
2017-01-25 04:43:42 -05:00
|
|
|
@settings.domainAnnouncements[2].domains = ["yahoo.com"]
|
2017-01-25 04:34:53 -05:00
|
|
|
result = @handler._domainSpecificAnnouncements "someone@gmail.com"
|
|
|
|
result.length.should.equal 1
|
|
|
|
result[0].id.should.equal "id1"
|
|
|
|
done()
|
|
|
|
|
|
|
|
|