1616
1717import Utils from "@support/utils" ;
1818
19- describe . skip ( "Application tests" , ( ) => {
20- const { developer, password } = Utils . getUserInfo ( ) ;
19+ describe ( "Application tests" , ( ) => {
20+ const { publisher , developer, password } = Utils . getUserInfo ( ) ;
2121 const appName = Utils . generateName ( ) ;
2222 const appDescription = 'Key gen application description' ;
23+ const apiVersion = '2.0.0' ;
24+ const apiName = Utils . generateName ( ) ;
25+ const apiContext = apiName ;
26+ let appCreated = false ;
27+ let testApiId ;
28+
29+ const createAppForTest = ( ) => {
30+ cy . intercept ( '**/application-attributes' ) . as ( 'attrGet' ) ;
31+ cy . intercept ( 'POST' , '**/applications' ) . as ( 'createAppReq' ) ;
32+ cy . visit ( '/devportal/applications/create?tenant=carbon.super' ) ;
33+ cy . wait ( '@attrGet' , { timeout : 300000 } ) ;
34+
35+ cy . get ( '#application-name' ) . type ( appName ) ;
36+ cy . get ( '#application-description' ) . clear ( ) . type ( appDescription ) ;
37+
38+ // Some versions require selecting the quota explicitly before save.
39+ cy . get ( 'body' ) . then ( ( $body ) => {
40+ if ( $body . find ( '#per-token-quota' ) . length > 0 ) {
41+ cy . get ( '#per-token-quota' ) . click ( { force : true } ) ;
42+ cy . contains ( 'li' , 'Unlimited' ) . click ( { force : true } ) ;
43+ }
44+ } ) ;
45+
46+ cy . get ( '#itest-application-create-save' ) . click ( { force : true } ) ;
47+
48+ // Prefer API success + listing verification over fragile UI timing checks.
49+ cy . wait ( '@createAppReq' , { timeout : 120000 } ) . then ( ( { response } ) => {
50+ expect ( response , 'application create response' ) . to . exist ;
51+ expect ( [ 200 , 201 , 202 ] , 'application create status' ) . to . include ( response . statusCode ) ;
52+ appCreated = true ;
53+ } ) ;
54+ } ;
55+ const openSecurityRestrictionSelect = ( ) => {
56+ cy . get ( '[role="dialog"]' ) . last ( ) . within ( ( ) => {
57+ cy . contains ( 'label' , 'Security Restriction' )
58+ . parents ( '.MuiFormControl-root' )
59+ . find ( '[role="combobox"]' )
60+ . click ( ) ;
61+ } ) ;
62+ } ;
63+
64+ const selectSecurityRestriction = ( value ) => {
65+ openSecurityRestrictionSelect ( ) ;
66+ cy . get ( `li[data-value="${ value } "]` ) . click ( ) ;
67+ } ;
68+
2369 const checkIfKeyExists = ( ) => {
24- // Check if the key exists
25- cy . get ( '#access-token' , { timeout : 30000 } ) ;
26- cy . get ( '#access-token' ) . should ( 'not.be.empty' ) ;
27- cy . get ( '#generate-api-keys-close-btn' ) . click ( ) ;
28- }
29- it . only ( "Generate API Keys" , ( ) => {
30- cy . loginToDevportal ( developer , password ) ;
31- cy . createApp ( appName , appDescription ) ;
32-
33- // Generating keys production
34- cy . get ( '#production-keys-apikey' ) . click ( ) ;
35- // Generate with none option
36- cy . get ( '#generate-key-btn' ) . then ( ( ) => {
37- cy . get ( '#generate-key-btn' ) . click ( ) ;
38- cy . get ( '#generate-api-keys-btn' ) . click ( ) ;
39- } )
70+ // Check if the generated key is shown in the success view.
71+ cy . get ( '[role="dialog"]' ) . last ( ) . within ( ( ) => {
72+ cy . get ( '#api-key-value' , { timeout : 30000 } ) . should ( 'not.be.empty' ) ;
73+ cy . contains ( 'button' , 'Close' ) . click ( ) ;
74+ } ) ;
75+ // Ensure the modal is fully closed before starting the next key generation.
76+ cy . get ( '[role="dialog"]' ) . should ( 'not.exist' ) ;
77+ } ;
4078
41- checkIfKeyExists ( ) ;
79+ const generateApiKey = ( name , restrictionType = 'none' , restrictionValue = '' ) => {
80+ cy . contains ( 'button' , 'Generate API Key' , { timeout : 30000 } )
81+ . should ( 'be.visible' )
82+ . should ( 'not.be.disabled' ) ;
83+ cy . contains ( 'button' , 'Generate API Key' , { timeout : 30000 } ) . click ( { force : true } ) ;
84+ cy . get ( '[role="dialog"]' ) . last ( ) . within ( ( ) => {
85+ cy . contains ( 'label' , 'Name' ) . parents ( '.MuiFormControl-root' ) . find ( 'input' ) . type ( name ) ;
86+ } ) ;
4287
43- // Generate with ip option
44- cy . get ( '#api-key-restriction-ip' ) . click ( ) ;
45- cy . get ( '#ip-address-txt' ) . type ( '192.168.1.2' ) ;
46- cy . get ( '#ip-address-add-btn' ) . click ( ) ;
47- cy . get ( '#generate-key-btn' ) . click ( ) ;
48- cy . get ( '#generate-api-keys-btn' ) . click ( ) ;
88+ if ( restrictionType !== 'none' ) {
89+ selectSecurityRestriction ( restrictionType ) ;
90+ const restrictionLabel = restrictionType === 'ip' ? 'IP Address' : 'Referrer URL' ;
91+ cy . get ( '[role="dialog"]' ) . last ( ) . within ( ( ) => {
92+ cy . contains ( 'label' , restrictionLabel )
93+ . parents ( '.MuiFormControl-root' )
94+ . find ( 'input' )
95+ . type ( restrictionValue ) ;
96+ } ) ;
97+ }
4998
99+ cy . get ( '[role="dialog"]' ) . last ( ) . within ( ( ) => {
100+ // Re-query before click to avoid detached-element failures during dialog re-renders.
101+ cy . contains ( 'button' , / ^ G e n e r a t e A P I K e y $ / , { timeout : 30000 } ) . should ( 'be.visible' ) ;
102+ cy . contains ( 'button' , / ^ G e n e r a t e A P I K e y $ / , { timeout : 30000 } ) . click ( ) ;
103+ } ) ;
50104 checkIfKeyExists ( ) ;
105+ } ;
51106
52- cy . get ( '#api-key-restriction-referer' ) . click ( ) ;
53- cy . get ( '#referer-txt' ) . type ( 'www.example.com/path' ) ;
54- cy . get ( '#referer-add-btn' ) . click ( ) ;
55- cy . get ( '#generate-key-btn' ) . click ( ) ;
56- cy . get ( '#generate-api-keys-btn' ) . click ( ) ;
107+ const openRuntimeConfigurations = ( ) => {
108+ cy . get ( 'body' , { timeout : 60000 } ) . then ( ( $body ) => {
109+ if ( $body . find ( '#itest-api-details-api-config-acc' ) . length > 0 ) {
110+ cy . get ( '#itest-api-details-api-config-acc' ) . click ( { force : true } ) ;
111+ } else if ( $body . find ( '#itest-api-config' ) . length > 0 ) {
112+ cy . get ( '#itest-api-config' ) . click ( { force : true } ) ;
113+ }
114+ } ) ;
57115
58- checkIfKeyExists ( ) ;
59- } )
116+ cy . get ( '#left-menu-itemRuntimeConfigurations' , { timeout : 100000 } ) . click ( { force : true } ) ;
117+ } ;
118+
119+ it ( "Generate API Keys" , ( ) => {
120+ cy . loginToPublisher ( publisher , password ) ;
121+ Utils . addAPIWithEndpoints ( { name : apiName , version : apiVersion , context : apiContext } ) . then ( ( apiId ) => {
122+ testApiId = apiId ;
123+ cy . intercept ( 'PUT' , `**/apis/${ apiId } ` ) . as ( 'saveRuntimeConfig' ) ;
124+ cy . visit ( `/publisher/apis/${ apiId } /overview` ) ;
125+ openRuntimeConfigurations ( ) ;
126+ cy . get ( '#applicationLevel' , { timeout : 30000 } ) . should ( 'be.visible' ) ;
127+ cy . get ( '#applicationLevel' ) . then ( ( $panel ) => {
128+ if ( ! $panel . hasClass ( 'Mui-expanded' ) ) {
129+ cy . wrap ( $panel ) . find ( '.MuiAccordionSummary-root' ) . click ( ) ;
130+ }
131+ } ) ;
132+ cy . get ( '#api-security-api-key-checkbox' , { timeout : 15000 } ) . should ( 'exist' ) ;
133+ cy . get ( '#api-security-api-key-checkbox' ) . check ( { force : true } ) ;
134+ cy . get ( '#save-runtime-configurations' ) . click ( ) ;
135+ cy . wait ( '@saveRuntimeConfig' , { timeout : 30000 } ) ;
136+
137+ Utils . publishAPI ( apiId ) . then ( ( ) => {
138+ cy . logoutFromPublisher ( ) ;
139+ cy . loginToDevportal ( developer , password ) ;
140+
141+ createAppForTest ( ) ;
142+
143+ cy . visit ( `/devportal/apis/${ apiId } /api-keys?tenant=carbon.super` ) ;
144+ cy . url ( { timeout : 30000 } ) . should ( 'contain' , `/apis/${ apiId } /api-keys` ) ;
145+ cy . contains ( 'API Keys' , { timeout : 30000 } ) . should ( 'exist' ) ;
146+
147+ // Generate without restriction.
148+ generateApiKey ( `${ appName } -none` ) ;
149+
150+ // Generate with IP restriction.
151+ generateApiKey ( `${ appName } -ip` , 'ip' , '192.168.1.2' ) ;
152+
153+ // Generate with referrer restriction.
154+ generateApiKey ( `${ appName } -referrer` , 'referrer' , 'https://www.example.com/path' ) ;
155+ } ) ;
156+ } ) ;
157+ } ) ;
60158
61159 after ( ( ) => {
62- cy . deleteApp ( appName ) ;
63- } )
64- } )
160+ if ( appCreated ) {
161+ cy . deleteApp ( appName ) ;
162+ } else {
163+ cy . log ( `Skipping deleteApp for ${ appName } because app creation did not complete.` ) ;
164+ }
165+ if ( testApiId ) {
166+ Utils . deleteAPI ( testApiId ) ;
167+ }
168+ } ) ;
169+ } ) ;
0 commit comments