-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable adding E-Mail addresses to new user accounts using the CLI
Signed-off-by: Philip Gatzka <philip.gatzka@mailbox.org>
- Loading branch information
1 parent
d215d80
commit 9083cd5
Showing
3 changed files
with
248 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2021, Philip Gatzka (philip.gatzka@mailbox.org) | ||
* | ||
* @author Philip Gatzka <philip.gatzka@mailbox.org> | ||
* | ||
* @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/>. | ||
* | ||
*/ | ||
|
||
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<string, array> | ||
*/ | ||
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, | ||
]; | ||
} | ||
} |