add site form validation tests

This commit is contained in:
realaravinth
2021-05-06 18:16:13 +05:30
parent 20ee5c35c6
commit ab3147e11d
12 changed files with 393 additions and 199 deletions

View File

@@ -1,101 +0,0 @@
/*
* 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} from './levels';
import isBlankString from '../../../../utils/isBlankString';
import getFormUrl from '../../../../utils/getFormUrl';
import genJsonPayload from '../../../../utils/genJsonPayload';
import isNumber from '../../../../utils/isNumber';
import VIEWS from '../../../../views/v1/routes';
const SITE_KEY_FORM_CLASS = 'sitekey-form';
const FORM = <HTMLFormElement>document.querySelector(`.${SITE_KEY_FORM_CLASS}`);
//const FORM_SUBMIT_BUTTON_CLASS = "sitekey-form__submit";
//const FORM_SUBMIT_BUTTON = <HTMLButtonElement>document.querySelector(`.${FORM_SUBMIT_BUTTON_CLASS}`);
const addSubmitEventListener = () => {
FORM.addEventListener('submit', submit, true);
};
//const validateLevels = (e: Event) => {
// const numLevels = getNumLevels();
// // check if levels are unique and are in increasing order;
// // also if they are positive
// // also if level input field is accompanied by a "Add Level" button,
// // it shouldn't be used for validation
// for (let levelNum = 1; levelNum < numLevels; levelNum++) {
// const inputID = CONST.INPUT_ID_WITHOUT_LEVEL + levelNum;
// const inputElement = <HTMLInputElement>document.getElementById(inputID);
// const val = inputElement.value;
// const filed = CONST.LABEL_INNER_TEXT_WITHOUT_LEVEL + levelNum;
// isBlankString(val, filed, e);
// }
//};
const validateDescription = (e: Event) => {
const inputElement = <HTMLInputElement>document.getElementById('description');
const val = inputElement.value;
const filed = 'Description';
isBlankString(val, filed, e);
return val;
};
const validateDuration = (e: Event) => {
const duartionElement = <HTMLInputElement>document.getElementById('duration');
const duration = parseInt(duartionElement.value);
if (!isNumber(duration) || Number.isNaN(duration)) {
throw new Error('duration can contain nubers only');
}
if (duration <= 0) {
throw new Error('duration must be greater than zero');
}
return duration;
};
const submit = async (e: Event) => {
e.preventDefault();
const description = validateDescription(e);
const duration = validateDuration(e);
const formUrl = getFormUrl(FORM);
const levels = LEVELS.getLevels();
console.debug(`[form submition]: levels: ${levels}`);
const payload = {
levels: levels,
duration,
description,
};
console.debug(`[form submition] json payload: ${JSON.stringify(payload)}`);
const res = await fetch(formUrl, genJsonPayload(payload));
if (res.ok) {
alert('success');
window.location.assign(VIEWS.sitekey);
} else {
const err = await res.json();
alert(`error: ${err.error}`);
}
};
export default addSubmitEventListener;

View File

@@ -0,0 +1,64 @@
/*
* 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} from '../levels';
import getFormUrl from '../../../../../utils/getFormUrl';
import genJsonPayload from '../../../../../utils/genJsonPayload';
import VIEWS from '../../../../../views/v1/routes';
import validateDescription from './validateDescription';
import validateDuration from './validateDuration';
const SITE_KEY_FORM_CLASS = 'sitekey-form';
const FORM = <HTMLFormElement>document.querySelector(`.${SITE_KEY_FORM_CLASS}`);
const addSubmitEventListener = () => {
FORM.addEventListener('submit', submit, true);
};
const submit = async (e: Event) => {
e.preventDefault();
const description = validateDescription(e);
const duration = validateDuration(e);
const formUrl = getFormUrl(FORM);
const levels = LEVELS.getLevels();
console.debug(`[form submition]: levels: ${levels}`);
const payload = {
levels: levels,
duration,
description,
};
console.debug(`[form submition] json payload: ${JSON.stringify(payload)}`);
const res = await fetch(formUrl, genJsonPayload(payload));
if (res.ok) {
alert('success');
window.location.assign(VIEWS.sitekey);
} else {
const err = await res.json();
alert(`error: ${err.error}`);
}
};
export default addSubmitEventListener;

View 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 validateDescription from './validateDescription';
import {getAddForm, fillDescription} from '../setupTests';
document.body.innerHTML = getAddForm();
const emptyErr = "can't be empty";
it('validateDescription workds', () => {
try {
const event = new Event('submit');
validateDescription(event);
} catch (e) {
expect(e.message).toContain(emptyErr);
}
// fill and validate
fillDescription('testing');
const event = new Event('submit');
validateDescription(event);
});

View File

@@ -0,0 +1,28 @@
/*
* 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 isBlankString from '../../../../../utils/isBlankString';
const validateDescription = (e: Event) => {
const inputElement = <HTMLInputElement>document.getElementById('description');
const val = inputElement.value;
const filed = 'Description';
isBlankString(val, filed, e);
return val;
};
export default validateDescription;

View File

@@ -0,0 +1,74 @@
/*
* 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 isNumber from '../../../../../utils/isNumber';
//const validateDuration = (e: Event) => {
// const duartionElement = <HTMLInputElement>document.getElementById('duration');
// const duration = parseInt(duartionElement.value);
// if (!isNumber(duration) || Number.isNaN(duration)) {
// throw new Error('duration can contain nubers only');
// }
//
// if (duration <= 0) {
// throw new Error('duration must be greater than zero');
// }
// return duration;
//};
//
//export default validateDuration;
import validateDuration from './validateDuration';
import {getAddForm, fillDuration} from '../setupTests';
document.body.innerHTML = getAddForm();
const emptyErr = "can't be empty";
const NaNErr = 'duration can contain nubers only';
const zeroErr = 'duration must be greater than zero';
const duration = 30;
it('validateDuration workds', () => {
try {
const event = new Event('submit');
validateDuration(event);
} catch (e) {
expect(e.message).toContain(emptyErr);
}
// fill string error
try {
fillDuration('testing');
const event = new Event('submit');
validateDuration(event);
} catch (e) {
expect(e.message).toContain(NaNErr);
}
// zero err
try {
fillDuration(0);
const event = new Event('submit');
validateDuration(event);
} catch (e) {
expect(e.message).toContain(zeroErr);
}
fillDuration(duration);
const event = new Event('submit');
expect(validateDuration(event)).toBe(duration);
});

View File

@@ -0,0 +1,32 @@
/*
* 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 isNumber from '../../../../../utils/isNumber';
const validateDuration = (e: Event) => {
const duartionElement = <HTMLInputElement>document.getElementById('duration');
const duration = parseInt(duartionElement.value);
if (!isNumber(duration) || Number.isNaN(duration)) {
throw new Error('duration can contain nubers only');
}
if (duration <= 0) {
throw new Error('duration must be greater than zero');
}
return duration;
};
export default validateDuration;

View File

@@ -16,14 +16,39 @@
*/
import getLevelFields from './getLevelFields';
import {getAddForm, level1, level2, addLevel} from '../setupTests';
import {
getAddForm,
level1,
level2,
fillAddLevel,
addLevel,
} from '../setupTests';
document.body.innerHTML = getAddForm();
const visNumErr = 'visitor can contain nubers only';
const diffNumErr = 'difficulty can contain nubers only';
it('get levels fields works', () => {
addLevel(level1.visitor_threshold, level1.difficulty_factor);
expect(getLevelFields(1)).toEqual(level1);
// NaN visitor
try {
fillAddLevel('test', level2.difficulty_factor);
getLevelFields(2);
} catch (e) {
expect(e.message).toBe(visNumErr);
}
// Nan difficulty_factor
try {
fillAddLevel(level2.visitor_threshold, 'fooasdads');
getLevelFields(2);
} catch (e) {
expect(e.message).toBe(diffNumErr);
}
addLevel(level2.visitor_threshold, level2.difficulty_factor);
expect(getLevelFields(2)).toEqual(level2);
});

View File

@@ -19,6 +19,80 @@ import {Level} from './levels/index';
import CONST from './const';
import addLevelButtonAddEventListener from './addLevelButton';
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,
};
/** add level to DOM by filling add level form and clicking "Add" button */
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|string, diff: number|string) => {
addLevelButtonAddEventListener();
const level = getNumLevels();
const visitorField = <HTMLInputElement>(
document.getElementById(`${CONST.VISITOR_WITHOUT_LEVEL}${level}`)
);
visitorField.value = visitor.toString();
const diffField = <HTMLInputElement>(
document.getElementById(`${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`)
);
diffField.value = diff.toString();
};
/** Fill add level form without clicking add button */
export const editLevel = (level: number, visitor?: number, diff?: number) => {
if (visitor !== undefined) {
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();
}
};
/** Fill description in add level form */
export const fillDescription = (description: string) => {
const inputElement = <HTMLInputElement>document.getElementById('description');
inputElement.value = description;
};
/** Fill duration in add level form */
export const fillDuration = (duration: number | string) => {
const inputElement = <HTMLInputElement>document.getElementById('duration');
inputElement.value = duration.toString();
};
export const getAddForm = () => `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<h1 class="form__title">
@@ -89,65 +163,3 @@ export const getAddForm = () => `
<button class="sitekey-form__submit" type="submit">Submit</button>
</form>
`;
/** add level to DOM by filling add level form and clicking "Add" button */
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();
const level = getNumLevels();
const visitorField = <HTMLInputElement>(
document.getElementById(`${CONST.VISITOR_WITHOUT_LEVEL}${level}`)
);
visitorField.value = visitor.toString();
const diffField = <HTMLInputElement>(
document.getElementById(`${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`)
);
diffField.value = diff.toString();
};
/** Fill add level form without clicking add button */
export const editLevel = (level: number, visitor?: number, diff?: number) => {
if (visitor !== undefined) {
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,
};