diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-11-15 16:26:56 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-12-04 10:49:38 +0300 |
commit | ad29c8a46cc0bc6e783e82cc81ea5c701c66fb98 (patch) | |
tree | e4f2c3c193747b84c4e8e6145531b204a55c3ff5 /tests | |
parent | 26c13bc035177ae100054e21ba97f61f6d8d5c50 (diff) |
Persist provisioned accounts
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Integration/Service/MailTransmissionIntegrationTest.php | 3 | ||||
-rw-r--r-- | tests/Unit/Controller/SettingsControllerTest.php | 86 | ||||
-rw-r--r-- | tests/Unit/Db/MailAccountTest.php | 3 | ||||
-rw-r--r-- | tests/Unit/Http/Middleware/ProvisioningMiddlewareTest.php | 147 | ||||
-rw-r--r-- | tests/Unit/Migration/MigrateProvisioningConfigTest.php | 106 | ||||
-rw-r--r-- | tests/Unit/Service/AccountServiceTest.php | 24 | ||||
-rw-r--r-- | tests/Unit/Service/DefaultAccount/ManagerTest.php | 154 | ||||
-rw-r--r-- | tests/Unit/Service/Provisioning/ConfigMapperTest.php | 85 | ||||
-rw-r--r-- | tests/Unit/Service/Provisioning/ConfigTest.php (renamed from tests/Unit/Service/DefaultAccount/ConfigTest.php) | 4 | ||||
-rw-r--r-- | tests/Unit/Service/Provisioning/ManagerTest.php | 184 | ||||
-rw-r--r-- | tests/Unit/Service/Provisioning/TestConfig.php | 44 | ||||
-rw-r--r-- | tests/Unit/Settings/AdminSettingsTest.php | 74 |
12 files changed, 744 insertions, 170 deletions
diff --git a/tests/Integration/Service/MailTransmissionIntegrationTest.php b/tests/Integration/Service/MailTransmissionIntegrationTest.php index 8c7cd1837..3261ef44e 100644 --- a/tests/Integration/Service/MailTransmissionIntegrationTest.php +++ b/tests/Integration/Service/MailTransmissionIntegrationTest.php @@ -64,12 +64,14 @@ class MailTransmissionIntegrationTest extends TestCase { parent::setUp(); $this->resetImapAccount(); + $this->user = $this->createTestUser(); /** @var ICrypto $crypo */ $crypo = OC::$server->getCrypto(); /** @var MailAccountMapper $mapper */ $mapper = OC::$server->query(MailAccountMapper::class); $mailAccount = MailAccount::fromParams([ + 'userId' => $this->user->getUID(), 'email' => 'user@domain.tld', 'inboundHost' => 'localhost', 'inboundPort' => '993', @@ -86,7 +88,6 @@ class MailTransmissionIntegrationTest extends TestCase { $this->account = new Account($mailAccount); $this->attachmentService = OC::$server->query(IAttachmentService::class); - $this->user = $this->createTestUser(); $userFolder = OC::$server->getUserFolder($this->user->getUID()); $this->transmission = new MailTransmission( $userFolder, diff --git a/tests/Unit/Controller/SettingsControllerTest.php b/tests/Unit/Controller/SettingsControllerTest.php new file mode 100644 index 000000000..4e3280c11 --- /dev/null +++ b/tests/Unit/Controller/SettingsControllerTest.php @@ -0,0 +1,86 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\Mail\Tests\Unit\Controller; + +use ChristophWurst\Nextcloud\Testing\ServiceMockObject; +use OCA\Mail\Controller\SettingsController; +use OCA\Mail\Tests\Integration\TestCase; +use OCP\AppFramework\Http\JSONResponse; + +class SettingsControllerTest extends TestCase { + + /** @var ServiceMockObject */ + private $mock; + + /** @var SettingsController */ + private $controller; + + protected function setUp(): void { + parent::setUp(); + + $this->mock = $this->createServiceMock(SettingsController::class); + $this->controller = $this->mock->getService(); + } + + public function testProvisioning() { + $this->mock->getParameter('provisioningManager') + ->expects($this->once()) + ->method('newProvisioning') + ->with( + '%USERID%@domain.com', + '%USERID%@domain.com', + 'mx.domain.com', + 993, + 'ssl', + '%USERID%@domain.com', + 'mx.domain.com', + 567, + 'tls' + ); + + $response = $this->controller->provisioning( + '%USERID%@domain.com', + '%USERID%@domain.com', + 'mx.domain.com', + 993, + 'ssl', + '%USERID%@domain.com', + 'mx.domain.com', + 567, + 'tls' + ); + + $this->assertInstanceOf(JSONResponse::class, $response); + } + + public function testDeprovision() { + $this->mock->getParameter('provisioningManager') + ->expects($this->once()) + ->method('deprovision'); + + $response = $this->controller->deprovision(); + + $this->assertInstanceOf(JSONResponse::class, $response); + } +} diff --git a/tests/Unit/Db/MailAccountTest.php b/tests/Unit/Db/MailAccountTest.php index 3c6d01558..a4d1d7fcc 100644 --- a/tests/Unit/Db/MailAccountTest.php +++ b/tests/Unit/Db/MailAccountTest.php @@ -42,6 +42,7 @@ class MailAccountTest extends TestCase { $a->setOutboundPassword('xxxx'); $a->setOutboundSslMode('ssl'); $a->setEditorMode('html'); + $a->setProvisioned(false); $this->assertEquals(array( 'accountId' => 12345, @@ -57,6 +58,7 @@ class MailAccountTest extends TestCase { 'smtpSslMode' => 'ssl', 'signature' => null, 'editorMode' => 'html', + 'provisioned' => false, ), $a->toJson()); } @@ -75,6 +77,7 @@ class MailAccountTest extends TestCase { 'smtpSslMode' => 'ssl', 'signature' => null, 'editorMode' => null, + 'provisioned' => false, ]; $a = new MailAccount($expected); // TODO: fix inconsistency diff --git a/tests/Unit/Http/Middleware/ProvisioningMiddlewareTest.php b/tests/Unit/Http/Middleware/ProvisioningMiddlewareTest.php new file mode 100644 index 000000000..1fac60021 --- /dev/null +++ b/tests/Unit/Http/Middleware/ProvisioningMiddlewareTest.php @@ -0,0 +1,147 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\Mail\Tests\Unit\Http\Middleware; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Controller\PageController; +use OCA\Mail\Http\Middleware\ProvisioningMiddleware; +use OCA\Mail\Service\Provisioning\Manager; +use OCP\Authentication\Exceptions\CredentialsUnavailableException; +use OCP\Authentication\Exceptions\PasswordUnavailableException; +use OCP\Authentication\LoginCredentials\ICredentials; +use OCP\Authentication\LoginCredentials\IStore; +use OCP\ILogger; +use OCP\IUser; +use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; + +class ProvisioningMiddlewareTest extends TestCase { + + /** @var IUserSession|MockObject */ + private $userSession; + + /** @var IStore|MockObject */ + private $credentialStore; + + /** @var Manager|MockObject */ + private $provisioningManager; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var ProvisioningMiddleware */ + private $middleware; + + protected function setUp(): void { + parent::setUp(); + + $this->userSession = $this->createMock(IUserSession::class); + $this->credentialStore = $this->createMock(IStore::class); + $this->provisioningManager = $this->createMock(Manager::class); + $this->logger = $this->createMock(ILogger::class); + + $this->middleware = new ProvisioningMiddleware( + $this->userSession, + $this->credentialStore, + $this->provisioningManager, + $this->logger + ); + } + + public function testBeforeControllerNotLoggedIn() { + $this->credentialStore->expects($this->never()) + ->method('getLoginCredentials'); + $this->provisioningManager->expects($this->never()) + ->method('updatePassword'); + + $this->middleware->beforeController( + $this->createMock(PageController::class), + 'index' + ); + } + + public function testBeforeControllerNoCredentialsAvailable() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $this->credentialStore->expects($this->once()) + ->method('getLoginCredentials') + ->willThrowException($this->createMock(CredentialsUnavailableException::class)); + $this->provisioningManager->expects($this->never()) + ->method('updatePassword'); + + $this->middleware->beforeController( + $this->createMock(PageController::class), + 'index' + ); + } + + public function testBeforeControllerNoPasswordAvailable() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $credentials = $this->createMock(ICredentials::class); + $this->credentialStore->expects($this->once()) + ->method('getLoginCredentials') + ->willReturn($credentials); + $credentials->expects($this->once()) + ->method('getPassword') + ->willThrowException($this->createMock(PasswordUnavailableException::class)); + $this->provisioningManager->expects($this->never()) + ->method('updatePassword'); + + $this->middleware->beforeController( + $this->createMock(PageController::class), + 'index' + ); + } + + public function testBeforeController() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $credentials = $this->createMock(ICredentials::class); + $this->credentialStore->expects($this->once()) + ->method('getLoginCredentials') + ->willReturn($credentials); + $credentials->expects($this->once()) + ->method('getPassword') + ->willReturn('123456'); + $this->provisioningManager->expects($this->once()) + ->method('updatePassword') + ->with( + $user, + '123456' + ); + + $this->middleware->beforeController( + $this->createMock(PageController::class), + 'index' + ); + } + +} diff --git a/tests/Unit/Migration/MigrateProvisioningConfigTest.php b/tests/Unit/Migration/MigrateProvisioningConfigTest.php new file mode 100644 index 000000000..b75d1bc06 --- /dev/null +++ b/tests/Unit/Migration/MigrateProvisioningConfigTest.php @@ -0,0 +1,106 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\Mail\Tests\Unit\Migration; + +use ChristophWurst\Nextcloud\Testing\ServiceMockObject; +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Migration\MigrateProvisioningConfig; +use OCA\Mail\Service\Provisioning\Config; +use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; + +class MigrateProvisioningConfigTest extends TestCase { + + /** @var ServiceMockObject */ + private $mock; + + /** @var MigrateProvisioningConfig */ + private $repairStep; + + protected function setUp(): void { + parent::setUp(); + + $this->mock = $this->createServiceMock(MigrateProvisioningConfig::class); + $this->repairStep = $this->mock->getService(); + } + + + public function testRunNoConfigToMigrate() { + /** @var IOutput|MockObject $output */ + $output = $this->createMock(IOutput::class); + $this->mock->getParameter('config') + ->expects($this->once()) + ->method('getSystemValue') + ->with('app.mail.accounts.default') + ->willReturn(''); + + $this->repairStep->run($output); + } + + public function testRunAlreadyMigrated() { + /** @var IOutput|MockObject $output */ + $output = $this->createMock(IOutput::class); + $this->mock->getParameter('config') + ->expects($this->once()) + ->method('getSystemValue') + ->with('app.mail.accounts.default') + ->willReturn([]); + $this->mock->getParameter('provisioningManager') + ->expects($this->once()) + ->method('getConfig') + ->willReturn($this->createMock(Config::class)); + + $this->repairStep->run($output); + } + + public function testRun() { + /** @var IOutput|MockObject $output */ + $output = $this->createMock(IOutput::class); + $this->mock->getParameter('config') + ->expects($this->once()) + ->method('getSystemValue') + ->with('app.mail.accounts.default') + ->willReturn([]); + $this->mock->getParameter('provisioningManager') + ->expects($this->once()) + ->method('getConfig') + ->willReturn(null); + $this->mock->getParameter('provisioningManager') + ->expects($this->once()) + ->method('importConfig'); + $this->mock->getParameter('config') + ->expects($this->once()) + ->method('deleteSystemValue') + ->with('app.mail.accounts.default'); + + $this->repairStep->run($output); + } + + public function testGetName() { + $name = $this->repairStep->getName(); + + $this->assertEquals('Migrate Mail provisioning config from config.php to the database', $name); + } + +} diff --git a/tests/Unit/Service/AccountServiceTest.php b/tests/Unit/Service/AccountServiceTest.php index 887659b21..a07be412a 100644 --- a/tests/Unit/Service/AccountServiceTest.php +++ b/tests/Unit/Service/AccountServiceTest.php @@ -27,44 +27,42 @@ use OCA\Mail\Db\MailAccount; use OCA\Mail\Db\MailAccountMapper; use OCA\Mail\Service\AccountService; use OCA\Mail\Service\AliasesService; -use OCA\Mail\Service\DefaultAccount\Manager; use OCP\IL10N; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; class AccountServiceTest extends TestCase { /** @var string */ private $user = 'herbert'; - /** @var MailAccountMapper|PHPUnit_Framework_MockObject_MockObject */ + /** @var MailAccountMapper|MockObject */ private $mapper; - /** @var IL10N|PHPUnit_Framework_MockObject_MockObject */ + /** @var IL10N|MockObject */ private $l10n; - /** @var AccountService|PHPUnit_Framework_MockObject_MockObject */ + /** @var AccountService|MockObject */ private $accountService; - /** @var AliasesService|PHPUnit_Framework_MockObject_MockObject */ + /** @var AliasesService|MockObject */ private $aliasesService; - /** @var MailAccount|PHPUnit_Framework_MockObject_MockObject */ + /** @var MailAccount|MockObject */ private $account1; - /** @var MailAccount|PHPUnit_Framework_MockObject_MockObject */ + /** @var MailAccount|MockObject */ private $account2; - /** @var Manager|PHPUnit_Framework_MockObject_MockObject */ - private $defaultAccountManager; - protected function setUp(): void { parent::setUp(); $this->mapper = $this->createMock(MailAccountMapper::class); $this->l10n = $this->createMock(IL10N::class); - $this->defaultAccountManager = $this->createMock(Manager::class); $this->aliasesService = $this->createMock(AliasesService::class); - $this->accountService = new AccountService($this->mapper, $this->defaultAccountManager, $this->aliasesService); + $this->accountService = new AccountService( + $this->mapper, + $this->aliasesService + ); $this->account1 = $this->createMock(MailAccount::class); $this->account2 = $this->createMock(MailAccount::class); diff --git a/tests/Unit/Service/DefaultAccount/ManagerTest.php b/tests/Unit/Service/DefaultAccount/ManagerTest.php deleted file mode 100644 index 4e2ab7bbb..000000000 --- a/tests/Unit/Service/DefaultAccount/ManagerTest.php +++ /dev/null @@ -1,154 +0,0 @@ -<?php - -/** - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * - * 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 <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Mail\Tests\Unit\Service\DefaultAccount; - -use ChristophWurst\Nextcloud\Testing\TestCase; -use OCA\Mail\Db\MailAccount; -use OCA\Mail\Service\DefaultAccount\Manager; -use OCP\Authentication\Exceptions\CredentialsUnavailableException; -use OCP\Authentication\LoginCredentials\ICredentials; -use OCP\Authentication\LoginCredentials\IStore; -use OCP\IConfig; -use OCP\ILogger; -use OCP\IUser; -use OCP\IUserSession; -use OCP\Security\ICrypto; -use PHPUnit_Framework_MockObject_MockObject; - -class ManagerTest extends TestCase { - - /** @var IConfig|PHPUnit_Framework_MockObject_MockObject */ - private $config; - - /** @var IStore|PHPUnit_Framework_MockObject_MockObject */ - private $credentialStore; - - /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */ - private $logger; - - /** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */ - private $userSession; - - /** @var ICrypto|PHPUnit_Framework_MockObject_MockObject */ - private $crypto; - - /** @var Manager|PHPUnit_Framework_MockObject_MockObject */ - private $manager; - - protected function setUp(): void { - parent::setUp(); - - $this->config = $this->createMock(IConfig::class); - $this->credentialStore = $this->createMock(IStore::class); - $this->logger = $this->createMock(ILogger::class); - $this->userSession = $this->createMock(IUserSession::class); - $this->crypto = $this->createMock(ICrypto::class); - - $this->manager = new Manager($this->config, $this->credentialStore, $this->logger, $this->userSession, $this->crypto); - } - - public function testGetDefaultAccountWithoutConfigAvailble() { - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with($this->equalTo('app.mail.accounts.default'), $this->equalTo(null)) - ->willReturn(null); - - $account = $this->manager->getDefaultAccount(); - - $this->assertSame(null, $account); - } - - public function testGetDefaultAccountWithCredentialsUnavailable() { - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with($this->equalTo('app.mail.accounts.default'), $this->equalTo(null)) - ->willReturn([ - 'email' => '%EMAIL%', - ]); - $this->credentialStore->expects($this->once()) - ->method('getLoginCredentials') - ->willThrowException(new CredentialsUnavailableException()); - - $account = $this->manager->getDefaultAccount(); - - $this->assertSame(null, $account); - } - - public function testGetDefaultAccount() { - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with($this->equalTo('app.mail.accounts.default'), $this->equalTo(null)) - ->willReturn([ - 'email' => '%EMAIL%', - 'imapHost' => 'imap.domain.tld', - 'imapPort' => 993, - 'imapSslMode' => 'ssl', - 'smtpHost' => 'smtp.domain.tld', - 'smtpPort' => 465, - 'smtpSslMode' => 'tls', - ]); - $credentials = $this->createMock(ICredentials::class); - $user = $this->createMock(IUser::class); - $this->userSession->expects($this->once()) - ->method('getUser') - ->willReturn($user); - $this->credentialStore->expects($this->once()) - ->method('getLoginCredentials') - ->willReturn($credentials); - $credentials->expects($this->once()) - ->method('getPassword') - ->willReturn('123456'); - $this->crypto->expects($this->once()) - ->method('encrypt') - ->with($this->equalTo('123456')) - ->willReturn('encrypted'); - $expected = new MailAccount(); - $expected->setId(Manager::ACCOUNT_ID); - $user->expects($this->any()) - ->method('getUID') - ->willReturn('user123'); - $user->expects($this->any()) - ->method('getEMailAddress') - ->willReturn('user@domain.tld'); - $user->expects($this->once()) - ->method('getDisplayName') - ->willReturn('Test User'); - $expected->setUserId('user123'); - $expected->setEmail('user@domain.tld'); - $expected->setName('Test User'); - $expected->setInboundUser('user@domain.tld'); - $expected->setInboundHost('imap.domain.tld'); - $expected->setInboundPort(993); - $expected->setInboundSslMode('ssl'); - $expected->setInboundPassword('encrypted'); - $expected->setOutboundUser('user@domain.tld'); - $expected->setOutboundHost('smtp.domain.tld'); - $expected->setOutboundPort(465); - $expected->setOutboundSslMode('tls'); - $expected->setOutboundPassword('encrypted'); - - $account = $this->manager->getDefaultAccount(); - - $this->assertEquals($expected, $account); - } - -} diff --git a/tests/Unit/Service/Provisioning/ConfigMapperTest.php b/tests/Unit/Service/Provisioning/ConfigMapperTest.php new file mode 100644 index 000000000..45e9090cd --- /dev/null +++ b/tests/Unit/Service/Provisioning/ConfigMapperTest.php @@ -0,0 +1,85 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\Mail\Tests\Unit\Service\Provisioning; + +use ChristophWurst\Nextcloud\Testing\ServiceMockObject; +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Service\Provisioning\Config; +use OCA\Mail\Service\Provisioning\ConfigMapper; +use PHPUnit\Framework\MockObject\MockObject; + +class ConfigMapperTest extends TestCase { + + /** @var ServiceMockObject */ + private $mock; + + /** @var ConfigMapper */ + private $mapper; + + protected function setUp(): void { + parent::setUp(); + + $this->mock = $this->createServiceMock(ConfigMapper::class); + $this->mapper = $this->mock->getService(); + } + + public function testSave() { + /** @var Config|MockObject $config */ + $config = $this->createMock(Config::class); + $config->expects($this->once()) + ->method('jsonSerialize') + ->willReturn([]); + $this->mock->getParameter('config') + ->expects($this->once()) + ->method('setAppValue') + ->with('mail', 'provisioning_settings', '[]'); + + $this->mapper->save($config); + } + + public function testLoadNoConfig() { + $this->mock->getParameter('config') + ->expects($this->once()) + ->method('getAppValue') + ->with('mail', 'provisioning_settings') + ->willReturn(''); + + $config = $this->mapper->load(); + + $this->assertNull($config); + } + + public function testLoad() { + $this->mock->getParameter('config') + ->expects($this->once()) + ->method('getAppValue') + ->with('mail', 'provisioning_settings') + ->willReturn('[]'); + + $config = $this->mapper->load(); + + $this->assertInstanceOf(Config::class, $config); + } + +} diff --git a/tests/Unit/Service/DefaultAccount/ConfigTest.php b/tests/Unit/Service/Provisioning/ConfigTest.php index a05a4d26b..064843780 100644 --- a/tests/Unit/Service/DefaultAccount/ConfigTest.php +++ b/tests/Unit/Service/Provisioning/ConfigTest.php @@ -19,10 +19,10 @@ * */ -namespace OCA\Mail\Tests\Unit\Service\DefaultAccount; +namespace OCA\Mail\Tests\Unit\Service\Provisioning; use ChristophWurst\Nextcloud\Testing\TestCase; -use OCA\Mail\Service\DefaultAccount\Config; +use OCA\Mail\Service\Provisioning\Config; use OCP\IUser; class ConfigTest extends TestCase { diff --git a/tests/Unit/Service/Provisioning/ManagerTest.php b/tests/Unit/Service/Provisioning/ManagerTest.php new file mode 100644 index 000000000..66b0accfc --- /dev/null +++ b/tests/Unit/Service/Provisioning/ManagerTest.php @@ -0,0 +1,184 @@ +<?php +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\Mail\Tests\Unit\Service\Provisioning; + +use ChristophWurst\Nextcloud\Testing\ServiceMockObject; +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Db\MailAccount; +use OCA\Mail\Service\Provisioning\Config; +use OCA\Mail\Service\Provisioning\Manager; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\IUser; +use PHPUnit\Framework\MockObject\MockObject; + +class ManagerTest extends TestCase { + + /** @var ServiceMockObject */ + private $mock; + + /** @var Manager */ + private $manager; + + protected function setUp(): void { + parent::setUp(); + + $this->mock = $this->createServiceMock(Manager::class); + $this->manager = $this->mock->getService(); + } + + public function testProvision() { + $config = new TestConfig(); + $this->mock->getParameter('userManager') + ->expects($this->once()) + ->method('callForAllUsers'); + + $cnt = $this->manager->provision($config); + + $this->assertEquals(0, $cnt); + } + + public function testUpdateProvisionSingleUser() { + /** @var IUser|MockObject $user */ + $user = $this->createMock(IUser::class); + $config = new TestConfig(); + $account = $this->createMock(MailAccount::class); + $this->mock->getParameter('mailAccountMapper') + ->expects($this->once()) + ->method('findProvisionedAccount') + ->willReturn($account); + $this->mock->getParameter('mailAccountMapper') + ->expects($this->once()) + ->method('update') + ->with($account); + + $this->manager->provisionSingleUser($config, $user); + } + + public function testProvisionSingleUser() { + /** @var IUser|MockObject $user */ + $user = $this->createMock(IUser::class); + $config = new TestConfig(); + $this->mock->getParameter('mailAccountMapper') + ->expects($this->once()) + ->method('findProvisionedAccount') + ->willThrowException($this->createMock(DoesNotExistException::class)); + $this->mock->getParameter('mailAccountMapper') + ->expects($this->once()) + ->method('insert'); + + $this->manager->provisionSingleUser($config, $user); + } + + public function testGetNoConfig() { + $config = $this->manager->getConfig(); + + $this->assertNull($config); + } + + public function testGetConfig() { + $config = $this->createMock(Config::class); + $this->mock->getParameter('configMapper') + ->expects($this->once()) + ->method('load') + ->willReturn($config); + + $cfg = $this->manager->getConfig(); + + $this->assertSame($config, $cfg); + } + + public function testDeprovision() { + $config = new TestConfig(); + $this->mock->getParameter('mailAccountMapper') + ->expects($this->once()) + ->method('deleteProvisionedAccounts'); + $this->mock->getParameter('configMapper') + ->expects($this->once()) + ->method('load') + ->willReturn($config); + $this->mock->getParameter('configMapper') + ->expects($this->once()) + ->method('save') + ->willReturn($config); + + $this->manager->deprovision(); + + $this->assertEquals(false, $config->jsonSerialize()['active']); + } + + public function testImportConfig() { + $this->mock->getParameter('configMapper') + ->expects($this->once()) + ->method('save'); + + $this->manager->importConfig([ + 'email' => '%USERID%@domain.com', + ]); + } + + public function testUpdatePasswordNotProvisioned() { + /** @var IUser|MockObject $user */ + $user = $this->createMock(IUser::class); + $this->mock->getParameter('mailAccountMapper') + ->expects($this->once()) + ->method('findProvisionedAccount') + ->with($user) + ->willThrowException($this->createMock(DoesNotExistException::class)); + + $this->manager->updatePassword($user, '123456'); + } + + public function testUpdatePassword() { + /** @var IUser|MockObject $user */ + $user = $this->createMock(IUser::class); + $account = $this->createMock(MailAccount::class); + $this->mock->getParameter('mailAccountMapper') + ->expects($this->once()) + ->method('findProvisionedAccount') + ->willReturn($account); + $this->mock->getParameter('mailAccountMapper') + ->expects($this->once()) + ->method('update') + ->with($account); + + $this->manager->updatePassword($user, '123456'); + } + + public function testNewProvisioning() { + $this->mock->getParameter('configMapper') + ->expects($this->once()) + ->method('save'); + + $this->manager->newProvisioning( + '%USERID%@domain.com', + '%USERID%@domain.com', + 'mx.domain.com', + 993, + 'ssl', + '%USERID%@domain.com', + 'mx.domain.com', + 567, + 'tls' + ); + } +} diff --git a/tests/Unit/Service/Provisioning/TestConfig.php b/tests/Unit/Service/Provisioning/TestConfig.php new file mode 100644 index 000000000..66357a724 --- /dev/null +++ b/tests/Unit/Service/Provisioning/TestConfig.php @@ -0,0 +1,44 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\mail\tests\Unit\Service\Provisioning; + +use OCA\Mail\Service\Provisioning\Config; + +class TestConfig extends Config { + + public function __construct() { + parent::__construct([ + 'email' => '%USERID%@domain.com', + 'imapUser' => '%USERID%@domain.com', + 'imapHost' => 'mx.domain.com', + 'imapPort' => 993, + 'imapSslMode' => 'ssl', + 'smtpUser' => '%USERID%@domain.com', + 'smtpHost' => 'mx.domain.com', + 'smtpPort' => 567, + 'smtpSslMode' => 'tls', + ]); + } + +} diff --git a/tests/Unit/Settings/AdminSettingsTest.php b/tests/Unit/Settings/AdminSettingsTest.php new file mode 100644 index 000000000..c2e038e6c --- /dev/null +++ b/tests/Unit/Settings/AdminSettingsTest.php @@ -0,0 +1,74 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\Mail\Tests\Unit\Settings; + +use ChristophWurst\Nextcloud\Testing\ServiceMockObject; +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\AppInfo\Application; +use OCA\Mail\Settings\AdminSettings; +use OCP\AppFramework\Http\TemplateResponse; + +class AdminSettingsTest extends TestCase { + + /** @var ServiceMockObject */ + private $serviceMock; + + /** @var AdminSettings */ + private $settings; + + protected function setUp(): void { + parent::setUp(); + + $this->serviceMock = $this->createServiceMock(AdminSettings::class); + + $this->settings = $this->serviceMock->getService(); + } + + public function testGetSection() { + $section = $this->settings->getSection(); + + $this->assertSame('groupware', $section); + } + + public function testGetForm() { + $this->serviceMock->getParameter('initialStateService')->expects($this->once()) + ->method('provideInitialState') + ->with( + Application::APP_ID, + 'provisioning_settings', + $this->anything() + ); + $expected = new TemplateResponse(Application::APP_ID, 'settings-admin'); + + $form = $this->settings->getForm(); + + $this->assertEquals($expected, $form); + } + + public function testGetPriority() { + $priority = $this->settings->getPriority(); + + $this->assertIsInt($priority); + } +} |