-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from williamjehanne/master
feat(decypt): add EncryptionException to detect openssl_encrypt and openssl_decrypt failures
- Loading branch information
Showing
8 changed files
with
236 additions
and
6 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
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
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 |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
|
||
namespace Ekino\DataProtectionBundle\Encryptor; | ||
|
||
use Ekino\DataProtectionBundle\Exception\EncryptionException; | ||
|
||
/** | ||
* @author Rémi Marseille <[email protected]> | ||
*/ | ||
|
@@ -21,13 +23,17 @@ interface EncryptorInterface | |
/** | ||
* @param string $data | ||
* | ||
* @throws EncryptionException | ||
* | ||
* @return string | ||
*/ | ||
public function encrypt(string $data): string; | ||
|
||
/** | ||
* @param string $data | ||
* | ||
* @throws EncryptionException | ||
* | ||
* @return string | ||
*/ | ||
public function decrypt(string $data): string; | ||
|
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the ekino/data-protection-bundle project. | ||
* | ||
* (c) Ekino | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ekino\DataProtectionBundle\Exception; | ||
|
||
/** | ||
* Class EncryptionException. | ||
* | ||
* @author William JEHANNE <[email protected]> | ||
*/ | ||
class EncryptionException extends \RuntimeException | ||
{ | ||
} |
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
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
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,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the ekino/data-protection-bundle project. | ||
* | ||
* (c) Ekino | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ekino\DataProtectionBundle\Tests\Controller; | ||
|
||
use Ekino\DataProtectionBundle\Controller\LogsAdminController; | ||
use Ekino\DataProtectionBundle\Encryptor\EncryptorInterface; | ||
use Ekino\DataProtectionBundle\Exception\EncryptionException; | ||
use Ekino\DataProtectionBundle\Form\DataClass\Log; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\Form; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
/** | ||
* Class LogsAdminControllerTest. | ||
* | ||
* @author William JEHANNE <[email protected]> | ||
*/ | ||
class LogsAdminControllerTest extends TestCase | ||
{ | ||
/** | ||
* @var LogsAdminController|MockObject | ||
*/ | ||
private $controller; | ||
|
||
/** | ||
* @var EncryptorInterface|MockObject | ||
*/ | ||
private $encryptor; | ||
|
||
/** | ||
* Initialize test LogsAdminControllerTest. | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->encryptor = $this->createMock(EncryptorInterface::class); | ||
$this->controller = $this->getMockBuilder(LogsAdminController::class) | ||
->setConstructorArgs([$this->encryptor]) | ||
->disableOriginalClone() | ||
->disableArgumentCloning() | ||
->disallowMockingUnknownTypes() | ||
->setMethods(['addFlash', 'createForm', 'get', 'renderWithExtraParams']) | ||
->getMock(); | ||
} | ||
|
||
/** | ||
* Test decryptEncryptAction method of LogsAdminController. | ||
*/ | ||
public function testDecryptEncryptAction(): void | ||
{ | ||
$form = $this->createMock(Form::class); | ||
$log = $this->createMock(Log::class); | ||
$log->expects($this->once())->method('getContent')->willReturn('foo'); | ||
|
||
$response = $this->createMock(Response::class); | ||
$this->controller->expects($this->never())->method('addFlash'); | ||
$this->controller->expects($this->once())->method('renderWithExtraParams')->willReturn($response); | ||
|
||
$form->expects($this->once())->method('handleRequest'); | ||
$form->expects($this->once())->method('isSubmitted')->willReturn(true); | ||
$form->expects($this->once())->method('isValid')->willReturn(true); | ||
$form->expects($this->once())->method('getData')->willReturn($log); | ||
|
||
$this->controller->expects($this->once())->method('createForm')->willReturn($form); | ||
|
||
$request = $this->createMock(Request::class); | ||
|
||
$this->controller->decryptEncryptAction($request); | ||
} | ||
|
||
/** | ||
* Test decryptEncryptAction method of LogsAdminController not ok. | ||
*/ | ||
public function testDecryptEncryptActionNok(): void | ||
{ | ||
$this->encryptor->expects($this->any())->method('encrypt')->willThrowException(new EncryptionException()); | ||
|
||
$form = $this->createMock(Form::class); | ||
$log = $this->createMock(Log::class); | ||
$log->expects($this->once())->method('getContent')->willReturn('foo'); | ||
$log->expects($this->any())->method('isDecryptAction')->willReturn(false); | ||
|
||
$response = $this->createMock(Response::class); | ||
$this->controller->expects($this->once())->method('addFlash')->with('error'); | ||
$this->controller->expects($this->once())->method('renderWithExtraParams')->willReturn($response); | ||
|
||
$form->expects($this->once())->method('handleRequest'); | ||
$form->expects($this->once())->method('isSubmitted')->willReturn(true); | ||
$form->expects($this->once())->method('isValid')->willReturn(true); | ||
$form->expects($this->once())->method('getData')->willReturn($log); | ||
|
||
$this->controller->expects($this->once())->method('createForm')->willReturn($form); | ||
|
||
$request = $this->createMock(Request::class); | ||
|
||
$translator = $this->createMock(TranslatorInterface::class); | ||
$translator->expects($this->once())->method('trans')->willReturn('admin.logs.encrypt.error'); | ||
$this->controller->expects($this->once())->method('get')->with($this->equalTo('translator'))->willReturn($translator); | ||
|
||
$this->controller->decryptEncryptAction($request); | ||
} | ||
} |
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