Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Sep 13, 2023
1 parent 985f61f commit fb18945
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 97 deletions.
4 changes: 2 additions & 2 deletions lib/Handler/Pkcs12Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
private SystemConfig $systemConfig,
private CertificateEngineHandler $certificateEngineHandler,
private IL10N $l10n,
private JSignPdfHandler $jSignPdfHandler
private JSignPdfHandler $jSignPdfHandler,
) {
}

Expand Down Expand Up @@ -71,7 +71,7 @@ public function getPfx($uid): string {
if (!$node->getContent()) {
throw new LibresignException($this->l10n->t('Password to sign not defined. Create a password to sign.'), 400);
}
return $node->conten;
return $node->getContent();
}

private function getHandler(): SignEngineHandler {
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(
private UserElementMapper $userElementMapper,
private FolderService $folderService,
private IClientService $clientService,
private TimeFactory $timeFactory
private TimeFactory $timeFactory,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Service/FolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
private IUserMountCache $userMountCache,
private IConfig $config,
private IL10N $l10n,
private ?string $userId
private ?string $userId,
) {
$this->userId = $userId;
}
Expand Down
1 change: 0 additions & 1 deletion lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ private function getPdfToSign(FileEntity $fileData, File $originalFile): File {
$originalFile->getPath()
);

/** @var \OCP\Files\File */
$footer = $this->pkcs12Handler->getFooter($originalFile, $fileData->getUuid());
if ($footer) {
$background = $this->tempManager->getTemporaryFile('signed.pdf');
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use OCA\Libresign\Controller\PageController;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\FileService;
use OCA\Libresign\Service\IdentifyMethodService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Services\IAppConfig;
use OCP\AppFramework\Services\IInitialState;
use OCP\IRequest;
use OCP\IURLGenerator;
Expand All @@ -21,6 +23,8 @@ final class PageControllerTest extends TestCase {
private IInitialState|MockObject $initialState;
private AccountService|MockObject $accountService;
private IdentifyMethodService|MockObject $identifyMethodService;
private IAppConfig|MockObject $appConfig;
private FileService|MockObject $fileService;
private IURLGenerator|MockObject $url;
private PageController $controller;

Expand All @@ -30,13 +34,17 @@ public function setUp(): void {
$this->initialState = $this->createMock(IInitialState::class);
$this->accountService = $this->createMock(AccountService::class);
$this->identifyMethodService = $this->createMock(IdentifyMethodService::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->fileService = $this->createMock(FileService::class);
$this->url = $this->createMock(IURLGenerator::class);
$this->controller = new PageController(
$this->request,
$this->userSession,
$this->initialState,
$this->accountService,
$this->identifyMethodService,
$this->appConfig,
$this->fileService,
$this->url
);
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Handler/JSignPdfHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ public function setUp(): void {

public function testSignExistingFileSuccess() {
$inputFile = $this->createMock(\OC\Files\Node\File::class);
$certificate = $this->createMock(\OC\Files\Node\File::class);
$mock = $this->createMock(JSignPDF::class);
$mock->method('sign')->willReturn('content');
$this->class->setJSignPdf($mock);
$this->class->setInputFile($inputFile);
$this->class->setCertificate($certificate);
$this->class->setCertificate('');
$this->class->setPassword('password');
$actual = $this->class->sign();
$this->assertEquals('content', $actual);
Expand Down
24 changes: 15 additions & 9 deletions tests/Unit/Handler/Pkcs12HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use OCA\Libresign\Handler\CertificateEngine\CfsslHandler;
use OCA\Libresign\Handler\CertificateEngine\Handler as CertificateEngineHandler;
use OCA\Libresign\Handler\CertificateEngine\OpenSslHandler;
use OCA\Libresign\Handler\JSignPdfHandler;
use OCA\Libresign\Handler\Pkcs12Handler;
use OCA\Libresign\Service\FolderService;
use OCP\IConfig;
Expand All @@ -19,6 +20,7 @@ final class Pkcs12HandlerTest extends \OCA\Libresign\Tests\Unit\TestCase {
private SystemConfig $systemConfig;
private CfsslHandler|MockObject $cfsslHandler;
private IL10N|MockObject $l10n;
private JSignPdfHandler|MockObject $jSignPdfHandler;
private OpenSslHandler|MockObject $openSslHandler;
private CertificateEngineHandler|MockObject $certificateEngineHandler;
private array $cfsslHandlerBuffer = [];
Expand All @@ -33,13 +35,15 @@ public function setUp(): void {
$this->l10n
->method('t')
->will($this->returnArgument(0));
$this->jSignPdfHandler = $this->createMock(JSignPdfHandler::class);
$this->pkcs12Handler = new Pkcs12Handler(
$this->folderService,
$this->config,
$this->urlGenerator,
$this->systemConfig,
$this->certificateEngineHandler,
$this->l10n
$this->l10n,
$this->jSignPdfHandler,
);
}

Expand All @@ -61,7 +65,7 @@ public function testSavePfxWhenPfxFileExsitsAndIsAFile() {
$this->folderService->method('getFolder')->will($this->returnValue($node));

$actual = $this->pkcs12Handler->savePfx('userId', 'content');
$this->assertInstanceOf(\OCP\Files\File::class, $actual);
$this->assertEquals('content', $actual);
}

public function testGetPfxWithInvalidPfx() {
Expand All @@ -82,10 +86,10 @@ public function testGetPfxOk() {
$folder->method('get')->will($this->returnValue($file));
$this->folderService->method('getFolder')->will($this->returnValue($folder));
$actual = $this->pkcs12Handler->getPfx('userId');
$this->assertInstanceOf('\OCP\Files\Node', $actual);
$this->assertEquals('valid pfx content', $actual);
}

public function testWriteFooterWithoutValidationSite() {
public function testGetFooterWithoutValidationSite() {
$this->config = $this->createMock(IConfig::class);
$this->config
->method('getAppValue')
Expand All @@ -96,14 +100,15 @@ public function testWriteFooterWithoutValidationSite() {
$this->urlGenerator,
$this->systemConfig,
$this->certificateEngineHandler,
$this->l10n
$this->l10n,
$this->jSignPdfHandler,
);
$file = $this->createMock(\OCP\Files\File::class);
$actual = $this->pkcs12Handler->writeFooter($file, 'uuid');
$actual = $this->pkcs12Handler->getFooter($file, 'uuid');
$this->assertEmpty($actual);
}

public function testWriteFooterWithSuccess() {
public function testGetFooterWithSuccess() {
$this->config = $this->createMock(IConfig::class);
$this->config
->method('getAppValue')
Expand All @@ -120,15 +125,16 @@ public function testWriteFooterWithSuccess() {
$this->urlGenerator,
$this->systemConfig,
$this->certificateEngineHandler,
$this->l10n
$this->l10n,
$this->jSignPdfHandler,
);

$file = $this->createMock(\OCP\Files\File::class);
$file->method('getName')
->willReturn('small_valid.pdf');
$file->method('getContent')
->willReturn(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'));
$actual = $this->pkcs12Handler->writeFooter($file, 'uuid');
$actual = $this->pkcs12Handler->getFooter($file, 'uuid');
$this->assertEquals(18359, strlen($actual));
}

Expand Down
7 changes: 2 additions & 5 deletions tests/Unit/Handler/Pkcs7HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,16 @@ public function testSignWithSuccess() {
->willReturn($fileToSIgnTempNam);
$handler->setInputFile($fileToSign);

$certificate = $this->createMock(\OCP\Files\File::class);
$certKeys = json_decode(file_get_contents(__DIR__ . '/../../fixtures/cfssl/newcert-with-success.json'), true);
$certKeys = $certKeys['result'];
openssl_pkcs12_export($certKeys['certificate'], $certContent, $certKeys['private_key'], 'password');
$certificate
->method('getContent')
->willReturn($certContent);
$handler->setCertificate($certificate);
$handler->setCertificate($certContent);

$handler->setPassword('password');

$actual = $handler->sign();
$signedFile = $actual->getInternalPath();
$this->assertStringContainsString($content, file_get_contents($signedFile));
$this->assertGreaterThan($content, file_get_contents($signedFile));
}
}
24 changes: 23 additions & 1 deletion tests/Unit/Helper/ValidateHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Service\IdentifyMethodService;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\IConfig;
Expand All @@ -39,6 +40,7 @@ final class ValidateHelperTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IGroupManager|MockObject $groupManager;
private IUserManager $userManager;
private IRootFolder|MockObject $root;
private IUserMountCache|MockObject $userMountCache;

public function setUp(): void {
$this->l10n = $this->createMock(IL10N::class);
Expand All @@ -59,6 +61,7 @@ public function setUp(): void {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->root = $this->createMock(IRootFolder::class);
$this->userMountCache = $this->createMock(IUserMountCache::class);
}

private function getValidateHelper(): ValidateHelper {
Expand All @@ -77,7 +80,8 @@ private function getValidateHelper(): ValidateHelper {
$this->config,
$this->groupManager,
$this->userManager,
$this->root
$this->root,
$this->userMountCache,
);
return $validateHelper;
}
Expand All @@ -103,6 +107,9 @@ public function testValidateFileWhenFileIdDoesNotExist() {
$this->root->method('getById')->will($this->returnCallback(function () {
throw new \Exception('not found');
}));
$this->userMountCache
->method('getMountsForFileId')
->willreturn([]);
$this->getValidateHelper()->validateFile([
'file' => ['fileId' => 123],
'name' => 'test'
Expand All @@ -117,6 +124,9 @@ public function testValidateNewFileUsingFileIdWithSuccess() {
$this->root
->method('getById')
->willReturn([$file]);
$this->userMountCache
->method('getMountsForFileId')
->willreturn([]);
$actual = $this->getValidateHelper()->validateNewFile([
'file' => ['fileId' => 123],
'name' => 'test'
Expand Down Expand Up @@ -160,6 +170,9 @@ public function testValidateMimeTypeAcceptedByNodeId(string $mimetype, int $dest
if ($exception) {
$this->expectExceptionMessage($exception);
}
$this->userMountCache
->method('getMountsForFileId')
->willreturn([]);
$actual = $this->getValidateHelper()->validateMimeTypeAcceptedByNodeId(171, $destination);
if (!$exception) {
$this->assertNull($actual);
Expand Down Expand Up @@ -359,6 +372,9 @@ public function testValidateIfNodeIdExistsWhenGetFileThrowException() {
->will($this->returnCallback(function () {
throw new \Exception('not found');
}));
$this->userMountCache
->method('getMountsForFileId')
->willreturn([]);
$this->getValidateHelper()->validateIfNodeIdExists(171);
}

Expand All @@ -367,13 +383,19 @@ public function testValidateIfNodeIdExistsWithInvalidFile() {
$this->root
->method('getById')
->willReturn([0 => null]);
$this->userMountCache
->method('getMountsForFileId')
->willreturn([]);
$this->getValidateHelper()->validateIfNodeIdExists(171);
}

public function testValidateIfNodeIdExistsWithSuccess() {
$this->root
->method('getById')
->willReturn(['file']);
$this->userMountCache
->method('getMountsForFileId')
->willreturn([]);
$actual = $this->getValidateHelper()->validateIfNodeIdExists(171);
$this->assertNull($actual);
}
Expand Down
Loading

0 comments on commit fb18945

Please sign in to comment.