mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 06:14:59 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,58 +1,50 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Tests\PasswordRecover;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\MainBundle\Security\PasswordRecover\TokenManager;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
|
||||
/**
|
||||
*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Tests\PasswordRecover;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Security\PasswordRecover\TokenManager;
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class TokenManagerTest extends KernelTestCase
|
||||
{
|
||||
protected $tokenManager;
|
||||
|
||||
public static function setUpBefore()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
|
||||
$logger = self::$container
|
||||
->get('logger');
|
||||
|
||||
|
||||
$this->tokenManager = new TokenManager('secret', $logger);
|
||||
}
|
||||
|
||||
public function testGenerate()
|
||||
public static function setUpBefore()
|
||||
{
|
||||
}
|
||||
|
||||
public function testGenerate()
|
||||
{
|
||||
$tokenManager = $this->tokenManager;
|
||||
$user = (new User())->setUsernameCanonical('test');
|
||||
$expiration = new \DateTimeImmutable('tomorrow');
|
||||
|
||||
$expiration = new DateTimeImmutable('tomorrow');
|
||||
|
||||
$tokens = $tokenManager->generate($user, $expiration);
|
||||
|
||||
|
||||
$this->assertInternalType('array', $tokens);
|
||||
$this->assertArrayHasKey('h', $tokens);
|
||||
$this->assertArrayHasKey('t', $tokens);
|
||||
@@ -60,57 +52,57 @@ class TokenManagerTest extends KernelTestCase
|
||||
$this->assertNotEmpty($tokens['t']);
|
||||
$this->assertEquals($user->getUsernameCanonical(), $tokens['u']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @expectedException \UnexpectedValueException
|
||||
*/
|
||||
public function testGenerateEmptyUsernameCanonical()
|
||||
public function testGenerateEmptyUsernameCanonical()
|
||||
{
|
||||
$tokenManager = $this->tokenManager;
|
||||
// set a username, but not a username canonical
|
||||
$user = (new User())->setUsername('test');
|
||||
$expiration = new \DateTimeImmutable('tomorrow');
|
||||
|
||||
$expiration = new DateTimeImmutable('tomorrow');
|
||||
|
||||
$tokenManager->generate($user, $expiration);
|
||||
}
|
||||
|
||||
|
||||
public function testVerify()
|
||||
{
|
||||
$tokenManager = $this->tokenManager;
|
||||
$user = (new User())->setUsernameCanonical('test');
|
||||
$expiration = new \DateTimeImmutable('tomorrow');
|
||||
|
||||
$expiration = new DateTimeImmutable('tomorrow');
|
||||
|
||||
$tokens = $tokenManager->generate($user, $expiration);
|
||||
|
||||
|
||||
$hash = $tokens[TokenManager::HASH];
|
||||
$token = $tokens[TokenManager::TOKEN];
|
||||
$timestamp = $tokens[TokenManager::TIMESTAMP];
|
||||
|
||||
|
||||
$verification = $tokenManager->verify($hash, $token, $user, $timestamp);
|
||||
|
||||
|
||||
$this->assertTrue($verification);
|
||||
|
||||
|
||||
// test with altering token
|
||||
$this->assertFalse($tokenManager->verify($hash.'5', $token, $user, $timestamp));
|
||||
$this->assertFalse($tokenManager->verify($hash, $token.'25', $user, $timestamp));
|
||||
$this->assertFalse($tokenManager->verify($hash . '5', $token, $user, $timestamp));
|
||||
$this->assertFalse($tokenManager->verify($hash, $token . '25', $user, $timestamp));
|
||||
$this->assertFalse($tokenManager->verify($hash, $token, $user->setUsernameCanonical('test2'), $timestamp));
|
||||
$this->assertFalse($tokenManager->verify($hash, $token, $user, $timestamp+1));
|
||||
$this->assertFalse($tokenManager->verify($hash, $token, $user, $timestamp + 1));
|
||||
}
|
||||
|
||||
|
||||
public function testVerifyExpiredFails()
|
||||
{
|
||||
$tokenManager = $this->tokenManager;
|
||||
$user = (new User())->setUsernameCanonical('test');
|
||||
$expiration = (new \DateTimeImmutable('now'))->sub(new \DateInterval('PT1S'));
|
||||
|
||||
$expiration = (new DateTimeImmutable('now'))->sub(new DateInterval('PT1S'));
|
||||
|
||||
$tokens = $tokenManager->generate($user, $expiration);
|
||||
|
||||
|
||||
$hash = $tokens[TokenManager::HASH];
|
||||
$token = $tokens[TokenManager::TOKEN];
|
||||
$timestamp = $tokens[TokenManager::TIMESTAMP];
|
||||
|
||||
|
||||
$verification = $tokenManager->verify($hash, $token, $user, $timestamp);
|
||||
|
||||
|
||||
$this->assertFalse($verification);
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Tests\Security\Resolver;
|
||||
@@ -9,6 +16,10 @@ use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
|
||||
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CenterResolverDispatcherTest extends KernelTestCase
|
||||
{
|
||||
private CenterResolverDispatcherInterface $dispatcher;
|
||||
@@ -21,10 +32,10 @@ class CenterResolverDispatcherTest extends KernelTestCase
|
||||
|
||||
public function testResolveCenter()
|
||||
{
|
||||
$center = new Center();
|
||||
$center = new Center();
|
||||
|
||||
$resolved = $this->dispatcher->resolveCenter($center);
|
||||
$resolved = $this->dispatcher->resolveCenter($center);
|
||||
|
||||
$this->assertSame($center, $resolved);
|
||||
$this->assertSame($center, $resolved);
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Tests\Security\Resolver;
|
||||
|
||||
use Chill\MainBundle\Entity\HasScopeInterface;
|
||||
@@ -8,6 +15,10 @@ use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Security\Resolver\DefaultScopeResolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class DefaultScopeResolverTest extends TestCase
|
||||
{
|
||||
private DefaultScopeResolver $scopeResolver;
|
||||
@@ -21,8 +32,8 @@ class DefaultScopeResolverTest extends TestCase
|
||||
{
|
||||
$scope = new Scope();
|
||||
$entity = new class($scope) implements HasScopeInterface {
|
||||
|
||||
public function __construct(Scope $scope) {
|
||||
public function __construct(Scope $scope)
|
||||
{
|
||||
$this->scope = $scope;
|
||||
}
|
||||
|
||||
@@ -30,7 +41,6 @@ class DefaultScopeResolverTest extends TestCase
|
||||
{
|
||||
return $this->scope;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
$this->assertTrue($this->scopeResolver->supports($entity));
|
||||
@@ -41,8 +51,8 @@ class DefaultScopeResolverTest extends TestCase
|
||||
public function testHasScopesInterface()
|
||||
{
|
||||
$entity = new class($scopeA = new Scope(), $scopeB = new Scope()) implements HasScopesInterface {
|
||||
|
||||
public function __construct(Scope $scopeA, Scope $scopeB) {
|
||||
public function __construct(Scope $scopeA, Scope $scopeB)
|
||||
{
|
||||
$this->scopes = [$scopeA, $scopeB];
|
||||
}
|
||||
|
||||
@@ -58,5 +68,4 @@ class DefaultScopeResolverTest extends TestCase
|
||||
$this->assertSame($scopeA, $this->scopeResolver->resolveScope($entity)[0]);
|
||||
$this->assertSame($scopeB, $this->scopeResolver->resolveScope($entity)[1]);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Tests\Security\Resolver;
|
||||
|
||||
use Chill\MainBundle\Entity\HasScopeInterface;
|
||||
@@ -9,6 +16,10 @@ use Chill\MainBundle\Security\Resolver\DefaultScopeResolver;
|
||||
use Chill\MainBundle\Security\Resolver\ScopeResolverDispatcher;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class DefaultScopeResolverDispatcherTest extends TestCase
|
||||
{
|
||||
private ScopeResolverDispatcher $scopeResolverDispatcher;
|
||||
@@ -22,8 +33,8 @@ class DefaultScopeResolverDispatcherTest extends TestCase
|
||||
{
|
||||
$scope = new Scope();
|
||||
$entity = new class($scope) implements HasScopeInterface {
|
||||
|
||||
public function __construct(Scope $scope) {
|
||||
public function __construct(Scope $scope)
|
||||
{
|
||||
$this->scope = $scope;
|
||||
}
|
||||
|
||||
@@ -31,7 +42,6 @@ class DefaultScopeResolverDispatcherTest extends TestCase
|
||||
{
|
||||
return $this->scope;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
$this->assertTrue($this->scopeResolverDispatcher->isConcerned($entity));
|
||||
@@ -41,8 +51,8 @@ class DefaultScopeResolverDispatcherTest extends TestCase
|
||||
public function testHasScopesInterface()
|
||||
{
|
||||
$entity = new class($scopeA = new Scope(), $scopeB = new Scope()) implements HasScopesInterface {
|
||||
|
||||
public function __construct(Scope $scopeA, Scope $scopeB) {
|
||||
public function __construct(Scope $scopeA, Scope $scopeB)
|
||||
{
|
||||
$this->scopes = [$scopeA, $scopeB];
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user