From 52b0ae0400c12e9222a027d87a628da6f760634e Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sun, 1 Oct 2023 21:35:50 +1300 Subject: [PATCH 01/11] Basic functionality pass --- server/app.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server/app.js b/server/app.js index f10b6ff5e4..6b273e0768 100644 --- a/server/app.js +++ b/server/app.js @@ -208,6 +208,20 @@ app.get('/download/:id', asyncHandler(getBrew('share')), (req, res)=>{ res.status(200).send(brew.text); }); +//Serve brew styling +app.get('/css/:id', asyncHandler(getBrew('share')), (req, res)=>{ + const { brew } = req; + splitTextStyleAndMetadata(brew); + + console.log(brew); + + res.set({ + 'Cache-Control' : 'no-cache', + 'Content-Type' : 'text/css' + }); + res.status(200).send(brew.style); +}); + //User Page app.get('/user/:username', async (req, res, next)=>{ const ownAccount = req.account && (req.account.username == req.params.username); From 0a309ad0e17185d0d2c7b252468702cdab39ddf4 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sun, 1 Oct 2023 21:46:52 +1300 Subject: [PATCH 02/11] Remove debugging console.log --- server/app.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/app.js b/server/app.js index 6b273e0768..85ac459f5b 100644 --- a/server/app.js +++ b/server/app.js @@ -213,8 +213,6 @@ app.get('/css/:id', asyncHandler(getBrew('share')), (req, res)=>{ const { brew } = req; splitTextStyleAndMetadata(brew); - console.log(brew); - res.set({ 'Cache-Control' : 'no-cache', 'Content-Type' : 'text/css' From 7d699e455e97648be0432e97afd216e29817e39c Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 12 Aug 2024 18:48:28 +1200 Subject: [PATCH 03/11] Switch CSS route to call a function --- server/app.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/server/app.js b/server/app.js index d4107b9028..dae24b13ad 100644 --- a/server/app.js +++ b/server/app.js @@ -201,16 +201,20 @@ app.get('/download/:id', asyncHandler(getBrew('share')), (req, res)=>{ }); //Serve brew styling -app.get('/css/:id', asyncHandler(getBrew('share')), (req, res)=>{ +app.get('/css/:id', asyncHandler(getBrew('share')), ((req, res)=>{app.getCSS(req, res);})); + +app.getCSS = (req, res)=>{ const { brew } = req; + if(!brew) return res.status(404).send(); splitTextStyleAndMetadata(brew); + if(!brew.style) return res.status(404).send(); res.set({ 'Cache-Control' : 'no-cache', 'Content-Type' : 'text/css' }); - res.status(200).send(brew.style); -}); + return res.status(200).send(brew.style); +}; //User Page app.get('/user/:username', async (req, res, next)=>{ From fac2293b77dffb7cb4d9798ba0080db5f52ef539 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 12 Aug 2024 18:48:46 +1200 Subject: [PATCH 04/11] Add App test file --- server/app.spec.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 server/app.spec.js diff --git a/server/app.spec.js b/server/app.spec.js new file mode 100644 index 0000000000..4f262a79d8 --- /dev/null +++ b/server/app.spec.js @@ -0,0 +1,47 @@ +/* eslint-disable max-lines */ + +const app = require('./app.js').app; + +const res = { + _status : 0, + status : jest.fn((n)=>{res._status = n; return res;}), + _send : '', + send : jest.fn((data)=>{res._send = data; return res;}), + set : jest.fn(()=>{}), + setHeader : jest.fn(()=>{}), + reset : ()=>{ res._status = 0; res._send = ''; } +}; + +describe('Tests for app', ()=>{ + beforeEach(()=>{ + return res.reset(); + }); + + it('get CSS from a test brew that has a style', async ()=>{ + const testBrew = { title: 'test brew', shareId: 'iAmATestBrew', text: '```css\n\nI Have a style!\n````\n\n' }; + + const req = { brew: testBrew }; + await app.getCSS(req, res); + + expect(res).toHaveProperty('_status', 200); + expect(res).toHaveProperty('_send', '\nI Have a style!\n'); + }); + + it('get CSS from a test brew that has no style', async ()=>{ + const testBrew = { title: 'test brew', shareId: 'iAmATestBrew', text: 'No style data here' }; + + const req = { brew: testBrew }; + await app.getCSS(req, res); + + expect(res).toHaveProperty('_status', 404); + expect(res).toHaveProperty('_send', undefined); + }); + + it('get CSS from no brew', async ()=>{ + const req = { brew: undefined }; + await app.getCSS(req, res); + + expect(res).toHaveProperty('_status', 404); + expect(res).toHaveProperty('_send', undefined); + }); +}); From 3dc8eec1e6863379f8ec287b3a67b9115a5e7f7f Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 12 Aug 2024 18:49:01 +1200 Subject: [PATCH 05/11] Add app test NPM command --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 2dbff24ed8..d7576d1b15 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "test": "jest --runInBand", "test:api-unit": "jest \"server/.*.spec.js\" --verbose", "test:api-unit:themes": "jest \"server/.*.spec.js\" -t \"theme bundle\" --verbose", + "test:app": "jest \"server/app.spec.js\" --verbose", "test:coverage": "jest --coverage --silent --runInBand", "test:dev": "jest --verbose --watch", "test:basic": "jest tests/markdown/basic.test.js --verbose", From 00e113ff67dd3e7eb3f2f8873208aca7a68c3744 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Tue, 13 Aug 2024 22:02:54 +1200 Subject: [PATCH 06/11] Move getCSS to homebrew.api.js --- server/app.js | 17 ++--------------- server/homebrew.api.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/server/app.js b/server/app.js index dae24b13ad..9729f07bac 100644 --- a/server/app.js +++ b/server/app.js @@ -9,7 +9,7 @@ const yaml = require('js-yaml'); const app = express(); const config = require('./config.js'); -const { homebrewApi, getBrew, getUsersBrewThemes } = require('./homebrew.api.js'); +const { homebrewApi, getBrew, getUsersBrewThemes, getCSS } = require('./homebrew.api.js'); const GoogleActions = require('./googleActions.js'); const serveCompressedStaticAssets = require('./static-assets.mv.js'); const sanitizeFilename = require('sanitize-filename'); @@ -201,20 +201,7 @@ app.get('/download/:id', asyncHandler(getBrew('share')), (req, res)=>{ }); //Serve brew styling -app.get('/css/:id', asyncHandler(getBrew('share')), ((req, res)=>{app.getCSS(req, res);})); - -app.getCSS = (req, res)=>{ - const { brew } = req; - if(!brew) return res.status(404).send(); - splitTextStyleAndMetadata(brew); - if(!brew.style) return res.status(404).send(); - - res.set({ - 'Cache-Control' : 'no-cache', - 'Content-Type' : 'text/css' - }); - return res.status(200).send(brew.style); -}; +app.get('/css/:id', asyncHandler(getBrew('share')), (req, res)=>{getCSS(req, res);}); //User Page app.get('/user/:username', async (req, res, next)=>{ diff --git a/server/homebrew.api.js b/server/homebrew.api.js index c314454e2f..52fe57360e 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -148,6 +148,20 @@ const api = { next(); }; }, + + getCSS : async (req, res)=>{ + const { brew } = req; + if(!brew) return res.status(404).send(''); + splitTextStyleAndMetadata(brew); + if(!brew.style) return res.status(404).send(''); + + res.set({ + 'Cache-Control' : 'no-cache', + 'Content-Type' : 'text/css' + }); + return res.status(200).send(brew.style); + }, + mergeBrewText : (brew)=>{ let text = brew.text; if(brew.style !== undefined) { From 38168131e7491f646d4e8fe20b270c1f6555e028 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Tue, 13 Aug 2024 22:03:06 +1200 Subject: [PATCH 07/11] Remove app.spec.js --- server/app.spec.js | 47 ---------------------------------------------- 1 file changed, 47 deletions(-) delete mode 100644 server/app.spec.js diff --git a/server/app.spec.js b/server/app.spec.js deleted file mode 100644 index 4f262a79d8..0000000000 --- a/server/app.spec.js +++ /dev/null @@ -1,47 +0,0 @@ -/* eslint-disable max-lines */ - -const app = require('./app.js').app; - -const res = { - _status : 0, - status : jest.fn((n)=>{res._status = n; return res;}), - _send : '', - send : jest.fn((data)=>{res._send = data; return res;}), - set : jest.fn(()=>{}), - setHeader : jest.fn(()=>{}), - reset : ()=>{ res._status = 0; res._send = ''; } -}; - -describe('Tests for app', ()=>{ - beforeEach(()=>{ - return res.reset(); - }); - - it('get CSS from a test brew that has a style', async ()=>{ - const testBrew = { title: 'test brew', shareId: 'iAmATestBrew', text: '```css\n\nI Have a style!\n````\n\n' }; - - const req = { brew: testBrew }; - await app.getCSS(req, res); - - expect(res).toHaveProperty('_status', 200); - expect(res).toHaveProperty('_send', '\nI Have a style!\n'); - }); - - it('get CSS from a test brew that has no style', async ()=>{ - const testBrew = { title: 'test brew', shareId: 'iAmATestBrew', text: 'No style data here' }; - - const req = { brew: testBrew }; - await app.getCSS(req, res); - - expect(res).toHaveProperty('_status', 404); - expect(res).toHaveProperty('_send', undefined); - }); - - it('get CSS from no brew', async ()=>{ - const req = { brew: undefined }; - await app.getCSS(req, res); - - expect(res).toHaveProperty('_status', 404); - expect(res).toHaveProperty('_send', undefined); - }); -}); From 5847b246efe82288da2b1140526ba4546fe1892b Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Tue, 13 Aug 2024 22:05:03 +1200 Subject: [PATCH 08/11] Add getCSS test --- server/homebrew.api.spec.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/server/homebrew.api.spec.js b/server/homebrew.api.spec.js index 5f1739b972..287e68ce4b 100644 --- a/server/homebrew.api.spec.js +++ b/server/homebrew.api.spec.js @@ -916,4 +916,31 @@ brew`); expect(saved.googleId).toEqual(brew.googleId); }); }); + describe('Get CSS tests', ()=>{ + it('should return CSS for a brew', async ()=>{ + const testBrew = { title: 'test brew', text: '```css\n\nI Have a style!\n````\n\n' }; + + const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); + api.getId = jest.fn(()=>({ id: '1', googleId: undefined })); + model.get = jest.fn(()=>toBrewPromise(testBrew)); + + const fn = api.getBrew('share', true); + const req = { brew: {} }; + const res = { + status : jest.fn(()=>res), + send : jest.fn(()=>{}), + set : jest.fn(()=>{}), + setHeader : jest.fn(()=>{}) + }; + const next = jest.fn(); + await fn(req, null, next); + await api.getCSS(req, res); + + expect(req.brew).toEqual(testBrew); + expect(req.brew).toHaveProperty('style', '\nI Have a style!\n'); + expect(next).toHaveBeenCalled(); + expect(api.getId).toHaveBeenCalledWith(req); + expect(model.get).toHaveBeenCalledWith({ shareId: '1' }); + }); + }); }); From ae336f14294a7323f888ccdd477495a12b8bae68 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Tue, 13 Aug 2024 22:30:59 +1200 Subject: [PATCH 09/11] Add extra getCSS tests --- server/homebrew.api.spec.js | 54 ++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/server/homebrew.api.spec.js b/server/homebrew.api.spec.js index 287e68ce4b..22c53a4093 100644 --- a/server/homebrew.api.spec.js +++ b/server/homebrew.api.spec.js @@ -50,6 +50,7 @@ describe('Tests for api', ()=>{ res = { status : jest.fn(()=>res), send : jest.fn(()=>{}), + set : jest.fn(()=>{}), setHeader : jest.fn(()=>{}) }; @@ -917,7 +918,7 @@ brew`); }); }); describe('Get CSS tests', ()=>{ - it('should return CSS for a brew', async ()=>{ + it('get CSS - successful', async ()=>{ const testBrew = { title: 'test brew', text: '```css\n\nI Have a style!\n````\n\n' }; const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); @@ -926,21 +927,54 @@ brew`); const fn = api.getBrew('share', true); const req = { brew: {} }; - const res = { - status : jest.fn(()=>res), - send : jest.fn(()=>{}), - set : jest.fn(()=>{}), - setHeader : jest.fn(()=>{}) - }; const next = jest.fn(); await fn(req, null, next); await api.getCSS(req, res); expect(req.brew).toEqual(testBrew); expect(req.brew).toHaveProperty('style', '\nI Have a style!\n'); - expect(next).toHaveBeenCalled(); - expect(api.getId).toHaveBeenCalledWith(req); - expect(model.get).toHaveBeenCalledWith({ shareId: '1' }); + expect(res.set).toHaveBeenCalledWith({ + 'Cache-Control' : 'no-cache', + 'Content-Type' : 'text/css' + }); + }); + + it('get CSS - no style in brew', async ()=>{ + const testBrew = { title: 'test brew', text: 'I don\'t have a style!' }; + + const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); + api.getId = jest.fn(()=>({ id: '1', googleId: undefined })); + model.get = jest.fn(()=>toBrewPromise(testBrew)); + + const fn = api.getBrew('share', true); + const req = { brew: {} }; + const next = jest.fn(); + await fn(req, null, next); + await api.getCSS(req, res); + + expect(req.brew).toEqual(testBrew); + expect(req.brew).toHaveProperty('style'); + expect(res.status).toHaveBeenCalledWith(404); + expect(res.send).toHaveBeenCalledWith(''); + }); + + it('get CSS - no brew', async ()=>{ + const testBrew = { }; + + const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); + api.getId = jest.fn(()=>({ id: '1', googleId: undefined })); + model.get = jest.fn(()=>toBrewPromise(testBrew)); + + const fn = api.getBrew('share', true); + const req = { brew: {} }; + const next = jest.fn(); + await fn(req, null, next); + await api.getCSS(req, res); + + expect(req.brew).toEqual(testBrew); + expect(req.brew).toHaveProperty('style'); + expect(res.status).toHaveBeenCalledWith(404); + expect(res.send).toHaveBeenCalledWith(''); }); }); }); From 014482b3d571ed000ffc88b9fe72c00e5efc8a70 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 17 Aug 2024 10:26:59 +1200 Subject: [PATCH 10/11] Remove app.spec.js test command --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index d7576d1b15..2dbff24ed8 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "test": "jest --runInBand", "test:api-unit": "jest \"server/.*.spec.js\" --verbose", "test:api-unit:themes": "jest \"server/.*.spec.js\" -t \"theme bundle\" --verbose", - "test:app": "jest \"server/app.spec.js\" --verbose", "test:coverage": "jest --coverage --silent --runInBand", "test:dev": "jest --verbose --watch", "test:basic": "jest tests/markdown/basic.test.js --verbose", From 3d3ad3f284b7b337e75584f9278724c4414bbb95 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Mon, 26 Aug 2024 19:58:28 -0400 Subject: [PATCH 11/11] Small tweaks --- package.json | 1 + server/homebrew.api.spec.js | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 8226325cd8..f8f4ceb3ce 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "test": "jest --runInBand", "test:api-unit": "jest \"server/.*.spec.js\" --verbose", "test:api-unit:themes": "jest \"server/.*.spec.js\" -t \"theme bundle\" --verbose", + "test:api-unit:css": "jest \"server/.*.spec.js\" -t \"Get CSS\" --verbose", "test:coverage": "jest --coverage --silent --runInBand", "test:dev": "jest --verbose --watch", "test:basic": "jest tests/markdown/basic.test.js --verbose", diff --git a/server/homebrew.api.spec.js b/server/homebrew.api.spec.js index 22c53a4093..d168c73fb8 100644 --- a/server/homebrew.api.spec.js +++ b/server/homebrew.api.spec.js @@ -917,8 +917,8 @@ brew`); expect(saved.googleId).toEqual(brew.googleId); }); }); - describe('Get CSS tests', ()=>{ - it('get CSS - successful', async ()=>{ + describe('Get CSS', ()=>{ + it('should return brew style content as CSS text', async ()=>{ const testBrew = { title: 'test brew', text: '```css\n\nI Have a style!\n````\n\n' }; const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); @@ -933,13 +933,15 @@ brew`); expect(req.brew).toEqual(testBrew); expect(req.brew).toHaveProperty('style', '\nI Have a style!\n'); + expect(res.status).toHaveBeenCalledWith(200); + expect(res.send).toHaveBeenCalledWith("\nI Have a style!\n"); expect(res.set).toHaveBeenCalledWith({ 'Cache-Control' : 'no-cache', 'Content-Type' : 'text/css' }); }); - it('get CSS - no style in brew', async ()=>{ + it('should return 404 when brew has no style content', async ()=>{ const testBrew = { title: 'test brew', text: 'I don\'t have a style!' }; const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); @@ -958,7 +960,7 @@ brew`); expect(res.send).toHaveBeenCalledWith(''); }); - it('get CSS - no brew', async ()=>{ + it('should return 404 when brew does not exist', async ()=>{ const testBrew = { }; const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew }));