From 30833b5116410cfd72994e44a9a2915113aa8ba9 Mon Sep 17 00:00:00 2001 From: Ariel Date: Mon, 16 Sep 2024 16:29:28 -0400 Subject: [PATCH] merge project1 changes to team repo --- src/user/settings.js | 25 ++++++++++--------------- test/user.js | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/user/settings.js b/src/user/settings.js index d85a712ba6..1dee000c89 100644 --- a/src/user/settings.js +++ b/src/user/settings.js @@ -98,22 +98,10 @@ module.exports = function (User) { User.saveSettings = async function (uid, data) { const maxPostsPerPage = meta.config.maxPostsPerPage || 20; - if ( - !data.postsPerPage || - parseInt(data.postsPerPage, 10) <= 1 || - parseInt(data.postsPerPage, 10) > maxPostsPerPage - ) { - throw new Error(`[[error:invalid-pagination-value, 2, ${maxPostsPerPage}]]`); - } - const maxTopicsPerPage = meta.config.maxTopicsPerPage || 20; - if ( - !data.topicsPerPage || - parseInt(data.topicsPerPage, 10) <= 1 || - parseInt(data.topicsPerPage, 10) > maxTopicsPerPage - ) { - throw new Error(`[[error:invalid-pagination-value, 2, ${maxTopicsPerPage}]]`); - } + + validatePagination(data.postsPerPage, maxPostsPerPage); + validatePagination(data.topicsPerPage, maxTopicsPerPage); const languageCodes = await languages.listCodes(); if (data.userLang && !languageCodes.includes(data.userLang)) { @@ -161,6 +149,13 @@ module.exports = function (User) { return await User.getSettings(uid); }; + function validatePagination(value, maxLimit) { + const intValue = parseInt(value, 10); + if (!value || intValue <= 1 || intValue > maxLimit) { + throw new Error(`[[error:invalid-pagination-value, 2, ${maxLimit}]]`); + } + } + User.updateDigestSetting = async function (uid, dailyDigestFreq) { await db.sortedSetsRemove(['digest:day:uids', 'digest:week:uids', 'digest:month:uids'], uid); if (['day', 'week', 'biweek', 'month'].includes(dailyDigestFreq)) { diff --git a/test/user.js b/test/user.js index 25c0ddc6f0..f24238b162 100644 --- a/test/user.js +++ b/test/user.js @@ -2698,4 +2698,26 @@ describe('User', () => { }); }); }); + describe('User.saveSettings', () => { + it('should throw an error if postsPerPage exceeds the maximum limit', async () => { + const mockUid = 'testUser'; + const invalidData = { postsPerPage: 1000, topicsPerPage: 10 }; + try { + await User.saveSettings(mockUid, invalidData); + } catch (error) { + assert.strictEqual(error.message, '[[error:invalid-pagination-value, 2, 20]]'); + } + }); + + it('should throw an error if topicsPerPage is less than the minimum limit', async () => { + const mockUid = 'testUser'; + const invalidData = { postsPerPage: 10, topicsPerPage: 0 }; + + try { + await User.saveSettings(mockUid, invalidData); + } catch (error) { + assert.strictEqual(error.message, '[[error:invalid-pagination-value, 2, 20]]'); + } + }); + }); });