diff --git a/mock_server.js b/mock_server.js index f848042..d83e77a 100644 --- a/mock_server.js +++ b/mock_server.js @@ -45,9 +45,11 @@ async function start_server () { .end (`foo:${res.connection.user_id}`); }); - app.listen (3000); - - return app; + return new Promise ((res) => { + const listener = app.listen (0, () => { + res (listener.address ().port); + }); + }); } module.exports = { start_server }; diff --git a/package.json b/package.json index 7f9d0b5..9f45e13 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "@sapphirecode/eslint-config": "^2.1.4", "@stryker-mutator/core": "^3.2.3", "@stryker-mutator/javascript-mutator": "^3.2.3", - "ava": "^3.8.2", + "ava": "^3.10.1", "eslint": "^7.0.0", "express": "^4.17.1", "node-fetch": "^2.6.0", @@ -28,11 +28,11 @@ }, "dependencies": { "@sapphirecode/consts": "^1.1.18", - "@sapphirecode/password-helper": "^1.0.35", - "@sapphirecode/crypto-helper": "^1.1.44" + "@sapphirecode/crypto-helper": "^1.1.44", + "@sapphirecode/password-helper": "^1.0.35" }, "files": [ "LICENSE", "index.js" ] -} \ No newline at end of file +} diff --git a/test/index.js b/test/index.js index 5e1dd75..ae8df09 100644 --- a/test/index.js +++ b/test/index.js @@ -14,27 +14,35 @@ const client = require ('@sapphirecode/auth-client-helper'); const consts = require ('@sapphirecode/consts'); const fetch = require ('node-fetch'); +let port = 0; + test.before (async () => { - await mock_server.start_server (); + port = await mock_server.start_server (); }); -test ('login', async (t) => { +test.only ('login', async (t) => { + console.log ('logging in'); + console.log ('port:', port); const session = await client.login ( 'testuser', 'foo', - 'http://localhost:3000' + `http://localhost:${port}` ); - + console.log ('server respond'); t.is (typeof session, 'string'); - const resp = await fetch ('http://localhost:3000', { headers: { session } }); + const resp = await fetch ( + `http://localhost:${port}`, + { headers: { session } } + ); t.is (resp.status, consts.http.status_ok); t.is (await resp.text (), 'foo:69'); + console.log ('done test'); }); test ('allow access to excluded paths', async (t) => { - const resp = await fetch ('http://localhost:3000/noauthreg'); + const resp = await fetch (`http://localhost:${port}/noauthreg`); t.is (resp.status, consts.http.status_ok); t.is (await resp.text (), 'foo:undefined'); @@ -42,7 +50,7 @@ test ('allow access to excluded paths', async (t) => { test ('allow access to excluded paths with correct method', async (t) => { const resp = await fetch ( - 'http://localhost:3000/noauthobj', + `http://localhost:${port}/noauthobj`, { method: 'POST' } ); @@ -52,7 +60,7 @@ test ('allow access to excluded paths with correct method', async (t) => { test ('reject access to excluded paths with wrong method', async (t) => { const resp = await fetch ( - 'http://localhost:3000/noauthobj' + `http://localhost:${port}/noauthobj` ); t.is (resp.status, consts.http.status_unauthorized); @@ -62,7 +70,7 @@ test ('reject invalid user', async (t) => { await t.throwsAsync (client.login ( 'foo', 'foo', - 'http://localhost:3000' + `http://localhost:${port}` )); }); @@ -70,6 +78,6 @@ test ('reject invalid password', async (t) => { await t.throwsAsync (client.login ( 'testuser', 'bar', - 'http://localhost:3000' + `http://localhost:${port}` )); }); diff --git a/yarn.lock b/yarn.lock index eff5e8f..d8bd5a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -592,7 +592,7 @@ astral-regex@^2.0.0: resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31" integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ== -ava@^3.8.2: +ava@^3.10.1: version "3.10.1" resolved "https://registry.yarnpkg.com/ava/-/ava-3.10.1.tgz#a4e68b1a2bb248fa0d96529d23dd83f57082e944" integrity sha512-+w86ZHyFHIGCABi7NUrn/WJMyC+fDj0BSIlFNVS45WDKAD5vxbIiDWeclctxOOc2KDPfQD7tFOURSBz0FBLD0A==