sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ IoT/ Policy.test.ts

import * as AWS from "@alchemy.run/aws";
import { Policy } from "@alchemy.run/aws/IoT";
import { describe, expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as iot from "@distilled.cloud/aws/iot";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";

const { test } = Test.make({ providers: AWS.providers() });

const assertPolicyGone = (policyName: string) =>
  iot.getPolicy({ policyName }).pipe(
    Effect.flatMap(() => Effect.fail(new Error(`policy ${policyName} still exists`))),
    Effect.catchTag("ResourceNotFoundException", () => Effect.void),
    Effect.retry({
      while: (e) => e instanceof Error,
      schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
    }),
  );

describe.sequential(
  "AWS.IoT.Policy",
  { tags: ["provider:aws", "provider:aws:iot", "live"] },
  () => {
    test.provider(
      "creates a policy, updates the document to a new default version, and deletes it",
      (stack) =>
        Effect.gen(function* () {
          yield* stack.destroy();

          const created = yield* stack.deploy(
            Effect.gen(function* () {
              const policy = yield* Policy("DevicePolicy", {
                policyDocument: {
                  Version: "2012-10-17",
                  Statement: [{ Effect: "Allow", Action: "iot:Connect", Resource: "*" }],
                },
              });
              return { policyName: policy.policyName };
            }),
          );

          const observed = yield* iot.getPolicy({ policyName: created.policyName });
          expect(observed.policyDocument).toContain("iot:Connect");
          const firstVersion = observed.defaultVersionId;

          // Update the document — creates a new default version.
          yield* stack.deploy(
            Effect.gen(function* () {
              yield* Policy("DevicePolicy", {
                policyDocument: {
                  Version: "2012-10-17",
                  Statement: [
                    { Effect: "Allow", Action: "iot:Connect", Resource: "*" },
                    { Effect: "Allow", Action: "iot:Publish", Resource: "*" },
                  ],
                },
              });
            }),
          );
          const updated = yield* iot.getPolicy({ policyName: created.policyName });
          expect(updated.policyDocument).toContain("iot:Publish");
          expect(updated.defaultVersionId).not.toEqual(firstVersion);

          yield* stack.destroy();
          yield* assertPolicyGone(created.policyName);
        }),
      { timeout: 180_000 },
    );
  },
);