E2E: derive Clerk origin from publishable key; default test creds
This commit is contained in:
@@ -1,11 +1,16 @@
|
|||||||
import { defineConfig } from "cypress";
|
import { defineConfig } from "cypress";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
env: {
|
||||||
|
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,
|
||||||
|
// Optional overrides.
|
||||||
|
CLERK_ORIGIN: process.env.CYPRESS_CLERK_ORIGIN,
|
||||||
|
CLERK_TEST_EMAIL: process.env.CYPRESS_CLERK_TEST_EMAIL,
|
||||||
|
CLERK_TEST_OTP: process.env.CYPRESS_CLERK_TEST_OTP,
|
||||||
|
},
|
||||||
e2e: {
|
e2e: {
|
||||||
baseUrl: "http://localhost:3000",
|
baseUrl: "http://localhost:3000",
|
||||||
video: false,
|
specPattern: "cypress/e2e/**/*.cy.{js,jsx,ts,tsx}",
|
||||||
screenshotOnRunFailure: true,
|
|
||||||
specPattern: "cypress/e2e/**/*.cy.{ts,tsx,js,jsx}",
|
|
||||||
supportFile: "cypress/support/e2e.ts",
|
supportFile: "cypress/support/e2e.ts",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,20 +1,25 @@
|
|||||||
/// <reference types="cypress" />
|
/// <reference types="cypress" />
|
||||||
|
|
||||||
type ClerkOtpLoginOptions = {
|
function getEnv(name: string, fallback?: string): string {
|
||||||
clerkOrigin: string;
|
|
||||||
email: string;
|
|
||||||
otp: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
function requireEnv(name: string): string {
|
|
||||||
const value = Cypress.env(name) as string | undefined;
|
const value = Cypress.env(name) as string | undefined;
|
||||||
if (!value) {
|
if (value) return value;
|
||||||
throw new Error(
|
if (fallback !== undefined) return fallback;
|
||||||
`Missing Cypress env var ${name}. ` +
|
throw new Error(
|
||||||
`Set it via CYPRESS_${name}=... in CI/local before running Clerk login tests.`,
|
`Missing Cypress env var ${name}. ` +
|
||||||
);
|
`Set it via CYPRESS_${name}=... in CI/local before running Clerk login tests.`,
|
||||||
}
|
);
|
||||||
return value;
|
}
|
||||||
|
|
||||||
|
function clerkOriginFromPublishableKey(): string {
|
||||||
|
const key = getEnv("NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY");
|
||||||
|
|
||||||
|
// pk_test_<base64(domain$)> OR pk_live_<...>
|
||||||
|
const m = /^pk_(?:test|live)_(.+)$/.exec(key);
|
||||||
|
if (!m) throw new Error(`Unexpected Clerk publishable key format: ${key}`);
|
||||||
|
|
||||||
|
const decoded = atob(m[1]); // e.g. beloved-ghost-73.clerk.accounts.dev$
|
||||||
|
const domain = decoded.replace(/\$$/, "");
|
||||||
|
return `https://${domain}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeOrigin(value: string): string {
|
function normalizeOrigin(value: string): string {
|
||||||
@@ -22,48 +27,43 @@ function normalizeOrigin(value: string): string {
|
|||||||
const url = new URL(value);
|
const url = new URL(value);
|
||||||
return url.origin;
|
return url.origin;
|
||||||
} catch {
|
} catch {
|
||||||
// allow providing just an origin-like string
|
|
||||||
return value.replace(/\/$/, "");
|
return value.replace(/\/$/, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Cypress.Commands.add("loginWithClerkOtp", () => {
|
Cypress.Commands.add("loginWithClerkOtp", () => {
|
||||||
const clerkOrigin = normalizeOrigin(requireEnv("CLERK_ORIGIN"));
|
const clerkOrigin = normalizeOrigin(
|
||||||
const email = requireEnv("CLERK_TEST_EMAIL");
|
getEnv("CLERK_ORIGIN", clerkOriginFromPublishableKey()),
|
||||||
const otp = requireEnv("CLERK_TEST_OTP");
|
);
|
||||||
|
const email = getEnv("CLERK_TEST_EMAIL", "jane+clerk_test@example.com");
|
||||||
|
const otp = getEnv("CLERK_TEST_OTP", "424242");
|
||||||
|
|
||||||
const opts: ClerkOtpLoginOptions = { clerkOrigin, email, otp };
|
// Navigate to a dedicated sign-in route that renders Clerk SignIn top-level.
|
||||||
|
// Cypress cannot reliably drive Clerk modal/iframe flows.
|
||||||
// Navigate to a dedicated sign-in route that performs a top-level redirect
|
|
||||||
// to Clerk hosted sign-in (avoids modal/iframe limitations in Cypress).
|
|
||||||
cy.visit("/sign-in");
|
cy.visit("/sign-in");
|
||||||
|
|
||||||
// The Clerk UI is typically hosted on a different origin (clerk.accounts.dev / clerk.com).
|
|
||||||
// Use cy.origin to drive the UI in Chrome.
|
|
||||||
cy.origin(
|
cy.origin(
|
||||||
opts.clerkOrigin,
|
clerkOrigin,
|
||||||
{ args: { email: opts.email, otp: opts.otp } },
|
{ args: { email, otp } },
|
||||||
({ email, otp }) => {
|
({ email: e, otp: o }) => {
|
||||||
// Email / identifier input
|
|
||||||
cy.get('input[type="email"], input[name="identifier"], input[autocomplete="email"]', {
|
cy.get('input[type="email"], input[name="identifier"], input[autocomplete="email"]', {
|
||||||
timeout: 20_000,
|
timeout: 20_000,
|
||||||
})
|
})
|
||||||
.first()
|
.first()
|
||||||
.clear()
|
.clear()
|
||||||
.type(email, { delay: 10 });
|
.type(e, { delay: 10 });
|
||||||
|
|
||||||
// Submit / continue
|
|
||||||
cy.get('button[type="submit"], button')
|
cy.get('button[type="submit"], button')
|
||||||
.contains(/continue|sign in|send|next/i)
|
.contains(/continue|sign in|send|next/i)
|
||||||
.click({ force: true });
|
.click({ force: true });
|
||||||
|
|
||||||
// OTP input - Clerk commonly uses autocomplete=one-time-code
|
cy.get(
|
||||||
cy.get('input[autocomplete="one-time-code"], input[name*="code"], input[inputmode="numeric"]', {
|
'input[autocomplete="one-time-code"], input[name*="code"], input[inputmode="numeric"]',
|
||||||
timeout: 20_000,
|
{ timeout: 20_000 },
|
||||||
})
|
)
|
||||||
.first()
|
.first()
|
||||||
.clear()
|
.clear()
|
||||||
.type(otp, { delay: 10 });
|
.type(o, { delay: 10 });
|
||||||
|
|
||||||
// Final submit (some flows auto-submit)
|
// Final submit (some flows auto-submit)
|
||||||
cy.get("body").then(($body) => {
|
cy.get("body").then(($body) => {
|
||||||
@@ -86,12 +86,8 @@ declare global {
|
|||||||
namespace Cypress {
|
namespace Cypress {
|
||||||
interface Chainable {
|
interface Chainable {
|
||||||
/**
|
/**
|
||||||
* Logs in via the real Clerk modal using deterministic OTP credentials.
|
* Logs in via real Clerk using deterministic OTP credentials.
|
||||||
*
|
* Defaults (non-secret): jane+clerk_test@example.com / 424242.
|
||||||
* Requires env vars:
|
|
||||||
* - CYPRESS_CLERK_ORIGIN (e.g. https://<subdomain>.clerk.accounts.dev)
|
|
||||||
* - CYPRESS_CLERK_TEST_EMAIL
|
|
||||||
* - CYPRESS_CLERK_TEST_OTP
|
|
||||||
*/
|
*/
|
||||||
loginWithClerkOtp(): Chainable<void>;
|
loginWithClerkOtp(): Chainable<void>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user