diff --git a/lib/Command/CreateAccount.php b/lib/Command/CreateAccount.php index 61c32b4544f90b81696dc8c68c3c148f3127407d..a1b95a283045b32955f2db87d1a14120848e1e35 100644 --- a/lib/Command/CreateAccount.php +++ b/lib/Command/CreateAccount.php @@ -25,6 +25,7 @@ namespace OCA\Mail\Command; use OCA\Mail\Db\MailAccount; use OCA\Mail\Service\AccountService; +use OCP\IUserManager; use OCP\Security\ICrypto; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -52,11 +53,17 @@ class CreateAccount extends Command { /** @var \OCP\Security\ICrypto */ private $crypto; - public function __construct(AccountService $service, ICrypto $crypto) { + /** @var IUserManager */ + private $userManager; + + public function __construct(AccountService $service, + ICrypto $crypto, + IUserManager $userManager) { parent::__construct(); $this->accountService = $service; $this->crypto = $crypto; + $this->userManager = $userManager; } /** @@ -99,6 +106,11 @@ class CreateAccount extends Command { $smtpUser = $input->getArgument(self::ARGUMENT_SMTP_USER); $smtpPassword = $input->getArgument(self::ARGUMENT_SMTP_PASSWORD); + if (!$this->userManager->userExists($userId)) { + $output->writeln("<error>User $userId does not exist</error>"); + return 1; + } + $account = new MailAccount(); $account->setUserId($userId); $account->setName($name); diff --git a/tests/Unit/Command/CreateAccountTest.php b/tests/Unit/Command/CreateAccountTest.php index 317b2fa8a73f7c9d0a03ad4e9e4ff8581ae0d4a8..a86fe7f0c0baca783d0bc521055c0180daa07c00 100644 --- a/tests/Unit/Command/CreateAccountTest.php +++ b/tests/Unit/Command/CreateAccountTest.php @@ -23,10 +23,14 @@ namespace OCA\Mail\Tests\Unit\Command; use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Command\CreateAccount; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; class CreateAccountTest extends TestCase { private $service; private $crypto; + private $userManager; private $command; private $args = [ 'user-id', @@ -51,8 +55,9 @@ class CreateAccountTest extends TestCase { ->disableOriginalConstructor() ->getMock(); $this->crypto = $this->getMockBuilder('\OCP\Security\ICrypto')->getMock(); + $this->userManager = $this->createMock(IUserManager::class); - $this->command = new CreateAccount($this->service, $this->crypto); + $this->command = new CreateAccount($this->service, $this->crypto, $this->userManager); } public function testName() { @@ -71,4 +76,40 @@ class CreateAccountTest extends TestCase { $this->assertTrue(in_array($actArg->getName(), $this->args)); } } + + public function testInvalidUserId() { + $userId = 'invalidUser'; + $data = [ + 'user-id' => $userId, + 'name' => '', + 'email' => '', + 'imap-host' => '', + 'imap-port' => 0, + 'imap-ssl-mode' => '', + 'imap-user' => '', + 'imap-password' => '', + 'smtp-host' => '', + 'smtp-port' => 0, + 'smtp-ssl-mode' => '', + 'smtp-user' => '', + 'smtp-password' => '', + ]; + + $input = $this->createMock(InputInterface::class); + $input->method('getArgument') + ->willReturnCallback(function ($arg) use ($data) { + return $data[$arg]; + }); + $output = $this->createMock(OutputInterface::class); + $output->expects($this->once()) + ->method('writeln') + ->with("<error>User $userId does not exist</error>"); + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with($userId) + ->willReturn(false); + + $this->assertEquals(1, $this->command->run($input, $output)); + } }