| 1 | import * as Stripe from "@/Stripe"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import { expect } from "alchemy-test"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | |
| 8 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 9 | |
| 10 | const logLevel = Effect.provideService( |
| 11 | MinimumLogLevel, |
| 12 | process.env.DEBUG ? "Debug" : "Info", |
| 13 | ); |
| 14 | |
| 15 | test.provider( |
| 16 | "creates a logical restricted key with default props", |
| 17 | (stack) => |
| 18 | Effect.gen(function* () { |
| 19 | yield* stack.destroy(); |
| 20 | |
| 21 | const created = yield* stack.deploy( |
| 22 | Effect.gen(function* () { |
| 23 | const token = yield* Stripe.RestrictedApiKey("HostToken"); |
| 24 | yield* token.bind("RetrieveProduct", { |
| 25 | permissions: ["products_read"], |
| 26 | }); |
| 27 | return token; |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | expect(created.id).toEqual(expect.any(String)); |
| 32 | expect(created.name).toEqual(expect.any(String)); |
| 33 | expect(Redacted.isRedacted(created.value)).toEqual(true); |
| 34 | expect(created.permissions).toEqual(["products_read"]); |
| 35 | |
| 36 | yield* stack.destroy(); |
| 37 | }).pipe(logLevel), |
| 38 | { |
| 39 | tags: ["provider:stripe", "provider:stripe:restrictedapikey", "live"], |
| 40 | timeout: 120_000, |
| 41 | }, |
| 42 | ); |
| 43 | |
| 44 | test.provider( |
| 45 | "creates a logical restricted key and merges bound permissions", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | yield* stack.destroy(); |
| 49 | |
| 50 | const created = yield* stack.deploy( |
| 51 | Effect.gen(function* () { |
| 52 | const token = yield* Stripe.RestrictedApiKey("HostToken", { |
| 53 | permissions: ["customers_read"], |
| 54 | }); |
| 55 | yield* token.bind("RetrieveProduct", { |
| 56 | permissions: ["products_read"], |
| 57 | }); |
| 58 | return token; |
| 59 | }), |
| 60 | ); |
| 61 | |
| 62 | expect(created.id).toEqual(expect.any(String)); |
| 63 | expect(created.name).toEqual(expect.any(String)); |
| 64 | expect(Redacted.isRedacted(created.value)).toEqual(true); |
| 65 | expect(created.permissions).toEqual(["customers_read", "products_read"]); |
| 66 | |
| 67 | const updated = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | const token = yield* Stripe.RestrictedApiKey("HostToken", { |
| 70 | permissions: ["customers_read", "customers_write"], |
| 71 | }); |
| 72 | yield* token.bind("RetrieveProduct", { |
| 73 | permissions: ["products_read"], |
| 74 | }); |
| 75 | return token; |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(updated.id).toEqual(created.id); |
| 80 | expect(updated.permissions).toEqual([ |
| 81 | "customers_read", |
| 82 | "customers_write", |
| 83 | "products_read", |
| 84 | ]); |
| 85 | |
| 86 | yield* stack.destroy(); |
| 87 | }).pipe(logLevel), |
| 88 | { |
| 89 | tags: ["provider:stripe", "provider:stripe:restrictedapikey", "live"], |
| 90 | timeout: 120_000, |
| 91 | }, |
| 92 | ); |