Skip to content

Commit cfe31b1

Browse files
committed
Fix cypress
1 parent 986721c commit cfe31b1

6 files changed

Lines changed: 57 additions & 97 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: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
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-
101
/**
112
* WorkOS authentication plugin for Cypress
123
* Provides tasks for programmatic authentication using WorkOS SDK
134
*/
145
export function registerWorkOSTasks(
15-
on: PluginEvents,
16-
config: PluginConfigOptions
6+
on: Cypress.PluginEvents,
7+
config: Cypress.PluginConfigOptions,
178
) {
189
on("task", {
19-
authenticateWithWorkOS({
20-
email,
21-
password,
22-
workosApiKey,
23-
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-
28-
const workos = new WorkOS(workosApiKey, {
29-
apiHostname: process.env.WORKOS_API_HOSTNAME,
10+
async authenticateWithWorkOS({ email, password }) {
11+
const { WorkOS } = await import("@workos-inc/node");
12+
const workos = new WorkOS(process.env.WORKOS_API_KEY!, {
13+
apiHostname: process.env.WORKOS_API_HOSTNAME!,
3014
});
3115

3216
return workos.userManagement.authenticateWithPassword({
33-
clientId: workosClientId,
17+
clientId: process.env.WORKOS_CLIENT_ID!,
3418
email,
3519
password,
20+
session: {
21+
sealSession: true,
22+
cookiePassword: process.env.WORKOS_COOKIE_PASSWORD,
23+
},
3624
});
3725
},
3826
});

cypress/support/commands.ts

Lines changed: 12 additions & 52 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,53 +14,28 @@ 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");
35-
36-
if (!workosApiKey || !workosClientId) {
37-
throw new Error(
38-
"Missing WORKOS_API_KEY or WORKOS_CLIENT_ID in Cypress environment"
39-
);
40-
}
17+
const cookieName = Cypress.env("WORKOS_COOKIE_NAME") ?? "wos-session";
4118

4219
cy.log(`Authenticating user: ${username}`);
4320

4421
// Step 1: Authenticate with WorkOS API directly (same as Playwright)
45-
cy.task("authenticateWithWorkOS", {
46-
email: username, // Treat username as email
47-
password: password,
48-
workosApiKey,
49-
workosClientId,
50-
}).then((authResponse: any) => {
51-
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
22+
return cy
23+
.task("authenticateWithWorkOS", {
24+
email: username, // Treat username as email
25+
password: password,
26+
})
27+
.then((authResponse) => {
28+
cy.log("API authentication successful");
29+
// set the wos-session cookie
30+
cy.setCookie(cookieName, authResponse.sealedSession!);
6831
});
69-
});
7032
},
7133
{
7234
validate() {
7335
// Validate that the session is still valid by checking authenticated state
7436
cy.visit("/");
7537
cy.get("body").should("contain.text", "Welcome back");
7638
},
77-
}
39+
},
7840
);
7941
});
80-
81-
export {};

cypress/support/index.d.ts

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

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)