Skip to content

Commit

Permalink
Add dto, processor and security service test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Mar 4, 2024
1 parent 80f80fc commit b53adfd
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 66 deletions.
27 changes: 27 additions & 0 deletions tests/Unit/Dto/Token/CreateTest.php
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());
}
}
27 changes: 27 additions & 0 deletions tests/Unit/Dto/Token/InfoTest.php
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());
}
}
28 changes: 28 additions & 0 deletions tests/Unit/Dto/Token/OutputTest.php
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());
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Dto/Token/RefreshTest.php
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());
}
}
66 changes: 0 additions & 66 deletions tests/Unit/Resources/Token/PostProcessorTest.php

This file was deleted.

149 changes: 149 additions & 0 deletions tests/Unit/Service/Security/SecurityServiceTest.php
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;
}
}
Loading

0 comments on commit b53adfd

Please sign in to comment.