2021-03-23 05:24:20 +03:00
const should = require ( 'should' ) ;
const sinon = require ( 'sinon' ) ;
2021-10-07 19:16:58 +03:00
const moment = require ( 'moment' ) ;
2021-10-06 13:12:21 +03:00
const Notifications = require ( '../../../../../core/server/services/notifications/notifications' ) ;
const { owner } = require ( '../../../../utils/fixtures/context' ) ;
2021-03-23 05:24:20 +03:00
describe ( 'Notifications Service' , function ( ) {
2021-03-23 08:02:52 +03:00
it ( 'can browse non-major version upgrade notifications' , function ( ) {
const settingsCache = {
get : sinon . fake . returns ( [ {
dismissible : true ,
id : '130f7c24-113a-4768-a698-12a8b34223f1' ,
type : 'info' ,
message : ` <strong>Ghost 5.1.3 is now available</strong> - You are using an old version of Ghost, which means you don't have access to the latest features. <a href= \' https://ghost.org/changelog/4/ \' target= \' _blank \' rel= \' noopener \' >Read more!</a> ` ,
createdAt : '2021-03-16T12:55:20.000Z' ,
addedAt : '2021-03-17T01:41:20.906Z'
} ] )
} ;
const notificationSvc = new Notifications ( {
settingsCache ,
ghostVersion : {
full : '4.1.0'
}
} ) ;
const notifications = notificationSvc . browse ( { user : owner } ) ;
should . exist ( notifications ) ;
notifications . length . should . equal ( 1 ) ;
} ) ;
it ( 'can browse major version upgrade notifications' , function ( ) {
2021-03-23 05:24:20 +03:00
const settingsCache = {
get : sinon . fake . returns ( [ {
dismissible : true ,
location : 'bottom' ,
status : 'alert' ,
2021-03-23 06:42:46 +03:00
id : '130f7c24-113a-4768-a698-12a8b34223f6' ,
2021-03-23 05:24:20 +03:00
custom : true ,
createdAt : '2021-03-16T12:55:20.000Z' ,
type : 'info' ,
top : true ,
2021-03-23 06:42:46 +03:00
message : ` <strong>Ghost 5.0 is now available</strong> - You are using an old version of Ghost, which means you don't have access to the latest features. <a href= \' https://ghost.org/changelog/4/ \' target= \' _blank \' rel= \' noopener \' >Read more!</a> ` ,
2021-03-23 05:24:20 +03:00
seen : true ,
addedAt : '2021-03-17T01:41:20.906Z' ,
seenBy : [ '1' ]
2021-03-23 06:42:46 +03:00
} ] )
} ;
const notificationSvc = new Notifications ( {
settingsCache ,
ghostVersion : {
full : '4.0.0'
2021-03-23 05:24:20 +03:00
}
2021-03-23 06:42:46 +03:00
} ) ;
const notifications = notificationSvc . browse ( { user : owner } ) ;
should . exist ( notifications ) ;
notifications . length . should . equal ( 1 ) ;
} ) ;
2021-03-23 08:02:52 +03:00
it ( 'cannot see 2.0 version upgrade notifications in Ghost 3.0' , function ( ) {
2021-03-23 06:42:46 +03:00
const settingsCache = {
get : sinon . fake . returns ( [ {
dismissible : true ,
location : 'bottom' ,
status : 'alert' ,
id : '130f7c24-113a-4768-a698-12a8b34223f7' ,
custom : true ,
createdAt : '2020-03-16T12:55:20.000Z' ,
type : 'info' ,
top : true ,
message : ` <strong>Ghost 2.0 is now available</strong> - You are using an old version of Ghost, which means you don't have access to the latest features. ` ,
seen : true ,
addedAt : '2020-03-17T01:41:20.906Z' ,
seenBy : [ '1' ]
} ] )
2021-03-23 05:24:20 +03:00
} ;
const notificationSvc = new Notifications ( {
settingsCache ,
ghostVersion : {
full : '3.0.0'
}
} ) ;
const notifications = notificationSvc . browse ( { user : owner } ) ;
should . exist ( notifications ) ;
2021-03-23 06:42:46 +03:00
notifications . length . should . equal ( 0 ) ;
2021-03-23 05:24:20 +03:00
} ) ;
2021-03-23 08:02:52 +03:00
it ( 'cannot see 4.0 version upgrade notifications in Ghost 4.0' , function ( ) {
2021-03-23 05:24:20 +03:00
const settingsCache = {
get : sinon . fake . returns ( [ {
dismissible : true ,
location : 'bottom' ,
status : 'alert' ,
2021-03-23 06:42:46 +03:00
id : '130f7c24-113a-4768-a698-12a8b34223f8' ,
2021-03-23 05:24:20 +03:00
custom : true ,
createdAt : '2021-03-16T12:55:20.000Z' ,
type : 'info' ,
top : true ,
2021-03-23 06:42:46 +03:00
message : ` <strong>Ghost 4.0 is now available</strong> - You are using an old version of Ghost, which means you don't have access to the latest features. ` ,
2021-03-23 05:24:20 +03:00
seen : true ,
addedAt : '2021-03-17T01:41:20.906Z' ,
seenBy : [ '1' ]
2021-03-23 06:42:46 +03:00
} ] )
} ;
const notificationSvc = new Notifications ( {
settingsCache ,
ghostVersion : {
full : '4.0.0'
2021-03-23 05:24:20 +03:00
}
2021-03-23 06:42:46 +03:00
} ) ;
const notifications = notificationSvc . browse ( { user : owner } ) ;
should . exist ( notifications ) ;
notifications . length . should . equal ( 0 ) ;
} ) ;
2021-03-23 08:02:52 +03:00
it ( 'cannot see 5.0 version upgrade notifications in Ghost 5.0' , function ( ) {
2021-03-23 06:42:46 +03:00
const settingsCache = {
get : sinon . fake . returns ( [ {
dismissible : true ,
location : 'bottom' ,
status : 'alert' ,
id : '130f7c24-113a-4768-a698-12a8b34223f9' ,
custom : true ,
createdAt : '2022-03-16T12:55:20.000Z' ,
type : 'info' ,
top : true ,
message : ` <strong>Ghost 5.0 is now available</strong> - You are using an old version of Ghost, which means you don't have access to the latest features. ` ,
seen : true ,
addedAt : '2022-03-17T01:41:20.906Z' ,
seenBy : [ '1' ]
} ] )
2021-03-23 05:24:20 +03:00
} ;
const notificationSvc = new Notifications ( {
settingsCache ,
ghostVersion : {
2021-03-23 06:42:46 +03:00
full : '5.0.0'
2021-03-23 05:24:20 +03:00
}
} ) ;
const notifications = notificationSvc . browse ( { user : owner } ) ;
should . exist ( notifications ) ;
notifications . length . should . equal ( 0 ) ;
} ) ;
2021-06-24 15:33:01 +03:00
2021-10-07 19:16:58 +03:00
describe ( 'add' , function ( ) {
it ( 'adds a single notification when no previous exist' , function ( ) {
const existingNotifications = [ ] ;
const settingsCache = {
get : sinon . fake . returns ( existingNotifications )
} ;
const notificationsSvc = new Notifications ( {
settingsCache ,
ghostVersion : {
full : '4.1.0'
}
} ) ;
const { allNotifications , notificationsToAdd } = notificationsSvc . add ( {
notifications : [ {
custom : true ,
createdAt : moment ( ) . toDate ( ) ,
status : 'alert' ,
type : 'info' ,
2021-10-07 19:24:05 +03:00
dismissible : false ,
2021-10-07 19:16:58 +03:00
top : true ,
message : 'Hello test world!'
} ]
} ) ;
allNotifications . length . should . equal ( 0 ) ;
notificationsToAdd . length . should . equal ( 1 ) ;
2021-10-07 19:24:05 +03:00
const createdNotification = notificationsToAdd [ 0 ] ;
createdNotification . id . should . not . be . undefined ( ) ;
createdNotification . custom . should . be . true ( ) ;
createdNotification . createdAt . should . not . be . undefined ( ) ;
createdNotification . status . should . equal ( 'alert' ) ;
createdNotification . type . should . equal ( 'info' ) ;
createdNotification . dismissible . should . be . false ( ) ;
createdNotification . top . should . be . true ( ) ;
createdNotification . message . should . equal ( 'Hello test world!' ) ;
2021-10-07 19:25:57 +03:00
createdNotification . createdAtVersion . should . equal ( '4.1.0' ) ;
2021-10-07 19:16:58 +03:00
} ) ;
} ) ;
2021-06-24 15:33:01 +03:00
describe ( 'Stored notifications data corruption recovery' , function ( ) {
2021-06-24 15:36:47 +03:00
it ( 'should correct broken notifications data on browse' , function ( ) {
2021-06-24 15:33:01 +03:00
const settingsCache = {
get : sinon . fake . returns ( {
message : 'this object should be an array!'
} )
} ;
const settingsModelStub = sinon . stub ( ) . resolves ( ) ;
const notificationSvc = new Notifications ( {
settingsCache ,
ghostVersion : {
full : '5.0.0'
} ,
SettingsModel : {
edit : settingsModelStub
}
} ) ;
const notifications = notificationSvc . browse ( { user : owner } ) ;
should . exist ( notifications ) ;
notifications . length . should . equal ( 0 ) ;
settingsModelStub . called . should . equal ( true ) ;
settingsModelStub . args [ 0 ] [ 0 ] . should . eql ( [ {
key : 'notifications' ,
value : '[]'
} ] ) ;
} ) ;
2021-06-24 15:36:47 +03:00
it ( 'does not trigger correction when the data is in valid format' , function ( ) {
2021-06-24 15:33:01 +03:00
const settingsCache = {
get : sinon . fake . returns ( [ {
message : 'this works! 5.1.0'
} ] )
} ;
const settingsModelStub = sinon . stub ( ) . resolves ( ) ;
const notificationSvc = new Notifications ( {
settingsCache ,
ghostVersion : {
full : '5.0.0'
} ,
SettingsModel : {
edit : settingsModelStub
}
} ) ;
const notifications = notificationSvc . browse ( { user : owner } ) ;
should . exist ( notifications ) ;
notifications . length . should . equal ( 1 ) ;
settingsModelStub . called . should . equal ( false ) ;
} ) ;
} ) ;
2021-03-23 05:24:20 +03:00
} ) ;