Skip to content

Commit

Permalink
Merge d5fb44e into 3a92507
Browse files Browse the repository at this point in the history
  • Loading branch information
vitormattos committed Apr 11, 2023
2 parents 3a92507 + d5fb44e commit 0ce7f66
Show file tree
Hide file tree
Showing 17 changed files with 750 additions and 724 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/integration.yml → .github/workflows/behat.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Integration tests
name: Behat

on:
pull_request:
Expand All @@ -22,9 +22,8 @@ env:
APP_NAME: libresign

jobs:
integration:
behat:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
Expand All @@ -36,7 +35,7 @@ jobs:

services:
postgres:
image: postgres:14
image: postgres:15
ports:
- 4445:5432/tcp
env:
Expand Down Expand Up @@ -72,11 +71,11 @@ jobs:
path: apps/${{ env.APP_NAME }}

- name: Set up php ${{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
uses: shivammathur/setup-php@d30ad8b1843ace22e6698ab99bbafaa747b6bd0d # 2.24.0
with:
php-version: ${{ matrix.php-versions }}
tools: phpunit
extensions: mbstring, iconv, fileinfo, intl, sqlite, pdo_sqlite, mysql, pdo_mysql, pgsql, pdo_pgsql,
extensions: mbstring, iconv, fileinfo, intl, imagick, sqlite, pdo_sqlite, mysql, pdo_mysql, pgsql, pdo_pgsql,
coverage: none

- name: Set up dependencies
Expand All @@ -85,6 +84,11 @@ jobs:
composer install
composer --working-dir=tests/integration install
- name: Install Ghostscript
run: |
sudo apt-get update
sudo apt-get install ghostscript
- name: Set up Nextcloud
run: |
if [ "${{ matrix.databases }}" = "mysql" ]; then
Expand All @@ -109,4 +113,4 @@ jobs:
working-directory: apps/${{ env.APP_NAME }}/tests/integration
env:
BEHAT_ROOT_DIR: ../../../../
run: vendor/bin/behat -f junit -f pretty
run: vendor/bin/behat -f junit -f pretty --colors
8 changes: 7 additions & 1 deletion lib/Db/AccountFileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ private function formatListRow(array $row, string $url): array {
return $row;
}

/**
* @param array $files
* @param FileUser[] $signers
* @return array
*/
private function assocFileToFileUserAndFormat(array $files, array $signers): array {
foreach ($files as $key => $file) {
$totalSigned = 0;
Expand All @@ -241,7 +246,8 @@ private function assocFileToFileUserAndFormat(array $files, array $signers): arr
->format('Y-m-d H:i:s'),
'sign_date' => null,
'uid' => $signer->getUserId(),
'fileUserId' => $signer->getId()
'fileUserId' => $signer->getId(),
'identify' => $signer->getIdentifyMethod(),
];
if ($signer->getSigned()) {
$data['sign_date'] = (new \DateTime())
Expand Down
5 changes: 5 additions & 0 deletions lib/Db/FileUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* @method string getCode()
* @method void setMetadata(array $metadata)
* @method string getMetadata()
* @method void setIdentifyMethod(string $identifyMethod)
* @method string getIdentifyMethod()
*/
class FileUser extends Entity {
/** @var integer */
Expand Down Expand Up @@ -68,6 +70,9 @@ class FileUser extends Entity {
/** @var string */
protected $metadata;

/** @var string */
protected $identifyMethod;

public function __construct() {
$this->addType('id', 'integer');
$this->addType('fileId', 'integer');
Expand Down
3 changes: 2 additions & 1 deletion lib/Db/FileUserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ private function assocFileToFileUserAndFormat(string $userId, array $files, arra
'sign_date' => null,
'uid' => $signer->getUserId(),
'fileUserId' => $signer->getId(),
'me' => $userId === $signer->getUserId()
'me' => $userId === $signer->getUserId(),
'identify' => $signer->getIdentifyMethod(),
];

if ($data['me']) {
Expand Down
10 changes: 10 additions & 0 deletions lib/Helper/ValidateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ public function haveValidMail(array $data, ?int $type = null): void {
}
}

public function validateIdentifyMethod(string $identify): void {
if (!in_array($identify, ['nextcloud', 'email-link', 'email-token', 'sms-token'])) {
// TRANSLATORS When is requested to a person to sign a file, is
// necessary identify what is the identification method. The
// identification method is used to define how will be the sign
// flow.
throw new LibresignException($this->l10n->t('Invalid identification method'));
}
}

public function signerWasAssociated(array $signer): void {
try {
$libresignFile = $this->fileMapper->getByFileId();
Expand Down
56 changes: 56 additions & 0 deletions lib/Migration/Version8000Date20230402103824.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Libresign\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version8000Date20230402103824 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper */
$schema = $schemaClosure();
$table = $schema->getTable('libresign_file_user');
if (!$table->hasColumn('identify_method')) {
$table->addColumn('identify_method', Types::STRING, [
'notnull' => true,
'default' => 'nextcloud',
'length' => 30,
]);
return $schema;
}
return null;
}
}
15 changes: 13 additions & 2 deletions lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ private function associateToUsers(array $data, int $fileId): array {
return $return;
}

public function getUserIdentifyMethod(array $user): string {
if (array_key_exists('identify', $user)) {
return $user['identify'];
}
return $this->config->getAppValue(Application::APP_ID, 'identify_method', 'nextcloud') ?? 'nextcloud';
}

/**
* @psalm-suppress MixedReturnStatement
*/
Expand All @@ -299,6 +306,8 @@ private function setDataToUser(FileUserEntity $fileUser, array $user, int $fileI
if (!$fileUser->getUuid()) {
$fileUser->setUuid(UUIDUtil::getUUID());
}
$identifyMethod = $this->getUserIdentifyMethod($user);
$fileUser->setIdentifyMethod($identifyMethod);
$fileUser->setEmail($user['email']);
if (!empty($user['description']) && $fileUser->getDescription() !== $user['description']) {
$fileUser->setDescription($user['description']);
Expand Down Expand Up @@ -369,6 +378,8 @@ public function validateUsers(array $data): void {
$emails = [];
foreach ($data['users'] as $index => $user) {
$this->validateHelper->haveValidMail($user);
$identifyMethod = $this->getUserIdentifyMethod($user);
$this->validateHelper->validateIdentifyMethod($identifyMethod);
$emails[$index] = strtolower($this->getUserEmail($user));
}
$uniques = array_unique($emails);
Expand Down Expand Up @@ -826,7 +837,7 @@ public function getInfoOfFileToSignUsingFileUserUuid(?string $uuid, ?IUser $user
'errors' => [$this->l10n->t('Invalid UUID')],
]));
}
$this->trhowIfFileUserNotExists($uuid, $user, $fileUser);
$this->trhowIfCantIdentifyUser($uuid, $user, $fileUser);
$this->throwIfUserIsNotSigner($user, $fileUser);
$this->throwIfAlreadySigned($fileEntity, $fileUser);
$this->throwIfInvalidUser($uuid, $user);
Expand Down Expand Up @@ -910,7 +921,7 @@ private function throwIfAlreadySigned(FileEntity $fileEntity, ?FileUserEntity $f
}
}

private function trhowIfFileUserNotExists(string $uuid, ?IUser $user, ?FileUserEntity $fileUser): void {
private function trhowIfCantIdentifyUser(string $uuid, ?IUser $user, ?FileUserEntity $fileUser): void {
if ($fileUser instanceof FileUserEntity) {
$fileUserId = $fileUser->getUserId();
if ($fileUserId) {
Expand Down
15 changes: 0 additions & 15 deletions lib/Service/TFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function getNodeFromData(array $data): \OCP\Files\Node {
if ($extension === 'pdf') {
$this->validatePdfStringWithFpdi($content);
}
$data['name'] = $this->sanitizeName($data['name'], $extension);

$userFolder = $this->folderService->getFolder();
$folderName = $this->folderService->getFolderName($data, $data['userManager']);
Expand Down Expand Up @@ -80,20 +79,6 @@ private function getExtension(string $content): string {
return '';
}

private function sanitizeName(?string $name, string $extension): ?string {
//MimeTypeDetector
if (!empty($name)) {
$extensionWithDot = substr($name, strlen($name) - strlen($extension) - 1);
if ($extensionWithDot[0] === '.') {
if (ltrim($extensionWithDot, '.') !== $extension) {
throw new LibresignException($this->l10n->t('Invalid file type.'));
}
return rtrim($name, '.' . $extension);
}
}
return $name;
}

/**
* @return resource|string
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Helper/ValidateHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OCA\Libresign\Db\FileTypeMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Db\UserElementMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Helper\ValidateHelper;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
Expand Down Expand Up @@ -612,4 +613,26 @@ public function dataValidateExistingFile() {
[['file' => ['fileId' => 171]], '', ''],
];
}

/**
* @dataProvider dataValidateIdentifyMethod
*/
public function testValidateIdentifyMethod(string $identifyMethod, bool $throwException): void {
if ($throwException) {
$this->expectException(LibresignException::class);
}
$return = $this->getValidateHelper()->validateIdentifyMethod($identifyMethod);
$this->assertNull($return);
}

public function dataValidateIdentifyMethod(): array {
return [
['', true],
['invalid', true],
['nextcloud', false],
['email-link', false],
['email-token', false],
['sms-token', false],
];
}
}
3 changes: 3 additions & 0 deletions tests/Unit/Service/SignFileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public function testSaveUsingUuid() {
$this->assertEquals(36, strlen($subject[0]));
return true;
})],
[$this->equalTo('setIdentifyMethod'), $this->equalTo(['nextcloud'])],
[$this->equalTo('setEmail'), $this->equalTo(['user@test.coop'])],
[$this->equalTo('getDescription')],
[$this->equalTo('setDescription'), $this->equalTo(['Please, sign'])],
Expand All @@ -337,6 +338,7 @@ public function testSaveUsingUuid() {
['setFileId', [], null],
['getUuid', [], null],
['setUuid', [], null],
['setIdentifyMethod', [], null],
['setEmail', [], null],
['getDescription', [], null],
['setDescription', [], null],
Expand All @@ -353,6 +355,7 @@ public function testSaveUsingUuid() {
$user = $this->createMock(\OCP\IUser::class);
$user->method('getDisplayName')->willReturn('John Doe');
$this->userManager->method('getByEmail')->willReturn([$user]);
$this->config->method('getAppValue')->will($this->returnValue('nextcloud'));
$actual = $this->getService()->save([
'uuid' => 'the-uuid-here',
'users' => [
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"jarnaiz/behat-junit-formatter": "^1.3"
},
"require-dev": {
"libresign/behat-builtin-extension": "^0.2.0"
"libresign/behat-builtin-extension": "^0.4.3",
"libresign/nextcloud-behat": "^0.5.0"
}
}
Loading

0 comments on commit 0ce7f66

Please sign in to comment.