-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Johan
committed
Feb 5, 2024
1 parent
898a31f
commit 384add3
Showing
3 changed files
with
70 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import test, { describe } from "node:test"; | ||
import * as assert from "assert"; | ||
|
||
import { | ||
generateSecret, | ||
decodeSecret, | ||
generateTOTP, | ||
verifyTOTP, | ||
} from "./index.js"; | ||
|
||
describe("Generated test suite", () => { | ||
// Test generateSecret | ||
test("generateSecret", (t) => { | ||
const secret = generateSecret(); | ||
assert.strictEqual(typeof secret, "string", "Secret should be a string"); | ||
assert.strictEqual( | ||
secret.length, | ||
32, | ||
"Default secret length should be 32 characters after Base32 encoding" | ||
); | ||
}); | ||
|
||
// Test decodeSecret | ||
test("decodeSecret", (t) => { | ||
const secret = "JBSWY3DPEHPK3PXP"; | ||
const decoded = decodeSecret(secret); | ||
assert.strictEqual( | ||
Buffer.isBuffer(Buffer.from(decoded)), | ||
true, | ||
"Decoded secret should be a byte array" | ||
); | ||
}); | ||
|
||
// Test | ||
test("generateTOTP", (t) => { | ||
const secret = generateSecret(); | ||
const totp = generateTOTP(secret); | ||
assert.strictEqual(typeof totp, "number", "TOTP should be a number"); | ||
assert.strictEqual( | ||
totp >= 0 && totp < 1000000, | ||
true, | ||
"TOTP should be a 6-digit number" | ||
); | ||
}); | ||
|
||
// Test | ||
test("verifyTOTP", (t) => { | ||
const secret = generateSecret(); | ||
const totp = generateTOTP(secret); | ||
const isVerified = verifyTOTP(totp, secret); | ||
assert.strictEqual( | ||
isVerified, | ||
true, | ||
"TOTP verification should succeed with the correct token" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import test from "node:test"; | ||
import * as assert from "assert"; | ||
import * as U from "./utils"; | ||
|
||
test("secretFromUrl", () => { | ||
const url = "otpauth://totp/Digis?secret=JTYH5G4IZPVMSQK3CVF5DH6IT3HDV3NI&algorithm=SHA256&digits=6&period=1"; | ||
const url = | ||
"otpauth://totp/Digis?secret=JTYH5G4IZPVMSQK3CVF5DH6IT3HDV3NI&algorithm=SHA256&digits=6&period=1"; | ||
const secret = "JTYH5G4IZPVMSQK3CVF5DH6IT3HDV3NI"; | ||
|
||
expect(U.secretFromUrl(url)).toEqual(secret); | ||
assert.strictEqual(U.secretFromUrl(url), secret); | ||
}); | ||
|