@snowtop/ent-password
Version:
password datatype for ents
129 lines (128 loc) • 4.49 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const password_validator_1 = __importDefault(require("password-validator"));
const password_1 = require("./password");
const bcrypt = __importStar(require("bcryptjs"));
let schema = new password_validator_1.default();
schema
.is()
.min(8)
.is()
.max(20)
.has()
.uppercase()
.has()
.lowercase()
.has()
.digits()
.has()
.not()
.spaces()
.has()
.symbols();
function pw() {
return (0, password_1.PasswordType)().validate((val) => {
return !!schema.validate(val);
});
}
describe("password-validator", () => {
test("too short", async () => {
expect(pw().valid("hE11$o")).toBe(false);
});
test("too long", async () => {
expect(pw().valid("hellohellohelloA$B1hello")).toBe(false);
});
test("no upper", async () => {
expect(pw().valid("hello1$234")).toBe(false);
});
test("no symbols", async () => {
expect(pw().valid("helloABCDgjsdsd1s")).toBe(false);
});
test("no digits", async () => {
expect(pw().valid("helloABCDgjsdsds$")).toBe(false);
});
test("has spaces", async () => {
expect(pw().valid("helloABCD gjsdsds1$")).toBe(false);
});
test("valid", async () => {
expect(pw().valid("helloABCDgjsdsds1$")).toBe(true);
});
});
describe("manual format", () => {
test("min Len", async () => {
let pw = (0, password_1.PasswordType)().minLen(10);
expect(pw.valid("hello")).toBe(false);
expect(pw.valid("hello123456")).toBe(true);
});
test("max Len", async () => {
let pw = (0, password_1.PasswordType)().maxLen(10);
expect(pw.valid("hello")).toBe(true);
expect(pw.valid("hellohello")).toBe(true);
expect(pw.valid("hellohellohello")).toBe(false);
});
test("length", async () => {
let pw = (0, password_1.PasswordType)().length(10);
expect(pw.valid("hello")).toBe(false);
expect(pw.valid("hellohello")).toBe(true);
});
test("regex match", async () => {
let pw = (0, password_1.PasswordType)().match(/^[a-zA-Z0-9_-]{5,20}$/);
// bad regex!
expect(pw.valid("password")).toBe(true);
expect(pw.valid("Pa$$w0rd")).toBe(false);
});
test("regex does not match", async () => {
let pw = (0, password_1.PasswordType)().doesNotMatch(/^[a-zA-Z0-9_-]{5,20}$/);
// bad regex!
expect(pw.valid("password")).toBe(false);
expect(pw.valid("Pa$$w0rd")).toBe(true);
});
});
describe("password format", () => {
async function testPw(password, cost) {
let p = pw();
if (cost) {
p = pw().cost(cost);
}
let hash = await p.format(password);
expect(hash).not.toEqual(password);
// default cost
expect(bcrypt.getRounds(hash)).toBe(cost || 10);
expect(bcrypt.compareSync(password, hash)).toBe(true);
}
test("format no cost", async () => {
await testPw("Pa$$w0rd");
});
test("format no cost", async () => {
await testPw("Pa$$w0rd", 12); // not going higher than this because we want test to be fast enough
});
});
test("log value", () => {
expect(pw().logValue("password")).toEqual("********");
});