From 8baf0fc8490c788b678a4ae9e8b7908d0f336864 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Mon, 26 Jun 2023 23:26:59 -0400 Subject: [PATCH] Add additional test for when logged in, but not in author list --- server/homebrew.api.js | 5 ++--- server/homebrew.api.spec.js | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index a986c0019..60c86b6d4 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -61,9 +61,8 @@ const api = { const reason = googleError.errors?.[0].reason; if(reason == 'notFound') { throw { ...googleError, HBErrorCode: '02', authors: stub?.authors, account: req.account?.username }; - } - else { - throw { ...googleError, HBErrorCode: '01'}; + } else { + throw { ...googleError, HBErrorCode: '01' }; } } // Combine the Homebrewery stub with the google brew, or if the stub doesn't exist just use the google brew diff --git a/server/homebrew.api.spec.js b/server/homebrew.api.spec.js index 6afe1b53c..0adbcda4f 100644 --- a/server/homebrew.api.spec.js +++ b/server/homebrew.api.spec.js @@ -183,7 +183,7 @@ describe('Tests for api', ()=>{ expect(next).toHaveBeenCalled(); }); - it('throws if invalid author', async ()=>{ + it('throws if not logged in as author', async ()=>{ api.getId = jest.fn(()=>({ id: '1', googleId: undefined })); model.get = jest.fn(()=>toBrewPromise({ title: 'test brew', authors: ['a'] })); @@ -197,7 +197,24 @@ describe('Tests for api', ()=>{ err = e; } - expect(err).toEqual({ HBErrorCode: '04', message: 'User is not logged in', name: 'Access Error', status: 401, authors: [ 'a' ], brewTitle: 'test brew' }); + expect(err).toEqual({ HBErrorCode: '04', message: 'User is not logged in', name: 'Access Error', status: 401, brewTitle: 'test brew', authors: ['a'] }); + }); + + it('throws if logged in as invalid author', async ()=>{ + api.getId = jest.fn(()=>({ id: '1', googleId: undefined })); + model.get = jest.fn(()=>toBrewPromise({ title: 'test brew', authors: ['a'] })); + + const fn = api.getBrew('edit', true); + const req = { brew: {}, account: { username: 'b' } }; + + let err; + try { + await fn(req, null, null); + } catch (e) { + err = e; + } + + expect(err).toEqual({ HBErrorCode: '03', message: 'User is not an Author', name: 'Access Error', status: 401, brewTitle: 'test brew', authors: ['a'] }); }); it('does not throw if no authors', async ()=>{