| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as integrations from "@distilled.cloud/gcp/integrations_v1"; |
| 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 | integrations.getProjectsLocationsAuthConfigs({ 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 | const credential = { |
| 29 | credentialType: "USERNAME_AND_PASSWORD" as const, |
| 30 | usernameAndPassword: { username: "alchemy", password: "test-secret" }, |
| 31 | }; |
| 32 | |
| 33 | test.provider( |
| 34 | "getProjectsLocationsAuthConfigs on a missing config fails with a typed tag", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | const { project } = yield* GcpEnvironment.current; |
| 38 | yield* stack.destroy(); |
| 39 | |
| 40 | const error = yield* Effect.flip( |
| 41 | integrations.getProjectsLocationsAuthConfigs({ |
| 42 | name: `projects/${project}/locations/us-central1/authConfigs/alchemy-missing-auth`, |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toEqual("NotFound"); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | }).pipe(logLevel), |
| 49 | { |
| 50 | tags: ["provider:gcp", "provider:gcp:integrations", "live"], |
| 51 | timeout: 90_000, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | test.provider( |
| 56 | "create, update, and delete an auth config", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* GCP.Integrations.AuthConfig("Salesforce", { |
| 64 | location: "us-central1", |
| 65 | displayName: "alchemy-salesforce", |
| 66 | description: "basic auth", |
| 67 | credentialType: "USERNAME_AND_PASSWORD", |
| 68 | decryptedCredential: credential, |
| 69 | visibility: "PRIVATE", |
| 70 | }); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | expect(created.name).toContain("/authConfigs/"); |
| 75 | expect(created.location).toEqual("us-central1"); |
| 76 | expect(created.displayName).toEqual("alchemy-salesforce"); |
| 77 | expect(created.description).toEqual("basic auth"); |
| 78 | expect(created.credentialType).toEqual("USERNAME_AND_PASSWORD"); |
| 79 | |
| 80 | const fetched = yield* integrations.getProjectsLocationsAuthConfigs({ |
| 81 | name: created.name, |
| 82 | }); |
| 83 | expect(fetched.name).toEqual(created.name); |
| 84 | expect(fetched.description).toContain("alchemy-id="); |
| 85 | expect(fetched.description).toContain("basic auth"); |
| 86 | |
| 87 | const updated = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | return yield* GCP.Integrations.AuthConfig("Salesforce", { |
| 90 | authConfigId: created.authConfigId, |
| 91 | location: "us-central1", |
| 92 | displayName: "alchemy-salesforce", |
| 93 | description: "updated auth", |
| 94 | credentialType: "USERNAME_AND_PASSWORD", |
| 95 | decryptedCredential: credential, |
| 96 | visibility: "PRIVATE", |
| 97 | }); |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | expect(updated.name).toEqual(created.name); |
| 102 | expect(updated.description).toEqual("updated auth"); |
| 103 | |
| 104 | const fetchedUpdate = yield* integrations.getProjectsLocationsAuthConfigs( |
| 105 | { |
| 106 | name: updated.name, |
| 107 | }, |
| 108 | ); |
| 109 | expect(fetchedUpdate.description).toContain("updated auth"); |
| 110 | |
| 111 | yield* stack.destroy(); |
| 112 | const gone = yield* waitUntilGone(created.name); |
| 113 | expect(gone).toEqual("gone"); |
| 114 | }).pipe(logLevel), |
| 115 | { |
| 116 | tags: ["provider:gcp", "provider:gcp:integrations", "live"], |
| 117 | timeout: 90_000, |
| 118 | }, |
| 119 | ); |