mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 09:11:26 +00:00
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: 2dee8e1174
Co-authored-by: mudrii <220262+mudrii@users.noreply.github.com>
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Reviewed-by: @obviyus
162 lines
4.0 KiB
TypeScript
162 lines
4.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import type { OpenClawConfig } from "../../config/config.js";
|
|
import { resolveApiKeyForProfile } from "./oauth.js";
|
|
import type { AuthProfileStore } from "./types.js";
|
|
|
|
function cfgFor(profileId: string, provider: string, mode: "api_key" | "token" | "oauth") {
|
|
return {
|
|
auth: {
|
|
profiles: {
|
|
[profileId]: { provider, mode },
|
|
},
|
|
},
|
|
} satisfies OpenClawConfig;
|
|
}
|
|
|
|
describe("resolveApiKeyForProfile config compatibility", () => {
|
|
it("accepts token credentials when config mode is oauth", async () => {
|
|
const profileId = "anthropic:token";
|
|
const store: AuthProfileStore = {
|
|
version: 1,
|
|
profiles: {
|
|
[profileId]: {
|
|
type: "token",
|
|
provider: "anthropic",
|
|
token: "tok-123",
|
|
},
|
|
},
|
|
};
|
|
|
|
const result = await resolveApiKeyForProfile({
|
|
cfg: cfgFor(profileId, "anthropic", "oauth"),
|
|
store,
|
|
profileId,
|
|
});
|
|
expect(result).toEqual({
|
|
apiKey: "tok-123",
|
|
provider: "anthropic",
|
|
email: undefined,
|
|
});
|
|
});
|
|
|
|
it("rejects token credentials when config mode is api_key", async () => {
|
|
const profileId = "anthropic:token";
|
|
const store: AuthProfileStore = {
|
|
version: 1,
|
|
profiles: {
|
|
[profileId]: {
|
|
type: "token",
|
|
provider: "anthropic",
|
|
token: "tok-123",
|
|
},
|
|
},
|
|
};
|
|
|
|
const result = await resolveApiKeyForProfile({
|
|
cfg: cfgFor(profileId, "anthropic", "api_key"),
|
|
store,
|
|
profileId,
|
|
});
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("accepts oauth credentials when config mode is token (bidirectional compat)", async () => {
|
|
const profileId = "anthropic:oauth";
|
|
const store: AuthProfileStore = {
|
|
version: 1,
|
|
profiles: {
|
|
[profileId]: {
|
|
type: "oauth",
|
|
provider: "anthropic",
|
|
access: "access-123",
|
|
refresh: "refresh-123",
|
|
expires: Date.now() + 60_000,
|
|
},
|
|
},
|
|
};
|
|
|
|
const result = await resolveApiKeyForProfile({
|
|
cfg: cfgFor(profileId, "anthropic", "token"),
|
|
store,
|
|
profileId,
|
|
});
|
|
// token ↔ oauth are bidirectionally compatible bearer-token auth paths.
|
|
expect(result).toEqual({
|
|
apiKey: "access-123",
|
|
provider: "anthropic",
|
|
email: undefined,
|
|
});
|
|
});
|
|
|
|
it("rejects credentials when provider does not match config", async () => {
|
|
const profileId = "anthropic:token";
|
|
const store: AuthProfileStore = {
|
|
version: 1,
|
|
profiles: {
|
|
[profileId]: {
|
|
type: "token",
|
|
provider: "anthropic",
|
|
token: "tok-123",
|
|
},
|
|
},
|
|
};
|
|
|
|
const result = await resolveApiKeyForProfile({
|
|
cfg: cfgFor(profileId, "openai", "token"),
|
|
store,
|
|
profileId,
|
|
});
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("resolveApiKeyForProfile token expiry handling", () => {
|
|
it("returns null for expired token credentials", async () => {
|
|
const profileId = "anthropic:token-expired";
|
|
const store: AuthProfileStore = {
|
|
version: 1,
|
|
profiles: {
|
|
[profileId]: {
|
|
type: "token",
|
|
provider: "anthropic",
|
|
token: "tok-expired",
|
|
expires: Date.now() - 1_000,
|
|
},
|
|
},
|
|
};
|
|
|
|
const result = await resolveApiKeyForProfile({
|
|
cfg: cfgFor(profileId, "anthropic", "token"),
|
|
store,
|
|
profileId,
|
|
});
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("accepts token credentials when expires is 0", async () => {
|
|
const profileId = "anthropic:token-no-expiry";
|
|
const store: AuthProfileStore = {
|
|
version: 1,
|
|
profiles: {
|
|
[profileId]: {
|
|
type: "token",
|
|
provider: "anthropic",
|
|
token: "tok-123",
|
|
expires: 0,
|
|
},
|
|
},
|
|
};
|
|
|
|
const result = await resolveApiKeyForProfile({
|
|
cfg: cfgFor(profileId, "anthropic", "token"),
|
|
store,
|
|
profileId,
|
|
});
|
|
expect(result).toEqual({
|
|
apiKey: "tok-123",
|
|
provider: "anthropic",
|
|
email: undefined,
|
|
});
|
|
});
|
|
});
|