Files
validation/index.test.ts
T
pascal 1179c4b1da Validation
Package, implementation and tests
2024-05-19 22:47:42 +02:00

84 lines
2.8 KiB
TypeScript

import {describe, expect, test} from 'bun:test'
import * as f from '.'
// TODO : Test logging
describe('email', () => {
const emails: [string, boolean][] = [
['pascal@pband.ch', true],
['p@p.ch', true],
[`${'a'.repeat(64)}@${'b'.repeat(60)}.ch`, true],
[`${'a'.repeat(128)}@${'b'.repeat(64)}.ch`, false],
['google.ch@test.com', true],
['pascal+truc@pband.ch', true],
['pascal+.@pband.ch', false],
['pascal+p.@pband.ch', false],
['pascal@pband.', false],
['pascal@pband', false],
['pascal@.ch', false],
['@pband.ch', false],
['prout', false],
['google.ch', false],
]
for (const [email, valid] of emails) {
test(`"${email}" is ${valid ? 'valid' : 'invalid'}`, () => {
const res = expect(f.email_is_valid(email))
if (valid) {
res.toBeTrue()
} else {
res.toBeFalse()
}
})
}
})
describe('Password strength', () => {
// TODO : More passwords lol
const pwds: [string, f.PasswordStrength][] = [
// ['test', f.PasswordStrength.Strong],
['OkPassword', f.PasswordStrength.Easy],
]
for (const pwd of pwds) {
for (const level of [f.PasswordStrength.Weak, f.PasswordStrength.Easy, f.PasswordStrength.Medium, f.PasswordStrength.Strong, f.PasswordStrength.Hard]) {
const should_pass = level <= pwd[1]
test(`"${pwd[0]}" should ${should_pass ? '' : 'not '}pass level ${level}`, () => {
const res = expect(f.password_is_strong(pwd[0], level as f.PasswordStrength))
if (should_pass) {
res.toBeTrue()
} else {
res.toBeFalse()
}
})
}
}
})
describe('Contains HTML', () => {
const texts: [string, boolean][] = [
['Hello, world!', false],
['<html><body><h1>Hello, world!</h1></body></html>', true],
['<html><body><h1>Hello, world!</h1></body></html><script>console.log("Hello, world!")</script>', true],
['Hello <strong>guy !</strong>', true],
['Hello <strong>guy !</strong><script>console.log("Hello, world!")</script>', true],
['Simple balise <br/>', true],
['Simple balise <br />', true],
['Simple balise <br>', true],
['Simple math : 1 < 2', false],
['Simple math : 1 < 2 > 0', false],
]
for (const i in texts) {
const [text, contains] = texts[i]
test(`Text ${i} : should ${contains ? '' : 'not '}contain HTML`, () => {
const res = expect(f.contains_html(text))
if (contains) {
res.toBeTrue()
} else {
res.toBeFalse()
}
})
}
})