From 0d782e91c5eb50b354291607df7a5a57b0ca0279 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Fri, 13 Mar 2020 20:08:38 +0100 Subject: [PATCH] add tests for specialized exceptions --- mock_server.js | 7 +++++-- test/index.js | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/mock_server.js b/mock_server.js index 928e695..906a3a4 100644 --- a/mock_server.js +++ b/mock_server.js @@ -27,9 +27,12 @@ async function start_server () { return user; return null; - })); + }, [ + /noauthreg/u, + { method: 'POST', regex: /noauthobj/u } + ])); - app.get ('/', (req, res) => { + app.use ((req, res) => { res.status (consts.http.status_ok) .end ('foo'); }); diff --git a/test/index.js b/test/index.js index 40f91f3..d0850d2 100644 --- a/test/index.js +++ b/test/index.js @@ -29,10 +29,35 @@ test ('login', async (t) => { const resp = await fetch ('http://localhost:3000', { headers: { session } }); - t.is (await resp.status, consts.http.status_ok); + t.is (resp.status, consts.http.status_ok); t.is (await resp.text (), 'foo'); }); +test ('allow access to excluded paths', async (t) => { + const resp = await fetch ('http://localhost:3000/noauthreg'); + + t.is (resp.status, consts.http.status_ok); + t.is (await resp.text (), 'foo'); +}); + +test ('allow access to excluded paths with correct method', async (t) => { + const resp = await fetch ( + 'http://localhost:3000/noauthobj', + { method: 'POST' } + ); + + t.is (resp.status, consts.http.status_ok); + t.is (await resp.text (), 'foo'); +}); + +test ('reject access to excluded paths with wrong method', async (t) => { + const resp = await fetch ( + 'http://localhost:3000/noauthobj' + ); + + t.is (resp.status, consts.http.status_unauthorized); +}); + test ('reject invalid user', async (t) => { await t.throwsAsync (client.login ( 'foo',