-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathlocal-emulator-project.test.ts
More file actions
246 lines (218 loc) · 9.1 KB
/
local-emulator-project.test.ts
File metadata and controls
246 lines (218 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { randomUUID } from "crypto";
import fs from "fs/promises";
import path from "path";
import { describe } from "vitest";
import { it } from "../../../../../helpers";
import { backendContext, niceBackendFetch } from "../../../../backend-helpers";
const LOCAL_EMULATOR_PROJECT_ENDPOINT = "/api/v1/internal/local-emulator/project";
const LOCAL_EMULATOR_OWNER_TEAM_ID = "5a0c858b-d9e9-49d4-9943-8ce385d86428";
const isLocalEmulator = process.env.NEXT_PUBLIC_STACK_IS_LOCAL_EMULATOR === "true";
async function createTempConfigFile(): Promise<string> {
const filePath = `/tmp/${randomUUID()}/stack.config.ts`;
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, "export const config = {};\n", "utf-8");
return filePath;
}
describe("local emulator project endpoint", () => {
it("returns a clear error when local emulator mode is disabled", async ({ expect }) => {
if (isLocalEmulator) {
return;
}
const response = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: `/tmp/${randomUUID()}/stack.config.ts`,
},
});
expect(response.status).toBe(400);
expect(response.body).toContain("NEXT_PUBLIC_STACK_IS_LOCAL_EMULATOR=true");
});
it.runIf(isLocalEmulator)("rejects non-absolute paths", async ({ expect }) => {
for (const nonAbsolutePath of [
`relative/${randomUUID()}/stack.config.ts`,
`./relative/${randomUUID()}/stack.config.ts`,
"",
]) {
const response = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: nonAbsolutePath,
},
});
expect(response.status).toBe(400);
expect(response.body).toContain("absolute_file_path must be an absolute path");
}
});
it.runIf(isLocalEmulator)("writes default config for empty files", async ({ expect }) => {
const filePath = `/tmp/${randomUUID()}/stack.config.ts`;
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, "", "utf-8");
const response = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: filePath,
},
});
expect(response.status).toBe(200);
expect(JSON.parse(response.body.branch_config_override_string)).toEqual({});
expect(response.body.onboarding_status).toBe("config_choice");
expect(response.body.onboarding_outstanding).toBe(true);
const fileContent = await fs.readFile(filePath, "utf-8");
expect(fileContent).toMatchInlineSnapshot(`
deindent\`
import type { StackConfig } from "@stackframe/js";
export const config: StackConfig = "show-onboarding";
\` + "\\n"
`);
});
it.runIf(isLocalEmulator)("enables onboarding iff config is show-onboarding", async ({ expect }) => {
const filePath = `/tmp/${randomUUID()}/stack.config.ts`;
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(
filePath,
`import type { StackConfig } from "@stackframe/js";\n\nexport const config: StackConfig = "show-onboarding";\n`,
"utf-8",
);
const onboardingResponse = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: filePath,
},
});
expect(onboardingResponse.status).toBe(200);
expect(onboardingResponse.body.onboarding_status).toBe("config_choice");
expect(onboardingResponse.body.onboarding_outstanding).toBe(true);
await fs.writeFile(filePath, `export const config = {};\n`, "utf-8");
const completedResponse = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: filePath,
},
});
expect(completedResponse.status).toBe(200);
expect(completedResponse.body.project_id).toBe(onboardingResponse.body.project_id);
expect(completedResponse.body.onboarding_status).toBe("completed");
expect(completedResponse.body.onboarding_outstanding).toBe(false);
await fs.writeFile(
filePath,
`import type { StackConfig } from "@stackframe/js";\n\nexport const config: StackConfig = "show-onboarding";\n`,
"utf-8",
);
const onboardingAgainResponse = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: filePath,
},
});
expect(onboardingAgainResponse.status).toBe(200);
expect(onboardingAgainResponse.body.project_id).toBe(onboardingResponse.body.project_id);
expect(onboardingAgainResponse.body.onboarding_status).toBe("config_choice");
expect(onboardingAgainResponse.body.onboarding_outstanding).toBe(true);
});
it.runIf(isLocalEmulator)("creates path-based projects, reuses mappings, and returns valid credentials", async ({ expect }) => {
const pathA = await createTempConfigFile();
const pathB = await createTempConfigFile();
const responseA1 = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: pathA,
},
});
expect(responseA1.status).toBe(200);
expect(responseA1.body.project_id).toEqual(expect.any(String));
expect(responseA1.body.secret_server_key).toMatch(/^ssk_/);
expect(responseA1.body.super_secret_admin_key).toMatch(/^sak_/);
expect(JSON.parse(responseA1.body.branch_config_override_string)).toEqual({});
expect(responseA1.body.onboarding_status).toBe("completed");
expect(responseA1.body.onboarding_outstanding).toBe(false);
const responseA2 = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: pathA,
},
});
expect(responseA2.status).toBe(200);
expect(responseA2.body.project_id).toBe(responseA1.body.project_id);
expect(responseA2.body.secret_server_key).toBe(responseA1.body.secret_server_key);
expect(responseA2.body.super_secret_admin_key).toBe(responseA1.body.super_secret_admin_key);
const responseB = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: pathB,
},
});
expect(responseB.status).toBe(200);
expect(responseB.body.project_id).not.toBe(responseA1.body.project_id);
expect(JSON.parse(responseB.body.branch_config_override_string)).toEqual({});
expect(responseB.body.onboarding_status).toBe("completed");
expect(responseB.body.onboarding_outstanding).toBe(false);
backendContext.set({
projectKeys: {
projectId: responseA1.body.project_id,
superSecretAdminKey: responseA1.body.super_secret_admin_key,
},
});
const projectAResponse = await niceBackendFetch("/api/v1/internal/projects/current", {
method: "GET",
accessType: "admin",
});
expect(projectAResponse.status).toBe(200);
expect(projectAResponse.body.owner_team_id).toBe(LOCAL_EMULATOR_OWNER_TEAM_ID);
backendContext.set({
projectKeys: {
projectId: responseB.body.project_id,
superSecretAdminKey: responseB.body.super_secret_admin_key,
},
});
const projectBResponse = await niceBackendFetch("/api/v1/internal/projects/current", {
method: "GET",
accessType: "admin",
});
expect(projectBResponse.status).toBe(200);
expect(projectBResponse.body.owner_team_id).toBe(LOCAL_EMULATOR_OWNER_TEAM_ID);
});
it.runIf(isLocalEmulator)("updates onboarding_state on local emulator projects without mutating env config", async ({ expect }) => {
const filePath = `/tmp/${randomUUID()}/stack.config.ts`;
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, "", "utf-8");
const localEmulatorProjectResponse = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
accessType: "admin",
method: "POST",
body: {
absolute_file_path: filePath,
},
});
expect(localEmulatorProjectResponse.status).toBe(200);
backendContext.set({
projectKeys: {
projectId: localEmulatorProjectResponse.body.project_id,
superSecretAdminKey: localEmulatorProjectResponse.body.super_secret_admin_key,
},
});
const onboardingState = {
selected_config_choice: "create-new" as const,
selected_apps: ["authentication", "payments", "emails", "analytics"] as const,
selected_sign_in_methods: [] as const,
selected_email_theme_id: randomUUID(),
selected_payments_country: "US" as const,
};
const updateCurrentProjectResponse = await niceBackendFetch("/api/v1/internal/projects/current", {
accessType: "admin",
method: "PATCH",
body: {
onboarding_state: onboardingState,
},
});
expect(updateCurrentProjectResponse.status).toBe(200);
expect(updateCurrentProjectResponse.body.onboarding_state).toEqual(onboardingState);
});
});