2026-11-14 16:57:47 -08:00
|
|
|
import { describe, it, expect } from "vitest";
|
2026-11-17 12:16:55 -08:00
|
|
|
import { awsErrorMessage, looksDenied, toAwsError } from "./aws-error";
|
2026-11-14 16:57:47 -08:00
|
|
|
|
|
|
|
|
describe("toAwsError", () => {
|
|
|
|
|
it("passes a tagged AwsError through", () => {
|
|
|
|
|
const e = { kind: "sso_expired", profile: "dev" };
|
|
|
|
|
expect(toAwsError(e)).toBe(e);
|
|
|
|
|
});
|
2026-11-15 08:56:11 -08:00
|
|
|
it("passes a quota error through with its message", () => {
|
|
|
|
|
const e = { kind: "quota_exceeded", message: "An account limit was reached" };
|
|
|
|
|
expect(toAwsError(e)).toBe(e);
|
|
|
|
|
expect(awsErrorMessage(toAwsError(e))).toBe("An account limit was reached");
|
|
|
|
|
});
|
2026-11-14 16:57:47 -08:00
|
|
|
it("wraps strings and Errors as other", () => {
|
|
|
|
|
expect(toAwsError("boom")).toEqual({ kind: "other", message: "boom" });
|
|
|
|
|
expect(toAwsError(new Error("bad"))).toEqual({ kind: "other", message: "bad" });
|
|
|
|
|
});
|
|
|
|
|
it("rejects unknown kinds, inherited names and null", () => {
|
|
|
|
|
expect(toAwsError({ kind: "nope" }).kind).toBe("other");
|
|
|
|
|
expect(toAwsError({ kind: "toString" }).kind).toBe("other");
|
|
|
|
|
expect(toAwsError(null).kind).toBe("other");
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-11-17 12:16:55 -08:00
|
|
|
|
|
|
|
|
describe("access denied", () => {
|
|
|
|
|
it("shows AWS's message, naming the action when the message doesn't", () => {
|
|
|
|
|
expect(awsErrorMessage({ kind: "access_denied", action: "iam:CreateRole", message: "User: x is not authorized" })).toBe(
|
|
|
|
|
"AWS denied iam:CreateRole: User: x is not authorized",
|
|
|
|
|
);
|
|
|
|
|
expect(awsErrorMessage({ kind: "access_denied", action: "s3:PutObject", message: "denied s3:PutObject" })).toBe(
|
|
|
|
|
"AWS denied this request: denied s3:PutObject",
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
it("recognises refusals in OpenTofu output", () => {
|
|
|
|
|
expect(looksDenied("Error: creating IAM Role: AccessDenied: User is not authorized")).toBe(true);
|
|
|
|
|
expect(looksDenied("api error UnauthorizedOperation: You are not authorized")).toBe(true);
|
|
|
|
|
expect(looksDenied("Error: creating VPC: VpcLimitExceeded")).toBe(false);
|
|
|
|
|
expect(looksDenied(null)).toBe(false);
|
2026-11-17 12:19:04 -08:00
|
|
|
expect(looksDenied("blocked by an explicit deny in a service control policy")).toBe(false);
|
2026-11-17 12:16:55 -08:00
|
|
|
});
|
|
|
|
|
});
|