|
1 | 1 | import { assertEquals } from "./test_deps.ts";
|
2 |
| -import { checkPassword } from "./mod.ts"; |
| 2 | +import { |
| 3 | + checkPassword, |
| 4 | + checkPasswordWithResult, |
| 5 | + IParams, |
| 6 | + VerificationResult, |
| 7 | +} from "./mod.ts"; |
3 | 8 |
|
4 | 9 | Deno.test("Test with defaults", () => {
|
5 |
| - assertEquals(checkPassword({ password: "random123." }), true); |
| 10 | + assertResult({ password: "random123." }, { |
| 11 | + isValid: true, |
| 12 | + }); |
6 | 13 | });
|
7 | 14 |
|
8 | 15 | Deno.test("Test for minimum length", () => {
|
9 |
| - assertEquals(checkPassword({ password: "random123", minLen: 10 }), false); |
| 16 | + assertResult({ password: "random123", minLen: 10 }, { |
| 17 | + isValid: false, |
| 18 | + reason: "The password should contain at least 10 characters", |
| 19 | + }); |
10 | 20 | });
|
11 | 21 |
|
12 | 22 | Deno.test("Test for maximum length", () => {
|
13 |
| - assertEquals( |
14 |
| - checkPassword({ password: "random123", minLen: 2, maxLen: 5 }), |
15 |
| - false, |
16 |
| - ); |
| 23 | + assertResult({ password: "radnom123", minLen: 2, maxLen: 5 }, { |
| 24 | + isValid: false, |
| 25 | + reason: "The password should contain at most 5 characters", |
| 26 | + }); |
17 | 27 | });
|
18 | 28 |
|
19 | 29 | Deno.test("Test for numbers", () => {
|
20 |
| - assertEquals( |
21 |
| - checkPassword({ |
22 |
| - password: "random", |
23 |
| - minLen: 2, |
24 |
| - maxLen: 10, |
25 |
| - containsNum: true, |
26 |
| - }), |
27 |
| - false, |
28 |
| - ); |
| 30 | + assertResult({ |
| 31 | + password: "random", |
| 32 | + minLen: 2, |
| 33 | + maxLen: 10, |
| 34 | + containsNum: true, |
| 35 | + }, { |
| 36 | + isValid: false, |
| 37 | + reason: "The password should contain at least one digit", |
| 38 | + }); |
29 | 39 | });
|
30 | 40 |
|
31 | 41 | Deno.test("Test for special characters", () => {
|
32 |
| - assertEquals( |
33 |
| - checkPassword({ |
34 |
| - password: "random123", |
35 |
| - minLen: 2, |
36 |
| - maxLen: 10, |
37 |
| - containsNum: true, |
38 |
| - containsSpecialChar: true, |
39 |
| - }), |
40 |
| - false, |
41 |
| - ); |
| 42 | + assertResult({ |
| 43 | + password: "random123", |
| 44 | + minLen: 2, |
| 45 | + maxLen: 10, |
| 46 | + containsNum: true, |
| 47 | + containsSpecialChar: true, |
| 48 | + }, { |
| 49 | + isValid: false, |
| 50 | + reason: "The password should contain at least one special character", |
| 51 | + }); |
42 | 52 | });
|
43 | 53 |
|
44 | 54 | Deno.test("Test for alphabets", () => {
|
45 |
| - assertEquals( |
46 |
| - checkPassword({ |
47 |
| - password: "123456.!", |
48 |
| - minLen: 2, |
49 |
| - maxLen: 10, |
50 |
| - containsNum: true, |
51 |
| - containsSpecialChar: true, |
52 |
| - containsAlphabet: true, |
53 |
| - }), |
54 |
| - false, |
55 |
| - ); |
| 55 | + assertResult({ |
| 56 | + password: "123456.!", |
| 57 | + minLen: 2, |
| 58 | + maxLen: 10, |
| 59 | + containsNum: true, |
| 60 | + containsSpecialChar: true, |
| 61 | + containsAlphabet: true, |
| 62 | + }, { |
| 63 | + isValid: false, |
| 64 | + reason: "The password should contain at least one letter", |
| 65 | + }); |
56 | 66 | });
|
57 | 67 |
|
58 | 68 | Deno.test("Test for all options", () => {
|
59 |
| - assertEquals( |
60 |
| - checkPassword({ |
61 |
| - password: "abc123456.!", |
62 |
| - minLen: 2, |
63 |
| - maxLen: 12, |
64 |
| - containsNum: true, |
65 |
| - containsSpecialChar: true, |
66 |
| - containsAlphabet: true, |
67 |
| - }), |
68 |
| - true, |
69 |
| - ); |
| 69 | + assertResult({ |
| 70 | + password: "abc123456.!", |
| 71 | + minLen: 2, |
| 72 | + maxLen: 12, |
| 73 | + containsNum: true, |
| 74 | + containsSpecialChar: true, |
| 75 | + containsAlphabet: true, |
| 76 | + }, { |
| 77 | + isValid: true, |
| 78 | + }); |
70 | 79 | });
|
71 | 80 |
|
72 | 81 | Deno.test("Test for common password - Positive", () => {
|
73 |
| - assertEquals( |
74 |
| - checkPassword({ |
75 |
| - password: "batman123.?", |
76 |
| - minLen: 2, |
77 |
| - maxLen: 12, |
78 |
| - checkWithCommonPasswords: true, |
79 |
| - }), |
80 |
| - true, |
81 |
| - ); |
| 82 | + assertResult({ |
| 83 | + password: "batman123.?", |
| 84 | + minLen: 2, |
| 85 | + maxLen: 12, |
| 86 | + checkWithCommonPasswords: true, |
| 87 | + }, { |
| 88 | + isValid: true, |
| 89 | + }); |
82 | 90 | });
|
83 | 91 |
|
84 | 92 | Deno.test("Test for common password - Negative", () => {
|
85 |
| - assertEquals( |
86 |
| - checkPassword({ |
87 |
| - password: "batman", |
88 |
| - minLen: 2, |
89 |
| - maxLen: 12, |
90 |
| - containsNum: false, |
91 |
| - containsSpecialChar: false, |
92 |
| - containsAlphabet: true, |
93 |
| - checkWithCommonPasswords: true, |
94 |
| - }), |
95 |
| - false, |
96 |
| - ); |
| 93 | + assertResult({ |
| 94 | + password: "batman", |
| 95 | + minLen: 2, |
| 96 | + maxLen: 12, |
| 97 | + containsNum: false, |
| 98 | + containsSpecialChar: false, |
| 99 | + containsAlphabet: true, |
| 100 | + checkWithCommonPasswords: true, |
| 101 | + }, { |
| 102 | + isValid: false, |
| 103 | + reason: "The password should not be too common", |
| 104 | + }); |
97 | 105 | });
|
| 106 | + |
| 107 | +/** |
| 108 | + * Test both `checkPassword` and `checkPasswordWithResult` against the provided case with custom assert function |
| 109 | + * @param params |
| 110 | + * @param expectedResult |
| 111 | + */ |
| 112 | +function assertResult(params: IParams, expectedResult: VerificationResult) { |
| 113 | + // Invoking checkPassword() |
| 114 | + assertEquals(checkPassword(params), expectedResult.isValid); |
| 115 | + |
| 116 | + // Invoking checkPasswordWithResult() |
| 117 | + assertEquals(checkPasswordWithResult(params), expectedResult); |
| 118 | +} |
0 commit comments