simplify dev dependencies, extend test

This commit is contained in:
Timo Hocker 2020-03-08 14:03:18 +01:00
parent ca2674e502
commit 898942c436
4 changed files with 40 additions and 1224 deletions

View File

@ -3,13 +3,9 @@
'use strict';
const express = require ('express');
const cookie_parser = require ('cookie-parser');
const auth = require ('./index');
const knex = require ('knex');
const path = require ('path');
const consts = require ('@scode/consts');
const crypto = require ('@scode/crypto-helper');
const fs = require ('fs-extra');
const password_helper = require ('@scode/password-helper');
@ -17,24 +13,6 @@ const password_helper = require ('@scode/password-helper');
* start the 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 name = 'testuser';
@ -42,23 +20,17 @@ async function start_server () {
const password = await password_helper.hash (
crypto.hash_sha512 ('foo', salt)
);
const user = { name, salt, password };
await db ('users')
.insert ({ name, password, salt });
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];
app.use (auth ((user_name) => {
if (user.name === user_name)
return user;
return null;
}));
app.get ('/', (req, res) => {
res.status (consts.http_consts.status_ok)
res.status (consts.http.status_ok)
.end ('foo');
});

View File

@ -8,15 +8,12 @@
"@scode/auth-client-helper": "^1.0.6",
"@scode/crypto-helper": "^1.1.9",
"@scode/eslint-config": "^1.2.26",
"@scode/password-helper": "^1.0.3",
"ava": "^3.5.0",
"cookie-parser": "^1.4.4",
"eslint": "^6.8.0",
"express": "^4.17.1",
"fs-extra": "^8.1.0",
"knex": "^0.20.11",
"nyc": "^15.0.0",
"sqlite3": "^4.1.1",
"@scode/password-helper": "^1.0.3"
"node-fetch": "^2.6.0",
"nyc": "^15.0.0"
},
"scripts": {
"lint": "eslint .",

View File

@ -11,6 +11,8 @@
const test = require ('ava');
const mock_server = require ('../mock_server');
const client = require ('@scode/auth-client-helper');
const consts = require ('@scode/consts');
const fetch = require ('node-fetch');
test.before (async () => {
await mock_server.start_server ();
@ -24,6 +26,11 @@ test ('login', async (t) => {
);
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) => {

1210
yarn.lock

File diff suppressed because it is too large Load Diff