mirror of https://github.com/openclaw/openclaw.git
test: tighten shared auth store coverage
This commit is contained in:
parent
6a9e141c7a
commit
2fe4c4f8e5
|
|
@ -50,6 +50,36 @@ describe("device-auth-store", () => {
|
||||||
).toBeNull();
|
).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns null for missing stores and malformed token entries", () => {
|
||||||
|
expect(
|
||||||
|
loadDeviceAuthTokenFromStore({
|
||||||
|
adapter: createAdapter().adapter,
|
||||||
|
deviceId: "device-1",
|
||||||
|
role: "operator",
|
||||||
|
}),
|
||||||
|
).toBeNull();
|
||||||
|
|
||||||
|
const { adapter } = createAdapter({
|
||||||
|
version: 1,
|
||||||
|
deviceId: "device-1",
|
||||||
|
tokens: {
|
||||||
|
operator: {
|
||||||
|
token: 123 as unknown as string,
|
||||||
|
role: "operator",
|
||||||
|
scopes: [],
|
||||||
|
updatedAtMs: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
loadDeviceAuthTokenFromStore({
|
||||||
|
adapter,
|
||||||
|
deviceId: "device-1",
|
||||||
|
role: "operator",
|
||||||
|
}),
|
||||||
|
).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
it("stores normalized roles and deduped sorted scopes while preserving same-device tokens", () => {
|
it("stores normalized roles and deduped sorted scopes while preserving same-device tokens", () => {
|
||||||
vi.spyOn(Date, "now").mockReturnValue(1234);
|
vi.spyOn(Date, "now").mockReturnValue(1234);
|
||||||
const { adapter, writes, readStore } = createAdapter({
|
const { adapter, writes, readStore } = createAdapter({
|
||||||
|
|
@ -130,6 +160,44 @@ describe("device-auth-store", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("overwrites existing entries for the same normalized role", () => {
|
||||||
|
vi.spyOn(Date, "now").mockReturnValue(2222);
|
||||||
|
const { adapter, readStore } = createAdapter({
|
||||||
|
version: 1,
|
||||||
|
deviceId: "device-1",
|
||||||
|
tokens: {
|
||||||
|
operator: {
|
||||||
|
token: "old-token",
|
||||||
|
role: "operator",
|
||||||
|
scopes: ["operator.read"],
|
||||||
|
updatedAtMs: 10,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const entry = storeDeviceAuthTokenInStore({
|
||||||
|
adapter,
|
||||||
|
deviceId: "device-1",
|
||||||
|
role: " operator ",
|
||||||
|
token: "new-token",
|
||||||
|
scopes: ["operator.write"],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(entry).toEqual({
|
||||||
|
token: "new-token",
|
||||||
|
role: "operator",
|
||||||
|
scopes: ["operator.write"],
|
||||||
|
updatedAtMs: 2222,
|
||||||
|
});
|
||||||
|
expect(readStore()).toEqual({
|
||||||
|
version: 1,
|
||||||
|
deviceId: "device-1",
|
||||||
|
tokens: {
|
||||||
|
operator: entry,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("avoids writes when clearing missing roles or mismatched devices", () => {
|
it("avoids writes when clearing missing roles or mismatched devices", () => {
|
||||||
const missingRole = createAdapter({
|
const missingRole = createAdapter({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue