1import * as AWS from "@/AWS";
2import { AWSEnvironment } from "@/AWS/Environment";
3import * as Output from "@/Output";
4import * as Provider from "@/Provider";
5import * as Test from "./Test.ts";
6import * as ag from "@distilled.cloud/aws/api-gateway";
7import { expect } from "alchemy-test";
8import * as Effect from "effect/Effect";
9import { TestFunction, TestFunctionLive } from "../Lambda/handler.ts";
10import { assertRestApiDeleted } from "./assertions.ts";
11
12const { test } = Test.make({ providers: AWS.providers() });
13
14const authorizerUri = process.env.ALCHEMY_TEST_AUTHORIZER_URI;
15
16/**
17 * Requires a Lambda authorizer invocation URI accepted by API Gateway.
18 */
19test.provider.skipIf(!!process.env.FAST || !authorizerUri)(
20 "create and update Lambda TOKEN authorizer",
21 (stack) =>
22 Effect.gen(function* () {
23 yield* stack.destroy();
24
25 const uri = authorizerUri!;
26
27 const { api, authorizer } = yield* stack.deploy(
28 Effect.gen(function* () {
29 const api = yield* AWS.ApiGateway.RestApi("AgAuthorizerApi", {
30 endpointConfiguration: { types: ["REGIONAL"] },
31 });
32 const authorizer = yield* AWS.ApiGateway.Authorizer("AgAuthorizer", {
33 restApiId: api.restApiId,
34 type: "TOKEN",
35 authorizerUri: uri,
36 identitySource: "method.request.header.Authorization",
37 authorizerResultTtl: "60 seconds",
38 });
39 return { api, authorizer };
40 }),
41 );
42
43 yield* stack.deploy(
44 Effect.gen(function* () {
45 const apiAgain = yield* AWS.ApiGateway.RestApi("AgAuthorizerApi", {
46 endpointConfiguration: { types: ["REGIONAL"] },
47 });
48 yield* AWS.ApiGateway.Authorizer("AgAuthorizer", {
49 restApiId: apiAgain.restApiId,
50 type: "TOKEN",
51 authorizerUri: uri,
52 identitySource: "method.request.header.Authorization",
53 authorizerResultTtl: "120 seconds",
54 });
55 }),
56 );
57
58 const remote = yield* ag.getAuthorizer({
59 restApiId: api.restApiId,
60 authorizerId: authorizer.authorizerId,
61 });
62 expect(remote.authorizerResultTtlInSeconds).toEqual(120);
63
64 yield* stack.destroy();
65 yield* assertRestApiDeleted(api.restApiId);
66 }),
67 { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
68);
69
70test.provider.skipIf(!!process.env.FAST)(
71 "list enumerates the deployed authorizer",
72 (stack) =>
73 Effect.gen(function* () {
74 yield* stack.destroy();
75
76 const { region } = yield* AWSEnvironment.current;
77
78 const { authorizer } = yield* stack.deploy(
79 Effect.gen(function* () {
80 const fn = yield* TestFunction.pipe(Effect.provide(TestFunctionLive));
81 const api = yield* AWS.ApiGateway.RestApi("AgAuthorizerListApi", {
82 endpointConfiguration: { types: ["REGIONAL"] },
83 });
84 const authorizer = yield* AWS.ApiGateway.Authorizer(
85 "AgAuthorizerList",
86 {
87 restApiId: api.restApiId,
88 type: "TOKEN",
89 authorizerUri: Output.map(
90 fn.functionArn,
91 (arn: string) =>
92 `arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${arn}/invocations`,
93 ),
94 identitySource: "method.request.header.Authorization",
95 },
96 );
97 return { authorizer };
98 }),
99 );
100
101 const provider = yield* Provider.findProvider(AWS.ApiGateway.Authorizer);
102 const all = yield* provider.list();
103
104 expect(all.some((a) => a.authorizerId === authorizer.authorizerId)).toBe(
105 true,
106 );
107
108 yield* stack.destroy();
109 yield* assertRestApiDeleted(authorizer.restApiId);
110 }),
111 {
112 tags: [
113 "provider:aws",
114 "provider:aws:apigateway",
115 "provider:aws:lambda",
116 "live",
117 ],
118 timeout: 300_000,
119 },
120);