simplify dev dependencies, extend test
This commit is contained in:
parent
ca2674e502
commit
898942c436
@ -3,13 +3,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const express = require ('express');
|
const express = require ('express');
|
||||||
const cookie_parser = require ('cookie-parser');
|
|
||||||
const auth = require ('./index');
|
const auth = require ('./index');
|
||||||
const knex = require ('knex');
|
|
||||||
const path = require ('path');
|
|
||||||
const consts = require ('@scode/consts');
|
const consts = require ('@scode/consts');
|
||||||
const crypto = require ('@scode/crypto-helper');
|
const crypto = require ('@scode/crypto-helper');
|
||||||
const fs = require ('fs-extra');
|
|
||||||
const password_helper = require ('@scode/password-helper');
|
const password_helper = require ('@scode/password-helper');
|
||||||
|
|
||||||
|
|
||||||
@ -17,24 +13,6 @@ const password_helper = require ('@scode/password-helper');
|
|||||||
* start the server
|
* start the server
|
||||||
*/
|
*/
|
||||||
async function start_server () {
|
async function start_server () {
|
||||||
const db_path = path.join (__dirname, 'db.sqlite');
|
|
||||||
|
|
||||||
if (await fs.exists (db_path))
|
|
||||||
await fs.unlink (db_path);
|
|
||||||
|
|
||||||
const db = knex ({
|
|
||||||
client: 'sqlite',
|
|
||||||
connection: { filename: db_path },
|
|
||||||
useNullAsDefault: true
|
|
||||||
});
|
|
||||||
|
|
||||||
await db.schema.createTable ('users', (table) => {
|
|
||||||
table.increments ('id');
|
|
||||||
table.string ('name');
|
|
||||||
table.string ('password');
|
|
||||||
table.string ('salt');
|
|
||||||
});
|
|
||||||
|
|
||||||
const app = express ();
|
const app = express ();
|
||||||
|
|
||||||
const name = 'testuser';
|
const name = 'testuser';
|
||||||
@ -42,23 +20,17 @@ async function start_server () {
|
|||||||
const password = await password_helper.hash (
|
const password = await password_helper.hash (
|
||||||
crypto.hash_sha512 ('foo', salt)
|
crypto.hash_sha512 ('foo', salt)
|
||||||
);
|
);
|
||||||
|
const user = { name, salt, password };
|
||||||
|
|
||||||
await db ('users')
|
app.use (auth ((user_name) => {
|
||||||
.insert ({ name, password, salt });
|
if (user.name === user_name)
|
||||||
|
return user;
|
||||||
app.use (cookie_parser ());
|
|
||||||
app.use (auth (async (user) => {
|
|
||||||
const users = await db ('users')
|
|
||||||
.select ('id', 'password', 'salt')
|
|
||||||
.where ({ name: user });
|
|
||||||
if (users.length === 1)
|
|
||||||
return users[0];
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
app.get ('/', (req, res) => {
|
app.get ('/', (req, res) => {
|
||||||
res.status (consts.http_consts.status_ok)
|
res.status (consts.http.status_ok)
|
||||||
.end ('foo');
|
.end ('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -8,15 +8,12 @@
|
|||||||
"@scode/auth-client-helper": "^1.0.6",
|
"@scode/auth-client-helper": "^1.0.6",
|
||||||
"@scode/crypto-helper": "^1.1.9",
|
"@scode/crypto-helper": "^1.1.9",
|
||||||
"@scode/eslint-config": "^1.2.26",
|
"@scode/eslint-config": "^1.2.26",
|
||||||
|
"@scode/password-helper": "^1.0.3",
|
||||||
"ava": "^3.5.0",
|
"ava": "^3.5.0",
|
||||||
"cookie-parser": "^1.4.4",
|
|
||||||
"eslint": "^6.8.0",
|
"eslint": "^6.8.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"fs-extra": "^8.1.0",
|
"node-fetch": "^2.6.0",
|
||||||
"knex": "^0.20.11",
|
"nyc": "^15.0.0"
|
||||||
"nyc": "^15.0.0",
|
|
||||||
"sqlite3": "^4.1.1",
|
|
||||||
"@scode/password-helper": "^1.0.3"
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
const test = require ('ava');
|
const test = require ('ava');
|
||||||
const mock_server = require ('../mock_server');
|
const mock_server = require ('../mock_server');
|
||||||
const client = require ('@scode/auth-client-helper');
|
const client = require ('@scode/auth-client-helper');
|
||||||
|
const consts = require ('@scode/consts');
|
||||||
|
const fetch = require ('node-fetch');
|
||||||
|
|
||||||
test.before (async () => {
|
test.before (async () => {
|
||||||
await mock_server.start_server ();
|
await mock_server.start_server ();
|
||||||
@ -24,6 +26,11 @@ test ('login', async (t) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
t.is (typeof session, 'string');
|
t.is (typeof session, 'string');
|
||||||
|
|
||||||
|
const resp = await fetch ('http://localhost:3000', { headers: { session } });
|
||||||
|
|
||||||
|
t.is (await resp.status, consts.http.status_ok);
|
||||||
|
t.is (await resp.text (), 'foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
test ('reject invalid user', async (t) => {
|
test ('reject invalid user', async (t) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user