Skip to content

Commit

Permalink
chore: convert method to get instance of appConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Jan 10, 2025
1 parent c3fdf4d commit aaaf647
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 155 deletions.
37 changes: 14 additions & 23 deletions lib/Handler/CertificateEngine/CfsslHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,49 +314,40 @@ private function getBinary(): string {
return $this->binary;
}

$appKeys = $this->appConfig->getAppKeys();
$binary = '';
if (in_array('cfssl_bin', $appKeys)) {
$binary = $this->appConfig->getValueString(Application::APP_ID, 'cfssl_bin');
if (!file_exists($binary)) {
$this->appConfig->deleteAppValue('cfssl_bin');
}
}

if (!$binary) {
throw new LibresignException('Binary of CFSSL not found. Install binaries.');
}

if (PHP_OS_FAMILY === 'Windows') {
throw new LibresignException('Incompatible with Windows');
}

return $binary;
if ($this->appConfig->hasKey(Application::APP_ID, 'cfssl_bin')) {
$binary = $this->appConfig->getValueString(Application::APP_ID, 'cfssl_bin');
if (!file_exists($binary)) {
$this->appConfig->deleteKey(Application::APP_ID, 'cfssl_bin');
}
return $binary;
}
throw new LibresignException('Binary of CFSSL not found. Install binaries.');
}

private function getCfsslUri(): string {
if ($this->cfsslUri) {
return $this->cfsslUri;
}

$appKeys = $this->appConfig->getAppKeys();
if (in_array('cfssl_uri', $appKeys)) {
if ($uri = $this->appConfig->getValueString(Application::APP_ID, 'cfssl_uri')) {
return $uri;
}
// In case config is an empty string
$this->appConfig->deleteAppValue('cfssl_uri');
if ($uri = $this->appConfig->getValueString(Application::APP_ID, 'cfssl_uri')) {
return $uri;
}
// In case config is an empty string
$this->appConfig->deleteKey(Application::APP_ID, 'cfssl_uri');

$this->cfsslUri = self::CFSSL_URI;
return $this->cfsslUri;
}

public function setCfsslUri($uri): void {
if ($uri) {
$this->appConfig->setAppValue('cfssl_uri', $uri);
$this->appConfig->setValueString(Application::APP_ID, 'cfssl_uri', $uri);
} else {
$this->appConfig->deleteAppValue('cfssl_uri');
$this->appConfig->deleteKey(Application::APP_ID, 'cfssl_uri');
}
$this->cfsslUri = $uri;
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/Settings/Validation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default {
OCP.AppConfig.setValue('libresign', 'validation_site', this.$refs.urlInput.value.trim())
},
async toggleSetting(setting, value) {
OCP.AppConfig.setValue('libresign', setting, value ? true : false)
OCP.AppConfig.setValue('libresign', setting, !!value)
},
placeHolderValidationUrl(data) {
if (data !== '') {
Expand Down
5 changes: 2 additions & 3 deletions tests/Api/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ByJG\ApiTools\OpenApi\OpenApiSchema;
use ByJG\Util\Psr7\MessageException;
use ByJG\Util\Psr7\Response;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Tests\Unit\TestCase;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -51,9 +52,7 @@ public function setUp(): void {
$systemConfig->setValue('auth.bruteforce.protection.enabled', false);

// Reset settings
$this->mockAppConfig([
'identification_documents' => '0',
]);
$this->getMockAppConfig()->setValueBool(Application::APP_ID, 'identification_documents', false);

$this->request = new \OCA\Libresign\Tests\Api\ApiRequester();
}
Expand Down
22 changes: 10 additions & 12 deletions tests/Api/Controller/AccountControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\Libresign\Tests\Api\Controller;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Tests\Api\ApiTestCase;

/**
Expand Down Expand Up @@ -44,16 +45,15 @@ public function testAccountCreateWithInvalidUuid():void {
*/
public function testAccountCreateWithSuccess():void {
$this->markTestSkipped('Need to reimplement this test, stated to failure after add multiple certificate engine');
$this->mockAppConfig([
'cfssl_bin' => '',
'rootCert' => json_encode([
'commonName' => 'LibreCode',
'names' => [
'C' => ['value' => 'BR'],
]
]),
'certificate_engine' => 'openssl',
$appConfig = $this->getMockAppConfig();
$appConfig->setValueString(Application::APP_ID, 'cfssl_bin', '');
$appConfig->setValueArray(Application::APP_ID, 'rootCert', [
'commonName' => 'LibreCode',
'names' => [
'C' => ['value' => 'BR'],
]
]);
$appConfig->setValueString(Application::APP_ID, 'certificate_engine', 'openssl');

$user = $this->createAccount('username', 'password');

Expand Down Expand Up @@ -176,9 +176,7 @@ public function testMeWithAuthenticatedUser():void {
public function testApprovalListWithSuccess():void {
$this->createAccount('allowapprove', 'password', 'testGroup');

$this->mockAppConfig([
'approval_group' => '["testGroup"]'
]);
$this->getMockAppConfig()->setValueArray(Application::APP_ID, 'approval_group', ['testGroup']);

$this->request
->withPath('/api/v1/account/files/approval/list')
Expand Down
29 changes: 14 additions & 15 deletions tests/Api/Controller/AdminControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use bovigo\vfs\vfsStream;
use donatj\MockWebServer\Response;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Tests\Api\ApiTestCase;

/**
Expand Down Expand Up @@ -41,21 +42,19 @@ public function testGenerateCertificateWithSuccess():void {
self::$server->setResponseOfPath('/api/v1/cfssl/health', new Response(
'{"success":true,"result":{"healthy":true},"errors":[],"messages":[]}'
));
$cfsslConfig = [
'rootCert' => json_encode([
'commonName' => 'LibreCode',
'names' => [
'C' => ['value' => 'BR'],
'ST' => ['value' => 'RJ'],
'L' => ['value' => 'Rio de Janeiro'],
'O' => ['value' => 'LibreCode Coop'],
'OU' => ['value' => 'LibreSign'],
],
]),
'cfssl_uri' => self::$server->getServerRoot() . '/api/v1/cfssl/',
'config_path' => 'vfs://home/'
];
$this->mockAppConfig($cfsslConfig);
$appConfig = $this->getMockAppConfig();
$appConfig->setValueArray(Application::APP_ID, 'rootCert', [
'commonName' => 'LibreCode',
'names' => [
'C' => ['value' => 'BR'],
'ST' => ['value' => 'RJ'],
'L' => ['value' => 'Rio de Janeiro'],
'O' => ['value' => 'LibreCode Coop'],
'OU' => ['value' => 'LibreSign'],
],
]);
$appConfig->setValueString(Application::APP_ID, 'cfssl_uri', self::$server->getServerRoot() . '/api/v1/cfssl/');
$appConfig->setValueString(Application::APP_ID, 'config_path', 'vfs://home/');
$cfsslConfig['rootCert'] = json_decode($cfsslConfig['rootCert'], true);

// Configure request
Expand Down
19 changes: 8 additions & 11 deletions tests/Api/Controller/FileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\Libresign\Tests\Api\Controller;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Tests\Api\ApiTestCase;

/**
Expand All @@ -19,8 +20,7 @@ final class FileControllerTest extends ApiTestCase {
* @runInSeparateProcess
*/
public function testValidateUsignUuidWithInvalidData():void {
$this->mockAppConfig([]);

$this->getMockAppConfig();
$this->request
->withPath('/api/v1/file/validate/uuid/invalid')
->assertResponseCode(404);
Expand Down Expand Up @@ -79,12 +79,11 @@ public function testValidateWithSuccessUsingUnloggedUser():void {
public function testValidateWithSuccessUsingSigner():void {
$user = $this->createAccount('username', 'password');
$user->setEMailAddress('[email protected]');
$this->mockAppConfig([
'identify_methods' => [
[
'name' => 'email',
'enabled' => 1,
],
$appConfig = $this->getMockAppConfig();
$appConfig->setValueArray(Application::APP_ID, 'identify_methods', [
[
'name' => 'email',
'enabled' => 1,
],
]);

Expand Down Expand Up @@ -136,9 +135,7 @@ public function testControllerListWithEmptyData():void {
*/
public function testSendNewFile():void {
$this->createAccount('allowrequestsign', 'password');
$this->mockAppConfig([
'groups_request_sign' => '["admin","testGroup"]',
]);
$this->getMockAppConfig()->setValueArray(Application::APP_ID, 'groups_request_sign', ['admin','testGroup']);
$this->request
->withRequestHeader([
'Authorization' => 'Basic ' . base64_encode('allowrequestsign:password'),
Expand Down
2 changes: 1 addition & 1 deletion tests/Api/Controller/FileElementControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testPostSuccess():array {
$signers = $this->getSignersFromFileId($file->getId());
$signers[0]->setSigned(time());

$this->mockAppConfig([]);
$this->getMockAppConfig();
$this->request
->withPath('/api/v1/file-element/' . $file->getUuid())
->withMethod('POST')
Expand Down
8 changes: 4 additions & 4 deletions tests/Api/Controller/NotifyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\Libresign\Tests\Api\Controller;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Tests\Api\ApiTestCase;

/**
Expand Down Expand Up @@ -44,10 +45,9 @@ public function testNotifySignersWithError():void {
*/
public function testNotifySignersWithSuccess():void {
$user = $this->createAccount('allowrequestsign', 'password', 'testGroup');
$this->mockAppConfig([
'groups_request_sign' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0,
]);
$appConfig = $this->getMockAppConfig();
$appConfig->setValueArray(Application::APP_ID, 'groups_request_sign', ['admin','testGroup']);
$appConfig->setValueBool(Application::APP_ID, 'notifyUnsignedUser', true);
$file = $this->requestSignFile([
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
'name' => 'test',
Expand Down
15 changes: 7 additions & 8 deletions tests/Api/Controller/RequestSignatureControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\Libresign\Tests\Api\Controller;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Tests\Api\ApiTestCase;

/**
Expand Down Expand Up @@ -44,10 +45,9 @@ public function testPostRegisterWithValidationFailure():void {
public function testPostRegisterWithSuccess():void {
$this->createAccount('allowrequestsign', 'password', 'testGroup');

$this->mockAppConfig([
'groups_request_sign' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0,
]);
$appConfig = $this->getMockAppConfig();
$appConfig->setValueArray(Application::APP_ID, 'groups_request_sign', ['admin','testGroup']);
$appConfig->setValueBool(Application::APP_ID, 'notifyUnsignedUser', false);

$this->request
->withMethod('POST')
Expand Down Expand Up @@ -104,10 +104,9 @@ public function testPatchRegisterWithValidationFailure():void {
public function testPatchRegisterWithSuccess():void {
$user = $this->createAccount('allowrequestsign', 'password', 'testGroup');

$this->mockAppConfig([
'groups_request_sign' => '["admin","testGroup"]',
'notifyUnsignedUser' => 0,
]);
$appConfig = $this->getMockAppConfig();
$appConfig->setValueArray(Application::APP_ID, 'groups_request_sign', ['admin','testGroup']);
$appConfig->setValueBool(Application::APP_ID, 'notifyUnsignedUser', false);

$user->setEMailAddress('[email protected]');
$file = $this->requestSignFile([
Expand Down
Loading

0 comments on commit aaaf647

Please sign in to comment.