mirror of
1
2
Fork 0

added tests

This commit is contained in:
Devshh 2020-04-06 11:02:18 +01:00
parent 8ab7f05723
commit e266c6f122
11 changed files with 810 additions and 32 deletions

770
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@
"server": "nodemon src/start.js --ignore dev/ --ignore public/ --ignore src/client/ --ignore dist/client/",
"rollup:dev": "NODE_ENV=development; rollup -c -w",
"rollup:build": "NODE_ENV=production; rollup -c",
"test": "node test/test-server.js"
"test": "mocha"
},
"keywords": [
"Captcha",
@ -36,10 +36,13 @@
"devDependencies": {
"@rollup/plugin-commonjs": "~11.0.2",
"@rollup/plugin-node-resolve": "~7.1.1",
"chai": "^4.2.0",
"eslint": "~6.8.0",
"eslint-config-google": "~0.14.0",
"mocha": "^7.1.1",
"nodemon": "~2.0.2",
"rollup": "~2.0.6",
"rollup-plugin-terser": "~5.3.0"
"rollup-plugin-terser": "~5.3.0",
"sinon": "^9.0.1"
}
}

View File

@ -1,5 +1,4 @@
import request from '../util/request.js';
import logger from '../../helpers/logging.js';
/**
* @typedef {import('../../models/Session.js').Session} Session
@ -12,7 +11,7 @@ import logger from '../../helpers/logging.js';
export default async function reloadSession(websiteKey) {
/** @type {Session} */
const session = await request(`/api/reload?k=${websiteKey}`);
logger.debug({session});
console.info({session});
return session;
}

View File

@ -1,14 +0,0 @@
// Reduces errors till express is fixed
import express from 'express';
/**
* @type {express.Router}
* @property {function} get
*/
export class Router {
/**
*
*/
constructor() {
return express.Router();
}
}

View File

@ -6,7 +6,7 @@ import fs from 'fs';
import db from './db/index.js';
import {IMAGES_FOLDER} from '../R.js';
import {randomBytes} from '../helpers/utils.js';
import utils from '../helpers/utils.js';
const CronJob = cron.CronJob;
@ -117,7 +117,7 @@ export function fetchImages(geoImageRequest) {
/** @type {OSCResponse} */
const json = JSON.parse(body);
for (const item of (json).currentPageItems) {
const filename = `${randomBytes(12)}`;
const filename = `${utils.randomBytes(12)}`;
const filepath = path.join(IMAGES_FOLDER, filename + '.jpg');
await saveImage(filepath, `https://openstreetcam.org/${item.lth_name}`);
await db.setMat({

View File

@ -1,6 +1,6 @@
import fs from 'fs';
import {IMAGES_FOLDER} from '../R.js';
import {randomChoice} from '../helpers/utils.js';
import utils from '../helpers/utils.js';
import logger from './logging.js';
/**
@ -11,7 +11,7 @@ export default () => {
fs.readdir(IMAGES_FOLDER, (err, files)=>{
if (err) return reject(err);
const filename = randomChoice(files);
const filename = utils.randomChoice(files);
logger.debug({filename});
return resolve(filename);

View File

@ -1,5 +1,5 @@
import pickRandomFile from './pickRandomFile.js';
import {randomBytes} from './utils.js';
import utils from './utils.js';
import db from '../helpers/db/index.js';
import {TAGS, MAX_SESSION_TIME} from '../R.js';
@ -14,7 +14,7 @@ import {TAGS, MAX_SESSION_TIME} from '../R.js';
*/
export default async function initializeSession(websiteKey, cookies) {
const image = (await pickRandomFile()).split('.')[0];
const sessionId = cookies[websiteKey] || randomBytes(8);
const sessionId = cookies[websiteKey] || utils.randomBytes(8);
const dbSession = await db.getSession(sessionId);

View File

@ -47,7 +47,7 @@ function randomChoice(arr) {
];
}
export {
export default {
randomBytes,
argmaxThresh,
randomChoice,

View File

@ -1,4 +1,4 @@
import {randomBytes} from './utils.js';
import utils from './utils.js';
import db from '../helpers/db/index.js';
import {
MAX_SESSION_TIME,
@ -7,7 +7,6 @@ import {
MAT_TOLERANCE,
SCORE_TOLERANCE,
} from '../R.js';
import {argmaxThresh} from './utils.js';
import pickRandomFile from './pickRandomFile.js';
import logger from './logging.js';
@ -74,8 +73,8 @@ export default async function verifyImage(sessionId, userMat) {
const imageMat = await db.getMat(image);
logger.debug({imageMat});
const trueArgmax = argmaxThresh(imageMat.mat, MAT_TOLERANCE).join(',');
const userArgmax = argmaxThresh(userMat, 1).join(',');
const trueArgmax = utils.argmaxThresh(imageMat.mat, MAT_TOLERANCE).join(',');
const userArgmax = utils.argmaxThresh(userMat, 1).join(',');
updateMat(imageMat, userMat, image);
@ -94,7 +93,7 @@ export default async function verifyImage(sessionId, userMat) {
}
if (score >= SCORE_TOLERANCE) {
const nonce = randomBytes(24);
const nonce = utils.randomBytes(24);
session.token = nonce;
await db.deleteSession(sessionId);

View File

@ -1,2 +0,0 @@
import {app} from '../src/server.js'
console.log(app)

23
test/utils.test.js Normal file
View File

@ -0,0 +1,23 @@
import utils from '../src/helpers/utils.js';
import chai from 'chai';
const {assert} = chai;
describe('utils', ()=>{
describe('#argmaxThresh', ()=>{
it('should give correct argmax with threshold', (done)=>{
assert.sameMembers(
utils.argmaxThresh([0, 0, 1, 0.3, 0.6, 0.5, 0.9999], 0.56), [2, 4, 6]);
done();
})
});
describe('#randomBytes', ()=>{
it('should create random values', (done)=>{
const rand1 = utils.randomBytes(8);
const rand2 = utils.randomBytes(8);
assert.notEqual(rand1, rand2);
done()
});
});
});