From 82ab9d00075bf00d611e819ef9c3a6cc72c12c26 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Wed, 27 Jan 2021 16:54:37 +0100 Subject: Add Sieve support * Expose managesieve port * Add sieve client factory * Add support for sieve to provisioning * Refactor test connectivity logic and add sieve. * Add support for sieve to provisioning * Add sieve to account form * Add debug logger for ManageSieve * Add api to get and update active script * Add error for managesieve exception * Add text editor to update existing script Signed-off-by: Daniel Kesselberg Signed-off-by: Christoph Wurst --- tests/Integration/Db/MailAccountTest.php | 2 + tests/Integration/Sieve/SieveClientFactoryTest.php | 117 +++++++++++++++++++++ tests/Integration/Sieve/SieveLoggerTest.php | 46 ++++++++ 3 files changed, 165 insertions(+) create mode 100644 tests/Integration/Sieve/SieveClientFactoryTest.php create mode 100644 tests/Integration/Sieve/SieveLoggerTest.php (limited to 'tests/Integration') diff --git a/tests/Integration/Db/MailAccountTest.php b/tests/Integration/Db/MailAccountTest.php index 8bf2af949..fd2ad11e3 100644 --- a/tests/Integration/Db/MailAccountTest.php +++ b/tests/Integration/Db/MailAccountTest.php @@ -69,6 +69,7 @@ class MailAccountTest extends TestCase { 'draftsMailboxId' => null, 'sentMailboxId' => null, 'trashMailboxId' => null, + 'sieveEnabled' => false, ], $a->toJson()); } @@ -95,6 +96,7 @@ class MailAccountTest extends TestCase { 'draftsMailboxId' => null, 'sentMailboxId' => null, 'trashMailboxId' => null, + 'sieveEnabled' => false, ]; $a = new MailAccount($expected); // TODO: fix inconsistency diff --git a/tests/Integration/Sieve/SieveClientFactoryTest.php b/tests/Integration/Sieve/SieveClientFactoryTest.php new file mode 100644 index 000000000..47964c32c --- /dev/null +++ b/tests/Integration/Sieve/SieveClientFactoryTest.php @@ -0,0 +1,117 @@ + + * + * Mail + * + * 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 + * + */ + +namespace OCA\Mail\Tests\Integration\Sieve; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use Horde\ManageSieve; +use OC; +use OCA\Mail\Account; +use OCA\Mail\Db\MailAccount; +use OCA\Mail\Sieve\SieveClientFactory; +use OCP\IConfig; +use OCP\Security\ICrypto; +use PHPUnit\Framework\MockObject\MockObject; + +class SieveClientFactoryTest extends TestCase { + + /** @var ICrypto|MockObject */ + private $crypto; + + /** @var IConfig|MockObject */ + private $config; + + /** @var SieveClientFactory */ + private $factory; + + protected function setUp(): void { + parent::setUp(); + + $this->crypto = $this->createMock(ICrypto::class); + $this->config = $this->createMock(IConfig::class); + + $this->config->method('getSystemValue') + ->willReturnCallback(static function ($key, $default) { + if ($key === 'app.mail.sieve.timeout') { + return 5; + } + if ($key === 'debug') { + return false; + } + return null; + }); + + $this->config->method('getSystemValueBool') + ->with('app.mail.verify-tls-peer', true) + ->willReturn(false); + + $this->factory = new SieveClientFactory($this->crypto, $this->config); + } + + /** + * @return Account + */ + private function getTestAccount() { + $mailAccount = new MailAccount(); + $mailAccount->setId(123); + $mailAccount->setEmail('user@domain.tld'); + $mailAccount->setInboundHost('127.0.0.1'); + $mailAccount->setInboundPort(993); + $mailAccount->setInboundSslMode('ssl'); + $mailAccount->setInboundUser('user@domain.tld'); + $mailAccount->setInboundPassword(OC::$server->get(ICrypto::class)->encrypt('mypassword')); + $mailAccount->setSieveHost('127.0.0.1'); + $mailAccount->setSievePort(4190); + $mailAccount->setSieveSslMode(''); + $mailAccount->setSieveUser(''); + $mailAccount->setSievePassword(''); + return new Account($mailAccount); + } + + public function testClientConnectivity() { + $account = $this->getTestAccount(); + $this->crypto->expects($this->once()) + ->method('decrypt') + ->with($account->getMailAccount()->getInboundPassword()) + ->willReturn('mypassword'); + + $client = $this->factory->getClient($account); + $this->assertInstanceOf(ManageSieve::class, $client); + } + + public function testClientInstallScript() { + $account = $this->getTestAccount(); + $this->crypto->expects($this->once()) + ->method('decrypt') + ->with($account->getMailAccount()->getInboundPassword()) + ->willReturn('mypassword'); + + $client = $this->factory->getClient($account); + + $client->installScript('test', '#test'); + $this->assertCount(1, $client->listScripts()); + + $client->removeScript('test'); + $this->assertCount(0, $client->listScripts()); + } +} diff --git a/tests/Integration/Sieve/SieveLoggerTest.php b/tests/Integration/Sieve/SieveLoggerTest.php new file mode 100644 index 000000000..fbb526a76 --- /dev/null +++ b/tests/Integration/Sieve/SieveLoggerTest.php @@ -0,0 +1,46 @@ + + * + * Mail + * + * 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 + * + */ + +namespace OCA\Mail\Tests\Integration\Sieve; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Sieve\SieveLogger; + +class SieveLoggerTest extends TestCase { + public function testOpenInvalidFile(): void { + $this->expectException(\InvalidArgumentException::class); + $this->expectDeprecationMessage('Unable to use "/root/horde_sieve.log" as log file for sieve.'); + new SieveLogger('/root/horde_sieve.log'); + } + + public function testWriteLog(): void { + $logFile = sys_get_temp_dir() . '/horde_sieve.log'; + @unlink($logFile); + + $logger = new SieveLogger($logFile); + $logger->debug('Test'); + unset($logger); + + $this->assertStringEqualsFile($logFile, 'Test'); + } +} -- cgit v1.2.3