Files
shrimp-server/test/PasswordHasher.test.ts
wc24 ca283c6733 v0.5.0: 重新组织目录结构,按职责拆分
- Shrimp.ts (875 行) 按类拆分到 Shrimp/(7 文件: Shrimp, ShrimpModule, ShrimpTableModule, ShrimpPower, ShrimpCrud, UserRoleModule, OperationLogModule)
- forMongo/: MongoShrimpCrud + MongoShrimp(原 ShrimpServer 的 mongo 装配部分,无 express)
- forExpress/: buildShrimpRouter + jwtAuth + types
- main/: index(0 依赖核心) + expmon(express+mongo 整合,导出 ShrimpServer) + types(纯协议)
- 入口子路径 server → expmon(express+mongo 整合方案命名更准确)
- 删除冗余的 index.ts / core.ts(合并为 main/index.ts)
2026-05-23 19:44:02 +08:00

65 lines
3.2 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest"
import { Shrimp, plainHasher, bcryptHasher, UserModule, MemoryShrimpCrud, ShrimpFieldTypes } from "../src/main"
import type { IPasswordHasher } from "../src/main"
describe("PasswordHasher 抽象", () => {
const original = Shrimp.passwordHasher
afterEach(() => {
Shrimp.passwordHasher = original
})
it("默认是 bcryptHasher", () => {
expect(Shrimp.passwordHasher).toBe(bcryptHasher)
})
it("plainHasher: hash 原样返回,verify 字符串等值", async () => {
expect(await plainHasher.hash("secret")).toBe("secret")
expect(await plainHasher.verify("secret", "secret")).toBe(true)
expect(await plainHasher.verify("secret", "other")).toBe(false)
})
it("通过替换 Shrimp.passwordHasher 让 Password 字段走自定义哈希", async () => {
const calls: string[] = []
const custom: IPasswordHasher = {
async hash(plain) { calls.push("hash:" + plain); return "X:" + plain },
async verify(plain, hashed) { calls.push("verify:" + plain + ":" + hashed); return hashed === "X:" + plain },
}
Shrimp.passwordHasher = custom
const shrimp = new Shrimp()
const userCrud = new MemoryShrimpCrud<{ _id: string; username: string; password: string; active: boolean; roleId?: string[] }>()
const userModule = new UserModule(shrimp, userCrud as any, [
{ key: "username", label: "用户名", type: ShrimpFieldTypes.String, required: true, editable: "addOnly" },
{ key: "password", label: "密码", type: ShrimpFieldTypes.Password, required: true, tableHide: true },
{ key: "active", label: "启用", type: ShrimpFieldTypes.Boolean, defaultValue: true },
])
await userModule.add({ username: "alice", password: "pw123", active: true, roleId: [] } as any)
const stored = (await userCrud.list({})).list[0]
expect(stored.password).toBe("X:pw123")
expect(calls).toContain("hash:pw123")
const ok = await userModule.getUser("alice", "pw123")
expect(ok.username).toBe("alice")
expect(calls.some(c => c.startsWith("verify:pw123:X:pw123"))).toBe(true)
})
it("plainHasher 端到端: 在不装 bcrypt 的场景下完成登录校验", async () => {
Shrimp.passwordHasher = plainHasher
const shrimp = new Shrimp()
const userCrud = new MemoryShrimpCrud<{ _id: string; username: string; password: string; active: boolean; roleId?: string[] }>()
const userModule = new UserModule(shrimp, userCrud as any, [
{ key: "username", label: "用户名", type: ShrimpFieldTypes.String, required: true, editable: "addOnly" },
{ key: "password", label: "密码", type: ShrimpFieldTypes.Password, required: true, tableHide: true },
{ key: "active", label: "启用", type: ShrimpFieldTypes.Boolean, defaultValue: true },
])
await userModule.add({ username: "bob", password: "open", active: true, roleId: [] } as any)
const user = await userModule.getUser("bob", "open")
expect(user.username).toBe("bob")
await expect(userModule.getUser("bob", "wrong")).rejects.toThrow()
})
})