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