1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-25 08:09:41 +02:00

chore: frontend testing setup (#1739)

* add vitest

* initialize lib w/ tests

* move to dev dep

* run tests in CI

* update file names

* move api folder to lib

* move api and api types to same folder

* update generator outpath

* rm husky

* i guess i _did_ need those types

* reorg types

* extract validators into testable components

* (WIP) start composable testing

* fix import type

* fix linter complaint

* simplify icon type def

* fix linter errors (maybe?)

* rename client file for sorting
This commit is contained in:
Hayden 2022-10-22 11:51:07 -08:00 committed by GitHub
parent 9f6bcc83d5
commit fcc5d99d40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
182 changed files with 902 additions and 487 deletions

View file

@ -0,0 +1,2 @@
export { scorePassword } from "./password";
export { required, email, whitespace, url, minLength, maxLength } from "./inputs";

View file

@ -0,0 +1,70 @@
import { expect, test } from "vitest";
import { required, email, whitespace, url, minLength, maxLength } from "./inputs";
export { scorePassword } from "./password";
test("validator required", () => {
const falsey = "This Field is Required";
expect(required("123")).toBe(true);
expect(required("")).toBe(falsey);
expect(required(undefined)).toBe(falsey);
expect(required(null)).toBe(falsey);
});
const nulls = [undefined, null];
test("validator email", () => {
const falsey = "Email Must Be Valid";
expect(email("123")).toBe(falsey);
expect(email("email@example.com")).toBe(true);
for (const n of nulls) {
expect(email(n)).toBe(falsey);
}
});
test("whitespace", () => {
const falsey = "No Whitespace Allowed";
expect(whitespace("123")).toBe(true);
expect(whitespace(" ")).toBe(falsey);
expect(whitespace("123 123")).toBe(falsey);
for (const n of nulls) {
expect(whitespace(n)).toBe(falsey);
}
});
test("url", () => {
const falsey = "Must Be A Valid URL";
expect(url("https://example.com")).toBe(true);
expect(url("")).toBe(falsey);
for (const n of nulls) {
expect(url(n)).toBe(falsey);
}
});
test("minLength", () => {
const min = 3;
const falsey = `Must Be At Least ${min} Characters`;
const fn = minLength(min);
expect(fn("123")).toBe(true);
expect(fn("12")).toBe(falsey);
expect(fn("")).toBe(falsey);
for (const n of nulls) {
expect(fn(n)).toBe(falsey);
}
});
test("maxLength", () => {
const max = 3;
const falsey = `Must Be At Most ${max} Characters`;
const fn = maxLength(max);
expect(fn("123")).toBe(true);
expect(fn("1234")).toBe(falsey);
expect(fn("")).toBe(true);
for (const n of nulls) {
expect(fn(n)).toBe(true);
}
});

View file

@ -0,0 +1,28 @@
const EMAIL_REGEX =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const URL_REGEX = /[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/;
export function required(v: string | undefined | null) {
return !!v || "This Field is Required";
}
export function email(v: string | undefined | null) {
return (!!v && EMAIL_REGEX.test(v)) || "Email Must Be Valid";
}
export function whitespace(v: string | null | undefined) {
return (!!v && v.split(" ").length <= 1) || "No Whitespace Allowed";
}
export function url(v: string | undefined | null) {
return (!!v && URL_REGEX.test(v)) || "Must Be A Valid URL";
}
export function minLength(min: number) {
return (v: string | undefined | null) => (!!v && v.length >= min) || `Must Be At Least ${min} Characters`;
}
export function maxLength(max: number) {
return (v: string | undefined | null) => !v || v.length <= max || `Must Be At Most ${max} Characters`;
}

View file

@ -0,0 +1,30 @@
import { describe, test, expect } from "vitest";
import { scorePassword } from "./password";
describe("scorePassword tests", () => {
test("flagged words should return negative number", () => {
const flaggedWords = ["password", "mealie", "admin", "qwerty", "login"];
for (const word of flaggedWords) {
expect(scorePassword(word)).toBe(0);
}
});
test("should return 0 for empty string", () => {
expect(scorePassword("")).toBe(0);
});
test("should return 0 for strings less than 6", () => {
expect(scorePassword("12345")).toBe(0);
});
test("should return positive number for long string", () => {
const result = expect(scorePassword("123456"));
result.toBeGreaterThan(0);
result.toBeLessThan(31);
});
test("should return max number for long string with all variations", () => {
expect(scorePassword("3bYWcfYOwqxljqeOmQXTLlBwkrH6HV")).toBe(100);
});
});

View file

@ -0,0 +1,45 @@
const flaggedWords = ["password", "mealie", "admin", "qwerty", "login"];
/**
* scorePassword returns a score for a given password between 0 and 100.
* if a password contains a flagged word, it returns 0.
* @param pass
* @returns
*/
export function scorePassword(pass: string): number {
let score = 0;
if (!pass) return score;
if (pass.length < 6) return score;
// Check for flagged words
for (const word of flaggedWords) {
if (pass.toLowerCase().includes(word)) {
return 0;
}
}
// award every unique letter until 5 repetitions
const letters: { [key: string]: number } = {};
for (let i = 0; i < pass.length; i++) {
letters[pass[i]] = (letters[pass[i]] || 0) + 1;
score += 5.0 / letters[pass[i]];
}
// bonus points for mixing it up
const variations: { [key: string]: boolean } = {
digits: /\d/.test(pass),
lower: /[a-z]/.test(pass),
upper: /[A-Z]/.test(pass),
nonWords: /\W/.test(pass),
};
let variationCount = 0;
for (const check in variations) {
variationCount += variations[check] === true ? 1 : 0;
}
score += (variationCount - 1) * 10;
return Math.max(Math.min(score, 100), 0);
}