| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as apigee from "@distilled.cloud/gcp/apigee_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 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 | // Needs a provisioned Apigee organization on the testing project (paid, or |
| 18 | // ~1h eval provisioning); without one calls fail with ApigeeResourceNotFound (403 "Permission |
| 19 | // denied on resource \"organizations/{project}\" (or it may not exist)"). |
| 20 | // Set GCP_TEST_APIGEE_ORG=1 when the org exists. |
| 21 | const runLifecycle = !!process.env.GCP_TEST_APIGEE_ORG; |
| 22 | |
| 23 | const waitUntilGone = (name: string) => |
| 24 | apigee.getOrganizationsAppgroupsApps({ name }).pipe( |
| 25 | Effect.as("found" as const), |
| 26 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 27 | Effect.catchTag("ApigeeResourceNotFound", () => |
| 28 | Effect.succeed("gone" as const), |
| 29 | ), |
| 30 | Effect.repeat({ |
| 31 | schedule: Schedule.spaced("1 second"), |
| 32 | until: (status) => status === "gone", |
| 33 | times: 10, |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | test.provider( |
| 38 | "getOrganizationsAppgroupsApps on a missing app fails with ApigeeResourceNotFound", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | const { project } = yield* GcpEnvironment.current; |
| 42 | const org = `organizations/${project}`; |
| 43 | |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const error = yield* Effect.flip( |
| 47 | apigee.getOrganizationsAppgroupsApps({ |
| 48 | name: `${org}/appgroups/missing-group/apps/alchemy-missing`, |
| 49 | }), |
| 50 | ); |
| 51 | expect(error._tag).toEqual("ApigeeResourceNotFound"); |
| 52 | |
| 53 | yield* stack.destroy(); |
| 54 | }).pipe(logLevel), |
| 55 | { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 }, |
| 56 | ); |
| 57 | |
| 58 | test.provider.skipIf(!runLifecycle)( |
| 59 | "create, update, and delete an appgroup app", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | yield* stack.destroy(); |
| 63 | |
| 64 | const created = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const group = yield* GCP.Apigee.Appgroup("Partners", { |
| 67 | displayName: "Partners", |
| 68 | email: "partners@example.com", |
| 69 | }); |
| 70 | const app = yield* GCP.Apigee.AppgroupsApp("Mobile", { |
| 71 | appgroup: group.appgroupId, |
| 72 | callbackUrl: "https://example.com/oauth", |
| 73 | attributes: [{ name: "team", value: "platform" }], |
| 74 | }); |
| 75 | return { group, app }; |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(created.app.appId).toEqual(expect.any(String)); |
| 80 | expect(created.app.appgroupId).toEqual(created.group.appgroupId); |
| 81 | expect(created.app.callbackUrl).toEqual("https://example.com/oauth"); |
| 82 | expect(created.app.attributes).toEqual( |
| 83 | expect.arrayContaining([{ name: "team", value: "platform" }]), |
| 84 | ); |
| 85 | |
| 86 | const fetched = yield* apigee.getOrganizationsAppgroupsApps({ |
| 87 | name: created.app.name, |
| 88 | }); |
| 89 | expect( |
| 90 | fetched.attributes?.some((item) => item.name === "alchemy-id"), |
| 91 | ).toEqual(true); |
| 92 | |
| 93 | const updated = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | const group = yield* GCP.Apigee.Appgroup("Partners", { |
| 96 | appgroupId: created.group.appgroupId, |
| 97 | displayName: "Partners", |
| 98 | email: "partners@example.com", |
| 99 | }); |
| 100 | const app = yield* GCP.Apigee.AppgroupsApp("Mobile", { |
| 101 | appgroup: group.appgroupId, |
| 102 | appId: created.app.appId, |
| 103 | callbackUrl: "https://example.com/updated", |
| 104 | attributes: [{ name: "team", value: "runtime" }], |
| 105 | }); |
| 106 | return { group, app }; |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | expect(updated.app.name).toEqual(created.app.name); |
| 111 | expect(updated.app.callbackUrl).toEqual("https://example.com/updated"); |
| 112 | expect(updated.app.attributes).toEqual( |
| 113 | expect.arrayContaining([{ name: "team", value: "runtime" }]), |
| 114 | ); |
| 115 | |
| 116 | yield* stack.destroy(); |
| 117 | |
| 118 | const gone = yield* waitUntilGone(created.app.name); |
| 119 | expect(gone).toEqual("gone"); |
| 120 | }).pipe(logLevel), |
| 121 | { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 }, |
| 122 | ); |