-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dto, processor and security service test
- Loading branch information
Showing
8 changed files
with
474 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under following license: | ||
* - Pimcore Commercial License (PCL) | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioApiBundle\Tests\Unit\Dto\Token; | ||
|
||
use Codeception\Test\Unit; | ||
use Pimcore\Bundle\StudioApiBundle\Dto\Token\Create; | ||
|
||
final class CreateTest extends Unit | ||
{ | ||
public function testTokenCreate(): void | ||
{ | ||
$info = new Create('token', 'test'); | ||
$this->assertSame('token', $info->getUsername()); | ||
$this->assertSame('test', $info->getPassword()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under following license: | ||
* - Pimcore Commercial License (PCL) | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioApiBundle\Tests\Unit\Dto\Token; | ||
|
||
use Codeception\Test\Unit; | ||
use Pimcore\Bundle\StudioApiBundle\Dto\Token\Info; | ||
|
||
final class InfoTest extends Unit | ||
{ | ||
public function testTokenInfo(): void | ||
{ | ||
$info = new Info('token', 'test'); | ||
$this->assertSame('token', $info->getToken()); | ||
$this->assertSame('test', $info->getUsername()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under following license: | ||
* - Pimcore Commercial License (PCL) | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioApiBundle\Tests\Unit\Dto\Token; | ||
|
||
use Codeception\Test\Unit; | ||
use Pimcore\Bundle\StudioApiBundle\Dto\Token\Output; | ||
|
||
final class OutputTest extends Unit | ||
{ | ||
public function testTokenOutput(): void | ||
{ | ||
$info = new Output('token',3600, 'test'); | ||
$this->assertSame('token', $info->getToken()); | ||
$this->assertSame(3600, $info->getLifetime()); | ||
$this->assertSame('test', $info->getUsername()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under following license: | ||
* - Pimcore Commercial License (PCL) | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioApiBundle\Tests\Unit\Dto\Token; | ||
|
||
use Codeception\Test\Unit; | ||
use Pimcore\Bundle\StudioApiBundle\Dto\Token\Refresh; | ||
|
||
final class RefreshTest extends Unit | ||
{ | ||
public function testTokenRefresh(): void | ||
{ | ||
$info = new Refresh('token'); | ||
$this->assertSame('token', $info->getToken()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under following license: | ||
* - Pimcore Commercial License (PCL) | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioApiBundle\Tests\Unit\Service\Security; | ||
|
||
use Codeception\Test\Unit; | ||
use Exception; | ||
use Pimcore\Bundle\StaticResolverBundle\Models\Tool\TmpStoreResolverInterface; | ||
use Pimcore\Bundle\StudioApiBundle\Dto\Token\Create; | ||
use Pimcore\Bundle\StudioApiBundle\Service\SecurityService; | ||
use Pimcore\Bundle\StudioApiBundle\Service\SecurityServiceInterface; | ||
use Pimcore\Model\Tool\TmpStore; | ||
use Pimcore\Security\User\User; | ||
use Pimcore\Security\User\UserProvider; | ||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | ||
use Symfony\Component\Security\Core\Exception\UserNotFoundException; | ||
|
||
|
||
final class SecurityServiceTest extends Unit | ||
{ | ||
/** | ||
* @throws Exception | ||
*/ | ||
public function testSecurityService(): void | ||
{ | ||
$securityService = $this->mockSecurityService(); | ||
$user = $securityService->authenticateUser(new Create('test', 'test')); | ||
|
||
$this->assertInstanceOf(User::class, $user); | ||
$this->assertSame('test', $user->getPassword()); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
|
||
public function testInvalidPassword(): void | ||
{ | ||
$securityService = $this->mockSecurityService(false); | ||
|
||
$this->expectExceptionMessage('Invalid credentials'); | ||
$securityService->authenticateUser(new Create('test', 'test')); | ||
} | ||
|
||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testUserNotFound(): void | ||
{ | ||
$securityService = $this->mockSecurityService(false, false); | ||
|
||
$this->expectExceptionMessage('Invalid credentials'); | ||
$securityService->authenticateUser(new Create('test', 'test')); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testTokenAllowedTrue(): void | ||
{ | ||
$securityService = $this->mockSecurityService(false, false); | ||
|
||
$this->assertTrue($securityService->isAllowed('test')); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testTokenAllowedFalse(): void | ||
{ | ||
$securityService = $this->mockSecurityService(false, false, false); | ||
|
||
$this->assertFalse($securityService->isAllowed('test')); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function mockSecurityService($validPassword = true, bool $withUser = true, bool $withTmpStore = true): SecurityServiceInterface | ||
{ | ||
return new SecurityService( | ||
$withUser ? $this->mockUserProviderWithUser() : $this->mockUserProviderWithOutUser(), | ||
$this->mockPasswordHasher($validPassword), | ||
$this->mockTmpStoreResolver($withTmpStore) | ||
); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function mockUserProviderWithUser(): UserProvider | ||
{ | ||
return $this->makeEmpty(UserProvider::class, [ | ||
'loadUserByIdentifier' => function() { | ||
return $this->makeEmpty(User::class, [ | ||
'getPassword' => 'test' | ||
]); | ||
} | ||
]); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function mockUserProviderWithOutUser(): UserProvider | ||
{ | ||
return $this->makeEmpty(UserProvider::class, [ | ||
'loadUserByIdentifier' => fn() => throw new UserNotFoundException('User not found') | ||
]); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function mockPasswordHasher($validPassword = true): UserPasswordHasherInterface | ||
{ | ||
return $this->makeEmpty(UserPasswordHasherInterface::class, [ | ||
'isPasswordValid' => $validPassword | ||
]); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function mockTmpStoreResolver($withTmpStore = true): TmpStoreResolverInterface | ||
{ | ||
return $this->makeEmpty(TmpStoreResolverInterface::class, [ | ||
'get' => $withTmpStore ? $this->mockTmpStore(): null | ||
]); | ||
} | ||
|
||
private function mockTmpStore(): TmpStore | ||
{ | ||
$tmpStore = new TmpStore(); | ||
$tmpStore->setId('test'); | ||
return $tmpStore; | ||
} | ||
} |
Oops, something went wrong.