From 35c65cf746d4144f5085b72220b3ff1b524ad03c Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Sun, 23 Jan 2022 23:22:01 +0300 Subject: [PATCH 1/6] Add tests for source/ route --- tests/routes/api/source.test.js | 92 +++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/routes/api/source.test.js diff --git a/tests/routes/api/source.test.js b/tests/routes/api/source.test.js new file mode 100644 index 0000000000..cdd645d029 --- /dev/null +++ b/tests/routes/api/source.test.js @@ -0,0 +1,92 @@ +const supertest = require('supertest'); + +// Mimic https responses to avoid being redirected all the time +const app = supertest.agent(require('app.js').app) + .set('X-Forwarded-Proto', 'https'); + +const DB = require('db.js'); +const Homebrew = require('homebrew.model.js'); + +// FIXME: outline config creation into a separate module +const config = require('nconf') + .argv() + .env({ lowerCase: true }) + .file('environment', { file: `config/${process.env.NODE_ENV}.json` }) + .file('defaults', { file: 'config/default.json' }); + +describe('/source/:id', ()=>{ + beforeAll(()=>{ + return DB.connect(config); + }); + + beforeAll(async ()=>{ + const regular_brew = new Homebrew.model({ + shareId : 'share-id-1', + text : 'This is text', + authors : ['this', 'is', 'list', 'of', 'authors'] + }); + await regular_brew.save(); + + const brew_with_special_symbols = new Homebrew.model({ + shareId : 'share-id-2', + text : '
&
' + }); + await brew_with_special_symbols.save(); + }); + + it('able to return a source of an existing brew', ()=>{ + return app.get('/source/share-id-1') + .send() + .expect(200) + .then((response)=>{ + expect(response.text).toBe('
This is text
'); + expect(response.headers).toHaveProperty('content-type', 'text/html; charset=utf-8'); + }); + }); + + it('encodes special symbols', ()=>{ + return app.get('/source/share-id-2') + .send() + .expect(200) + .then((response)=>{ + expect(response.text).toBe('
<div>&</div>
'); + expect(response.headers).toHaveProperty('content-type', 'text/html; charset=utf-8'); + }); + }); + + it('returns an error for a non-existing brew', ()=>{ + return app.get('/source/invalid-id') + .send() + // FIXME: we should be expecting 404 Not Found (#1983) + .expect(500); + }); + + it('returns an error for a non-existing brew (google id)', ()=>{ + return app.get('/source/non-existing-brew-id') + .send() + // FIXME: we should be expecting 404 Not Found (#1983) + .expect(500); + }); + + // FIXME: we should return an error instead of a home page here + it.skip('returns an error for a missing brew id', ()=>{ + return app.get('/source/') + .send() + .expect(404); + }); + + // FIXME: we should return an error instead of a home page here + it.skip('returns an error for a missing brew id #2', ()=>{ + return app.get('/source') + .send() + .expect(404); + }); + + afterAll(()=>{ + return Homebrew.model.deleteMany(); + }); + + afterAll(()=>{ + return DB.disconnect(); + }); +}); From e0d28d2c0be9e45a40bdcede02252c1d0f4003c3 Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Thu, 27 Jan 2022 21:22:15 +0300 Subject: [PATCH 2/6] Refactor tests, mock GoogleActions module --- server/__mocks__/googleActions.js | 40 ++++++++++++++++++ tests/routes/api/source.test.js | 67 ++++++++++++++++++------------- 2 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 server/__mocks__/googleActions.js diff --git a/server/__mocks__/googleActions.js b/server/__mocks__/googleActions.js new file mode 100644 index 0000000000..234ad3bc7b --- /dev/null +++ b/server/__mocks__/googleActions.js @@ -0,0 +1,40 @@ +const GoogleActionsMock = { + authCheck : (account, res)=>{ + // FIXME: implement + return null; + }, + getGoogleFolder : async (auth)=>{ + // FIXME: implement + return null; + }, + listGoogleBrews : async (req, res)=>{ + // FIXME: implement + return null; + }, + existsGoogleBrew : async (auth, id)=>{ + // FIXME: implement + return null; + }, + updateGoogleBrew : async (auth, brew)=>{ + // FIXME: implement + return null; + }, + newGoogleBrew : async (auth, brew)=>{ + // FIXME: implement + return null; + }, + readFileMetadata : async (auth, id, accessId, accessType)=>{ + // FIXME: implement + return null; + }, + deleteGoolgeBrew : async (req, res, id)=>{ + // FIXME: implement + return null; + }, + increaseView : async (id, accessId, accessType, brew)=>{ + // FIXME: implement + return null; + } +}; + +module.exports = GoogleActionsMock; diff --git a/tests/routes/api/source.test.js b/tests/routes/api/source.test.js index cdd645d029..64b0a596ed 100644 --- a/tests/routes/api/source.test.js +++ b/tests/routes/api/source.test.js @@ -1,5 +1,9 @@ const supertest = require('supertest'); +// We need to mock google actions to avoid accessing real servers in our tests +// (there are limits on unauthorized access) +jest.mock('googleActions.js'); + // Mimic https responses to avoid being redirected all the time const app = supertest.agent(require('app.js').app) .set('X-Forwarded-Proto', 'https'); @@ -20,18 +24,18 @@ describe('/source/:id', ()=>{ }); beforeAll(async ()=>{ - const regular_brew = new Homebrew.model({ + const regularBrew = new Homebrew.model({ shareId : 'share-id-1', text : 'This is text', authors : ['this', 'is', 'list', 'of', 'authors'] }); - await regular_brew.save(); - - const brew_with_special_symbols = new Homebrew.model({ - shareId : 'share-id-2', + await regularBrew.save(); + + const brewWithSpecialSymbol = new Homebrew.model({ + shareId : 'share-id-2', text : '
&
' - }); - await brew_with_special_symbols.save(); + }); + await brewWithSpecialSymbol.save(); }); it('able to return a source of an existing brew', ()=>{ @@ -39,48 +43,55 @@ describe('/source/:id', ()=>{ .send() .expect(200) .then((response)=>{ - expect(response.text).toBe('
This is text
'); - expect(response.headers).toHaveProperty('content-type', 'text/html; charset=utf-8'); + expect(response.text).toBe('
This is text
'); }); }); - it('encodes special symbols', ()=>{ - return app.get('/source/share-id-2') + it('encodes special symbols', ()=>{ + return app.get('/source/share-id-2') .send() .expect(200) .then((response)=>{ - expect(response.text).toBe('
<div>&</div>
'); - expect(response.headers).toHaveProperty('content-type', 'text/html; charset=utf-8'); + expect(response.text).toBe('
<div>&</div>
'); }); - }); + }); - it('returns an error for a non-existing brew', ()=>{ - return app.get('/source/invalid-id') + it('sets the correct response headers', ()=>{ + return app.get('/source/share-id-1') + .send() + .expect(200) + .then((response)=>{ + expect(response.headers).toHaveProperty('content-type', 'text/html; charset=utf-8'); + }); + }); + + it('returns an error for a non-existing brew', ()=>{ + return app.get('/source/invalid-id') .send() // FIXME: we should be expecting 404 Not Found (#1983) .expect(500); - }); + }); - it('returns an error for a non-existing brew (google id)', ()=>{ - return app.get('/source/non-existing-brew-id') + it('returns an error for a non-existing brew (google id)', ()=>{ + return app.get('/source/non-existing-brew-id') .send() // FIXME: we should be expecting 404 Not Found (#1983) .expect(500); - }); + }); - // FIXME: we should return an error instead of a home page here - it.skip('returns an error for a missing brew id', ()=>{ - return app.get('/source/') + // FIXME: we should return an error instead of a home page here + it.skip('returns an error for a missing brew id', ()=>{ + return app.get('/source/') .send() .expect(404); - }); + }); - // FIXME: we should return an error instead of a home page here - it.skip('returns an error for a missing brew id #2', ()=>{ - return app.get('/source') + // FIXME: we should return an error instead of a home page here + it.skip('returns an error for a missing brew id #2', ()=>{ + return app.get('/source') .send() .expect(404); - }); + }); afterAll(()=>{ return Homebrew.model.deleteMany(); From 580ab4ff1b5b4e8b9a64b5315c608d4e3baaec1e Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Sun, 20 Feb 2022 14:01:05 +0300 Subject: [PATCH 3/6] Rename test file --- tests/routes/{api => }/source.test.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/routes/{api => }/source.test.js (100%) diff --git a/tests/routes/api/source.test.js b/tests/routes/source.test.js similarity index 100% rename from tests/routes/api/source.test.js rename to tests/routes/source.test.js From 64190e29f83471160b33ae7cfd8c627ad6df2255 Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Sun, 20 Feb 2022 14:06:11 +0300 Subject: [PATCH 4/6] Updates after rebase + lint --- server/__mocks__/googleActions.js | 72 +++++++++++++++---------------- tests/routes/source.test.js | 9 ++-- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/server/__mocks__/googleActions.js b/server/__mocks__/googleActions.js index 234ad3bc7b..0937fa3d73 100644 --- a/server/__mocks__/googleActions.js +++ b/server/__mocks__/googleActions.js @@ -1,40 +1,40 @@ const GoogleActionsMock = { - authCheck : (account, res)=>{ - // FIXME: implement - return null; - }, - getGoogleFolder : async (auth)=>{ - // FIXME: implement - return null; - }, - listGoogleBrews : async (req, res)=>{ - // FIXME: implement - return null; - }, - existsGoogleBrew : async (auth, id)=>{ - // FIXME: implement - return null; - }, - updateGoogleBrew : async (auth, brew)=>{ - // FIXME: implement - return null; - }, - newGoogleBrew : async (auth, brew)=>{ - // FIXME: implement - return null; - }, - readFileMetadata : async (auth, id, accessId, accessType)=>{ - // FIXME: implement - return null; - }, - deleteGoolgeBrew : async (req, res, id)=>{ - // FIXME: implement - return null; - }, - increaseView : async (id, accessId, accessType, brew)=>{ - // FIXME: implement - return null; - } + authCheck : (account, res)=>{ + // FIXME: implement + return null; + }, + getGoogleFolder : async (auth)=>{ + // FIXME: implement + return null; + }, + listGoogleBrews : async (req, res)=>{ + // FIXME: implement + return null; + }, + existsGoogleBrew : async (auth, id)=>{ + // FIXME: implement + return null; + }, + updateGoogleBrew : async (auth, brew)=>{ + // FIXME: implement + return null; + }, + newGoogleBrew : async (auth, brew)=>{ + // FIXME: implement + return null; + }, + readFileMetadata : async (auth, id, accessId, accessType)=>{ + // FIXME: implement + return null; + }, + deleteGoolgeBrew : async (req, res, id)=>{ + // FIXME: implement + return null; + }, + increaseView : async (id, accessId, accessType, brew)=>{ + // FIXME: implement + return null; + } }; module.exports = GoogleActionsMock; diff --git a/tests/routes/source.test.js b/tests/routes/source.test.js index 64b0a596ed..1c23815c2e 100644 --- a/tests/routes/source.test.js +++ b/tests/routes/source.test.js @@ -11,12 +11,7 @@ const app = supertest.agent(require('app.js').app) const DB = require('db.js'); const Homebrew = require('homebrew.model.js'); -// FIXME: outline config creation into a separate module -const config = require('nconf') - .argv() - .env({ lowerCase: true }) - .file('environment', { file: `config/${process.env.NODE_ENV}.json` }) - .file('defaults', { file: 'config/default.json' }); +const config = require('config.js'); describe('/source/:id', ()=>{ beforeAll(()=>{ @@ -93,6 +88,8 @@ describe('/source/:id', ()=>{ .expect(404); }); + // FIXME: add tests for retrieving a Google brew source + afterAll(()=>{ return Homebrew.model.deleteMany(); }); From aa668db8b4a3af25df38f45bf0d7aa0a18f5b589 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sat, 2 Apr 2022 13:57:09 -0400 Subject: [PATCH 5/6] Update mocked Google Action names --- server/__mocks__/googleActions.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/server/__mocks__/googleActions.js b/server/__mocks__/googleActions.js index 0937fa3d73..f92684c4b1 100644 --- a/server/__mocks__/googleActions.js +++ b/server/__mocks__/googleActions.js @@ -7,15 +7,11 @@ const GoogleActionsMock = { // FIXME: implement return null; }, - listGoogleBrews : async (req, res)=>{ + listGoogleBrews : async (auth)=>{ // FIXME: implement return null; }, - existsGoogleBrew : async (auth, id)=>{ - // FIXME: implement - return null; - }, - updateGoogleBrew : async (auth, brew)=>{ + updateGoogleBrew : async (auth)=>{ // FIXME: implement return null; }, @@ -23,11 +19,11 @@ const GoogleActionsMock = { // FIXME: implement return null; }, - readFileMetadata : async (auth, id, accessId, accessType)=>{ + getGoogleBrew : async (id, accessId, accessType)=>{ // FIXME: implement return null; }, - deleteGoolgeBrew : async (req, res, id)=>{ + deleteGoogleBrew : async (auth, id)=>{ // FIXME: implement return null; }, From 6a2581e8d306feb67f51223255fe6bdafecd5da9 Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Tue, 27 Sep 2022 21:56:54 +0200 Subject: [PATCH 6/6] Add config for "test" environment --- config/test.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 config/test.json diff --git a/config/test.json b/config/test.json new file mode 100644 index 0000000000..df07de5b7a --- /dev/null +++ b/config/test.json @@ -0,0 +1,3 @@ +{ + "mongodb_uri": "mongodb://127.0.0.1/test-homebrewery" +} \ No newline at end of file