Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dedicated e2e login tests #17014

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions front/csrf.php
@@ -0,0 +1,48 @@
<?php

/**
* ---------------------------------------------------------------------
*
* 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/>.
*
* ---------------------------------------------------------------------
*/

$SECURITY_STRATEGY = 'no_check';

include('../inc/includes.php');

if (GLPI_ENVIRONMENT_TYPE !== 'testing') {
http_response_code(400);
die;
}

header('Content-Type: application/json');
echo json_encode([
'token' => Session::getNewCSRFToken()
]);
7 changes: 7 additions & 0 deletions front/login.php
Expand Up @@ -46,6 +46,13 @@

include('../inc/includes.php');

if (GLPI_ENVIRONMENT_TYPE === 'testing' && !isset($_SESSION['namfield'])) {
// Direct login attempt by the e2e tests.
$_SESSION['namfield'] = "username";
$_SESSION['pwdfield'] = "password";
$_SESSION['rmbfield'] = "remember_me";
$_SESSION["glpicookietest"] = 'testcookie';
}

if (!isset($_SESSION["glpicookietest"]) || ($_SESSION["glpicookietest"] != 'testcookie')) {
if (!Session::canWriteSessionFiles()) {
Expand Down
53 changes: 53 additions & 0 deletions tests/cypress/e2e/login.cy.js
@@ -0,0 +1,53 @@
/**
* ---------------------------------------------------------------------
*
* 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('Login tests', () => {
it('can login from the local database', () => {
cy.visit('/');
cy.title().should('eq', 'Authentication - GLPI');
cy.findByRole('textbox', {'name': "Login"}).type('e2e_tests');
cy.findByLabelText("Password").type('glpi');
cy.findByRole('checkbox', {name: "Remember me"}).check();
// Select 'local' from the 'auth' dropdown
cy.findByLabelText("Login source").select('local', { force: true });

cy.findByRole('button', {name: "Sign in"}).click();
// After logging in, the url should contain /front/central.php or /front/helpdesk.public.php
cy.url().should('match', /\/front\/(central|helpdesk.public).php/);

cy.getCookies().should('have.length.gte', 2).then((cookies) => {
// Should be two cookies starting with 'glpi_' and one of them should end with '_rememberme'
expect(cookies.filter((cookie) => cookie.name.startsWith('glpi_'))).to.have.length(2);
expect(cookies.filter((cookie) => cookie.name.startsWith('glpi_') && cookie.name.endsWith('_rememberme'))).to.have.length(1);
});
});
});
28 changes: 14 additions & 14 deletions tests/cypress/support/commands.js
Expand Up @@ -45,20 +45,20 @@ Cypress.Commands.add('login', (username = 'e2e_tests', password = 'glpi') => {
cy.session(
username,
() => {
cy.blockGLPIDashboards();
cy.visit('/');
cy.title().should('eq', 'Authentication - GLPI');
cy.findByRole('textbox', {'name': "Login"}).type(username);
cy.findByLabelText("Password").type(password);
cy.findByRole('checkbox', {name: "Remember me"}).check();
// Select 'local' from the 'auth' dropdown
cy.findByLabelText("Login source").select('local', { force: true });
// TODO: should be
// cy.findByRole('combobox', {name: "Login source"}).select2('local', { force: true });

cy.findByRole('button', {name: "Sign in"}).click();
// After logging in, the url should contain /front/central.php or /front/helpdesk.public.php
cy.url().should('match', /\/front\/(central|helpdesk.public).php/);
cy.request('/front/csrf.php').its('body.token').then((csrf) => {
cy.request({
method: 'POST',
url: '/front/login.php',
form: true,
body: {
username : username,
password : password,
remember_me : 'on',
auth : 'local',
_glpi_csrf_token: csrf,
}
});
});
},
{
validate: () => {
Expand Down