From d2a0ab684ac574c2ad34477c5031d2417846849f Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Wed, 19 Jun 2024 18:01:27 +0200 Subject: [PATCH] test fixes --- gulp/gulp-tests.js | 2 +- .../debug/GET-debug-timeTravelTime.test.js | 16 +++++++++++----- .../debug/POST-debug_addHourglass.test.js | 13 ++++++++++--- .../debug/POST-debug_addTenGems.test.js | 13 ++++++++++--- .../debug/POST-debug_jumpTime.test.js | 4 +++- .../debug/POST-debug_make-admin.test.js | 11 +++++++++-- .../debug/POST-debug_modify-inventory.test.js | 11 +++++++++-- .../debug/POST-debug_quest-progress.test.js | 11 +++++++++-- .../debug/POST-debug_set-cron.test.js | 11 +++++++++-- .../server/middlewares/ensureDevelopmentMode.js | 1 + 10 files changed, 72 insertions(+), 21 deletions(-) diff --git a/gulp/gulp-tests.js b/gulp/gulp-tests.js index beb1e43132..64ac63902f 100644 --- a/gulp/gulp-tests.js +++ b/gulp/gulp-tests.js @@ -45,7 +45,7 @@ function runInChildProcess (command, options = {}, envVariables = '') { } function integrationTestCommand (testDir) { - return `nyc --no-clean mocha ${testDir} --recursive --require ./test/helpers/start-server`; + return `nyc --silent --no-clean mocha ${testDir} --recursive --require ./test/helpers/start-server`; } /* Test task definitions */ diff --git a/test/api/v3/integration/debug/GET-debug-timeTravelTime.test.js b/test/api/v3/integration/debug/GET-debug-timeTravelTime.test.js index 329bf7cc9f..c2bcc3b87b 100644 --- a/test/api/v3/integration/debug/GET-debug-timeTravelTime.test.js +++ b/test/api/v3/integration/debug/GET-debug-timeTravelTime.test.js @@ -5,30 +5,36 @@ import { describe('GET /debug/time-travel-time', () => { let user; + let nconfStub; + before(async () => { user = await generateUser({ permissions: { fullAccess: true } }); }); - after(() => { - nconf.set('TIME_TRAVEL_ENABLED', false); + beforeEach(() => { + nconfStub = sandbox.stub(nconf, 'get'); + }); + + afterEach(() => { + nconfStub.restore(); }); it('returns the shifted time', async () => { - nconf.set('TIME_TRAVEL_ENABLED', true); + nconfStub.withArgs('TIME_TRAVEL_ENABLED').returns(true); const result = await user.get('/debug/time-travel-time'); expect(result.time).to.exist; await user.post('/debug/jump-time', { disable: true }); }); it('returns shifted when the user is not an admin', async () => { - nconf.set('TIME_TRAVEL_ENABLED', true); + nconfStub.withArgs('TIME_TRAVEL_ENABLED').returns(true); const regularUser = await generateUser(); const result = await regularUser.get('/debug/time-travel-time'); expect(result.time).to.exist; }); it('returns error when not in time travel mode', async () => { - sandbox.stub(nconf, 'get').withArgs('TIME_TRAVEL_ENABLED').returns(false); + nconfStub.withArgs('TIME_TRAVEL_ENABLED').returns(false); await expect(user.get('/debug/time-travel-time')) .eventually.be.rejected.and.to.deep.equal({ diff --git a/test/api/v3/integration/debug/POST-debug_addHourglass.test.js b/test/api/v3/integration/debug/POST-debug_addHourglass.test.js index 17520a4e75..6d7650e07a 100644 --- a/test/api/v3/integration/debug/POST-debug_addHourglass.test.js +++ b/test/api/v3/integration/debug/POST-debug_addHourglass.test.js @@ -5,16 +5,23 @@ import { describe('POST /debug/add-hourglass', () => { let userToGetHourGlass; + let nconfStub; before(async () => { userToGetHourGlass = await generateUser(); }); - after(() => { - nconf.set('IS_PROD', false); + beforeEach(() => { + nconfStub = sandbox.stub(nconf, 'get'); + nconfStub.withArgs('BASE_URL').returns('https://example.com'); + }); + + afterEach(() => { + nconfStub.restore(); }); it('adds Hourglass to the current user', async () => { + nconfStub.withArgs('DEBUG_ENABLED').returns(true); await userToGetHourGlass.post('/debug/add-hourglass'); const userWithHourGlass = await userToGetHourGlass.get('/user'); @@ -23,7 +30,7 @@ describe('POST /debug/add-hourglass', () => { }); it('returns error when not in production mode', async () => { - sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true); + nconfStub.withArgs('DEBUG_ENABLED').returns(false); await expect(userToGetHourGlass.post('/debug/add-hourglass')) .eventually.be.rejected.and.to.deep.equal({ diff --git a/test/api/v3/integration/debug/POST-debug_addTenGems.test.js b/test/api/v3/integration/debug/POST-debug_addTenGems.test.js index 2ac2834cf2..560381b4b0 100644 --- a/test/api/v3/integration/debug/POST-debug_addTenGems.test.js +++ b/test/api/v3/integration/debug/POST-debug_addTenGems.test.js @@ -5,16 +5,23 @@ import { describe('POST /debug/add-ten-gems', () => { let userToGainTenGems; + let nconfStub; before(async () => { userToGainTenGems = await generateUser(); }); - after(() => { - nconf.set('IS_PROD', false); + beforeEach(() => { + nconfStub = sandbox.stub(nconf, 'get'); + nconfStub.withArgs('BASE_URL').returns('https://example.com'); + }); + + afterEach(() => { + nconfStub.restore(); }); it('adds ten gems to the current user', async () => { + nconfStub.withArgs('DEBUG_ENABLED').returns(true); await userToGainTenGems.post('/debug/add-ten-gems'); const userWithTenGems = await userToGainTenGems.get('/user'); @@ -23,7 +30,7 @@ describe('POST /debug/add-ten-gems', () => { }); it('returns error when not in production mode', async () => { - sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true); + nconfStub.withArgs('DEBUG_ENABLED').returns(false); await expect(userToGainTenGems.post('/debug/add-ten-gems')) .eventually.be.rejected.and.to.deep.equal({ diff --git a/test/api/v3/integration/debug/POST-debug_jumpTime.test.js b/test/api/v3/integration/debug/POST-debug_jumpTime.test.js index 978b4fe05e..191badad03 100644 --- a/test/api/v3/integration/debug/POST-debug_jumpTime.test.js +++ b/test/api/v3/integration/debug/POST-debug_jumpTime.test.js @@ -6,6 +6,8 @@ import { describe('POST /debug/jump-time', () => { let user; let today; + let nconfStub; + before(async () => { user = await generateUser({ permissions: { fullAccess: true } }); today = new Date(); @@ -63,7 +65,7 @@ describe('POST /debug/jump-time', () => { }); it('returns error when not in time travel mode', async () => { - sandbox.stub(nconf, 'get').withArgs('TIME_TRAVEL_ENABLED').returns(false); + nconfStub.withArgs('TIME_TRAVEL_ENABLED').returns(false); await expect(user.post('/debug/jump-time', { offsetDays: 1 })) .eventually.be.rejected.and.to.deep.equal({ diff --git a/test/api/v3/integration/debug/POST-debug_make-admin.test.js b/test/api/v3/integration/debug/POST-debug_make-admin.test.js index f0303496a8..dcbcc2685b 100644 --- a/test/api/v3/integration/debug/POST-debug_make-admin.test.js +++ b/test/api/v3/integration/debug/POST-debug_make-admin.test.js @@ -5,16 +5,23 @@ import { describe('POST /debug/make-admin', () => { let user; + let nconfStub; before(async () => { user = await generateUser(); }); + beforeEach(() => { + nconfStub = sandbox.stub(nconf, 'get'); + nconfStub.withArgs('BASE_URL').returns('https://example.com'); + }); + afterEach(() => { - nconf.set('IS_PROD', false); + nconfStub.restore(); }); it('makes user an admin', async () => { + nconfStub.withArgs('DEBUG_ENABLED').returns(true); await user.post('/debug/make-admin'); await user.sync(); @@ -23,7 +30,7 @@ describe('POST /debug/make-admin', () => { }); it('returns error when not in production mode', async () => { - sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true); + nconfStub.withArgs('DEBUG_ENABLED').returns(false); await expect(user.post('/debug/make-admin')) .eventually.be.rejected.and.to.deep.equal({ diff --git a/test/api/v3/integration/debug/POST-debug_modify-inventory.test.js b/test/api/v3/integration/debug/POST-debug_modify-inventory.test.js index 5efec38a6b..1cd8e368b1 100644 --- a/test/api/v3/integration/debug/POST-debug_modify-inventory.test.js +++ b/test/api/v3/integration/debug/POST-debug_modify-inventory.test.js @@ -8,6 +8,7 @@ import { describe('POST /debug/modify-inventory', () => { let user; let originalItems; + let nconfStub; before(async () => { originalItems = { @@ -39,8 +40,14 @@ describe('POST /debug/modify-inventory', () => { }); }); + beforeEach(() => { + nconfStub = sandbox.stub(nconf, 'get'); + nconfStub.withArgs('DEBUG_ENABLED').returns(true); + nconfStub.withArgs('BASE_URL').returns('https://example.com'); + }); + afterEach(() => { - nconf.set('IS_PROD', false); + nconfStub.restore(); }); it('sets equipment', async () => { @@ -149,7 +156,7 @@ describe('POST /debug/modify-inventory', () => { }); it('returns error when not in production mode', async () => { - sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true); + nconfStub.withArgs('DEBUG_ENABLED').returns(false); await expect(user.post('/debug/modify-inventory')) .eventually.be.rejected.and.to.deep.equal({ diff --git a/test/api/v3/integration/debug/POST-debug_quest-progress.test.js b/test/api/v3/integration/debug/POST-debug_quest-progress.test.js index 8d17b68063..a0bc27f379 100644 --- a/test/api/v3/integration/debug/POST-debug_quest-progress.test.js +++ b/test/api/v3/integration/debug/POST-debug_quest-progress.test.js @@ -5,13 +5,20 @@ import { describe('POST /debug/quest-progress', () => { let user; + let nconfStub; beforeEach(async () => { user = await generateUser(); }); + beforeEach(() => { + nconfStub = sandbox.stub(nconf, 'get'); + nconfStub.withArgs('DEBUG_ENABLED').returns(true); + nconfStub.withArgs('BASE_URL').returns('https://example.com'); + }); + afterEach(() => { - nconf.set('IS_PROD', false); + nconfStub.restore(); }); it('errors if user is not on a quest', async () => { @@ -48,7 +55,7 @@ describe('POST /debug/quest-progress', () => { }); it('returns error when not in production mode', async () => { - sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true); + nconfStub.withArgs('DEBUG_ENABLED').returns(false); await expect(user.post('/debug/quest-progress')) .eventually.be.rejected.and.to.deep.equal({ diff --git a/test/api/v3/integration/debug/POST-debug_set-cron.test.js b/test/api/v3/integration/debug/POST-debug_set-cron.test.js index 331f19f5fa..671160e552 100644 --- a/test/api/v3/integration/debug/POST-debug_set-cron.test.js +++ b/test/api/v3/integration/debug/POST-debug_set-cron.test.js @@ -5,13 +5,20 @@ import { describe('POST /debug/set-cron', () => { let user; + let nconfStub; before(async () => { user = await generateUser(); }); + beforeEach(() => { + nconfStub = sandbox.stub(nconf, 'get'); + nconfStub.withArgs('DEBUG_ENABLED').returns(true); + nconfStub.withArgs('BASE_URL').returns('https://example.com'); + }); + afterEach(() => { - nconf.set('IS_PROD', false); + nconfStub.restore(); }); it('sets last cron', async () => { @@ -27,7 +34,7 @@ describe('POST /debug/set-cron', () => { }); it('returns error when not in production mode', async () => { - sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true); + nconfStub.withArgs('DEBUG_ENABLED').returns(false); await expect(user.post('/debug/set-cron')) .eventually.be.rejected.and.to.deep.equal({ diff --git a/website/server/middlewares/ensureDevelopmentMode.js b/website/server/middlewares/ensureDevelopmentMode.js index e31996b534..79dbd04bdf 100644 --- a/website/server/middlewares/ensureDevelopmentMode.js +++ b/website/server/middlewares/ensureDevelopmentMode.js @@ -4,6 +4,7 @@ import { } from '../libs/errors'; export default function ensureDevelopmentMode (req, res, next) { + console.log(nconf.get('DEBUG_ENABLED'), nconf.get('BASE_URL')); if (nconf.get('DEBUG_ENABLED') && nconf.get('BASE_URL') !== 'https://habitica.com') { next(); } else {