Skip to content

Commit 3c5bf12

Browse files
committed
Fix cypress
1 parent 986721c commit 3c5bf12

6 files changed

Lines changed: 60 additions & 82 deletions

File tree

cypress/README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ describe("Public Features", () => {
6666
});
6767
```
6868

69-
## Test Endpoint
70-
71-
The tests use the following API endpoint for programmatic authentication and session creation:
72-
73-
**Endpoint:** `POST /api/test/set-session`
74-
75-
- **Purpose:** Create session from authentication tokens
76-
- **Body:** `{ user, accessToken, refreshToken }`
77-
- **Response:** Uses WorkOS AuthKit's `saveSession` method to create encrypted session cookie
78-
7969
The endpoint is to be used for testing purposes only and is recommended to be disabled in production environments.
8070

8171
## Files

cypress/plugins/workos.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
import type { PluginEvents, PluginConfigOptions } from "cypress";
2-
3-
interface AuthenticateParams {
4-
email: string;
5-
password: string;
6-
workosApiKey: string;
7-
workosClientId: string;
8-
}
9-
1+
import { WorkOS } from "@workos-inc/node";
102
/**
113
* WorkOS authentication plugin for Cypress
124
* Provides tasks for programmatic authentication using WorkOS SDK
135
*/
146
export function registerWorkOSTasks(
15-
on: PluginEvents,
16-
config: PluginConfigOptions
7+
on: Cypress.PluginEvents,
8+
config: Cypress.PluginConfigOptions,
179
) {
1810
on("task", {
19-
authenticateWithWorkOS({
11+
async authenticateWithWorkOS({
2012
email,
2113
password,
2214
workosApiKey,
2315
workosClientId,
24-
}: AuthenticateParams) {
25-
// Import WorkOS SDK in Node.js context (can't be imported in browser context)
26-
const { WorkOS } = require("@workos-inc/node");
27-
16+
cookiePassword,
17+
}) {
2818
const workos = new WorkOS(workosApiKey, {
2919
apiHostname: process.env.WORKOS_API_HOSTNAME,
3020
});
@@ -33,6 +23,10 @@ export function registerWorkOSTasks(
3323
clientId: workosClientId,
3424
email,
3525
password,
26+
session: {
27+
sealSession: true,
28+
cookiePassword: process.env.WORKOS_COOKIE_PASSWORD,
29+
},
3630
});
3731
},
3832
});

cypress/support/commands.ts

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
/// <reference types="cypress" />
2-
3-
declare global {
4-
namespace Cypress {
5-
interface Chainable {
6-
/**
7-
* Login as the specified user using programmatic authentication
8-
* @param username - Username (email) to authenticate as
9-
* @param password - Password to authenticate with
10-
*/
11-
login(username: string, password: string): Chainable<void>;
12-
}
13-
}
14-
}
15-
161
/**
172
* Custom command for programmatic authentication
183
* Uses cy.session for caching per username-password combination
194
* Follows the same pattern as Playwright tests
205
*/
21-
Cypress.Commands.add("login", (username: string, password: string) => {
6+
Cypress.Commands.add("login", (username, password) => {
227
if (!username || !password) {
238
throw new Error("Both username and password are required");
249
}
@@ -29,14 +14,16 @@ Cypress.Commands.add("login", (username: string, password: string) => {
2914
cy.session(
3015
sessionId,
3116
() => {
32-
const workosApiKey = Cypress.env("WORKOS_API_KEY");
33-
const workosClientId = Cypress.env("WORKOS_CLIENT_ID");
34-
const baseURL = Cypress.env("TEST_BASE_URL");
17+
const workosApiKey = String(Cypress.env("WORKOS_API_KEY"));
18+
const workosClientId = String(Cypress.env("WORKOS_CLIENT_ID"));
19+
const cookieName =
20+
String(Cypress.env("WORKOS_COOKIE_NAME")) ?? "wos-session";
3521

36-
if (!workosApiKey || !workosClientId) {
37-
throw new Error(
38-
"Missing WORKOS_API_KEY or WORKOS_CLIENT_ID in Cypress environment"
39-
);
22+
if (!workosApiKey) {
23+
throw new Error("Missing WORKOS_API_KEY Cypress environment");
24+
}
25+
if (!workosClientId) {
26+
throw new Error("Missing WORKOS_CLIENT_ID Cypress environment");
4027
}
4128

4229
cy.log(`Authenticating user: ${username}`);
@@ -47,25 +34,10 @@ Cypress.Commands.add("login", (username: string, password: string) => {
4734
password: password,
4835
workosApiKey,
4936
workosClientId,
50-
}).then((authResponse: any) => {
37+
}).then((authResponse) => {
5138
cy.log("API authentication successful");
52-
53-
// Step 2: Call our test endpoint to save the session (same as Playwright)
54-
cy.request({
55-
method: "POST",
56-
url: `${baseURL}/api/test/set-session`,
57-
body: {
58-
user: authResponse.user,
59-
accessToken: authResponse.accessToken,
60-
refreshToken: authResponse.refreshToken,
61-
},
62-
}).then((response) => {
63-
expect(response.status).to.eq(200);
64-
cy.log("Session saved successfully");
65-
66-
// The endpoint sets the cookie
67-
// Cypress handles cookies from cy.request responses automatically
68-
});
39+
// set the wos-session cookie
40+
cy.setCookie(cookieName, authResponse.sealedSession!);
6941
});
7042
},
7143
{
@@ -74,8 +46,6 @@ Cypress.Commands.add("login", (username: string, password: string) => {
7446
cy.visit("/");
7547
cy.get("body").should("contain.text", "Welcome back");
7648
},
77-
}
49+
},
7850
);
7951
});
80-
81-
export {};

cypress/support/index.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
interface AuthenticateWithWorkOSParams {
2+
email: string;
3+
password: string;
4+
workosApiKey: string;
5+
workosClientId: string;
6+
}
7+
8+
interface AuthenticateWithWorkOSResponse {
9+
user: Record<string, unknown>;
10+
accessToken: string;
11+
refreshToken: string;
12+
sealedSession?: string;
13+
}
14+
15+
declare namespace Cypress {
16+
interface Chainable {
17+
login(username: string, password: string): Chainable<void>;
18+
19+
task(
20+
event: "authenticateWithWorkOS",
21+
arg: AuthenticateWithWorkOSParams,
22+
options?: Partial<Loggable & Timeoutable>,
23+
): Chainable<AuthenticateWithWorkOSResponse>;
24+
}
25+
}

cypress/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["es5", "dom", "dom.iterable", "esnext"],
5+
"types": ["cypress", "node"],
6+
"esModuleInterop": true,
7+
"moduleResolution": "node",
8+
"strict": true
9+
},
10+
"include": ["./**/*.ts"]
11+
}

tests/README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ TEST_BASE_URL=http://localhost:3000
2323
npm run test:playwright
2424
```
2525

26-
## Test Endpoint
27-
28-
The tests use the following API endpoint for programmatic authentication and session creation:
29-
30-
**Endpoint:** `POST /api/test/set-session`
31-
32-
- **Purpose:** Create session from authentication tokens
33-
- **Body:** `{ user, accessToken, refreshToken }`
34-
- **Response:** Uses WorkOS AuthKit's `saveSession` method to create encrypted session cookie
35-
36-
The endpoint is to be used for testing purposes only and is recommended to be disabled in production environments.
37-
3826
## Usage
3927

4028
**Import fixtures:**

0 commit comments

Comments
 (0)