test structure
This commit is contained in:
20
test/.eslintrc.js
Normal file
20
test/.eslintrc.js
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||
* This file is part of Requestor which is released under BSD-3-Clause.
|
||||
* See file 'LICENSE' for full license details.
|
||||
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
env: {
|
||||
commonjs: true,
|
||||
es6: true,
|
||||
node: true
|
||||
},
|
||||
extends: [
|
||||
'@scode/eslint-config-ts'
|
||||
],
|
||||
rules: {
|
||||
'node/no-unpublished-import': 'off'
|
||||
}
|
||||
}
|
88
test/KnexCrudHandler.ts
Normal file
88
test/KnexCrudHandler.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import knex from 'knex';
|
||||
import test from 'ava';
|
||||
import express, { Request, Response } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import { KnexCrudHandler } from '../lib/KnexCrudHandler';
|
||||
|
||||
const db = knex ({
|
||||
client: 'sqlite',
|
||||
connection: { filename: './db.sqlite' }
|
||||
});
|
||||
|
||||
/**
|
||||
* general auth
|
||||
*
|
||||
* @param {any} req request
|
||||
* @param {any} res response
|
||||
* @returns {Promise<boolean>} successful response
|
||||
*/
|
||||
function general_auth (req: Request, res: Response): Promise<boolean> {
|
||||
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<boolean>} successful response
|
||||
*/
|
||||
function read_auth (req: Request, res: Response): Promise<boolean> {
|
||||
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) => {});
|
Reference in New Issue
Block a user