mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-11-24 14:35:46 +00:00
levels tests
This commit is contained in:
parent
6069509504
commit
20ee5c35c6
@ -16,25 +16,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import getLevelFields from './getLevelFields';
|
import getLevelFields from './getLevelFields';
|
||||||
import {getAddForm, addLevel} from '../setupTests';
|
import {getAddForm, level1, level2, addLevel} from '../setupTests';
|
||||||
import {Level} from './index';
|
|
||||||
//import CONST from '../const';
|
|
||||||
|
|
||||||
document.body.innerHTML = getAddForm();
|
document.body.innerHTML = getAddForm();
|
||||||
|
|
||||||
const level1: Level = {
|
|
||||||
difficulty_factor: 200,
|
|
||||||
visitor_threshold: 500,
|
|
||||||
};
|
|
||||||
|
|
||||||
const level2: Level = {
|
|
||||||
difficulty_factor: 400,
|
|
||||||
visitor_threshold: 700,
|
|
||||||
};
|
|
||||||
|
|
||||||
it('get levels fields works', () => {
|
it('get levels fields works', () => {
|
||||||
addLevel(level1.visitor_threshold, level1.difficulty_factor);
|
addLevel(level1.visitor_threshold, level1.difficulty_factor);
|
||||||
console.log(document.body.innerHTML);
|
|
||||||
expect(getLevelFields(1)).toEqual(level1);
|
expect(getLevelFields(1)).toEqual(level1);
|
||||||
|
|
||||||
addLevel(level2.visitor_threshold, level2.difficulty_factor);
|
addLevel(level2.visitor_threshold, level2.difficulty_factor);
|
||||||
|
|||||||
@ -17,7 +17,6 @@
|
|||||||
|
|
||||||
import getNumLevels from './getNumLevels';
|
import getNumLevels from './getNumLevels';
|
||||||
import {getAddForm, addLevel} from '../setupTests';
|
import {getAddForm, addLevel} from '../setupTests';
|
||||||
//import CONST from '../const';
|
|
||||||
|
|
||||||
document.body.innerHTML = getAddForm();
|
document.body.innerHTML = getAddForm();
|
||||||
|
|
||||||
|
|||||||
@ -15,8 +15,6 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import getNumLevels from './getNumLevels';
|
|
||||||
|
|
||||||
/** Datatype represenging an mCaptcha level */
|
/** Datatype represenging an mCaptcha level */
|
||||||
export type Level = {
|
export type Level = {
|
||||||
difficulty_factor: number;
|
difficulty_factor: number;
|
||||||
@ -26,13 +24,9 @@ export type Level = {
|
|||||||
/** Datatype representing a collection of mCaptcha levels */
|
/** Datatype representing a collection of mCaptcha levels */
|
||||||
class Levels {
|
class Levels {
|
||||||
levels: Array<Level>;
|
levels: Array<Level>;
|
||||||
numOnScreen: number;
|
|
||||||
numRecoreded: number;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.levels = [];
|
this.levels = [];
|
||||||
this.numRecoreded = 0;
|
|
||||||
this.numOnScreen = getNumLevels();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
add = (newLevel: Level) => {
|
add = (newLevel: Level) => {
|
||||||
@ -42,7 +36,7 @@ class Levels {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (newLevel.visitor_threshold <= 0) {
|
if (newLevel.visitor_threshold <= 0) {
|
||||||
throw new Error('Visitors must be graeter than zero');
|
throw new Error('Visitors must be greater than zero');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.levels.length == 0) {
|
if (this.levels.length == 0) {
|
||||||
@ -53,28 +47,19 @@ class Levels {
|
|||||||
let msg;
|
let msg;
|
||||||
let count = 1;
|
let count = 1;
|
||||||
|
|
||||||
const validate = (level: Level, newLevel: Level) => {
|
this.levels.forEach(level => {
|
||||||
if (level.visitor_threshold >= newLevel.visitor_threshold) {
|
if (level.visitor_threshold >= newLevel.visitor_threshold) {
|
||||||
msg = `Level: ${newLevel} visitor count has to greater than previous levels. See ${count}`;
|
const msg = `Level: ${newLevel} visitor count has to greater than previous levels. See ${count}`;
|
||||||
return true;
|
throw new Error(msg);
|
||||||
}
|
} else if (level.difficulty_factor >= newLevel.difficulty_factor) {
|
||||||
|
const msg = `Level ${this.levels.length} difficulty has to greater than previous levels See ${count}`;
|
||||||
if (level.difficulty_factor >= newLevel.difficulty_factor) {
|
|
||||||
msg = `Level ${this.levels.length} difficulty has to greater than previous levels See ${count}`;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
count++;
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (this.levels.find(level => validate(level, newLevel))) {
|
|
||||||
alert(msg);
|
|
||||||
throw new Error(msg);
|
throw new Error(msg);
|
||||||
} else {
|
} else {
|
||||||
this.levels.push(newLevel);
|
count++;
|
||||||
this.numOnScreen += 1;
|
|
||||||
this.numRecoreded += 1;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.levels.push(newLevel);
|
||||||
};
|
};
|
||||||
|
|
||||||
get = () => this.levels;
|
get = () => this.levels;
|
||||||
@ -87,16 +72,6 @@ export const LEVELS = (function() {
|
|||||||
return {
|
return {
|
||||||
/** get levels */
|
/** get levels */
|
||||||
getLevels: () => levels.get(),
|
getLevels: () => levels.get(),
|
||||||
/**
|
|
||||||
* get levels displayed on screen.
|
|
||||||
* This includes the one with add level button
|
|
||||||
* */
|
|
||||||
getOnScreen: () => levels.numOnScreen,
|
|
||||||
/**
|
|
||||||
* get levels recorded using LEVELS
|
|
||||||
* This excludes the one with add level button
|
|
||||||
* */
|
|
||||||
getRecored: () => levels.numRecoreded,
|
|
||||||
|
|
||||||
/** add new level */
|
/** add new level */
|
||||||
add: (newLevel: Level) => levels.add(newLevel),
|
add: (newLevel: Level) => levels.add(newLevel),
|
||||||
|
|||||||
82
templates/panel/sitekey/add/ts/levels/levels.test.ts
Normal file
82
templates/panel/sitekey/add/ts/levels/levels.test.ts
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {LEVELS, Level} from './index';
|
||||||
|
import {level1, level1visErr, level1diffErr, level2} from '../setupTests';
|
||||||
|
|
||||||
|
const visitorErr = 'visitor count has to greater than previous levels';
|
||||||
|
const difficultyErr = 'difficulty has to greater than previous levels';
|
||||||
|
|
||||||
|
const zeroVisError = 'Visitors must be greater than zero';
|
||||||
|
const zeroDiffError = 'Difficulty must be greater than zero';
|
||||||
|
|
||||||
|
const zeroVis: Level = {
|
||||||
|
difficulty_factor: 10,
|
||||||
|
visitor_threshold: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const zeroDiff: Level = {
|
||||||
|
difficulty_factor: 0,
|
||||||
|
visitor_threshold: 10,
|
||||||
|
};
|
||||||
|
|
||||||
|
it('LEVELS works', () => {
|
||||||
|
// add level
|
||||||
|
LEVELS.add(level1);
|
||||||
|
expect(LEVELS.getLevels()).toEqual([level1]);
|
||||||
|
|
||||||
|
// add visitor count < prev level
|
||||||
|
try {
|
||||||
|
LEVELS.add(level1visErr);
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toContain(visitorErr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add difficulty < prev level
|
||||||
|
try {
|
||||||
|
LEVELS.add(level1diffErr);
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toContain(difficultyErr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add second level
|
||||||
|
LEVELS.add(level2);
|
||||||
|
expect(LEVELS.getLevels()).toEqual([level1, level2]);
|
||||||
|
|
||||||
|
// update level
|
||||||
|
const newLevel2 = level2;
|
||||||
|
newLevel2.difficulty_factor = 8000;
|
||||||
|
LEVELS.update(newLevel2, 2);
|
||||||
|
expect(LEVELS.getLevels()).toEqual([level1, newLevel2]);
|
||||||
|
|
||||||
|
// update second level
|
||||||
|
LEVELS.remove(1);
|
||||||
|
expect(LEVELS.getLevels()).toEqual([newLevel2]);
|
||||||
|
|
||||||
|
// visitor is 0
|
||||||
|
try {
|
||||||
|
LEVELS.add(zeroVis);
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toEqual(zeroVisError);
|
||||||
|
}
|
||||||
|
// difficulty is 0
|
||||||
|
try {
|
||||||
|
LEVELS.add(zeroDiff);
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toEqual(zeroDiffError);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -27,16 +27,12 @@ const updateLevel = (e: Event) => {
|
|||||||
|
|
||||||
let level;
|
let level;
|
||||||
if (id.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
|
if (id.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
|
||||||
level = id.slice(CONST.VISITOR_WITHOUT_LEVEL.length);
|
level = parseInt(id.slice(CONST.VISITOR_WITHOUT_LEVEL.length));
|
||||||
} else if (id.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) {
|
}
|
||||||
level = id.slice(CONST.DIFFICULTY_WITHOUT_LEVEL.length);
|
if (id.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) {
|
||||||
} else {
|
level = parseInt(id.slice(CONST.DIFFICULTY_WITHOUT_LEVEL.length));
|
||||||
throw new Error(
|
|
||||||
'update event was triggered by some element other than difficulty or visitor',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
level = parseInt(level);
|
|
||||||
if (Number.isNaN(level)) {
|
if (Number.isNaN(level)) {
|
||||||
console.error(`[updateLevel.ts] level # computed is not correct, got NaN`);
|
console.error(`[updateLevel.ts] level # computed is not correct, got NaN`);
|
||||||
}
|
}
|
||||||
|
|||||||
37
templates/panel/sitekey/add/ts/levels/validateLevel.test.ts
Normal file
37
templates/panel/sitekey/add/ts/levels/validateLevel.test.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import validateLevel from './validateLevel';
|
||||||
|
import {getAddForm, level1, fillAddLevel} from '../setupTests';
|
||||||
|
|
||||||
|
document.body.innerHTML = getAddForm();
|
||||||
|
|
||||||
|
it('validate levels fields works', () => {
|
||||||
|
// null error
|
||||||
|
expect(validateLevel(1)).toEqual(false);
|
||||||
|
|
||||||
|
fillAddLevel(level1.visitor_threshold, level1.difficulty_factor);
|
||||||
|
expect(validateLevel(1)).toEqual(true);
|
||||||
|
|
||||||
|
// zero visitor error
|
||||||
|
fillAddLevel(0, level1.difficulty_factor);
|
||||||
|
expect(validateLevel(1)).toEqual(false);
|
||||||
|
|
||||||
|
// zero difficulty error
|
||||||
|
fillAddLevel(level1.visitor_threshold, 0);
|
||||||
|
expect(validateLevel(1)).toEqual(false);
|
||||||
|
});
|
||||||
@ -23,13 +23,8 @@ import getLevelFields from './getLevelFields';
|
|||||||
* its contents
|
* its contents
|
||||||
* */
|
* */
|
||||||
const validateLevel = (id: number) => {
|
const validateLevel = (id: number) => {
|
||||||
const level = getLevelFields(id);
|
|
||||||
|
|
||||||
if (level === null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const level = getLevelFields(id);
|
||||||
LEVELS.add(level);
|
LEVELS.add(level);
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import getNumLevels from './levels/getNumLevels';
|
import getNumLevels from './levels/getNumLevels';
|
||||||
|
import {Level} from './levels/index';
|
||||||
import CONST from './const';
|
import CONST from './const';
|
||||||
import addLevelButtonAddEventListener from './addLevelButton';
|
import addLevelButtonAddEventListener from './addLevelButton';
|
||||||
|
|
||||||
@ -89,8 +90,17 @@ export const getAddForm = () => `
|
|||||||
</form>
|
</form>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
/** add level to DOM by filling add level form */
|
/** add level to DOM by filling add level form and clicking "Add" button */
|
||||||
export const addLevel = (visitor: number, diff: number) => {
|
export const addLevel = (visitor: number, diff: number) => {
|
||||||
|
fillAddLevel(visitor, diff);
|
||||||
|
const addLevelButton = <HTMLElement>(
|
||||||
|
document.querySelector(`.${CONST.ADD_LEVEL_BUTTON}`)
|
||||||
|
);
|
||||||
|
addLevelButton.click();
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Fill add level form without clicking add button */
|
||||||
|
export const fillAddLevel = (visitor: number, diff: number) => {
|
||||||
addLevelButtonAddEventListener();
|
addLevelButtonAddEventListener();
|
||||||
|
|
||||||
const level = getNumLevels();
|
const level = getNumLevels();
|
||||||
@ -103,10 +113,41 @@ export const addLevel = (visitor: number, diff: number) => {
|
|||||||
document.getElementById(`${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`)
|
document.getElementById(`${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`)
|
||||||
);
|
);
|
||||||
diffField.value = diff.toString();
|
diffField.value = diff.toString();
|
||||||
|
};
|
||||||
const addLevelButton = <HTMLElement>(
|
|
||||||
document.querySelector(`.${CONST.ADD_LEVEL_BUTTON}`)
|
/** Fill add level form without clicking add button */
|
||||||
);
|
export const editLevel = (level: number, visitor?: number, diff?: number) => {
|
||||||
|
if (visitor !== undefined) {
|
||||||
addLevelButton.click();
|
const visitorField = <HTMLInputElement>(
|
||||||
|
document.getElementById(`${CONST.VISITOR_WITHOUT_LEVEL}${level}`)
|
||||||
|
);
|
||||||
|
visitorField.value = visitor.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diff !== undefined) {
|
||||||
|
const diffField = <HTMLInputElement>(
|
||||||
|
document.getElementById(`${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`)
|
||||||
|
);
|
||||||
|
diffField.value = diff.toString();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const level1: Level = {
|
||||||
|
difficulty_factor: 200,
|
||||||
|
visitor_threshold: 500,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const level1diffErr: Level = {
|
||||||
|
difficulty_factor: 100,
|
||||||
|
visitor_threshold: 600,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const level1visErr: Level = {
|
||||||
|
difficulty_factor: 600,
|
||||||
|
visitor_threshold: 400,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const level2: Level = {
|
||||||
|
difficulty_factor: 400,
|
||||||
|
visitor_threshold: 700,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user