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 { } ;
0 commit comments