| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as dlp from "@distilled.cloud/gcp/dlp_v2"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | dlp.getProjectsLocationsContentPolicies({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsContentPolicies on a missing policy fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const error = yield* Effect.flip( |
| 36 | dlp.getProjectsLocationsContentPolicies({ |
| 37 | name: `projects/${project}/locations/us/contentPolicies/alchemy-missing`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toEqual("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 }, |
| 45 | ); |
| 46 | |
| 47 | test.provider( |
| 48 | "create, update, and delete a content policy", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | const created = yield* stack.deploy( |
| 54 | Effect.gen(function* () { |
| 55 | return yield* GCP.DLP.ContentPolicy("BlockEmail", { |
| 56 | location: "us", |
| 57 | displayName: "email", |
| 58 | inspectConfig: { infoTypes: [{ name: "EMAIL_ADDRESS" }] }, |
| 59 | rules: [ |
| 60 | { |
| 61 | conditions: [ |
| 62 | { |
| 63 | infoTypeCondition: { |
| 64 | infoTypes: { infoTypeNames: ["EMAIL_ADDRESS"] }, |
| 65 | }, |
| 66 | }, |
| 67 | ], |
| 68 | action: { returnVerdict: "BLOCK" }, |
| 69 | }, |
| 70 | ], |
| 71 | defaultAction: { returnVerdict: "ALLOW" }, |
| 72 | }); |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(created.contentPolicyId).toEqual(expect.any(String)); |
| 77 | expect(created.location).toEqual("us"); |
| 78 | expect(created.name).toContain("/contentPolicies/"); |
| 79 | expect(created.displayName).toEqual("email"); |
| 80 | expect(created.rules.length).toBeGreaterThan(0); |
| 81 | |
| 82 | const fetched = yield* dlp.getProjectsLocationsContentPolicies({ |
| 83 | name: created.name, |
| 84 | }); |
| 85 | expect(fetched.name).toEqual(created.name); |
| 86 | expect(fetched.displayName).toContain("alchemy-"); |
| 87 | |
| 88 | const updated = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | return yield* GCP.DLP.ContentPolicy("BlockEmail", { |
| 91 | contentPolicyId: created.contentPolicyId, |
| 92 | location: "us", |
| 93 | displayName: "email2", |
| 94 | inspectConfig: { infoTypes: [{ name: "EMAIL_ADDRESS" }] }, |
| 95 | rules: [ |
| 96 | { |
| 97 | conditions: [ |
| 98 | { |
| 99 | infoTypeCondition: { |
| 100 | infoTypes: { infoTypeNames: ["EMAIL_ADDRESS"] }, |
| 101 | }, |
| 102 | }, |
| 103 | ], |
| 104 | action: { returnVerdict: "BLOCK" }, |
| 105 | }, |
| 106 | ], |
| 107 | defaultAction: { returnVerdict: "ALLOW" }, |
| 108 | }); |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | expect(updated.name).toEqual(created.name); |
| 113 | expect(updated.displayName).toEqual("email2"); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | |
| 117 | const gone = yield* waitUntilGone(created.name); |
| 118 | expect(gone).toEqual("gone"); |
| 119 | }).pipe(logLevel), |
| 120 | { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 }, |
| 121 | ); |