Skip to content

Commit

Permalink
move to node test runner (no jest)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Feb 5, 2024
1 parent 898a31f commit 384add3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
57 changes: 57 additions & 0 deletions src/generated.tests.ts
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"
);
});
});
14 changes: 8 additions & 6 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import test, { describe } from "node:test";
import * as assert from "assert";

import * as I from "./index";
describe("Time-based OTP", () => {
const l = 20;
const secret = I.generateSecret(l);
test("secret generation", () => {
expect(secret.length).toEqual(32);
assert.strictEqual(secret.length, 32);
});

const token = I.generateTOTP(secret);

test("verify token", () => {
expect(I.verifyTOTP(token, secret)).toEqual(true);
assert.strictEqual(I.verifyTOTP(token, secret), true);
});
});

test("to QR string", () => {
const s =
"otpauth://totp/myname?secret=asecret&algorithm=SHA256&digits=6&period=15";
expect(I.toQRString("myname", "asecret")).toEqual(s);
assert.strictEqual(I.toQRString("myname", "asecret"), s);
});

//this is a test to see if the integration actually wors with the google auth
Expand All @@ -30,6 +33,5 @@ test("manual testing", () => {
console.log(I.toQRString("test", secret));
const t = 764975;
expect(I.verifyTOTP(t, secret)).toEqual(true);
});
*/
assert.strictEqual(I.verifyTOTP(t, secret), true);
});*/
8 changes: 5 additions & 3 deletions src/utils.test.ts
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);
});

0 comments on commit 384add3

Please sign in to comment.