test(server/utils): add tests for randomSecureToken

(bit ugly I have to say, as we are essentially partially testing "crypto" module here,
probably should be instead replaced by a version that mocks crypto module and checks, if the called functions match the expectations)
This commit is contained in:
Panagiotis Papadopoulos 2025-01-31 22:55:58 +01:00
parent a4ce2ddd5e
commit 9f2dd21865

View File

@ -24,7 +24,25 @@ describe("#randomString", () => {
}); });
describe.todo("#randomSecureToken", () => {}); describe("#randomSecureToken", () => {
// base64 -> 4 * (bytes/3) length -> if padding and rounding up is ignored for simplicity
// https://stackoverflow.com/a/13378842
const byteToBase64Length = (bytes: number) => 4 * (bytes / 3);
it("should return a string and use 32 bytes by default", () => {
const result = utils.randomSecureToken();
expect(result).toBeTypeOf("string");
expect(result.length).toBeGreaterThanOrEqual(byteToBase64Length(32));
});
it("should return a string and use passed byte length", () => {
const bytes = 16;
const result = utils.randomSecureToken(bytes);
expect(result).toBeTypeOf("string");
expect(result.length).toBeGreaterThanOrEqual(byteToBase64Length(bytes));
expect(result.length).toBeLessThan(44); // default argument uses 32 bytes -> which translates to 44 base64 legal chars
});
});
describe.todo("#md5", () => {}); describe.todo("#md5", () => {});