The best way to control Claude Code, Codex and more from any computer or phone

main
ts 135 lines 5.04 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { User, UserPool } from "@alchemy.run/aws/Cognito";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as cip from "@distilled.cloud/aws/cognito-identity-provider";
6 import * as Effect from "effect/Effect";
7 import * as Redacted from "effect/Redacted";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 const PASSWORD = "Alchemy-User-Passw0rd1";
12
13 const plain = (value: string | Redacted.Redacted<string> | undefined): string | undefined =>
14 value === undefined ? undefined : typeof value === "string" ? value : Redacted.value(value);
15
16 const attributeValue = (attributes: cip.AttributeType[] | undefined, name: string) =>
17 plain(attributes?.find((attribute) => attribute.Name === name)?.Value);
18
19 test.provider(
20 "create confirmed user, update attributes/enabled, rename (replace), delete",
21 (stack) =>
22 Effect.gen(function* () {
23 yield* stack.destroy();
24
25 const outputs = yield* stack.deploy(
26 Effect.gen(function* () {
27 const pool = yield* UserPool("UserTestPool", {
28 passwordPolicy: { minimumLength: 12, requireSymbols: false },
29 accountRecovery: [{ name: "admin_only", priority: 1 }],
30 });
31 const user = yield* User("Admin", {
32 userPoolId: pool.userPoolId,
33 attributes: {
34 email: "admin@example.com",
35 email_verified: "true",
36 },
37 password: Redacted.make(PASSWORD),
38 });
39 return { pool, user };
40 }),
41 );
42
43 expect(outputs.user.username).toBeDefined();
44 expect(outputs.user.sub).toBeTruthy();
45 // permanent password ⇒ account is CONFIRMED without any invite flow
46 expect(outputs.user.userStatus).toBe("CONFIRMED");
47
48 // out-of-band verification via distilled
49 const created = yield* cip.adminGetUser({
50 UserPoolId: outputs.pool.userPoolId,
51 Username: outputs.user.username,
52 });
53 expect(created.Enabled).toBe(true);
54 expect(created.UserStatus).toBe("CONFIRMED");
55 expect(attributeValue(created.UserAttributes, "email")).toBe("admin@example.com");
56 expect(attributeValue(created.UserAttributes, "email_verified")).toBe("true");
57
58 // attributes and enabled mutate in place
59 const updated = yield* stack.deploy(
60 Effect.gen(function* () {
61 const pool = yield* UserPool("UserTestPool", {
62 passwordPolicy: { minimumLength: 12, requireSymbols: false },
63 accountRecovery: [{ name: "admin_only", priority: 1 }],
64 });
65 const user = yield* User("Admin", {
66 userPoolId: pool.userPoolId,
67 attributes: {
68 email: "admin2@example.com",
69 email_verified: "true",
70 },
71 password: Redacted.make(PASSWORD),
72 enabled: false,
73 });
74 return { pool, user };
75 }),
76 );
77 expect(updated.user.username).toBe(outputs.user.username);
78 expect(updated.user.sub).toBe(outputs.user.sub);
79 const afterUpdate = yield* cip.adminGetUser({
80 UserPoolId: outputs.pool.userPoolId,
81 Username: outputs.user.username,
82 });
83 expect(afterUpdate.Enabled).toBe(false);
84 expect(attributeValue(afterUpdate.UserAttributes, "email")).toBe("admin2@example.com");
85
86 // an explicit username replaces the user
87 const renamed = yield* stack.deploy(
88 Effect.gen(function* () {
89 const pool = yield* UserPool("UserTestPool", {
90 passwordPolicy: { minimumLength: 12, requireSymbols: false },
91 accountRecovery: [{ name: "admin_only", priority: 1 }],
92 });
93 const user = yield* User("Admin", {
94 userPoolId: pool.userPoolId,
95 username: "explicit-admin",
96 attributes: {
97 email: "admin2@example.com",
98 email_verified: "true",
99 },
100 password: Redacted.make(PASSWORD),
101 });
102 return { pool, user };
103 }),
104 );
105 expect(renamed.user.username).toBe("explicit-admin");
106 expect(renamed.user.sub).not.toBe(outputs.user.sub);
107 const oldGone = yield* cip
108 .adminGetUser({
109 UserPoolId: outputs.pool.userPoolId,
110 Username: outputs.user.username,
111 })
112 .pipe(
113 Effect.map(() => false),
114 Effect.catchTag("UserNotFoundException", () => Effect.succeed(true)),
115 );
116 expect(oldGone).toBe(true);
117
118 yield* stack.destroy();
119 const gone = yield* cip
120 .adminGetUser({
121 UserPoolId: outputs.pool.userPoolId,
122 Username: "explicit-admin",
123 })
124 .pipe(
125 Effect.map(() => false),
126 // the pool is destroyed with the stack, so the pool itself being
127 // gone also proves the user is gone
128 Effect.catchTag(["UserNotFoundException", "ResourceNotFoundException"], () =>
129 Effect.succeed(true),
130 ),
131 );
132 expect(gone).toBe(true);
133 }),
134 { tags: ["provider:aws", "provider:aws:cognito", "live"], timeout: 120_000 },
135 );