1+ import { guidedTour } from '../../views/tour' ;
2+ import { nav } from '../../views/nav' ;
3+
4+
5+
6+ export { } ;
7+ declare global {
8+ namespace Cypress {
9+ interface Chainable {
10+ switchPerspective ( perspective : string ) ;
11+ uiLogin ( provider : string , username : string , password : string , oauthurl ?: string ) ;
12+ uiLogout ( ) ;
13+ cliLogin ( username ?, password ?, hostapi ?) ;
14+ cliLogout ( ) ;
15+ login ( provider ?: string , username ?: string , password ?: string , oauthurl ?: string ) : Chainable < Element > ;
16+ adminCLI ( command : string , options ?) ;
17+ executeAndDelete ( command : string ) ;
18+ }
19+ }
20+ }
21+
22+
23+ Cypress . Commands . add (
24+ 'login' ,
25+ (
26+ provider : string = Cypress . env ( 'LOGIN_IDP' ) ,
27+ username : string = Cypress . env ( 'LOGIN_USERNAME' ) ,
28+ password : string = Cypress . env ( 'LOGIN_PASSWORD' ) ,
29+ oauthurl : string ,
30+ ) => {
31+ cy . session (
32+ [ provider , username ] ,
33+ ( ) => {
34+ cy . visit ( Cypress . config ( 'baseUrl' ) ) ;
35+ cy . log ( 'Session - after visiting' ) ;
36+ cy . window ( ) . then (
37+ (
38+ win : any , // eslint-disable-line @typescript-eslint/no-explicit-any
39+ ) => {
40+ // Check if auth is disabled (for a local development environment)
41+ if ( win . SERVER_FLAGS ?. authDisabled ) {
42+ cy . task ( 'log' , ' skipping login, console is running with auth disabled' ) ;
43+ return ;
44+ }
45+ cy . exec (
46+ `oc get node --selector=hypershift.openshift.io/managed --kubeconfig ${ Cypress . env ( 'KUBECONFIG_PATH' ) } ` ,
47+ ) . then ( ( result ) => {
48+ cy . log ( result . stdout ) ;
49+ cy . task ( 'log' , result . stdout ) ;
50+ if ( result . stdout . includes ( 'Ready' ) ) {
51+ cy . log ( `Attempting login via cy.origin to: ${ oauthurl } ` ) ;
52+ cy . task ( 'log' , `Attempting login via cy.origin to: ${ oauthurl } ` ) ;
53+ cy . origin (
54+ oauthurl ,
55+ { args : { username, password } } ,
56+ ( { username, password } ) => {
57+ cy . get ( '#inputUsername' ) . type ( username ) ;
58+ cy . get ( '#inputPassword' ) . type ( password ) ;
59+ cy . get ( 'button[type=submit]' ) . click ( ) ;
60+ } ,
61+ ) ;
62+ } else {
63+ cy . task ( 'log' , ` Logging in as ${ username } using fallback on ${ oauthurl } ` ) ;
64+ cy . origin (
65+ oauthurl ,
66+ { args : { provider, username, password } } ,
67+ ( { provider, username, password } ) => {
68+ cy . get ( '[data-test-id="login"]' ) . should ( 'be.visible' ) ;
69+ cy . get ( 'body' ) . then ( ( $body ) => {
70+ if ( $body . text ( ) . includes ( provider ) ) {
71+ cy . contains ( provider ) . should ( 'be.visible' ) . click ( ) ;
72+ }
73+ } ) ;
74+ cy . get ( '#inputUsername' ) . type ( username ) ;
75+ cy . get ( '#inputPassword' ) . type ( password ) ;
76+ cy . get ( 'button[type=submit]' ) . click ( ) ;
77+ }
78+ ) ;
79+ }
80+ } ) ;
81+ } ,
82+ ) ;
83+ } ,
84+ {
85+ cacheAcrossSpecs : true ,
86+ validate ( ) {
87+ cy . byTestID ( "username" , { timeout : 120000 } ) . should ( 'be.visible' ) ;
88+ guidedTour . close ( ) ;
89+ } ,
90+ } ,
91+ ) ;
92+ } ,
93+ ) ;
94+
95+ Cypress . Commands . add ( 'switchPerspective' , ( perspective : string ) => {
96+ /* If side bar is collapsed then expand it
97+ before switching perspecting */
98+ cy . get ( 'body' ) . then ( ( body ) => {
99+ if ( body . find ( '.pf-m-collapsed' ) . length > 0 ) {
100+ cy . get ( '#nav-toggle' ) . click ( ) ;
101+ }
102+ } ) ;
103+ nav . sidenav . switcher . changePerspectiveTo ( perspective ) ;
104+ nav . sidenav . switcher . shouldHaveText ( perspective ) ;
105+ } ) ;
106+
107+ // To avoid influence from upstream login change
108+ Cypress . Commands . add ( 'uiLogin' , ( provider : string , username : string , password : string ) => {
109+ cy . log ( 'Commands uiLogin' ) ;
110+ cy . clearCookie ( 'openshift-session-token' ) ;
111+ cy . visit ( '/' ) ;
112+ cy . window ( ) . then (
113+ (
114+ win : any , // eslint-disable-line @typescript-eslint/no-explicit-any
115+ ) => {
116+ if ( win . SERVER_FLAGS ?. authDisabled ) {
117+ cy . task ( 'log' , 'Skipping login, console is running with auth disabled' ) ;
118+ return ;
119+ }
120+ cy . get ( '[data-test-id="login"]' ) . should ( 'be.visible' ) ;
121+ cy . get ( 'body' ) . then ( ( $body ) => {
122+ if ( $body . text ( ) . includes ( provider ) ) {
123+ cy . contains ( provider ) . should ( 'be.visible' ) . click ( ) ;
124+ } else if ( $body . find ( 'li.idp' ) . length > 0 ) {
125+ //Using the last idp if doesn't provider idp name
126+ cy . get ( 'li.idp' ) . last ( ) . click ( ) ;
127+ }
128+ } ) ;
129+ cy . get ( '#inputUsername' ) . type ( username ) ;
130+ cy . get ( '#inputPassword' ) . type ( password ) ;
131+ cy . get ( 'button[type=submit]' ) . click ( ) ;
132+ cy . byTestID ( 'username' , { timeout : 120000 } ) . should ( 'be.visible' ) ;
133+ } ,
134+ ) ;
135+ cy . switchPerspective ( 'Administrator' ) ;
136+ } ) ;
137+
138+ Cypress . Commands . add ( 'uiLogout' , ( ) => {
139+ cy . window ( ) . then (
140+ (
141+ win : any , // eslint-disable-line @typescript-eslint/no-explicit-any
142+ ) => {
143+ if ( win . SERVER_FLAGS ?. authDisabled ) {
144+ cy . log ( 'Skipping logout, console is running with auth disabled' ) ;
145+ return ;
146+ }
147+ cy . log ( 'Log out UI' ) ;
148+ cy . byTestID ( 'username' ) . click ( ) ;
149+ cy . byTestID ( 'log-out' ) . should ( 'be.visible' ) ;
150+ cy . byTestID ( 'log-out' ) . click ( { force : true } ) ;
151+ } ,
152+ ) ;
153+ } ) ;
154+
155+ Cypress . Commands . add ( 'cliLogin' , ( username ?, password ?, hostapi ?) => {
156+ const loginUsername = username || Cypress . env ( 'LOGIN_USERNAME' ) ;
157+ const loginPassword = password || Cypress . env ( 'LOGIN_PASSWORD' ) ;
158+ const hostapiurl = hostapi || Cypress . env ( 'HOST_API' ) ;
159+ cy . exec (
160+ `oc login -u ${ loginUsername } -p ${ loginPassword } ${ hostapiurl } --insecure-skip-tls-verify=true` ,
161+ { failOnNonZeroExit : false } ,
162+ ) . then ( ( result ) => {
163+ cy . log ( result . stderr ) ;
164+ cy . log ( result . stdout ) ;
165+ } ) ;
166+ } ) ;
167+
168+ Cypress . Commands . add ( 'cliLogout' , ( ) => {
169+ cy . exec ( `oc logout` , { failOnNonZeroExit : false } ) . then ( ( result ) => {
170+ cy . log ( result . stderr ) ;
171+ cy . log ( result . stdout ) ;
172+ } ) ;
173+ } ) ;
174+
175+ Cypress . Commands . add ( 'adminCLI' , ( command : string ) => {
176+ const kubeconfig = Cypress . env ( 'KUBECONFIG_PATH' ) ;
177+ cy . log ( `Run admin command: ${ command } ` ) ;
178+ cy . exec ( `${ command } --kubeconfig ${ kubeconfig } ` ) ;
179+ } ) ;
180+
181+ Cypress . Commands . add ( 'executeAndDelete' , ( command : string ) => {
182+ cy . exec ( command , { failOnNonZeroExit : false } )
183+ . then ( result => {
184+ if ( result . code !== 0 ) {
185+ cy . task ( 'logError' , `Command "${ command } " failed: ${ result . stderr || result . stdout } ` ) ;
186+ } else {
187+ cy . task ( 'log' , `Command "${ command } " executed successfully` ) ;
188+ }
189+ } ) ;
190+ } ) ;
0 commit comments