Skip to content

Commit

Permalink
E2E: Bypass UI for item creation using using the API
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienClairembault committed May 6, 2024
1 parent 11d6b22 commit 474eca5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
11 changes: 8 additions & 3 deletions install/empty_data.php
Expand Up @@ -71,6 +71,11 @@ public function getEmptyData(): array
{
$tables = [];

// API need to be enabled to ease e2e testing
$is_testing = GLPI_ENVIRONMENT_TYPE === "testing";
$enable_api = $is_testing ? "1" : "0";
$enable_api_login_credentials = $is_testing ? "1" : "0";

$tables['glpi_apiclients'] = [
[
'id' => 1,
Expand Down Expand Up @@ -285,8 +290,8 @@ public function getEmptyData(): array
'highcontrast_css' => '0',
'default_central_tab' => '0',
'smtp_check_certificate' => '1',
'enable_api' => '0',
'enable_api_login_credentials' => '0',
'enable_api' => $enable_api,
'enable_api_login_credentials' => $enable_api_login_credentials,
'enable_api_login_external_token' => '1',
'login_remember_time' => '604800',
'login_remember_default' => '1',
Expand Down Expand Up @@ -9128,7 +9133,7 @@ public function getEmptyData(): array
];

// Test environment data
if (GLPI_ENVIRONMENT_TYPE === 'testing') {
if ($is_testing) {
$root_entity = array_filter($tables['glpi_entities'], static fn ($e) => $e['id'] === 0);
$e2e_entity = array_shift($root_entity);
$e2e_entity = array_replace($e2e_entity, [
Expand Down
55 changes: 55 additions & 0 deletions tests/cypress/e2e/itil_followups.cy.js
@@ -0,0 +1,55 @@
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

describe("ITIL Followups", () => {
it("can add a followup to a new ticket", () => {
cy.createWithAPI("Ticket", {
name: "Open ticket"
}).then((id) => {
cy.login();
cy.visit(`/front/ticket.form.php?id=${id}`);
cy.findByRole('button', {name: "Answer"}).should('exist');
});
});

it("can't add a followup to a closed ticket", () => {
cy.createWithAPI("Ticket", {
name: "Closed ticket",
status: 6,
}).then((id) => {
cy.login();
cy.visit(`/front/ticket.form.php?id=${id}`);
cy.findByRole('button', {name: "Answer"}).should('not.exist');
});
});
});
43 changes: 43 additions & 0 deletions tests/cypress/support/commands.js
Expand Up @@ -33,6 +33,8 @@

import _ from 'lodash';

let api_token = null;

/**
* @memberof cy
* @method login
Expand Down Expand Up @@ -278,3 +280,44 @@ Cypress.Commands.add("getMany", (names) => {
}
return cy.wrap(values);
});

Cypress.Commands.add("createWithAPI", (url, values) => {
return cy.initApi().doApiRequest("POST", url, values).then(response => {
return response.body.id;
});
});

Cypress.Commands.add("updateWithAPI", (url, values) => {
cy.initApi().doApiRequest("PUT", url, values);
});

Cypress.Commands.add("initApi", () => {
if (api_token !== null) {
return api_token;
}

return cy.request({
auth: {
'user': 'e2e_tests',
'pass': 'glpi',
},
method: 'POST',
url: '/apirest.php/initSession',
}).then((response) => {
api_token = response.body.session_token;
return api_token;
});
});

Cypress.Commands.add("doApiRequest", {prevSubject: true}, (token, method, endpoint, values) => {
return cy.request({
method: method,
url: '/apirest.php/' + encodeURI(endpoint),
body: {input: values},
headers: {
'Session-Token': token,
}
}).then((response) => {
return response;
});
});

0 comments on commit 474eca5

Please sign in to comment.