|
|
|
|
@ -1,7 +1,11 @@
|
|
|
|
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
import type { ResolvedGatewayAuth } from "./auth.js";
|
|
|
|
|
import { authorizeGatewayBearerRequestOrReply } from "./http-auth-helpers.js";
|
|
|
|
|
import {
|
|
|
|
|
authorizeGatewayBearerRequestOrReply,
|
|
|
|
|
resolveGatewayRequestedOperatorScopes,
|
|
|
|
|
} from "./http-auth-helpers.js";
|
|
|
|
|
import { CLI_DEFAULT_OPERATOR_SCOPES } from "./method-scopes.js";
|
|
|
|
|
|
|
|
|
|
vi.mock("./auth.js", () => ({
|
|
|
|
|
authorizeHttpGatewayConnect: vi.fn(),
|
|
|
|
|
@ -13,11 +17,12 @@ vi.mock("./http-common.js", () => ({
|
|
|
|
|
|
|
|
|
|
vi.mock("./http-utils.js", () => ({
|
|
|
|
|
getBearerToken: vi.fn(),
|
|
|
|
|
getHeader: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const { authorizeHttpGatewayConnect } = await import("./auth.js");
|
|
|
|
|
const { sendGatewayAuthFailure } = await import("./http-common.js");
|
|
|
|
|
const { getBearerToken } = await import("./http-utils.js");
|
|
|
|
|
const { getBearerToken, getHeader } = await import("./http-utils.js");
|
|
|
|
|
|
|
|
|
|
describe("authorizeGatewayBearerRequestOrReply", () => {
|
|
|
|
|
const bearerAuth = {
|
|
|
|
|
@ -70,3 +75,60 @@ describe("authorizeGatewayBearerRequestOrReply", () => {
|
|
|
|
|
expect(vi.mocked(sendGatewayAuthFailure)).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("resolveGatewayRequestedOperatorScopes", () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns CLI_DEFAULT_OPERATOR_SCOPES when header is absent", () => {
|
|
|
|
|
vi.mocked(getHeader).mockReturnValue(undefined);
|
|
|
|
|
const req = {} as IncomingMessage;
|
|
|
|
|
const scopes = resolveGatewayRequestedOperatorScopes(req);
|
|
|
|
|
expect(scopes).toEqual(CLI_DEFAULT_OPERATOR_SCOPES);
|
|
|
|
|
// Returned array must be a copy, not the original constant.
|
|
|
|
|
expect(scopes).not.toBe(CLI_DEFAULT_OPERATOR_SCOPES);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns empty array when header is present but empty", () => {
|
|
|
|
|
vi.mocked(getHeader).mockReturnValue("");
|
|
|
|
|
const req = {} as IncomingMessage;
|
|
|
|
|
const scopes = resolveGatewayRequestedOperatorScopes(req);
|
|
|
|
|
expect(scopes).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns empty array when header is present but only whitespace", () => {
|
|
|
|
|
vi.mocked(getHeader).mockReturnValue(" ");
|
|
|
|
|
const req = {} as IncomingMessage;
|
|
|
|
|
const scopes = resolveGatewayRequestedOperatorScopes(req);
|
|
|
|
|
expect(scopes).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("parses comma-separated scopes from header", () => {
|
|
|
|
|
vi.mocked(getHeader).mockReturnValue("operator.write,operator.read");
|
|
|
|
|
const req = {} as IncomingMessage;
|
|
|
|
|
const scopes = resolveGatewayRequestedOperatorScopes(req);
|
|
|
|
|
expect(scopes).toEqual(["operator.write", "operator.read"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("trims whitespace around individual scopes", () => {
|
|
|
|
|
vi.mocked(getHeader).mockReturnValue(" operator.write , operator.read ");
|
|
|
|
|
const req = {} as IncomingMessage;
|
|
|
|
|
const scopes = resolveGatewayRequestedOperatorScopes(req);
|
|
|
|
|
expect(scopes).toEqual(["operator.write", "operator.read"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("filters out empty segments from trailing commas", () => {
|
|
|
|
|
vi.mocked(getHeader).mockReturnValue("operator.write,,operator.read,");
|
|
|
|
|
const req = {} as IncomingMessage;
|
|
|
|
|
const scopes = resolveGatewayRequestedOperatorScopes(req);
|
|
|
|
|
expect(scopes).toEqual(["operator.write", "operator.read"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns single scope when only one is declared", () => {
|
|
|
|
|
vi.mocked(getHeader).mockReturnValue("operator.approvals");
|
|
|
|
|
const req = {} as IncomingMessage;
|
|
|
|
|
const scopes = resolveGatewayRequestedOperatorScopes(req);
|
|
|
|
|
expect(scopes).toEqual(["operator.approvals"]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|