Skip to content
Snippets Groups Projects
Commit c06ea059 authored by Fabrice Gangler's avatar Fabrice Gangler :art:
Browse files

test: add fonctional tests (HomePage, HeathCheck)

parent fc2a42bd
No related branches found
No related tags found
1 merge request!29Feat user login change password forgot password
Pipeline #78584 failed
<?php
/*
* This file is part of the Comptoir-du-Libre software.
* <https://gitlab.adullact.net/Comptoir/comptoir-du-libre>
*
* Copyright (c) ADULLACT <https://adullact.org>
* Association des Développeurs et Utilisateurs de Logiciels Libres
* pour les Administrations et les Collectivités Territoriales
*
* Comptoir-du-Libre is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU Affero General Public License
* along with this software. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>.
*/
declare(strict_types=1);
namespace App\Tests\Functional\PublicPages;
use App\Tests\Functional\TestHelperTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
/**
* @group allow_mutation_testing_by_infection
*/
class FunctionalTestPublicPageHealthCheckTest extends WebTestCase
{
use TestHelperTrait;
public function testTryHttpPostMethodToHealthCheckRoute(): void
{
$client = static::createClient();
$crawler = $client->request('POST', '/health-check');
$this->assertResponseStatusCodeSame(Response::HTTP_METHOD_NOT_ALLOWED); // 405 Method Not Allowed
$this->assertResponseNotHasHeader("X-Comptoir-Test-Software-Database-Status");
}
public function testHttpHeadMethodToHealthCheckRoute(): void
{
$client = static::createClient();
$crawler = $client->request('HEAD', '/health-check');
$this->assertResponseHasHeader("X-Comptoir-Test-Software-Database-Status");
$statusCode = $client->getResponse()->getStatusCode();
if ($statusCode !== 200 && $statusCode !== 503) {
$this->assertTrue(false, "Bad HTTP status code [ $statusCode ] (Allowed is 503 or 200");
}
}
public function testHttpGetMethodToHealthCheckRoute(): void
{
$client = static::createClient();
$crawler = $client->request('GET', '/health-check');
$this->assertResponseHasHeader("X-Comptoir-Test-Software-Database-Status");
$this->assertSelectorTextContains('#webapp-database-status', 'DB_CONNECTION_');
$this->checkFooter($crawler, false);
$statusCode = $client->getResponse()->getStatusCode();
$dbStatus = $crawler->filter('#webapp-database-status')->innerText();
if ($statusCode !== 200 && $statusCode !== 503) {
$this->assertTrue(false, "Bad HTTP status code [ $statusCode ] (Allowed is 503 or 200");
} elseif ($statusCode === 200) {
$this->assertSelectorTextSame('#webapp-database-status', 'DB_CONNECTION_SUCCESSFUL');
$this->assertResponseHeaderSame("X-Comptoir-Test-Software-Database-Status", 'DB_CONNECTION_SUCCESSFUL');
} elseif (
$statusCode === 503 && $dbStatus === 'DB_CONNECTION_FAILED'
) {
// Check database status
if ($dbStatus === 'DB_CONNECTION_FAILED') {
$this->assertResponseHeaderSame("X-Comptoir-Test-Software-Database-Status", 'DB_CONNECTION_FAILED');
} elseif ($dbStatus === 'DB_CONNECTION_SUCCESSFUL') {
$this->assertResponseHeaderSame("X-Comptoir-Test-Software-Database-Status", 'DB_CONNECTION_SUCCESSFUL');
} else {// Bad database status code
$this->assertTrue(false, "Bad database status code [ $dbStatus ]");
}
} else { // HTTP status code 503, but not FAILED database status
$msg = "Bad HTTP status code [ $statusCode ] with ";
$msg .= "database status code [ $dbStatus ] ";
$this->assertTrue(false, "$msg");
}
}
}
<?php
/*
* This file is part of the Comptoir-du-Libre software.
* <https://gitlab.adullact.net/Comptoir/comptoir-du-libre>
*
* Copyright (c) ADULLACT <https://adullact.org>
* Association des Développeurs et Utilisateurs de Logiciels Libres
* pour les Administrations et les Collectivités Territoriales
*
* Comptoir-du-Libre is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU Affero General Public License
* along with this software. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>.
*/
declare(strict_types=1);
namespace App\Tests\Functional\PublicPages;
use App\Tests\Functional\TestHelperTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
/**
* @group allow_mutation_testing_by_infection
*/
class FunctionalTestPublicPageHomepageTest extends WebTestCase
{
use TestHelperTrait;
// Accessing Internal Objects
//////////////////////////////////////////////////////////////////////////////////////////////////////
// $crawler = $client->getCrawler(); // Crawler instance
// $history = $client->getHistory(); // client history
// $cookieJar = $client->getCookieJar(); // client cookie jar
// $requestHttpKernel = $client->getRequest(); // HttpKernel request instance
// $requestBrowserKit = $client->getInternalRequest(); // BrowserKit request instance
// $responseHttpKernel = $client->getResponse(); // HttpKernel response instance
// $responseBrowserKit = $client->getInternalResponse(); // BrowserKit response instance
//////////////////////////////////////////////////////////////////////////////////////////////////////
// dump($crawler);
// dump($history);
// dump($cookieJar);
// dump($requestHttpKernel);
// dump($requestBrowserKit);
// dump($responseHttpKernel);
// dump($responseBrowserKit);
// dump($requestBrowserKit->getCookies());
//////////////////////////////////////////////////////////////////////////////////////////////////////
public function testHomePageRedirectsToHomePageInDefaultLanguage(): void
{
$client = static::createClient();
$session = $this->createSession($client);
$crawler = $client->request('GET', '/');
$this->assertRouteSame('app_home');
$this->assertResponseStatusCodeSame(Response::HTTP_PERMANENTLY_REDIRECT); // HTTP status code = 308
$this->assertResponseHeaderSame("Location", '/en/');
$this->assertResponseHeaderSame("X-Comptoir-Test-Software-Webapp", '1');
}
public function testEnglishHomePageIsDisplayed(): void
{
$client = static::createClient();
$session = $this->createSession($client);
$crawler = $client->request('GET', '/en/');
// Basic checks
$this->assertRouteSame('app_home_i18n');
$this->assertResponseStatusCodeSame(Response::HTTP_OK); // HTTP status code = 200
// $this->assertResponseIsSuccessful(); // HTTP status code >= 200 and HTTP status code < 300;
$this->assertResponseHeaderSame("X-Comptoir-Test-Software-Webapp", '1');
// HTML content checks
$this->assertSelectorTextSame('h1', 'Comptoir du Libre TEST');
$this->assertPageTitleSame('Comptoir du Libre TEST');
$this->checkFooter($crawler, false);
// Cookies checks
//////////////////////////////////////////////////////////////////////////////////////////////////////
// $this->assertBrowserHasCookie('MOCKSESSID');
// $this->assertResponseHasCookie('MOCKSESSID'); // ---> FAIL ---> @@@TODO fixme
// HTTP Header checks
// ---> already tested in [ testAnonymousCanBrowsePublicUrls() ] method
// using [ commonSecurityHttpHeadersChecker() ] method
//////////////////////////////////////////////////////////////////////////////////////////////////////
// $this->assertResponseHeaderSame("Cache-Control", 'max-age=0, must-revalidate, private');
// $this->assertResponseHeaderSame(
// "Content-Security-Policy",
// 'report-uri /csp/report'
// ); // see: NelmioSecurityBundle
// $this->assertResponseHeaderSame("Content-Type", 'text/html; charset=UTF-8');
// $this->assertResponseHeaderSame(
// "Referrer-Policy",
// 'no-referrer, strict-origin-when-cross-origin'
// );
// $this->assertResponseHeaderSame("X-Content-Type-Options", 'nosniff');
// $this->assertResponseHeaderSame("X-Frame-Options", 'DENY');
// $this->assertResponseHeaderSame("X-Robots-Tag", 'noindex');
// $this->assertResponseHeaderSame("X-Xss-Protection", '1; mode=block');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment