diff --git a/test/api/v3/unit/models/user.test.js b/test/api/v3/unit/models/user.test.js index 63d99193b6..14959bd823 100644 --- a/test/api/v3/unit/models/user.test.js +++ b/test/api/v3/unit/models/user.test.js @@ -352,7 +352,12 @@ describe('User Model', () => { context('manage unallocated stats points notifications', () => { it('doesn\'t add a notification if there are no points to allocate', async () => { let user = new User(); + + user.flags.classSelected = true; + user.preferences.disableClasses = false; + user.stats.class = 'warrior'; user = await user.save(); // necessary for user.isSelected to work correctly + const oldNotificationsCount = user.notifications.length; user.stats.points = 0; @@ -363,6 +368,10 @@ describe('User Model', () => { it('removes a notification if there are no more points to allocate', async () => { let user = new User(); + + user.flags.classSelected = true; + user.preferences.disableClasses = false; + user.stats.class = 'warrior'; user.stats.points = 9; user = await user.save(); // necessary for user.isSelected to work correctly @@ -377,6 +386,9 @@ describe('User Model', () => { it('adds a notification if there are points to allocate', async () => { let user = new User(); + user.flags.classSelected = true; + user.preferences.disableClasses = false; + user.stats.class = 'warrior'; user = await user.save(); // necessary for user.isSelected to work correctly const oldNotificationsCount = user.notifications.length; @@ -391,6 +403,9 @@ describe('User Model', () => { it('adds a notification if the points to allocate have changed', async () => { let user = new User(); user.stats.points = 9; + user.flags.classSelected = true; + user.preferences.disableClasses = false; + user.stats.class = 'warrior'; user = await user.save(); // necessary for user.isSelected to work correctly const oldNotificationsCount = user.notifications.length; @@ -406,6 +421,37 @@ describe('User Model', () => { expect(user.notifications[0].data.points).to.equal(11); expect(user.notifications[0].id).to.not.equal(oldNotificationsUUID); }); + + it('does not add a notification if the user has disabled classes', async () => { + let user = new User(); + user.stats.points = 9; + user.flags.classSelected = true; + user.preferences.disableClasses = true; + user.stats.class = 'warrior'; + user = await user.save(); // necessary for user.isSelected to work correctly + + const oldNotificationsCount = user.notifications.length; + + user.stats.points = 9; + user = await user.save(); + + expect(user.notifications.length).to.equal(oldNotificationsCount); + }); + + it('does not add a notification if the user has not selected a class', async () => { + let user = new User(); + user.stats.points = 9; + user.flags.classSelected = false; + user.stats.class = 'warrior'; + user = await user.save(); // necessary for user.isSelected to work correctly + + const oldNotificationsCount = user.notifications.length; + + user.stats.points = 9; + user = await user.save(); + + expect(user.notifications.length).to.equal(oldNotificationsCount); + }); }); }); diff --git a/website/server/models/user/hooks.js b/website/server/models/user/hooks.js index 022137ef43..3667fb5860 100644 --- a/website/server/models/user/hooks.js +++ b/website/server/models/user/hooks.js @@ -253,8 +253,9 @@ schema.pre('save', true, function preSaveUser (next, done) { } // Manage unallocated stats points notifications - if (this.isSelected('stats') && this.isSelected('notifications')) { + if (this.isSelected('stats') && this.isSelected('notifications') && this.isSelected('flags') && this.isSelected('preferences')) { const pointsToAllocate = this.stats.points; + const classNotEnabled = !this.flags.classSelected || this.preferences.disableClasses; // Sometimes there can be more than 1 notification const existingNotifications = this.notifications.filter(notification => { @@ -271,7 +272,7 @@ schema.pre('save', true, function preSaveUser (next, done) { let notificationsToRemove = outdatedNotification ? existingNotificationsLength : existingNotificationsLength - 1; // If there are points to allocate and the notification is outdated, add a new notifications - if (pointsToAllocate > 0 && outdatedNotification) { + if (pointsToAllocate > 0 && !classNotEnabled && outdatedNotification) { this.addNotification('UNALLOCATED_STATS_POINTS', { points: pointsToAllocate }); }