added tests
This commit is contained in:
parent
8ab7f05723
commit
e266c6f122
File diff suppressed because it is too large
Load Diff
|
@ -13,7 +13,7 @@
|
||||||
"server": "nodemon src/start.js --ignore dev/ --ignore public/ --ignore src/client/ --ignore dist/client/",
|
"server": "nodemon src/start.js --ignore dev/ --ignore public/ --ignore src/client/ --ignore dist/client/",
|
||||||
"rollup:dev": "NODE_ENV=development; rollup -c -w",
|
"rollup:dev": "NODE_ENV=development; rollup -c -w",
|
||||||
"rollup:build": "NODE_ENV=production; rollup -c",
|
"rollup:build": "NODE_ENV=production; rollup -c",
|
||||||
"test": "node test/test-server.js"
|
"test": "mocha"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"Captcha",
|
"Captcha",
|
||||||
|
@ -36,10 +36,13 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-commonjs": "~11.0.2",
|
"@rollup/plugin-commonjs": "~11.0.2",
|
||||||
"@rollup/plugin-node-resolve": "~7.1.1",
|
"@rollup/plugin-node-resolve": "~7.1.1",
|
||||||
|
"chai": "^4.2.0",
|
||||||
"eslint": "~6.8.0",
|
"eslint": "~6.8.0",
|
||||||
"eslint-config-google": "~0.14.0",
|
"eslint-config-google": "~0.14.0",
|
||||||
|
"mocha": "^7.1.1",
|
||||||
"nodemon": "~2.0.2",
|
"nodemon": "~2.0.2",
|
||||||
"rollup": "~2.0.6",
|
"rollup": "~2.0.6",
|
||||||
"rollup-plugin-terser": "~5.3.0"
|
"rollup-plugin-terser": "~5.3.0",
|
||||||
|
"sinon": "^9.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import request from '../util/request.js';
|
import request from '../util/request.js';
|
||||||
import logger from '../../helpers/logging.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('../../models/Session.js').Session} Session
|
* @typedef {import('../../models/Session.js').Session} Session
|
||||||
|
@ -12,7 +11,7 @@ import logger from '../../helpers/logging.js';
|
||||||
export default async function reloadSession(websiteKey) {
|
export default async function reloadSession(websiteKey) {
|
||||||
/** @type {Session} */
|
/** @type {Session} */
|
||||||
const session = await request(`/api/reload?k=${websiteKey}`);
|
const session = await request(`/api/reload?k=${websiteKey}`);
|
||||||
logger.debug({session});
|
console.info({session});
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -6,7 +6,7 @@ import fs from 'fs';
|
||||||
|
|
||||||
import db from './db/index.js';
|
import db from './db/index.js';
|
||||||
import {IMAGES_FOLDER} from '../R.js';
|
import {IMAGES_FOLDER} from '../R.js';
|
||||||
import {randomBytes} from '../helpers/utils.js';
|
import utils from '../helpers/utils.js';
|
||||||
|
|
||||||
const CronJob = cron.CronJob;
|
const CronJob = cron.CronJob;
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ export function fetchImages(geoImageRequest) {
|
||||||
/** @type {OSCResponse} */
|
/** @type {OSCResponse} */
|
||||||
const json = JSON.parse(body);
|
const json = JSON.parse(body);
|
||||||
for (const item of (json).currentPageItems) {
|
for (const item of (json).currentPageItems) {
|
||||||
const filename = `${randomBytes(12)}`;
|
const filename = `${utils.randomBytes(12)}`;
|
||||||
const filepath = path.join(IMAGES_FOLDER, filename + '.jpg');
|
const filepath = path.join(IMAGES_FOLDER, filename + '.jpg');
|
||||||
await saveImage(filepath, `https://openstreetcam.org/${item.lth_name}`);
|
await saveImage(filepath, `https://openstreetcam.org/${item.lth_name}`);
|
||||||
await db.setMat({
|
await db.setMat({
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import {IMAGES_FOLDER} from '../R.js';
|
import {IMAGES_FOLDER} from '../R.js';
|
||||||
import {randomChoice} from '../helpers/utils.js';
|
import utils from '../helpers/utils.js';
|
||||||
import logger from './logging.js';
|
import logger from './logging.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,7 +11,7 @@ export default () => {
|
||||||
fs.readdir(IMAGES_FOLDER, (err, files)=>{
|
fs.readdir(IMAGES_FOLDER, (err, files)=>{
|
||||||
if (err) return reject(err);
|
if (err) return reject(err);
|
||||||
|
|
||||||
const filename = randomChoice(files);
|
const filename = utils.randomChoice(files);
|
||||||
logger.debug({filename});
|
logger.debug({filename});
|
||||||
|
|
||||||
return resolve(filename);
|
return resolve(filename);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import pickRandomFile from './pickRandomFile.js';
|
import pickRandomFile from './pickRandomFile.js';
|
||||||
import {randomBytes} from './utils.js';
|
import utils from './utils.js';
|
||||||
import db from '../helpers/db/index.js';
|
import db from '../helpers/db/index.js';
|
||||||
import {TAGS, MAX_SESSION_TIME} from '../R.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) {
|
export default async function initializeSession(websiteKey, cookies) {
|
||||||
const image = (await pickRandomFile()).split('.')[0];
|
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);
|
const dbSession = await db.getSession(sessionId);
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ function randomChoice(arr) {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export default {
|
||||||
randomBytes,
|
randomBytes,
|
||||||
argmaxThresh,
|
argmaxThresh,
|
||||||
randomChoice,
|
randomChoice,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {randomBytes} from './utils.js';
|
import utils from './utils.js';
|
||||||
import db from '../helpers/db/index.js';
|
import db from '../helpers/db/index.js';
|
||||||
import {
|
import {
|
||||||
MAX_SESSION_TIME,
|
MAX_SESSION_TIME,
|
||||||
|
@ -7,7 +7,6 @@ import {
|
||||||
MAT_TOLERANCE,
|
MAT_TOLERANCE,
|
||||||
SCORE_TOLERANCE,
|
SCORE_TOLERANCE,
|
||||||
} from '../R.js';
|
} from '../R.js';
|
||||||
import {argmaxThresh} from './utils.js';
|
|
||||||
import pickRandomFile from './pickRandomFile.js';
|
import pickRandomFile from './pickRandomFile.js';
|
||||||
import logger from './logging.js';
|
import logger from './logging.js';
|
||||||
|
|
||||||
|
@ -74,8 +73,8 @@ export default async function verifyImage(sessionId, userMat) {
|
||||||
|
|
||||||
const imageMat = await db.getMat(image);
|
const imageMat = await db.getMat(image);
|
||||||
logger.debug({imageMat});
|
logger.debug({imageMat});
|
||||||
const trueArgmax = argmaxThresh(imageMat.mat, MAT_TOLERANCE).join(',');
|
const trueArgmax = utils.argmaxThresh(imageMat.mat, MAT_TOLERANCE).join(',');
|
||||||
const userArgmax = argmaxThresh(userMat, 1).join(',');
|
const userArgmax = utils.argmaxThresh(userMat, 1).join(',');
|
||||||
|
|
||||||
updateMat(imageMat, userMat, image);
|
updateMat(imageMat, userMat, image);
|
||||||
|
|
||||||
|
@ -94,7 +93,7 @@ export default async function verifyImage(sessionId, userMat) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (score >= SCORE_TOLERANCE) {
|
if (score >= SCORE_TOLERANCE) {
|
||||||
const nonce = randomBytes(24);
|
const nonce = utils.randomBytes(24);
|
||||||
session.token = nonce;
|
session.token = nonce;
|
||||||
|
|
||||||
await db.deleteSession(sessionId);
|
await db.deleteSession(sessionId);
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
import {app} from '../src/server.js'
|
|
||||||
console.log(app)
|
|
|
@ -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()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue