Rewrite komoot E2E tests to use server-side seed endpoint
page.route() only intercepts browser requests, not server-side fetch calls. Add /api/e2e/komoot seed endpoint that creates a Komoot connection directly and returns a session cookie, so tests can verify UI state without mocking the Komoot API at the network layer. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
25f7d35e03
commit
4a5319fa72
3 changed files with 187 additions and 203 deletions
|
|
@ -18,6 +18,7 @@ export default [
|
|||
route("routes/:id/edit", "routes/routes.$id.edit.tsx"),
|
||||
route("api/e2e/seed", "routes/api.e2e.seed.ts"),
|
||||
route("api/e2e/route/:id", "routes/api.e2e.route.$id.ts"),
|
||||
route("api/e2e/komoot", "routes/api.e2e.komoot.ts"),
|
||||
route("api/routes/:id/callback", "routes/api.routes.$id.callback.ts"),
|
||||
route("api/routes/:id/edit-in-planner", "routes/api.routes.$id.edit-in-planner.ts"),
|
||||
route("api/routes/:id/gpx", "routes/api.routes.$id.gpx.ts"),
|
||||
|
|
|
|||
82
apps/journal/app/routes/api.e2e.komoot.ts
Normal file
82
apps/journal/app/routes/api.e2e.komoot.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
// POST /api/e2e/komoot
|
||||
// Seeds a Komoot connection for the e2e test user and returns a session cookie.
|
||||
// Only available when E2E=true. Never enabled in production.
|
||||
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { data } from "react-router";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { getDb } from "~/lib/db";
|
||||
import { users } from "@trails-cool/db/schema/journal";
|
||||
import { link } from "~/lib/connected-services/manager";
|
||||
import { sessionStorage } from "~/lib/auth/session.server";
|
||||
import { TERMS_VERSION } from "~/lib/legal";
|
||||
|
||||
function assertE2EEnabled() {
|
||||
if (process.env.E2E !== "true") {
|
||||
throw new Response("Not found", { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
const E2E_USER_USERNAME = "e2e-komoot-user";
|
||||
const E2E_USER_EMAIL = "e2e-komoot@localhost";
|
||||
const E2E_KOMOOT_USER_ID = "99999999999";
|
||||
|
||||
export async function action({ request }: { request: Request }) {
|
||||
assertE2EEnabled();
|
||||
if (request.method !== "POST") {
|
||||
return data({ error: "Method not allowed" }, { status: 405 });
|
||||
}
|
||||
|
||||
const body = request.headers.get("content-type")?.includes("application/json")
|
||||
? ((await request.json().catch(() => ({}))) as { mode?: string })
|
||||
: {};
|
||||
const mode = body.mode ?? "public";
|
||||
|
||||
const db = getDb();
|
||||
|
||||
// Upsert the e2e komoot test user
|
||||
await db
|
||||
.insert(users)
|
||||
.values({
|
||||
id: randomUUID(),
|
||||
username: E2E_USER_USERNAME,
|
||||
email: E2E_USER_EMAIL,
|
||||
displayName: "E2E Komoot User",
|
||||
domain: "localhost",
|
||||
termsAcceptedAt: new Date(),
|
||||
termsVersion: TERMS_VERSION,
|
||||
})
|
||||
.onConflictDoNothing({ target: users.username });
|
||||
|
||||
const [user] = await db
|
||||
.select({ id: users.id })
|
||||
.from(users)
|
||||
.where(eq(users.username, E2E_USER_USERNAME));
|
||||
|
||||
if (!user) throw new Error("e2e komoot seed: failed to upsert test user");
|
||||
|
||||
// Create Komoot connection
|
||||
const credentials =
|
||||
mode === "authenticated"
|
||||
? { mode: "authenticated", email: "e2e@komoot.test", encryptedPassword: "dummy", komootUserId: E2E_KOMOOT_USER_ID }
|
||||
: { mode: "public", komootUserId: E2E_KOMOOT_USER_ID };
|
||||
|
||||
await link({
|
||||
userId: user.id,
|
||||
provider: "komoot",
|
||||
credentialKind: mode === "authenticated" ? "web-login" : "public",
|
||||
credentials,
|
||||
providerUserId: E2E_KOMOOT_USER_ID,
|
||||
grantedScopes: [],
|
||||
});
|
||||
|
||||
// Create a session so the test can navigate as this user
|
||||
const session = await sessionStorage.getSession();
|
||||
session.set("userId", user.id);
|
||||
const cookie = await sessionStorage.commitSession(session);
|
||||
|
||||
return data(
|
||||
{ userId: user.id, username: E2E_USER_USERNAME },
|
||||
{ headers: { "Set-Cookie": cookie } },
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue