Big refactor

* Methods to handle pkcs are moved to PkcsHandler
* Tests moved to testsuites
This commit is contained in:
Vitor Mattos 2021-06-25 10:35:23 -03:00
parent 9059fa1f4b
commit 75bace2bf7
16 changed files with 700 additions and 729 deletions

View file

@ -2,12 +2,12 @@
namespace OCA\Libresign\Controller;
use OC\Files\Filesystem;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\JLibresignHandler;
use OCA\Libresign\Handler\PkcsHandler;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\MailService;
@ -35,6 +35,8 @@ class SignFileController extends ApiController {
private $fileMapper;
/** @var IRootFolder */
private $root;
/** @var PkcsHandler */
private $pkcsHandler;
/** @var SignFileService */
private $signFile;
/** @var AccountService */
@ -54,6 +56,7 @@ class SignFileController extends ApiController {
FileUserMapper $fileUserMapper,
FileMapper $fileMapper,
IRootFolder $root,
PkcsHandler $pkcsHandler,
IUserSession $userSession,
AccountService $account,
SignFileService $signFile,
@ -67,6 +70,7 @@ class SignFileController extends ApiController {
$this->fileUserMapper = $fileUserMapper;
$this->fileMapper = $fileMapper;
$this->root = $root;
$this->pkcsHandler = $pkcsHandler;
$this->userSession = $userSession;
$this->account = $account;
$this->signFile = $signFile;
@ -274,7 +278,7 @@ class SignFileController extends ApiController {
$signedFile = $this->signFile->sign($fileData, $fileUser, $password);
$fileToSign = $this->signFile->getFileToSing($fileData);
$certificatePath = $this->account->getPfx($fileUser->getUserId());
$certificatePath = $this->pkcsHandler->getPfx($fileUser->getUserId());
list(, $signedContent) = $this->libresignHandler->signExistingFile($fileToSign, $certificatePath, $password);
$fileToSign->putContent($signedContent);
$fileUser->setSigned(time());

View file

@ -0,0 +1,56 @@
<?php
namespace OCA\Libresign\Handler;
use OC\Files\Filesystem;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Service\FolderService;
use OCP\Files\File;
class PkcsHandler {
/** @var string */
private $pfxFilename = 'signature.pfx';
/** @var FolderService */
private $folderService;
public function __construct(
FolderService $folderService
) {
$this->folderService = $folderService;
}
public function savePfx($uid, $content): File {
$this->folderService->setUserId($uid);
Filesystem::initMountPoints($uid);
$folder = $this->folderService->getFolder();
if ($folder->nodeExists($this->pfxFilename)) {
$file = $folder->get($this->pfxFilename);
if (!$file instanceof File) {
throw new LibresignException("path {$this->pfxFilename} already exists and is not a file!", 400);
}
$file->putContent($content);
return $file;
}
$file = $folder->newFile($this->pfxFilename);
$file->putContent($content);
return $file;
}
/**
* Get pfx file
*
* @param string $uid user id
* @return \OCP\Files\Node
*/
public function getPfx($uid) {
Filesystem::initMountPoints($uid);
$this->folderService->setUserId($uid);
$folder = $this->folderService->getFolder();
if (!$folder->nodeExists($this->pfxFilename)) {
throw new \Exception('Password to sign not defined. Create a password to sign', 400);
}
return $folder->get($this->pfxFilename);
}
}

View file

@ -11,6 +11,7 @@ use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Db\ReportDao;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\CfsslHandler;
use OCA\Libresign\Handler\PkcsHandler;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Settings\Mailer\NewUserMailHelper;
@ -34,8 +35,6 @@ class AccountService {
private $fileUser;
/** @var IUserManager */
protected $userManager;
/** @var FolderService */
private $folder;
/** @var IRootFolder */
private $root;
/** @var IConfig */
@ -48,14 +47,14 @@ class AccountService {
private $urlGenerator;
/** @var CfsslHandler */
private $cfsslHandler;
/** @var PkcsHandler */
private $pkcsHandler;
/** @var FileMapper */
private $fileMapper;
/** @var ReportDao */
private $reportDao;
/** @var SignFileService */
private $signFile;
/** @var string */
private $pfxFilename = 'signature.pfx';
/** @var \OCA\Libresign\DbFile */
private $fileData;
/** @var \OCA\Files\Node\File */
@ -71,7 +70,6 @@ class AccountService {
IL10N $l10n,
FileUserMapper $fileUserMapper,
IUserManager $userManager,
FolderService $folder,
IRootFolder $root,
FileMapper $fileMapper,
ReportDao $reportDao,
@ -81,6 +79,7 @@ class AccountService {
ValidateHelper $validateHelper,
IURLGenerator $urlGenerator,
CfsslHandler $cfsslHandler,
PkcsHandler $pkcsHandler,
IGroupManager $groupManager,
AccountFileService $accountFileService,
AccountFileMapper $accountFileMapper
@ -88,7 +87,6 @@ class AccountService {
$this->l10n = $l10n;
$this->fileUserMapper = $fileUserMapper;
$this->userManager = $userManager;
$this->folder = $folder;
$this->root = $root;
$this->fileMapper = $fileMapper;
$this->reportDao = $reportDao;
@ -98,6 +96,7 @@ class AccountService {
$this->validateHelper = $validateHelper;
$this->urlGenerator = $urlGenerator;
$this->cfsslHandler = $cfsslHandler;
$this->pkcsHandler = $pkcsHandler;
$this->groupManager = $groupManager;
$this->accountFileService = $accountFileService;
$this->accountFileMapper = $accountFileMapper;
@ -249,41 +248,7 @@ class AccountService {
if (!$content) {
throw new LibresignException('Failure on generate certificate', 1);
}
return $this->savePfx($uid, $content);
}
private function savePfx($uid, $content): File {
$this->folder->setUserId($uid);
Filesystem::initMountPoints($uid);
$folder = $this->folder->getFolder();
if ($folder->nodeExists($this->pfxFilename)) {
$file = $folder->get($this->pfxFilename);
if (!$file instanceof File) {
throw new LibresignException("path {$this->pfxFilename} already exists and is not a file!", 400);
}
$file->putContent($content);
return $file;
}
$file = $folder->newFile($this->pfxFilename);
$file->putContent($content);
return $file;
}
/**
* Get pfx file
*
* @param string $uid user id
* @return \OCP\Files\Node
*/
public function getPfx($uid) {
Filesystem::initMountPoints($uid);
$this->folder->setUserId($uid);
$folder = $this->folder->getFolder();
if (!$folder->nodeExists($this->pfxFilename)) {
throw new \Exception('Password to sign not defined. Create a password to sign', 400);
}
return $folder->get($this->pfxFilename);
return $this->pkcsHandler->savePfx($uid, $content);
}
/**
@ -394,7 +359,7 @@ class AccountService {
return false;
}
try {
$this->getPfx($userId);
$this->pkcsHandler->getPfx($userId);
return true;
} catch (\Throwable $th) {
}

View file

@ -10,6 +10,7 @@ use OCA\Libresign\Db\FileUser as FileUserEntity;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\JLibresignHandler;
use OCA\Libresign\Handler\PkcsHandler;
use OCA\Libresign\Helper\ValidateHelper;
use OCP\AppFramework\Http;
use OCP\Files\File;
@ -41,6 +42,8 @@ class SignFileService {
private $fileMapper;
/** @var FileUserMapper */
private $fileUserMapper;
/** @var PkcsHandler */
private $pkcsHandler;
/** @var FolderService */
private $folderService;
/** @var IClientService */
@ -64,6 +67,7 @@ class SignFileService {
IL10N $l10n,
FileMapper $fileMapper,
FileUserMapper $fileUserMapper,
PkcsHandler $pkcsHandler,
FolderService $folderService,
IClientService $client,
IUserManager $userManager,
@ -78,6 +82,7 @@ class SignFileService {
$this->l10n = $l10n;
$this->fileMapper = $fileMapper;
$this->fileUserMapper = $fileUserMapper;
$this->pkcsHandler = $pkcsHandler;
$this->folderService = $folderService;
$this->client = $client;
$this->userManager = $userManager;
@ -445,7 +450,7 @@ class SignFileService {
public function sign(FileEntity $fileData, FileUserEntity $fileUser, string $password): \OCP\Files\File {
$fileToSign = $this->getFileToSing($fileData);
$certificatePath = $this->account->getPfx($fileUser->getUserId());
$certificatePath = $this->pkcsHandler->getPfx($fileUser->getUserId());
list(, $signedContent) = $this->libresignHandler->signExistingFile($fileToSign, $certificatePath, $password);
$fileToSign->putContent($signedContent);
$fileUser->setSigned(time());

View file

@ -13,6 +13,13 @@
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests/Unit</directory>
<directory suffix="Test.php">tests/Api</directory>
</testsuite>
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
<testsuite name="api">
<directory suffix="Test.php">tests/Api</directory>
</testsuite>
</testsuites>

View file

@ -1,6 +1,6 @@
<?php
namespace OCA\Libresign\Tests\Unit;
namespace OCA\Libresign\Tests\Api;
use ByJG\ApiTools\AbstractRequester;
use ByJG\ApiTools\ApiRequester;
@ -16,10 +16,10 @@ use ByJG\ApiTools\OpenApi\OpenApiSchema;
use ByJG\Util\Psr7\MessageException;
use ByJG\Util\Psr7\Response;
use donatj\MockWebServer\MockWebServer;
use OCA\Libresign\Tests\Unit\TestCase;
use Symfony\Component\Yaml\Yaml;
class ApiTestCase extends TestCase {
use UserTrait;
/**
* @var Schema
*/

View file

@ -1,8 +1,8 @@
<?php
namespace OCA\Libresign\Tests\Unit\Controller;
namespace OCA\Libresign\Tests\Api\Controller;
use OCA\Libresign\Tests\Unit\ApiTestCase;
use OCA\Libresign\Tests\Api\ApiTestCase;
/**
* @group DB

View file

@ -1,9 +1,9 @@
<?php
namespace OCA\Libresign\Tests\Unit\Controller;
namespace OCA\Libresign\Tests\Api\Controller;
use donatj\MockWebServer\Response;
use OCA\Libresign\Tests\Unit\ApiTestCase;
use OCA\Libresign\Tests\Api\ApiTestCase;
use org\bovigo\vfs\vfsStream;
/**

View file

@ -1,12 +1,14 @@
<?php
namespace OCA\Libresign\Tests\Unit\Controller;
namespace OCA\Libresign\Tests\Api\Controller;
use OCA\Libresign\Tests\Api\ApiTestCase;
/**
* @internal
* @group DB
*/
final class LibresignControllerTest extends \OCA\Libresign\Tests\Unit\ApiTestCase {
final class LibresignControllerTest extends ApiTestCase {
/**
* @runInSeparateProcess
*/

View file

@ -1,8 +1,8 @@
<?php
namespace OCA\Libresign\Tests\Unit\Controller;
namespace OCA\Libresign\Tests\Api\Controller;
use OCA\Libresign\Tests\Unit\ApiTestCase;
use OCA\Libresign\Tests\Api\ApiTestCase;
/**
* @group DB

View file

@ -0,0 +1,505 @@
<?php
namespace OCA\Libresign\Tests\Api\Controller;
use donatj\MockWebServer\Response;
use Jeidison\JSignPDF\JSignPDF;
use OCA\Libresign\Tests\Api\ApiTestCase;
/**
* @group DB
*/
final class SignFileControllerTest extends ApiTestCase {
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithInvalidFileToSign() {
$this->createUser('username', 'password');
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/file_id/invalid')
->withRequestBody([
'password' => 'secretPassword'
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('Invalid data to sign file', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithInvalidUuidToSign() {
$this->createUser('username', 'password');
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/invalid')
->withRequestBody([
'password' => 'secretPassword'
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('Invalid data to sign file', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithAlreadySignedFile() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$file['users'][0]->setSigned(time());
$fileUser = \OC::$server->get(\OCA\Libresign\Db\FileUserMapper::class);
$fileUser->update($file['users'][0]);
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => 'secretPassword'
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('File already signed by you', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithNotFoundFile() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$folderService = \OC::$server->get(\OCA\Libresign\Service\FolderService::class);
$libresignFolder = $folderService->getFolder();
$libresignFolder->delete();
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => 'secretPassword'
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('File not found', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithoutPfx() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => ''
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('Password to sign not defined. Create a password to sign', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithEmptyCertificatePassword() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$accountService = \OC::$server->get(\OCA\Libresign\Service\AccountService::class);
$accountService->generateCertificate('person@test.coop', 'secretPassword', 'username');
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => ''
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('Certificate Password is Empty.', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithSuccess() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$accountService = \OC::$server->get(\OCA\Libresign\Service\AccountService::class);
$accountService->generateCertificate('person@test.coop', 'secretPassword', 'username');
$mock = $this->createMock(JSignPDF::class);
$mock->method('sign')->willReturn('content');
$jsignHandler = \OC::$server->get(\OCA\Libresign\Handler\JLibresignHandler::class);
$jsignHandler->setJSignPdf($mock);
\OC::$server->registerService(\OCA\Libresign\Handler\JLibresignHandler::class, function () use ($mock) {
return $mock;
});
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => 'secretPassword'
]);
$this->assertRequest();
}
/**
* @runInSeparateProcess
*/
public function testPostRegisterWithValidationFailure() {
$this->createUser('username', 'password');
$this->request
->withMethod('POST')
->withPath('/sign/register')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'name' => 'filename',
'file' => [],
'users' => []
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('You are not allowed to request signing', $body['message']);
}
/**
* @runInSeparateProcess
*/
public function testPostRegisterWithSuccess() {
$this->createUser('username', 'password');
$this->mockConfig([
'libresign' => [
'webhook_authorized' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0
]
]);
$this->request
->withMethod('POST')
->withPath('/sign/register')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'name' => 'filename',
'file' => [
'base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))
],
'users' => [
[
'email' => 'user@test.coop'
]
]
]);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$body['data']['users'][] = ['email' => 'user@test.coop'];
$this->addFile($body['data']);
}
/**
* @runInSeparateProcess
*/
public function testPatchRegisterWithValidationFailure() {
$this->createUser('username', 'password');
$this->request
->withMethod('PATCH')
->withPath('/sign/register')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'uuid' => '12345678-1234-1234-1234-123456789012',
'users' => []
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('You are not allowed to request signing', $body['message']);
}
/**
* @runInSeparateProcess
*/
public function testPatchRegisterWithSuccess() {
$user = $this->createUser('username', 'password');
$this->mockConfig([
'libresign' => [
'webhook_authorized' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0
]
]);
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$this->request
->withMethod('PATCH')
->withPath('/sign/register')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'uuid' => $file['uuid'],
'users' => [
[
'email' => 'user@test.coop'
]
]
]);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$body['data']['users'][] = ['email' => 'user@test.coop'];
}
/**
* @runInSeparateProcess
*/
public function testDeleteRegisterWithValidationFailure() {
$user = $this->createUser('username', 'password');
$this->request
->withMethod('DELETE')
->withPath('/sign/register/signature')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'uuid' => 'invalid',
'users' => []
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('You are not allowed to request signing', $body['message']);
}
/**
* @runInSeparateProcess
*/
public function testDeleteRegisterWithSuccess() {
$user = $this->createUser('username', 'password');
$this->mockConfig([
'libresign' => [
'webhook_authorized' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0
]
]);
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'user01@test.coop'
]
],
'userManager' => $user
]);
$this->request
->withMethod('DELETE')
->withPath('/sign/register/signature')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'uuid' => $file['uuid'],
'users' => [
[
'email' => 'user01@test.coop'
]
]
]);
$this->assertRequest();
}
/**
* @runInSeparateProcess
*/
public function testAccountSignatureEndpointWithSuccess() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
self::$server->setResponseOfPath('/api/v1/cfssl/newcert', new Response(
file_get_contents(__DIR__ . '/../../fixtures/cfssl/newcert-with-success.json')
));
$this->mockConfig([
'libresign' => [
'notifyUnsignedUser' => 0,
'commonName' => 'CommonName',
'country' => 'Brazil',
'organization' => 'Organization',
'organizationUnit' => 'organizationUnit',
'cfsslUri' => self::$server->getServerRoot() . '/api/v1/cfssl/'
]
]);
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'signPassword' => 'password'
])
->withPath('/account/signature');
$home = $user->getHome();
$this->assertFileDoesNotExist($home . '/files/LibreSign/signature.pfx');
$this->assertRequest();
$this->assertFileExists($home . '/files/LibreSign/signature.pfx');
}
/**
* @runInSeparateProcess
*/
public function testAccountSignatureEndpointWithFailure() {
$this->createUser('username', 'password');
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'signPassword' => ''
])
->withPath('/account/signature')
->assertResponseCode(401);
$this->assertRequest();
}
}

View file

@ -6,11 +6,6 @@ namespace OCA\Libresign\Tests\Unit;
* @group DB
*/
final class PageControllerTest extends TestCase {
use UserTrait;
/**
* @runInSeparateProcess
*/
public function testIndexScriptsAndTemplate() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$response = $controller->index();
@ -18,9 +13,6 @@ final class PageControllerTest extends TestCase {
$this->assertContains('libresign/js/libresign-main', \OC_Util::$scripts);
}
/**
* @runInSeparateProcess
*/
public function testIndexInitialState() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$controller->index();
@ -29,9 +21,6 @@ final class PageControllerTest extends TestCase {
$this->assertArrayHasKey('libresign-config', $initialStates);
}
/**
* @runInSeparateProcess
*/
public function testSignScriptsAndTemplate() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$response = $controller->sign('uuid');
@ -39,9 +28,6 @@ final class PageControllerTest extends TestCase {
$this->assertContains('libresign/js/libresign-external', \OC_Util::$scripts);
}
/**
* @runInSeparateProcess
*/
public function testSignPolices() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$response = $controller->sign('uuid');
@ -50,9 +36,6 @@ final class PageControllerTest extends TestCase {
$this->assertContains("'self'", $polices->getAllowedFrameDomains());
}
/**
* @runInSeparateProcess
*/
public function testSignInitialState() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$controller->sign('uuid');
@ -61,9 +44,6 @@ final class PageControllerTest extends TestCase {
$this->assertArrayHasKey('libresign-config', $initialStates);
}
/**
* @runInSeparateProcess
*/
public function testGetPdfNotFound() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$response = $controller->getPdf('uuid');
@ -71,9 +51,6 @@ final class PageControllerTest extends TestCase {
$this->assertEquals(404, $response->getStatus());
}
/**
* @runInSeparateProcess
*/
public function testGetPdfHeader() {
$user = $this->createUser('username', 'password');
@ -96,9 +73,6 @@ final class PageControllerTest extends TestCase {
$this->assertEquals('application/pdf', $headers['Content-Type']);
}
/**
* @runInSeparateProcess
*/
public function testGetPdfStatusCode() {
$user = $this->createUser('username', 'password');
@ -119,9 +93,6 @@ final class PageControllerTest extends TestCase {
$this->assertEquals(200, $response->getStatus());
}
/**
* @runInSeparateProcess
*/
public function testGetPdfUserNotFound() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$response = $controller->getPdfUser('uuid');
@ -129,9 +100,6 @@ final class PageControllerTest extends TestCase {
$this->assertEquals(404, $response->getStatus());
}
/**
* @runInSeparateProcess
*/
public function testGetPdfUserHeaderAndStatusCode() {
$user = $this->createUser('username', 'password');
@ -159,9 +127,6 @@ final class PageControllerTest extends TestCase {
$this->assertEquals(200, $response->getStatus());
}
/**
* @runInSeparateProcess
*/
public function testValidationScriptsAndTemplate() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$response = $controller->validation();
@ -169,9 +134,6 @@ final class PageControllerTest extends TestCase {
$this->assertContains('libresign/js/libresign-validation', \OC_Util::$scripts);
}
/**
* @runInSeparateProcess
*/
public function testValidationInitialState() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$controller->validation();
@ -180,9 +142,6 @@ final class PageControllerTest extends TestCase {
$this->assertArrayHasKey('libresign-config', $initialStates);
}
/**
* @runInSeparateProcess
*/
public function testResetPasswordScriptsAndTemplate() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$response = $controller->resetPassword();
@ -190,9 +149,6 @@ final class PageControllerTest extends TestCase {
$this->assertContains('libresign/js/libresign-main', \OC_Util::$scripts);
}
/**
* @runInSeparateProcess
*/
public function testResetPasswordInitialState() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$controller->validation();
@ -201,9 +157,6 @@ final class PageControllerTest extends TestCase {
$this->assertArrayHasKey('libresign-config', $initialStates);
}
/**
* @runInSeparateProcess
*/
public function testValidationFileScriptsAndTemplate() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$response = $controller->validationFile('uuid');
@ -211,9 +164,6 @@ final class PageControllerTest extends TestCase {
$this->assertContains('libresign/js/libresign-validation', \OC_Util::$scripts);
}
/**
* @runInSeparateProcess
*/
public function testValidationFileInitialState() {
$controller = \OC::$server->get(\OCA\Libresign\Controller\PageController::class);
$controller->validationFile('uuid');

View file

@ -2,16 +2,15 @@
namespace OCA\Libresign\Tests\Unit\Controller;
use donatj\MockWebServer\Response;
use Jeidison\JSignPDF\JSignPDF;
use OCA\Libresign\Controller\SignFileController;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Handler\JLibresignHandler;
use OCA\Libresign\Handler\PkcsHandler;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\MailService;
use OCA\Libresign\Service\SignFileService;
use OCA\Libresign\Tests\Unit\ApiTestCase;
use OCA\Libresign\Tests\Unit\TestCase;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
@ -23,16 +22,14 @@ use OCP\IUserSession;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\LoggerInterface;
/**
* @group DB
*/
final class SignFileControllerTest extends ApiTestCase {
final class SignFileControllerTest extends TestCase {
use ProphecyTrait;
public function testSignFileWithSuccess() {
$request = $this->prophesize(IRequest::class);
$fileUserMapper = $this->prophesize(FileUserMapper::class);
$fileMapper = $this->prophesize(FileMapper::class);
$root = $this->createMock(IRootFolder::class);
$pkcsHandler = $this->createMock(PkcsHandler::class);
$l10n = $this->createMock(IL10N::class);
$l10n
->method('t')
@ -91,6 +88,7 @@ final class SignFileControllerTest extends ApiTestCase {
$fileUserMapper->reveal(),
$fileMapper->reveal(),
$root,
$pkcsHandler,
$userSession,
$accountService,
$signFile,
@ -131,6 +129,7 @@ final class SignFileControllerTest extends ApiTestCase {
$fileUserMapper = $this->prophesize(FileUserMapper::class);
$fileMapper = $this->prophesize(FileMapper::class);
$root = $this->createMock(IRootFolder::class);
$pkcsHandler = $this->createMock(PkcsHandler::class);
$l10n = $this->createMock(IL10N::class);
$l10n
->method('t')
@ -151,6 +150,7 @@ final class SignFileControllerTest extends ApiTestCase {
$fileUserMapper->reveal(),
$fileMapper->reveal(),
$root,
$pkcsHandler,
$userSession,
$accountService,
$signFile,
@ -165,497 +165,4 @@ final class SignFileControllerTest extends ApiTestCase {
static::assertSame(["parameter '{$paramenterMissing}' is required!"], $result->getData()['errors']);
static::assertSame(422, $result->getStatus());
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithInvalidFileToSign() {
$this->createUser('username', 'password');
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/file_id/invalid')
->withRequestBody([
'password' => 'secretPassword'
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('Invalid data to sign file', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithInvalidUuidToSign() {
$this->createUser('username', 'password');
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/invalid')
->withRequestBody([
'password' => 'secretPassword'
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('Invalid data to sign file', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithAlreadySignedFile() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$file['users'][0]->setSigned(time());
$fileUser = \OC::$server->get(\OCA\Libresign\Db\FileUserMapper::class);
$fileUser->update($file['users'][0]);
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => 'secretPassword'
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('File already signed by you', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithNotFoundFile() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$folderService = \OC::$server->get(\OCA\Libresign\Service\FolderService::class);
$libresignFolder = $folderService->getFolder();
$libresignFolder->delete();
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => 'secretPassword'
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('File not found', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithoutPfx() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => ''
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('Password to sign not defined. Create a password to sign', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithEmptyCertificatePassword() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$accountService = \OC::$server->get(\OCA\Libresign\Service\AccountService::class);
$accountService->generateCertificate('person@test.coop', 'secretPassword', 'username');
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => ''
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('Certificate Password is Empty.', $body['errors'][0]);
}
/**
* @runInSeparateProcess
*/
public function testSignUsingFileIdWithSuccess() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$accountService = \OC::$server->get(\OCA\Libresign\Service\AccountService::class);
$accountService->generateCertificate('person@test.coop', 'secretPassword', 'username');
$mock = $this->createMock(JSignPDF::class);
$mock->method('sign')->willReturn('content');
$jsignHandler = \OC::$server->get(\OCA\Libresign\Handler\JLibresignHandler::class);
$jsignHandler->setJSignPdf($mock);
\OC::$server->registerService(\OCA\Libresign\Handler\JLibresignHandler::class, function () use ($mock) {
return $mock;
});
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withPath('/sign/uuid/' . $file['users'][0]->getUuid())
->withRequestBody([
'password' => 'secretPassword'
]);
$this->assertRequest();
}
/**
* @runInSeparateProcess
*/
public function testPostRegisterWithValidationFailure() {
$this->createUser('username', 'password');
$this->request
->withMethod('POST')
->withPath('/sign/register')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'name' => 'filename',
'file' => [],
'users' => []
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('You are not allowed to request signing', $body['message']);
}
/**
* @runInSeparateProcess
*/
public function testPostRegisterWithSuccess() {
$this->createUser('username', 'password');
$this->mockConfig([
'libresign' => [
'webhook_authorized' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0
]
]);
$this->request
->withMethod('POST')
->withPath('/sign/register')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'name' => 'filename',
'file' => [
'base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))
],
'users' => [
[
'email' => 'user@test.coop'
]
]
]);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$body['data']['users'][] = ['email' => 'user@test.coop'];
$this->addFile($body['data']);
}
/**
* @runInSeparateProcess
*/
public function testPatchRegisterWithValidationFailure() {
$this->createUser('username', 'password');
$this->request
->withMethod('PATCH')
->withPath('/sign/register')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'uuid' => '12345678-1234-1234-1234-123456789012',
'users' => []
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('You are not allowed to request signing', $body['message']);
}
/**
* @runInSeparateProcess
*/
public function testPatchRegisterWithSuccess() {
$user = $this->createUser('username', 'password');
$this->mockConfig([
'libresign' => [
'webhook_authorized' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0
]
]);
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'person@test.coop'
]
],
'userManager' => $user
]);
$this->request
->withMethod('PATCH')
->withPath('/sign/register')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'uuid' => $file['uuid'],
'users' => [
[
'email' => 'user@test.coop'
]
]
]);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$body['data']['users'][] = ['email' => 'user@test.coop'];
}
/**
* @runInSeparateProcess
*/
public function testDeleteRegisterWithValidationFailure() {
$user = $this->createUser('username', 'password');
$this->request
->withMethod('DELETE')
->withPath('/sign/register/signature')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'uuid' => 'invalid',
'users' => []
])
->assertResponseCode(422);
$response = $this->assertRequest();
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals('You are not allowed to request signing', $body['message']);
}
/**
* @runInSeparateProcess
*/
public function testDeleteRegisterWithSuccess() {
$user = $this->createUser('username', 'password');
$this->mockConfig([
'libresign' => [
'webhook_authorized' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0
]
]);
$user->setEMailAddress('person@test.coop');
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
'users' => [
[
'email' => 'user01@test.coop'
]
],
'userManager' => $user
]);
$this->request
->withMethod('DELETE')
->withPath('/sign/register/signature')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'uuid' => $file['uuid'],
'users' => [
[
'email' => 'user01@test.coop'
]
]
]);
$this->assertRequest();
}
/**
* @runInSeparateProcess
*/
public function testAccountSignatureEndpointWithSuccess() {
$user = $this->createUser('username', 'password');
$user->setEMailAddress('person@test.coop');
self::$server->setResponseOfPath('/api/v1/cfssl/newcert', new Response(
file_get_contents(__DIR__ . '/../../fixtures/cfssl/newcert-with-success.json')
));
$this->mockConfig([
'libresign' => [
'notifyUnsignedUser' => 0,
'commonName' => 'CommonName',
'country' => 'Brazil',
'organization' => 'Organization',
'organizationUnit' => 'organizationUnit',
'cfsslUri' => self::$server->getServerRoot() . '/api/v1/cfssl/'
]
]);
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'signPassword' => 'password'
])
->withPath('/account/signature');
$home = $user->getHome();
$this->assertFileDoesNotExist($home . '/files/LibreSign/signature.pfx');
$this->assertRequest();
$this->assertFileExists($home . '/files/LibreSign/signature.pfx');
}
/**
* @runInSeparateProcess
*/
public function testAccountSignatureEndpointWithFailure() {
$this->createUser('username', 'password');
$this->request
->withMethod('POST')
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('username:password'),
'Content-Type' => 'application/json'
])
->withRequestBody([
'signPassword' => ''
])
->withPath('/account/signature')
->assertResponseCode(401);
$this->assertRequest();
}
}

View file

@ -0,0 +1,63 @@
<?php
use OCA\Libresign\Handler\PkcsHandler;
use OCA\Libresign\Service\FolderService;
final class PkcsHandlerTest extends \OCA\Libresign\Tests\Unit\TestCase {
/** @var PkcsHandler */
protected $pkcsHandler;
/** @var FolderService */
protected $folderService;
public function setUp(): void {
$this->folderService = $this->createMock(FolderService::class);
$this->pkcsHandler = new PkcsHandler(
$this->folderService
);
}
public function testGetPfxWithInvalidUser() {
$this->expectErrorMessage('Backends provided no user object for invalidUser');
$this->pkcsHandler->getPfx('invalidUser');
}
public function testGetPfxWithInvalidPfx() {
$backend = $this->createMock(\OC\User\Database::class);
$backend->method('implementsActions')
->willReturn(true);
$backend->method('userExists')
->willReturn(true);
$backend->method('getRealUID')
->willReturn('userId');
$userManager = \OC::$server->getUserManager();
$userManager->clearBackends();
$userManager->registerBackend($backend);
$node = $this->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($this->returnValue(false));
$this->folderService->method('getFolder')->will($this->returnValue($node));
$this->expectErrorMessage('Password to sign not defined. Create a password to sign');
$this->expectExceptionCode(400);
$this->pkcsHandler->getPfx('userId');
}
public function testGetPfxOk() {
$backend = $this->createMock(\OC\User\Database::class);
$backend->method('implementsActions')
->willReturn(true);
$backend->method('userExists')
->willReturn(true);
$backend->method('getRealUID')
->willReturn('userId');
$userManager = \OC::$server->getUserManager();
$userManager->clearBackends();
$userManager->registerBackend($backend);
$node = $this->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($this->returnValue(true));
$node->method('get')->will($this->returnValue($node));
$this->folderService->method('getFolder')->will($this->returnValue($node));
$actual = $this->pkcsHandler->getPfx('userId');
$this->assertInstanceOf('\OCP\Files\Node', $actual);
}
}

View file

@ -8,13 +8,12 @@ use OCA\Libresign\Db\FileUser;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Db\ReportDao;
use OCA\Libresign\Handler\CfsslHandler;
use OCA\Libresign\Handler\PkcsHandler;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Service\AccountFileService;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\FolderService;
use OCA\Libresign\Service\SignFileService;
use OCA\Libresign\Tests\Unit\UserTrait;
use OCA\Settings\Mailer\NewUserMailHelper;
use OCP\Files\IRootFolder;
use OCP\IConfig;
@ -28,15 +27,12 @@ use OCP\IUserManager;
* @group DB
*/
final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
use UserTrait;
/** @var IL10N */
private $l10n;
/** @var FileUserMapper */
private $fileUserMapper;
/** @var IUserManager */
private $userManagerInstance;
/** @var FolderService */
private $folder;
/** @var IRootFolder */
private $root;
/** @var FileMapper */
@ -69,7 +65,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
->will($this->returnArgument(0));
$this->fileUserMapper = $this->createMock(FileUserMapper::class);
$this->userManagerInstance = $this->createMock(IUserManager::class);
$this->folder = $this->createMock(FolderService::class);
$this->root = $this->createMock(IRootFolder::class);
$this->fileMapper = $this->createMock(FileMapper::class);
$this->reportDao = $this->createMock(ReportDao::class);
@ -79,15 +74,15 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->validateHelper = \OC::$server->get(\OCA\Libresign\Helper\ValidateHelper::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->cfsslHandler = $this->createMock(CfsslHandler::class);
$this->pkcsHandler = $this->createMock(PkcsHandler::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->accountFileService = $this->createMock(AccountFileService::class);
$this->AccountFileMapper = $this->createMock(AccountFileMapper::class);
$this->accountFileMapper = $this->createMock(AccountFileMapper::class);
$this->service = new AccountService(
$this->l10n,
$this->fileUserMapper,
$this->userManagerInstance,
$this->folder,
$this->root,
$this->fileMapper,
$this->reportDao,
@ -97,9 +92,10 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->validateHelper,
$this->urlGenerator,
$this->cfsslHandler,
$this->pkcsHandler,
$this->groupManager,
$this->accountFileService,
$this->AccountFileMapper
$this->accountFileMapper
);
}
@ -115,7 +111,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->l10n,
$this->fileUserMapper,
$this->userManagerInstance,
$this->folder,
$this->root,
$this->fileMapper,
$this->reportDao,
@ -125,9 +120,10 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->validateHelper,
$this->urlGenerator,
$this->cfsslHandler,
$this->pkcsHandler,
$this->groupManager,
$this->accountFileService,
$this->AccountFileMapper
$this->accountFileMapper
);
$this->expectExceptionMessage($expectedErrorMessage);
$this->service->validateCreateToSign($arguments);
@ -265,7 +261,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->l10n,
$this->fileUserMapper,
$this->userManagerInstance,
$this->folder,
$this->root,
$this->fileMapper,
$this->reportDao,
@ -275,9 +270,10 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->validateHelper,
$this->urlGenerator,
$this->cfsslHandler,
$this->pkcsHandler,
$this->groupManager,
$this->accountFileService,
$this->AccountFileMapper
$this->accountFileMapper
);
$this->expectExceptionMessage($expectedErrorMessage);
$this->service->validateCertificateData($arguments);
@ -385,7 +381,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->l10n,
$this->fileUserMapper,
$this->userManagerInstance,
$this->folder,
$this->root,
$this->fileMapper,
$this->reportDao,
@ -395,9 +390,10 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->validateHelper,
$this->urlGenerator,
$this->cfsslHandler,
$this->pkcsHandler,
$this->groupManager,
$this->accountFileService,
$this->AccountFileMapper
$this->accountFileMapper
);
$actual = $this->service->validateCreateToSign([
'uuid' => '12345678-1234-1234-1234-123456789012',
@ -416,41 +412,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->service->generateCertificate('uid', 'password', 'username');
}
public function testGenerateCertificateAndSaveToAFolderAndNotAFile() {
$node = $this->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($this->returnValue(true));
$node->method('get')->will($this->returnValue($node));
$this->folder->method('getFolder')->will($this->returnValue($node));
$backend = $this->createMock(\OC\User\Database::class);
$backend->method('implementsActions')
->willReturn(true);
$backend->method('userExists')
->willReturn(true);
$backend->method('getRealUID')
->willReturn('userId');
$userManager = \OC::$server->getUserManager();
$userManager->clearBackends();
$userManager->registerBackend($backend);
$this->cfsslHandler
->method('__call')
->will($this->returnValue($this->cfsslHandler));
$this->cfsslHandler
->method('generateCertificate')
->will($this->returnValue('raw content of pfx file'));
$this->expectErrorMessage('path signature.pfx already exists and is not a file!');
$this->expectExceptionCode(400);
$this->service->generateCertificate('uid', 'password', 'username');
}
public function testGenerateCertificateAndSuccessfullySavedToAnExistingFile() {
$node = $this->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($this->returnValue(true));
$file = $this->createMock(\OCP\Files\File::class);
$node->method('get')->will($this->returnValue($file));
$this->folder->method('getFolder')->will($this->returnValue($node));
$backend = $this->createMock(\OC\User\Database::class);
$backend->method('implementsActions')
->willReturn(true);
@ -473,12 +435,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
}
public function testGenerateCertificateAndSuccessfullySavedToANewFile() {
$node = $this->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($this->returnValue(false));
$file = $this->createMock(\OCP\Files\File::class);
$node->method('newFile')->will($this->returnValue($file));
$this->folder->method('getFolder')->will($this->returnValue($node));
$backend = $this->createMock(\OC\User\Database::class);
$backend->method('implementsActions')
->willReturn(true);
@ -500,51 +456,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->assertInstanceOf('\OCP\Files\File', $actual);
}
public function testGetPfxWithInvalidUser() {
$this->expectErrorMessage('Backends provided no user object for invalidUser');
$this->service->getPfx('invalidUser');
}
public function testGetPfxWithInvalidPfx() {
$backend = $this->createMock(\OC\User\Database::class);
$backend->method('implementsActions')
->willReturn(true);
$backend->method('userExists')
->willReturn(true);
$backend->method('getRealUID')
->willReturn('userId');
$userManager = \OC::$server->getUserManager();
$userManager->clearBackends();
$userManager->registerBackend($backend);
$node = $this->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($this->returnValue(false));
$this->folder->method('getFolder')->will($this->returnValue($node));
$this->expectErrorMessage('Password to sign not defined. Create a password to sign');
$this->expectExceptionCode(400);
$this->service->getPfx('userId');
}
public function testGetPfxOk() {
$backend = $this->createMock(\OC\User\Database::class);
$backend->method('implementsActions')
->willReturn(true);
$backend->method('userExists')
->willReturn(true);
$backend->method('getRealUID')
->willReturn('userId');
$userManager = \OC::$server->getUserManager();
$userManager->clearBackends();
$userManager->registerBackend($backend);
$node = $this->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($this->returnValue(true));
$node->method('get')->will($this->returnValue($node));
$this->folder->method('getFolder')->will($this->returnValue($node));
$actual = $this->service->getPfx('userId');
$this->assertInstanceOf('\OCP\Files\Node', $actual);
}
public function testCreateToSignWithErrorInSendingEmail() {
$fileUser = $this->createMock(\OCA\Libresign\Db\FileUser::class);
$this->fileUserMapper->method('getByUuid')->will($this->returnValue($fileUser));
@ -568,12 +479,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->userManagerInstance->method('createUser')->will($this->returnValue($userToSign));
$this->config->method('getAppValue')->will($this->returnValue('no'));
$node = $this->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($this->returnValue(false));
$file = $this->createMock(\OCP\Files\File::class);
$node->method('newFile')->will($this->returnValue($file));
$this->folder->method('getFolder')->will($this->returnValue($node));
$backend = $this->createMock(\OC\User\Database::class);
$backend->method('implementsActions')
->willReturn(true);
@ -607,7 +512,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->l10n,
$this->fileUserMapper,
$this->userManagerInstance,
$this->folder,
$this->root,
$this->fileMapper,
$this->reportDao,
@ -617,9 +521,10 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->validateHelper,
$this->urlGenerator,
$this->cfsslHandler,
$this->pkcsHandler,
$this->groupManager,
$this->accountFileService,
$this->AccountFileMapper
$this->accountFileMapper
);
$actual = $this->service->getConfig($uuid, $userId, $formatOfPdfOnSign);
$actual = json_encode($actual);
@ -647,7 +552,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'This is not your file'
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
], null
],
@ -668,7 +573,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'This is not your file'
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
], function ($self) {
$self->createUser('username', 'password');
@ -789,7 +694,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'Invalid user'
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
], function ($self) {
$fileUser = $self->createMock(FileUser::class);
@ -816,7 +721,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'File not found'
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
], function ($self) {
$self->createUser('username', 'password');
@ -853,11 +758,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
]
], function ($self) {
$self->createUser('username', 'password');
$node = $self->createMock(\OCP\Files\Folder::class);
$node->method('nodeExists')->will($self->returnValue(true));
$file = $self->createMock(\OCP\Files\File::class);
$node->method('get')->will($self->returnValue($file));
$self->folder->method('getFolder')->will($self->returnValue($node));
}
],
[ // #11
@ -875,7 +775,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'name' => null
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
], function ($self) {
$self->createUser('username', 'password');
@ -921,7 +821,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'name' => null
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
], function ($self) {
$self->createUser('username', 'password');
@ -951,7 +851,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
->will($self->returnValue([$node]));
}
],
[ // #14
[ // #13
'uuid', 'username', 'file',
[
'action' => JSActions::ACTION_SIGN,
@ -966,7 +866,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'name' => null
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
], function ($self) {
$self->createUser('username', 'password');
@ -997,7 +897,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
->will($self->returnValue([$node]));
}
],
[ // #15
[ // #14
'uuid', 'username', 'nodeId',
[
'action' => JSActions::ACTION_SIGN,
@ -1012,7 +912,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'name' => null
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
], function ($self) {
$self->createUser('username', 'password');
@ -1090,7 +990,7 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
'name' => null
],
'settings' => [
'hasSignatureFile' => false
'hasSignatureFile' => true
]
])
);

View file

@ -3,6 +3,7 @@
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Handler\JLibresignHandler;
use OCA\Libresign\Handler\PkcsHandler;
use OCA\Libresign\Service\FolderService;
use OCA\Libresign\Service\MailService;
use OCA\Libresign\Service\SignFileService;
@ -24,6 +25,8 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
private $groupManager;
/** @var IL10N */
private $l10n;
/** @var PkcsHandler */
private $pkcsHandler;
/** @var SignFileService */
private $service;
/** @var FileMapper */
@ -63,6 +66,7 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->file = $this->createMock(FileMapper::class);
$this->fileUser = $this->createMock(FileUserMapper::class);
$this->user = $this->createMock(IUser::class);
$this->pkcsHandler = $this->createMock(PkcsHandler::class);
$this->clientService = $this->createMock(IClientService::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->mail = $this->createMock(MailService::class);
@ -77,6 +81,7 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->l10n,
$this->file,
$this->fileUser,
$this->pkcsHandler,
$this->folder,
$this->clientService,
$this->userManager,
@ -603,6 +608,7 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->l10n,
$this->file,
$this->fileUser,
$this->pkcsHandler,
$this->folder,
$this->clientService,
$this->userManager,
@ -634,6 +640,7 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
$this->l10n,
$this->file,
$this->fileUser,
$this->pkcsHandler,
$this->folder,
$this->clientService,
$this->userManager,