Skip to content

Commit c2297cf

Browse files
committed
test cases for bcrypt and scrypt
1 parent d5fa4c5 commit c2297cf

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
77
"scripts": {
8-
"test": "mocha -r ts-node/register ./test/**/*.ts --timeout 20000 --recursive",
8+
"test": "mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
99
"build": "napi build --release && tsc",
1010
"prepare": "npm run build"
1111
},
@@ -26,14 +26,12 @@
2626
},
2727
"devDependencies": {
2828
"@napi-rs/cli": "^2.17.0",
29-
"@types/chai": "^4.3.5",
30-
"@types/mocha": "^10.0.1",
29+
"@types/chai": "^4.3.11",
30+
"@types/mocha": "^10.0.6",
3131
"@types/node-fetch": "^2.6.3",
32-
"chai": "^4.3.7",
32+
"chai": "^4.4.1",
3333
"mocha": "^10.2.0",
3434
"ts-node": "^10.9.1",
3535
"typescript": "^5.0.3"
36-
},
37-
"dependencies": {
3836
}
3937
}

test-ts/password-hasher-test.spec.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { assert, expect } from "chai";
2+
import { BCryptWrapper } from "../src-ts/password-hashers/index";
3+
import { ScryptWrapper } from "../src-ts/password-hashers/index";
4+
import {
5+
PasswordHasherFactory,
6+
PasswordHasherType,
7+
} from "../src-ts/password-hashers/";
8+
9+
describe("Bcrypt Tests", () => {
10+
it("hash", () => {
11+
const hasher: BCryptWrapper = new BCryptWrapper();
12+
const password: string = "ThisOneBadPassword!@";
13+
const hashedPassword: string = hasher.hashPassword(password);
14+
assert.notEqual(hashedPassword, password);
15+
});
16+
17+
it("verify pass", () => {
18+
const hasher: BCryptWrapper = new BCryptWrapper();
19+
const password: string = "NotThisPassword!@";
20+
const hashedPassword: string = hasher.hashPassword(password);
21+
const isValid: boolean = hasher.verifyPassword(hashedPassword, password);
22+
expect(isValid).to.equal(true);
23+
});
24+
25+
it("verify fail", () => {
26+
const hasher: BCryptWrapper = new BCryptWrapper();
27+
const password: string = "NotThisPassword!@";
28+
const hashedPassword: string = hasher.hashPassword(password);
29+
const isValid: boolean = hasher.verifyPassword(
30+
hashedPassword,
31+
"ThesePasswordsDoNotMatch",
32+
);
33+
expect(isValid).to.equal(false);
34+
});
35+
});
36+
37+
describe("Scrypt Tests", () => {
38+
it("hash with factory", () => {
39+
const hasher: ScryptWrapper = PasswordHasherFactory.getHasher(
40+
PasswordHasherType.Scrypt,
41+
);
42+
const password: string = "ScryptRocks";
43+
const hashed: string = hasher.hashPassword(password);
44+
assert.notEqual(password, hashed);
45+
});
46+
47+
it("verify pass with factory", () => {
48+
const hasher: ScryptWrapper = PasswordHasherFactory.getHasher(
49+
PasswordHasherType.Scrypt,
50+
);
51+
const password: string = "ScryptRocks1231231";
52+
const hashed: string = hasher.hashPassword(password);
53+
const verified: boolean = hasher.verifyPassword(hashed, password);
54+
assert.isTrue(verified);
55+
});
56+
57+
it("verify fail with factory", () => {
58+
const hasher: ScryptWrapper = PasswordHasherFactory.getHasher(
59+
PasswordHasherType.Scrypt,
60+
);
61+
const password: string = "ScryptRocksSomeGarbageText";
62+
const hashed: string = hasher.hashPassword(password);
63+
const verified: boolean = hasher.verifyPassword(
64+
hashed,
65+
"make this fail, its not the same",
66+
);
67+
assert.isNotTrue(verified);
68+
});
69+
});

0 commit comments

Comments
 (0)