Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
53 lines
1.8 KiB
JavaScript
53 lines
1.8 KiB
JavaScript
"use strict"
|
|
|
|
const test = require("node:test")
|
|
const assert = require("node:assert/strict")
|
|
const { adminCredentialsMatch, createSessionToken, createApiKey, hashSecret, parseCookies } = require("../lib/security.cjs")
|
|
|
|
test("adminCredentialsMatch returns true for correct credentials", (t) => {
|
|
assert.equal(adminCredentialsMatch("admin", "secret-value-123", {
|
|
username: "admin", password: "secret-value-123"
|
|
}), true)
|
|
})
|
|
|
|
test("adminCredentialsMatch returns false for wrong password", (t) => {
|
|
assert.equal(adminCredentialsMatch("admin", "wrong-value-123", {
|
|
username: "admin", password: "secret-value-123"
|
|
}), false)
|
|
})
|
|
|
|
test("adminCredentialsMatch returns false for wrong username", (t) => {
|
|
assert.equal(adminCredentialsMatch("notadmin", "secret-value-123", {
|
|
username: "admin", password: "secret-value-123"
|
|
}), false)
|
|
})
|
|
|
|
test("createApiKey returns plaintext matching hms_ prefix", (t) => {
|
|
const key = createApiKey()
|
|
assert.match(key.plaintext, /^hms_[A-Za-z0-9_-]{40,}$/)
|
|
assert.equal(key.suffix.length, 4)
|
|
assert.equal(key.suffix, key.plaintext.slice(-4))
|
|
})
|
|
|
|
test("createSessionToken returns 64-char hex hash", (t) => {
|
|
const token = createSessionToken()
|
|
assert.equal(token.hash.length, 64)
|
|
assert.match(token.plaintext, /^[A-Za-z0-9_-]+$/)
|
|
})
|
|
|
|
test("hashSecret produces consistent output", (t) => {
|
|
assert.equal(hashSecret("hello"), hashSecret("hello"))
|
|
assert.notEqual(hashSecret("hello"), hashSecret("world"))
|
|
})
|
|
|
|
test("parseCookies parses a multi-value cookie header", (t) => {
|
|
const result = parseCookies("a=1; hermes_admin=abc")
|
|
assert.deepEqual(result, { a: "1", hermes_admin: "abc" })
|
|
})
|
|
|
|
test("parseCookies returns empty object for null/undefined", (t) => {
|
|
assert.deepEqual(parseCookies(null), {})
|
|
assert.deepEqual(parseCookies(undefined), {})
|
|
assert.deepEqual(parseCookies(""), {})
|
|
})
|