Updated security expressions #148 (#166)

* replace has_role() with is_granted()
* added basic integration test classes for main controller
* added basic integration test classes for admin controller
This commit is contained in:
Kevin Papst
2018-06-19 22:08:44 +02:00
committed by GitHub
parent 856e7ccaeb
commit 75a6bf3ef6
24 changed files with 465 additions and 22 deletions

View File

@@ -10,6 +10,7 @@
namespace App\Tests\Controller;
use App\DataFixtures\AppFixtures;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@@ -22,14 +23,44 @@ abstract class ControllerBaseTest extends WebTestCase
const DEFAULT_LANGUAGE = 'en';
/**
* @param string $role
* @return Client
*/
protected function getClientForAuthenticatedUser()
protected function getClientForAuthenticatedUser(string $role = User::ROLE_USER)
{
$client = self::createClient([], [
'PHP_AUTH_USER' => AppFixtures::USERNAME_USER,
'PHP_AUTH_PW' => AppFixtures::DEFAULT_PASSWORD,
]);
switch($role) {
case User::ROLE_SUPER_ADMIN:
$client = self::createClient([], [
'PHP_AUTH_USER' => AppFixtures::USERNAME_SUPER_ADMIN,
'PHP_AUTH_PW' => AppFixtures::DEFAULT_PASSWORD,
]);
break;
case User::ROLE_ADMIN:
$client = self::createClient([], [
'PHP_AUTH_USER' => AppFixtures::USERNAME_ADMIN,
'PHP_AUTH_PW' => AppFixtures::DEFAULT_PASSWORD,
]);
break;
case User::ROLE_TEAMLEAD:
$client = self::createClient([], [
'PHP_AUTH_USER' => AppFixtures::USERNAME_TEAMLEAD,
'PHP_AUTH_PW' => AppFixtures::DEFAULT_PASSWORD,
]);
break;
case User::ROLE_USER:
$client = self::createClient([], [
'PHP_AUTH_USER' => AppFixtures::USERNAME_USER,
'PHP_AUTH_PW' => AppFixtures::DEFAULT_PASSWORD,
]);
break;
default:
$client = null;
break;
}
return $client;
}
@@ -46,23 +77,64 @@ abstract class ControllerBaseTest extends WebTestCase
}
/**
* @param Client $client
* @param string $url
* @param string $method
*/
protected function assertUrlIsSecured(string $url, $method = 'GET')
protected function assertRequestIsSecured(Client $client, string $url, $method = 'GET')
{
$client = self::createClient();
$client->request($method, '/' . self::DEFAULT_LANGUAGE . $url);
$this->assertTrue($client->getResponse()->isRedirect());
$this->assertTrue(
$client->getResponse()->isRedirect(),
sprintf('The secure URL %s is not protected.', $url . $client->getResponse()->getContent())
);
$this->assertEquals(
'http://localhost/' . self::DEFAULT_LANGUAGE . '/login',
$client->getResponse()->getTargetUrl(),
sprintf('The %s secure URL redirects to the login form.', $url)
sprintf('The secure URL %s does not redirect to the login form.', $url)
);
}
/**
* @param string $url
* @param string $method
* @param Client|null $client
*/
protected function assertUrlIsSecured(string $url, $method = 'GET')
{
$client = self::createClient();
$this->assertRequestIsSecured($client, $url, $method);
}
/**
* @param string $role
* @param string $url
* @param string $method
*/
protected function assertUrlIsSecuredForRole(string $role, string $url, string $method = 'GET')
{
$client = $this->getClientForAuthenticatedUser($role);
$client->request($method, '/' . self::DEFAULT_LANGUAGE . $url);
$this->assertFalse(
$client->getResponse()->isSuccessful(),
sprintf('The secure URL %s is not protected for role %s', $url, $role)
);
$this->assertContains('Symfony\Component\Security\Core\Exception\AccessDeniedException', $client->getResponse()->getContent());
}
/**
* @param Client $client
* @param string $url
*/
protected function assertAccessIsGranted(Client $client, $url)
{
$this->request($client, $url);
$this->assertTrue($client->getResponse()->isSuccessful());
// TODO improve this test?
}
/**
* @param Client $client
*/
@@ -71,4 +143,21 @@ abstract class ControllerBaseTest extends WebTestCase
$this->assertFalse($client->getResponse()->isSuccessful());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
/**
* @param Client $client
* @param string $classname
*/
protected function assertMainContentClass(Client $client, $classname)
{
$this->assertContains('<section class="content '.$classname.'">', $client->getResponse()->getContent());
}
/**
* @param Client $client
*/
protected function assertHasDataTable(Client $client)
{
$this->assertContains('<table class="table table-striped table-hover dataTable" role="grid">', $client->getResponse()->getContent());
}
}