1- /* TODO:
2- * - Inline arguments as much as possible.
3- * e.g. `cmkUsername` as param -> Cypress.env('cmkUsername') in function body.
4- * (we can't call it with anything else, so I don't see the point in having to
5- * specify them at each call site.)
6- * - Move these helpers to custom Cypress commands.
7- * See https://docs.cypress.io/api/cypress-api/custom-commands
8- * This way, they integrate better into the framework.
9- */
1+ export { } ;
102
11- export function createCmkAutomationUser ( cmkUser : string , cmkPassword : string ) {
3+ const cmkAutomationUser = 'cmkuser' ;
4+ const cmkAutomationPassword = 'somepassword123457' ;
5+
6+ Cypress . Commands . add ( 'createCmkAutomationUser' , ( ) => {
127 cy . request ( {
138 method : 'POST' ,
149 url : Cypress . env ( 'cypressToCheckmkUrl' ) + '/check_mk/api/1.0/domain-types/user_config/collections/all' ,
1510 auth : {
1611 bearer : `${ Cypress . env ( 'cmkUsername' ) } ${ Cypress . env ( 'cmkPassword' ) } ` ,
1712 } ,
1813 body : {
19- username : cmkUser ,
20- fullname : cmkUser ,
14+ username : cmkAutomationUser ,
15+ fullname : cmkAutomationUser ,
2116 roles : [ 'admin' ] ,
2217 auth_option : {
2318 auth_type : 'automation' ,
24- secret : cmkPassword ,
19+ secret : cmkAutomationPassword ,
2520 } ,
2621 } ,
2722 } ) ;
28- }
23+ } ) ;
2924
30- export function deleteCmkAutomationUser ( cmkUser : string , cmkPassowrd : string , failOnStatusCode : boolean = true ) {
25+ Cypress . Commands . add ( 'deleteCmkAutomationUser' , ( failOnStatusCode : boolean ) => {
3126 cy . request ( {
3227 method : 'DELETE' ,
33- url : Cypress . env ( 'cypressToCheckmkUrl' ) + '/check_mk/api/1.0/objects/user_config/' + cmkUser ,
28+ url : Cypress . env ( 'cypressToCheckmkUrl' ) + '/check_mk/api/1.0/objects/user_config/' + cmkAutomationUser ,
3429 auth : {
3530 bearer : `${ Cypress . env ( 'cmkUsername' ) } ${ Cypress . env ( 'cmkPassword' ) } ` ,
3631 } ,
3732 failOnStatusCode : failOnStatusCode ,
3833 } ) ;
39- }
34+ } ) ;
4035
41- export function createCmkHost ( hostName : string ) {
36+ Cypress . Commands . add ( ' createCmkHost' , ( hostName : string ) => {
4237 cy . request ( {
4338 method : 'POST' ,
4439 url : Cypress . env ( 'cypressToCheckmkUrl' ) + '/check_mk/api/1.0/domain-types/host_config/collections/all' ,
@@ -55,9 +50,9 @@ export function createCmkHost(hostName: string) {
5550 } ,
5651 } ,
5752 } ) ;
58- }
53+ } ) ;
5954
60- export function deleteCmkHost ( hostName : string ) {
55+ Cypress . Commands . add ( ' deleteCmkHost' , ( hostName : string ) => {
6156 cy . request ( {
6257 method : 'DELETE' ,
6358 url : Cypress . env ( 'cypressToCheckmkUrl' ) + '/check_mk/api/1.0/objects/host_config/' + hostName ,
@@ -66,16 +61,16 @@ export function deleteCmkHost(hostName: string) {
6661 bearer : `${ Cypress . env ( 'cmkUsername' ) } ${ Cypress . env ( 'cmkPassword' ) } ` ,
6762 } ,
6863 } ) ;
69- }
64+ } ) ;
7065
71- function waitForActivation ( activation_id : string , waitingTime : number = 1000 ) {
66+ Cypress . Commands . add ( ' waitForActivation' , ( activationID : string , waitingTime : number ) => {
7267 cy . wait ( waitingTime ) ;
7368 cy . request ( {
7469 method : 'GET' ,
7570 url :
7671 Cypress . env ( 'cypressToCheckmkUrl' ) +
7772 '/check_mk/api/1.0/objects/activation_run/' +
78- activation_id +
73+ activationID +
7974 '/actions/wait-for-completion/invoke' ,
8075 followRedirect : false ,
8176 headers : { accept : 'application/json' } ,
@@ -85,11 +80,11 @@ function waitForActivation(activation_id: string, waitingTime: number = 1000) {
8580 } ) . then ( ( response ) => {
8681 if ( response . status === 204 ) return ;
8782
88- waitForActivation ( activation_id ) ;
83+ cy . waitForActivation ( activationID , 1000 ) ;
8984 } ) ;
90- }
85+ } ) ;
9186
92- export function activateCmkChanges ( siteName : string ) {
87+ Cypress . Commands . add ( ' activateCmkChanges' , ( siteName : string ) => {
9388 cy . on ( 'uncaught:exception' , ( err , runnable ) => {
9489 // activating changes is raising an uncaught exception
9590 // with no error message.
@@ -124,11 +119,11 @@ export function activateCmkChanges(siteName: string) {
124119 const activation_id = response . body . id ;
125120 cy . log ( 'Activation ID: ' + activation_id ) ;
126121
127- waitForActivation ( activation_id ) ;
122+ cy . waitForActivation ( activation_id , 1000 ) ;
128123 } ) ;
129- }
124+ } ) ;
130125
131- export function executeServiceDiscovery ( hostName : string , mode : string ) {
126+ Cypress . Commands . add ( ' executeServiceDiscovery' , ( hostName : string , mode : string ) => {
132127 cy . request ( {
133128 method : 'POST' ,
134129 url :
@@ -145,4 +140,18 @@ export function executeServiceDiscovery(hostName: string, mode: string) {
145140 } ) . then ( ( response ) => {
146141 expect ( response . status ) . is . equal ( 200 ) ;
147142 } ) ;
143+ } ) ;
144+
145+ declare global {
146+ namespace Cypress {
147+ interface Chainable {
148+ createCmkAutomationUser ( ) : Chainable < void > ;
149+ deleteCmkAutomationUser ( failOnStatusCode : boolean ) : Chainable < void > ;
150+ createCmkHost ( hostName : string ) : Chainable < void > ;
151+ deleteCmkHost ( hostName : string ) : Chainable < void > ;
152+ waitForActivation ( activationID : string , waitingTime : number ) : Chainable < void > ;
153+ activateCmkChanges ( siteName : string ) : Chainable < void > ;
154+ executeServiceDiscovery ( hotname : string , mode : string ) : Chainable < void > ;
155+ }
156+ }
148157}
0 commit comments