import knex from 'knex'; import test from 'ava'; import express, { Request, Response } from 'express'; import { http } from '@scode/consts'; import { KnexCrudHandler } from '../lib/DatabaseCrudHandler'; const db = knex ({ client: 'sqlite', connection: { filename: './db.sqlite' } }); /** * general auth * * @param {any} req request * @param {any} res response * @returns {Promise} successful response */ function general_auth (req: Request, res: Response): Promise { return new Promise ((resolve) => { if (req.headers.auth === 'on') { resolve (true); } else { res.status (http.status_forbidden); res.end ('auth failed'); resolve (false); } }); } /** * read auth * * @param {any} req request * @param {any} res response * @returns {Promise} successful response */ function read_auth (req: Request, res: Response): Promise { return new Promise ((resolve) => { if (req.headers.readauth === 'on') { resolve (true); } else { res.status (http.status_forbidden); res.end ('readauth failed'); resolve (false); } }); } test.before (async () => { await db.schema.dropTableIfExists ('test'); await db.schema.createTable ('test', (t) => { t.increments ('id'); t.string ('name'); t.integer ('number'); }); const no_auth = new KnexCrudHandler (db, 'test', [ 'name', 'number' ], {}); const auth = new KnexCrudHandler (db, 'test', [ 'name' ], { optional_columns: [ 'number' ], general_authorization: general_auth, read_authorization: read_auth }); const app = express (); app.use ('/test', no_auth.get_router ()); app.use ('/auth', auth.get_router ()); // eslint-disable-next-line no-magic-numbers app.listen (3000); }); test ('insert data', (t) => {}); test ('read data', (t) => {}); test ('update data', (t) => {}); test ('read updated data', (t) => {}); test ('[a] insert data', (t) => {}); test ('[a] insert data without auth', (t) => {}); test ('[a] insert data without optional column', (t) => {}); test ('[a] read data', (t) => {}); test ('[a] read data without auth', (t) => {}); test ('[a] read data without readauth', (t) => {});