From b587ec39f426a50c1ff28a091ba1c5e910bd02dd Mon Sep 17 00:00:00 2001 From: Philip Gatzka Date: Sat, 16 Oct 2021 13:27:28 +0200 Subject: Enable adding E-Mail addresses to new user accounts using the CLI Signed-off-by: Philip Gatzka --- tests/Core/Command/User/AddTest.php | 126 ++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/Core/Command/User/AddTest.php (limited to 'tests') diff --git a/tests/Core/Command/User/AddTest.php b/tests/Core/Command/User/AddTest.php new file mode 100644 index 00000000000..1445e3cfec6 --- /dev/null +++ b/tests/Core/Command/User/AddTest.php @@ -0,0 +1,126 @@ + + * + * @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 . + * + */ + +declare(strict_types=1); + +namespace Core\Command\User; + +use Egulias\EmailValidator\EmailValidator; +use OC\Core\Command\User\Add; +use OCA\Settings\Mailer\NewUserMailHelper; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IUser; +use OCP\IUserManager; +use OCP\Mail\IEMailTemplate; +use OCP\Security\ISecureRandom; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class AddTest extends TestCase { + + /** + * @dataProvider addEmailDataProvider + */ + public function testAddEmail(?string $email, bool $isValid, bool $shouldSendMail): void { + $userManager = static::createMock(IUserManager::class); + $groupManager = static::createStub(IGroupManager::class); + $user = static::createMock(IUser::class); + $config = static::createMock(IConfig::class); + $mailHelper = static::createMock(NewUserMailHelper::class); + $eventDispatcher = static::createStub(IEventDispatcher::class); + $secureRandom = static::createStub(ISecureRandom::class); + + $consoleInput = static::createMock(InputInterface::class); + $consoleOutput = static::createMock(OutputInterface::class); + + $user->expects($isValid ? static::once() : static::never()) + ->method('setSystemEMailAddress') + ->with(static::equalTo($email)); + + $userManager->method('createUser') + ->willReturn($user); + + $config->method('getAppValue') + ->willReturn($shouldSendMail ? 'yes' : 'no'); + + $mailHelper->method('generateTemplate') + ->willReturn(static::createMock(IEMailTemplate::class)); + + $mailHelper->expects($isValid && $shouldSendMail ? static::once() : static::never()) + ->method('sendMail'); + + $consoleInput->method('getOption') + ->will(static::returnValueMap([ + ['password-from-env', ''], + ['email', $email], + ['group', []], + ])); + + $addCommand = new Add( + $userManager, + $groupManager, + new EmailValidator(), + $config, + $mailHelper, + $eventDispatcher, + $secureRandom + ); + + $this->invokePrivate($addCommand, 'execute', [ + $consoleInput, + $consoleOutput + ]); + } + + /** + * @return \Generator + */ + public function addEmailDataProvider(): \Generator { + yield 'Valid E-Mail' => [ + 'info@example.com', + true, + true, + ]; + + yield 'Invalid E-Mail' => [ + 'info@@example.com', + false, + true, + ]; + + yield 'No E-Mail' => [ + '', + false, + true, + ]; + + yield 'Valid E-Mail, but no mail should be sent' => [ + 'info@example.com', + true, + false, + ]; + } +} -- cgit v1.2.3