node-red-contrib-password-g.../test/PasswordGenerator_spec.ts

60 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-07-09 03:49:18 +02:00
import "mocha";
import { expect } from "chai";
2021-07-10 19:18:20 +02:00
import * as sinon from "sinon";
2021-07-11 09:03:32 +02:00
import { generatePassword, DisableOptions } from "../lib/PasswordGenerator";
2021-07-09 03:49:18 +02:00
2021-07-10 10:33:07 +02:00
describe("PasswordGenerator", () => {
2021-07-11 09:03:32 +02:00
afterEach(() => {
sinon.restore();
});
it("should throw an error when length is 4", (done) => {
generatePassword(4)
.then(() => done("Error didn't occur."))
.catch(() => done());
});
it("should return password when length is 5", async () => {
const result = await generatePassword(5);
expect(result).to.lengthOf(5);
});
[
{ regex: /0123456789/, title: "number" },
{ regex: / /, title: "space" },
{ regex: /abcdefghijklmnopqrstuvwxyz/, title: "lowercase" },
{ regex: /ABCDEFGHIJKLMNOPQRSTUVWXYZ/, title: "uppercase" },
{ regex: /!\"#\$%&'\(\)\*\+,-.\//, title: "special characters group 1" },
{ regex: /:;<=>\?@/, title: "special characters group 2" },
{ regex: /\[\\\]\^_`/, title: "special characters group 3" },
{ regex: /\{|\}~/, title: "special characters group 4" },
].forEach((testData) => {
it(`should contain ${testData.title} without option`, async () => {
const fakeData = Array.from(Array(256).keys());
sinon.stub(Array, "from").returns(fakeData);
const result = await generatePassword(256);
expect(result).to.match(testData.regex);
});
});
it("should not contain number when option number is true", async () => {
const fakeData = Array.from(Array(256).keys());
sinon.stub(Array, "from").returns(fakeData);
const result = await generatePassword(256, { number: true });
expect(result).not.to.match(/\d/);
});
it("should not contain space when option space is true", async () => {
const fakeData = Array.from(Array(256).keys());
sinon.stub(Array, "from").returns(fakeData);
const result = await generatePassword(256, { space: true });
expect(result).not.to.contain(" ");
});
it("should not contain special characters when option space is true", async () => {
const fakeData = Array.from(Array(256).keys());
sinon.stub(Array, "from").returns(fakeData);
const result = await generatePassword(256, { special: true });
expect(result).not.to.match(/[\[!\"#\$%&'\(\)\*\+,\-./:;<=>\?@\[\\\]\^_`{|}\]]/);
2021-07-09 03:49:18 +02:00
});
});