Skip to content

Commit 0859d03

Browse files
committed
Move 'api_helpers' functions to Cypress 'api_commands'
In this way we make all API-related functions new Cypress custom commands. Moreover, redundant input parameters are removed where possible.
1 parent 9285f8c commit 0859d03

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

tests/cypress/e2e/spec.cy.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1+
import '../support/api_commands';
12
import '../support/commands';
2-
import {
3-
activateCmkChanges,
4-
createCmkAutomationUser,
5-
createCmkHost,
6-
deleteCmkAutomationUser,
7-
deleteCmkHost,
8-
executeServiceDiscovery,
9-
} from './api_helpers';
103
import {
114
inputFilterSelector,
125
inputGraphTypeSelector,
@@ -28,15 +21,15 @@ describe('e2e tests', () => {
2821
const hostName1 = 'localhost_grafana1';
2922

3023
before(function () {
31-
deleteCmkAutomationUser(cmkUser, cmkPassword, false); // clean-up possible existing user
32-
createCmkAutomationUser(cmkUser, cmkPassword);
24+
cy.deleteCmkAutomationUser(false); // clean-up possible existing user
25+
cy.createCmkAutomationUser();
3326

34-
createCmkHost(hostName0);
35-
createCmkHost(hostName1);
27+
cy.createCmkHost(hostName0);
28+
cy.createCmkHost(hostName1);
3629

37-
executeServiceDiscovery(hostName0, 'new');
38-
executeServiceDiscovery(hostName1, 'new');
39-
activateCmkChanges('cmk');
30+
cy.executeServiceDiscovery(hostName0, 'new');
31+
cy.executeServiceDiscovery(hostName1, 'new');
32+
cy.activateCmkChanges('cmk');
4033

4134
cy.loginGrafana();
4235
cy.addCmkDatasource(cmkUser, cmkPassword);
@@ -170,10 +163,10 @@ describe('e2e tests', () => {
170163
after(function () {
171164
cy.rmCmkDatasource();
172165

173-
deleteCmkHost(hostName0);
174-
deleteCmkHost(hostName1);
166+
cy.deleteCmkHost(hostName0);
167+
cy.deleteCmkHost(hostName1);
175168

176-
deleteCmkAutomationUser(cmkUser, cmkPassword);
177-
activateCmkChanges('cmk');
169+
cy.deleteCmkAutomationUser(true);
170+
cy.activateCmkChanges('cmk');
178171
});
179172
});
Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
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

Comments
 (0)