mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 10:05:41 +00:00
edit sitekey, router pattern matching, sitekey update optimization, rm level delete and level err handling
This commit is contained in:
50
templates/panel/sitekey/edit/edit.test.ts
Normal file
50
templates/panel/sitekey/edit/edit.test.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 getNumLevels from '../add/ts/levels/getNumLevels';
|
||||
import {addLevel} from '../add/ts/setupTests';
|
||||
import setup from '../../../components/error/setUpTests';
|
||||
import * as SETUP from './setupTest';
|
||||
|
||||
document.body.innerHTML = SETUP.EDIT_FORM;
|
||||
document.body.appendChild(setup());
|
||||
|
||||
jest.useFakeTimers();
|
||||
|
||||
it('edit sitekey works', () => {
|
||||
expect(getNumLevels()).toBe(2);
|
||||
// add a level
|
||||
addLevel(5, 6);
|
||||
expect(getNumLevels()).toBe(3);
|
||||
|
||||
// add second level
|
||||
addLevel(8, 9);
|
||||
expect(getNumLevels()).toBe(4);
|
||||
|
||||
jest.runAllTimers();
|
||||
|
||||
// expect(trim(a)).toBe(trim(finalHtml()));
|
||||
|
||||
// try to add negative parameters
|
||||
addLevel(-4, -9);
|
||||
expect(getNumLevels()).toBe(4);
|
||||
|
||||
// try to add duplicate level
|
||||
addLevel(6, 7);
|
||||
expect(getNumLevels()).toBe(4);
|
||||
|
||||
});
|
||||
36
templates/panel/sitekey/edit/existing-level.html
Normal file
36
templates/panel/sitekey/edit/existing-level.html
Normal file
@@ -0,0 +1,36 @@
|
||||
<. let num = count + 1; .>
|
||||
<fieldset class="sitekey__level-container" id="level-group-<.= num .>">
|
||||
<legend class="sitekey__level-title">
|
||||
Level <.= num .>
|
||||
</legend>
|
||||
|
||||
<label class="sitekey-form__level-label" for="visitor<.= num .>"
|
||||
>Visitor
|
||||
<input
|
||||
class="sitekey-form__level-input"
|
||||
type="number"
|
||||
name="visitor<.= num .>"
|
||||
value="<.= level.visitor_threshold .>"
|
||||
id="visitor<.= num .>"
|
||||
/>
|
||||
</label>
|
||||
<label class="sitekey-form__level-label" for="difficulty<.= num .>">
|
||||
Difficulty
|
||||
<input
|
||||
type="number"
|
||||
id="difficulty<.= num .>"
|
||||
class="sitekey-form__level-input"
|
||||
value="<.= level.difficulty_factor .>"
|
||||
id="difficulty<.= num .>"
|
||||
/>
|
||||
</label>
|
||||
<label class="sitekey-form__level-label--hidden" for="remove<.= num .>">
|
||||
RemoveLevel
|
||||
<input
|
||||
class="sitekey-form__level-remove-level-button"
|
||||
type="button"
|
||||
id="remove-level<.= num .>"
|
||||
id="remove-level1"
|
||||
value="x"
|
||||
/></label>
|
||||
</fieldset>
|
||||
12
templates/panel/sitekey/edit/index.html
Normal file
12
templates/panel/sitekey/edit/index.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<. const URL: &str = crate::V1_API_ROUTES.levels.update; .>
|
||||
<. const READONLY: bool = false; .>
|
||||
<. include!("../view/__form-top.html"); .>
|
||||
<. for (count, level) in levels.iter().enumerate() { .>
|
||||
<. include!("./existing-level.html"); .>
|
||||
<. } .>
|
||||
<. let level = levels.len() + 1; .>
|
||||
<. include!("../add/add-level.html"); .>
|
||||
<button data-sitekey="<.= key .>" class="sitekey-form__submit" type="submit">
|
||||
Submit
|
||||
</button>
|
||||
<. include!("../view/__form-bottom.html"); .>
|
||||
82
templates/panel/sitekey/edit/index.ts
Normal file
82
templates/panel/sitekey/edit/index.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 * as Add from '../add/ts/form/';
|
||||
import addLevelButtonAddEventListener from '../add/ts/addLevelButton';
|
||||
import {addRemoveLevelButtonEventListenerAll} from '../add/ts/removeLevelButton';
|
||||
import getNumLevels from '../add/ts/levels/getNumLevels';
|
||||
import validateLevel from '../add/ts/levels/validateLevel';
|
||||
import * as UpdateLevel from '../add/ts/levels/updateLevel';
|
||||
import validateDescription from '../add/ts/form/validateDescription';
|
||||
import validateDuration from '../add/ts/form/validateDuration';
|
||||
import {LEVELS} from '../add/ts/levels';
|
||||
|
||||
import getFormUrl from '../../../utils/getFormUrl';
|
||||
import genJsonPayload from '../../../utils/genJsonPayload';
|
||||
import createError from '../../../components/error';
|
||||
|
||||
import VIEWS from '../../../views/v1/routes';
|
||||
|
||||
const BTN = <HTMLElement>document.querySelector('.sitekey-form__submit');
|
||||
const key = BTN.dataset.sitekey;
|
||||
|
||||
const submit = async (e: Event) => {
|
||||
e.preventDefault();
|
||||
|
||||
const description = validateDescription(e);
|
||||
const duration = validateDuration(e);
|
||||
|
||||
const formUrl = getFormUrl(Add.FORM);
|
||||
|
||||
const levels = LEVELS.getLevels();
|
||||
console.debug(`[form submition]: levels: ${levels}`);
|
||||
|
||||
const payload = {
|
||||
levels,
|
||||
duration,
|
||||
description,
|
||||
key,
|
||||
};
|
||||
|
||||
console.debug(`[form submition] json payload: ${JSON.stringify(payload)}`);
|
||||
|
||||
const res = await fetch(formUrl, genJsonPayload(payload));
|
||||
if (res.ok) {
|
||||
window.location.assign(VIEWS.viewSitekey(key));
|
||||
} else {
|
||||
const err = await res.json();
|
||||
createError(err.error);
|
||||
}
|
||||
};
|
||||
|
||||
const addSubmitEventListener = () => {
|
||||
Add.FORM.addEventListener('submit', submit, true);
|
||||
};
|
||||
|
||||
const bootstrapLevels = () => {
|
||||
const levels = getNumLevels();
|
||||
addRemoveLevelButtonEventListenerAll();
|
||||
for (let i = 1; i <= levels - 1; i++) {
|
||||
validateLevel(i);
|
||||
UpdateLevel.register(i);
|
||||
}
|
||||
};
|
||||
|
||||
export const index = () => {
|
||||
addSubmitEventListener();
|
||||
addLevelButtonAddEventListener();
|
||||
bootstrapLevels();
|
||||
};
|
||||
107
templates/panel/sitekey/edit/setupTest.ts
Normal file
107
templates/panel/sitekey/edit/setupTest.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
export const EDIT_FORM = `
|
||||
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/update" method="post" >
|
||||
<h1 class="form__title">
|
||||
Sitekey: test
|
||||
</h1>
|
||||
<label class="sitekey-form__label" for="description">
|
||||
Description
|
||||
<input
|
||||
class="sitekey-form__input"
|
||||
type="text"
|
||||
name="description"
|
||||
id="description"
|
||||
required=""
|
||||
value="test"
|
||||
/>
|
||||
</label>
|
||||
<label class="sitekey-form__label" for="duration">
|
||||
Cooldown Duratoin(in seconds)
|
||||
<input
|
||||
class="sitekey-form__input"
|
||||
type="number"
|
||||
name="duration"
|
||||
id="duration"
|
||||
min="0"
|
||||
required=""
|
||||
value="30"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<fieldset class="sitekey__level-container" id="level-group-1">
|
||||
<legend class="sitekey__level-title">
|
||||
Level 1
|
||||
</legend>
|
||||
|
||||
<label class="sitekey-form__level-label" for="visitor1"
|
||||
>Visitor
|
||||
<input
|
||||
class="sitekey-form__level-input"
|
||||
type="number"
|
||||
name="visitor1"
|
||||
value="4"
|
||||
id="visitor1"
|
||||
/>
|
||||
</label>
|
||||
<label class="sitekey-form__level-label" for="difficulty1">
|
||||
Difficulty
|
||||
<input
|
||||
type="number"
|
||||
id="difficulty1"
|
||||
class="sitekey-form__level-input"
|
||||
value="5"
|
||||
/>
|
||||
</label>
|
||||
<label class="sitekey-form__level-label--hidden" for="remove1">
|
||||
RemoveLevel
|
||||
<input
|
||||
class="sitekey-form__level-remove-level-button"
|
||||
type="button"
|
||||
id="remove-level1"
|
||||
value="x"
|
||||
/></label>
|
||||
</fieldset>
|
||||
|
||||
<fieldset class="sitekey__level-container" id="level-group-2">
|
||||
<legend class="sitekey__level-title">
|
||||
Level 2
|
||||
</legend>
|
||||
<label class="sitekey-form__level-label" for="visitor2"
|
||||
>Visitor
|
||||
<input
|
||||
class="sitekey-form__level-input"
|
||||
type="number"
|
||||
name="visitor2"
|
||||
id="visitor2"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label class="sitekey-form__level-label" for="difficulty2">
|
||||
Difficulty
|
||||
<input
|
||||
type="number"
|
||||
name="difficulty2"
|
||||
class="sitekey-form__level-input"
|
||||
id="difficulty2"
|
||||
/>
|
||||
</label>
|
||||
<label class="sitekey-form__level-label--hidden" for="add">
|
||||
<span class="sitekey-form__add-level-btn-spacer">Add level</span>
|
||||
<input
|
||||
class="sitekey-form__level-add-level-button"
|
||||
type="button"
|
||||
name="add"
|
||||
id="add"
|
||||
value="Add"
|
||||
/>
|
||||
</label>
|
||||
</fieldset>
|
||||
<button
|
||||
data-sitekey="9FGkkukDRFDk7FgJmjXKxeHjFHUcxNez"
|
||||
class="sitekey-form__submit"
|
||||
type="submit"
|
||||
>
|
||||
Submit
|
||||
</button>
|
||||
</form>
|
||||
<div id="err__container"></div>
|
||||
`;
|
||||
Reference in New Issue
Block a user