Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/federation/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
</background-jobs>
<commands>
<command>OCA\Federation\Command\SyncFederationAddressBooks</command>
<command>OCA\Federation\Command\TrustedServerAdd</command>
<command>OCA\Federation\Command\TrustedServerList</command>
<command>OCA\Federation\Command\TrustedServerRemove</command>
</commands>
<settings>
<admin>OCA\Federation\Panels\Admin</admin>
Expand Down
75 changes: 75 additions & 0 deletions apps/federation/lib/Command/TrustedServerAdd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* @copyright Copyright (c) 2023, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Federation\Command;

use OCA\Federation\TrustedServers;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TrustedServerAdd extends Command {
public const ERROR_ALREADY_TRUSTED = 1;
public const ERROR_NO_OWNCLOUD_FOUND = 2;

/** @var TrustedServers */
private $trustedServers;

/**
* @param TrustedServers $trustedServers
*/
public function __construct(TrustedServers $trustedServers) {
parent::__construct();
$this->trustedServers = $trustedServers;
}

protected function configure() {
$this
->setName('federation:trusted-servers:add')
->setDescription('Adds a new trusted server')
->addArgument(
'url',
InputArgument::REQUIRED,
'The url pointing to the server, such as https://myserver:8888/server/owncloud'
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$url = $input->getArgument('url');
if ($this->trustedServers->isTrustedServer($url)) {
$output->writeln('<error>The server is already in the list of trusted servers.</error>');
return self::ERROR_ALREADY_TRUSTED;
}

if (!$this->trustedServers->isOwnCloudServer($url)) {
$output->writeln('<error>No ownCloud server found</error>');
return self::ERROR_NO_OWNCLOUD_FOUND;
}

$id = $this->trustedServers->addServer($url);
$output->writeln("Server added with id {$id}");

return 0;
}
}
69 changes: 69 additions & 0 deletions apps/federation/lib/Command/TrustedServerList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* @copyright Copyright (c) 2023, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Federation\Command;

use OCA\Federation\TrustedServers;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TrustedServerList extends Command {
/** @var TrustedServers */
private $trustedServers;

/**
* @param TrustedServers $trustedServers
*/
public function __construct(TrustedServers $trustedServers) {
parent::__construct();
$this->trustedServers = $trustedServers;
}

protected function configure() {
$this
->setName('federation:trusted-servers:list')
->setDescription('List the trusted servers');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$servers = $this->trustedServers->getServers();

$statusMap = [
TrustedServers::STATUS_OK => 'OK',
TrustedServers::STATUS_PENDING => 'Pending',
TrustedServers::STATUS_FAILURE => 'Failure',
TrustedServers::STATUS_ACCESS_REVOKED => 'Revoked',
];

$table = new Table($output);
$table->setHeaders(['id', 'server', 'status']);
foreach ($servers as $server) {
$table->addRow([$server['id'], $server['url'], $statusMap[$server['status']] ?? 'Unknown']);
}
$table->render();
return 0;
}
}
66 changes: 66 additions & 0 deletions apps/federation/lib/Command/TrustedServerRemove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* @copyright Copyright (c) 2023, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Federation\Command;

use OCA\Federation\TrustedServers;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TrustedServerRemove extends Command {
/** @var TrustedServers */
private $trustedServers;

/**
* @param TrustedServers $trustedServers
*/
public function __construct(TrustedServers $trustedServers) {
parent::__construct();
$this->trustedServers = $trustedServers;
}

protected function configure() {
$this
->setName('federation:trusted-servers:remove')
->setDescription('Remove a trusted server')
->addArgument(
'id',
InputArgument::REQUIRED,
'The id of the server. Check with occ federation:trusted-servers:list'
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$id = (int)$input->getArgument('id');
try {
$this->trustedServers->removeServer($id);
$output->writeln("Removed server with id {$id}");
} catch (\Exception $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
return 1;
}
return 0;
}
}
71 changes: 71 additions & 0 deletions apps/federation/tests/Command/TrustedServerAddTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* @copyright Copyright (c) 2023, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Federation\Tests\Command;

use OCA\Federation\Command\TrustedServerAdd;
use OCA\Federation\TrustedServers;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;

/**
* @group DB
*/
class TrustedServerAddTest extends TestCase {
/** @var TrustedServers */
private $trustedServers;

/** @var TrustedServerAdd */
private $command;

protected function setUp(): void {
$this->trustedServers = $this->createMock(TrustedServers::class);

$this->command = new CommandTester(new TrustedServerAdd($this->trustedServers));
}

public function testExecuteAlreadyTrusted() {
$this->trustedServers->method('isTrustedServer')->willReturn(true);

$this->command->execute(['url' => 'https://some.server/path/owncloud']);
$this->assertSame(TrustedServerAdd::ERROR_ALREADY_TRUSTED, $this->command->getStatusCode());
}

public function testExecuteNotFound() {
$this->trustedServers->method('isTrustedServer')->willReturn(false);
$this->trustedServers->method('isOwnCloudServer')->willReturn(false);

$this->command->execute(['url' => 'https://some.server/path/owncloud']);
$this->assertSame(TrustedServerAdd::ERROR_NO_OWNCLOUD_FOUND, $this->command->getStatusCode());
}

public function testExecute() {
$server = 'https://some.server/path/owncloud';

$this->trustedServers->method('isTrustedServer')->willReturn(false);
$this->trustedServers->method('isOwnCloudServer')->willReturn(true);
$this->trustedServers->expects($this->once())
->method('addServer')
->with($server)
->willReturn(33);

$this->command->execute(['url' => $server]);
$this->assertSame(0, $this->command->getStatusCode());
}
}
56 changes: 56 additions & 0 deletions apps/federation/tests/Command/TrustedServerListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* @copyright Copyright (c) 2023, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Federation\Tests\Command;

use OCA\Federation\Command\TrustedServerList;
use OCA\Federation\TrustedServers;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;

/**
* @group DB
*/
class TrustedServerListTest extends TestCase {
/** @var TrustedServers */
private $trustedServers;

/** @var TrustedServerList */
private $command;

protected function setUp(): void {
$this->trustedServers = $this->createMock(TrustedServers::class);

$this->command = new CommandTester(new TrustedServerList($this->trustedServers));
}

public function testExecute() {
$this->trustedServers->method('getServers')
->willReturn([
['id' => 45, 'url' => 'https://my.server1', 'status' => 2],
['id' => 55, 'url' => 'https://my.server22', 'status' => 1],
]);

$this->command->execute([]);
$output = $this->command->getDisplay();
// expect the information of each server in the same row, no special formatting
$this->assertMatchesRegularExpression('/^.*45.*my\.server1.*Pending.*$/m', $output);
$this->assertMatchesRegularExpression('/^.*55.*my\.server22.*OK.*$/m', $output);
}
}
Loading