1import * as AWS from "@/AWS";
2import { ResourceServer, UserPool } from "@/AWS/Cognito";
3import * as Test from "@/Test/Alchemy";
4import * as cip from "@distilled.cloud/aws/cognito-identity-provider";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7
8const { test } = Test.make({ providers: AWS.providers() });
9
10test.provider(
11 "create, mutate scopes, delete resource server",
12 (stack) =>
13 Effect.gen(function* () {
14 yield* stack.destroy();
15
16 const outputs = yield* stack.deploy(
17 Effect.gen(function* () {
18 const pool = yield* UserPool("RSTestPool", {});
19 const server = yield* ResourceServer("Api", {
20 userPoolId: pool.userPoolId,
21 identifier: "https://api.alchemy-test.example.com",
22 name: "Test API",
23 scopes: [{ scopeName: "read", scopeDescription: "Read access" }],
24 });
25 return { pool, server };
26 }),
27 );
28
29 expect(outputs.server.identifier).toBe(
30 "https://api.alchemy-test.example.com",
31 );
32 expect(outputs.server.name).toBe("Test API");
33
34 // out-of-band verification via distilled
35 const created = yield* cip.describeResourceServer({
36 UserPoolId: outputs.pool.userPoolId,
37 Identifier: outputs.server.identifier,
38 });
39 expect(created.ResourceServer?.Name).toBe("Test API");
40 expect(created.ResourceServer?.Scopes).toHaveLength(1);
41
42 // scopes and name mutate in place
43 yield* stack.deploy(
44 Effect.gen(function* () {
45 const pool = yield* UserPool("RSTestPool", {});
46 const server = yield* ResourceServer("Api", {
47 userPoolId: pool.userPoolId,
48 identifier: "https://api.alchemy-test.example.com",
49 name: "Renamed API",
50 scopes: [
51 { scopeName: "read", scopeDescription: "Read access" },
52 { scopeName: "write", scopeDescription: "Write access" },
53 ],
54 });
55 return { pool, server };
56 }),
57 );
58 const afterUpdate = yield* cip.describeResourceServer({
59 UserPoolId: outputs.pool.userPoolId,
60 Identifier: outputs.server.identifier,
61 });
62 expect(afterUpdate.ResourceServer?.Name).toBe("Renamed API");
63 expect(afterUpdate.ResourceServer?.Scopes).toHaveLength(2);
64
65 yield* stack.destroy();
66 const gone = yield* cip
67 .describeResourceServer({
68 UserPoolId: outputs.pool.userPoolId,
69 Identifier: outputs.server.identifier,
70 })
71 .pipe(
72 Effect.map(() => false),
73 Effect.catchTag("ResourceNotFoundException", () =>
74 Effect.succeed(true),
75 ),
76 );
77 expect(gone).toBe(true);
78 }),
79 { tags: ["provider:aws", "provider:aws:cognito", "live"], timeout: 120_000 },
80);