* * @author Arthur Schiwon * @author Brad Rubenstein * @author Christoph Wurst * @author Jan-Christoph Borchardt * @author Joas Schilling * @author Julius Härtl * @author Liam JACK * @author Lukas Reschke * @author Morris Jobke * @author Roeland Jago Douma * @author Simon Spannagel * @author zulan * * @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 . * */ namespace OCA\Settings\Tests\Mailer; use OC\Mail\EMailTemplate; use OC\Mail\Message; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Defaults; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; use OCP\IUser; use OCP\L10N\IFactory; use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; use OCP\Security\ICrypto; use OCP\Security\ISecureRandom; use Test\TestCase; class NewUserMailHelperTest extends TestCase { /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */ private $defaults; /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ private $urlGenerator; /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ private $l10n; /** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */ private $mailer; /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */ private $secureRandom; /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ private $timeFactory; /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ private $config; /** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */ private $crypto; /** @var \OCA\Settings\Mailer\NewUserMailHelper */ private $newUserMailHelper; protected function setUp(): void { parent::setUp(); $this->defaults = $this->createMock(Defaults::class); $this->defaults->method('getLogo') ->willReturn('myLogo'); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->l10n = $this->createMock(IL10N::class); $this->l10nFactory = $this->createMock(IFactory::class); $this->mailer = $this->createMock(IMailer::class); $template = new EMailTemplate( $this->defaults, $this->urlGenerator, $this->l10nFactory, 'test.TestTemplate', [] ); $this->mailer->method('createEMailTemplate') ->willReturn($template); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->config = $this->createMock(IConfig::class); $this->config ->expects($this->any()) ->method('getSystemValue') ->willReturnCallback(function ($arg) { switch ($arg) { case 'secret': return 'MyInstanceWideSecret'; case 'customclient_desktop': return 'https://nextcloud.com/install/#install-clients'; } return ''; }); $this->crypto = $this->createMock(ICrypto::class); $this->l10n->method('t') ->willReturnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); }); $this->l10nFactory->method('get') ->willReturnCallback(function ($text, $lang) { return $this->l10n; }); $this->newUserMailHelper = new \OCA\Settings\Mailer\NewUserMailHelper( $this->defaults, $this->urlGenerator, $this->l10nFactory, $this->mailer, $this->secureRandom, $this->timeFactory, $this->config, $this->crypto, 'no-reply@nextcloud.com' ); } public function testGenerateTemplateWithPasswordResetToken() { $this->secureRandom ->expects($this->once()) ->method('generate') ->with(21, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER ) ->willReturn('MySuperLongSecureRandomToken'); $this->timeFactory ->expects($this->once()) ->method('getTime') ->willReturn('12345'); /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->any()) ->method('getEmailAddress') ->willReturn('recipient@example.com'); $this->crypto ->expects($this->once()) ->method('encrypt') ->with('12345:MySuperLongSecureRandomToken', 'recipient@example.comMyInstanceWideSecret') ->willReturn('TokenCiphertext'); $user ->expects($this->any()) ->method('getUID') ->willReturn('john'); $this->config ->expects($this->once()) ->method('setUserValue') ->with('john', 'core', 'lostpassword', 'TokenCiphertext'); $this->urlGenerator ->expects($this->at(0)) ->method('linkToRouteAbsolute') ->with('core.lost.resetform', ['userId' => 'john', 'token' => 'MySuperLongSecureRandomToken']) ->willReturn('https://example.com/resetPassword/MySuperLongSecureRandomToken'); $user ->expects($this->any()) ->method('getDisplayName') ->willReturn('john'); $user ->expects($this->at(5)) ->method('getUID') ->willReturn('john'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $this->defaults ->expects($this->any()) ->method('getTextColorPrimary') ->willReturn('#ffffff'); $expectedHtmlBody = <<
 

Welcome aboard

 

Welcome to your TestCloud account, you can add, protect, and share your data.

Your username is: john

 
Set your password
Install Client
 
                                                           
EOF; $expectedTextBody = <<newUserMailHelper->generateTemplate($user, true); $this->assertEquals($expectedHtmlBody, $result->renderHtml()); $this->assertEquals($expectedTextBody, $result->renderText()); $this->assertSame('OC\Mail\EMailTemplate', get_class($result)); } public function testGenerateTemplateWithoutPasswordResetToken() { $this->urlGenerator ->expects($this->at(0)) ->method('getAbsoluteURL') ->with('/') ->willReturn('https://example.com/'); /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->any()) ->method('getDisplayName') ->willReturn('John Doe'); $user ->expects($this->any()) ->method('getUID') ->willReturn('john'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $this->defaults ->expects($this->any()) ->method('getTextColorPrimary') ->willReturn('#ffffff'); $expectedHtmlBody = <<
 

Welcome aboard John Doe

 

Welcome to your TestCloud account, you can add, protect, and share your data.

Your username is: john

 
Go to TestCloud
Install Client
 
                                                           
EOF; $expectedTextBody = <<newUserMailHelper->generateTemplate($user, false); $this->assertEquals($expectedHtmlBody, $result->renderHtml()); $this->assertEquals($expectedTextBody, $result->renderText()); $this->assertSame('OC\Mail\EMailTemplate', get_class($result)); } public function testGenerateTemplateWithoutUserId() { $this->urlGenerator ->expects($this->at(0)) ->method('getAbsoluteURL') ->with('/') ->willReturn('https://example.com/'); /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->any()) ->method('getDisplayName') ->willReturn('John Doe'); $user ->expects($this->any()) ->method('getUID') ->willReturn('john'); $user ->expects($this->atLeastOnce()) ->method('getBackendClassName') ->willReturn('LDAP'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $this->defaults ->expects($this->any()) ->method('getTextColorPrimary') ->willReturn('#ffffff'); $expectedHtmlBody = <<
 

Welcome aboard John Doe

 

Welcome to your TestCloud account, you can add, protect, and share your data.

 
Go to TestCloud
Install Client
 
                                                           
EOF; $expectedTextBody = <<newUserMailHelper->generateTemplate($user, false); $this->assertEquals($expectedHtmlBody, $result->renderHtml()); $this->assertEquals($expectedTextBody, $result->renderText()); $this->assertSame('OC\Mail\EMailTemplate', get_class($result)); } public function testSendMail() { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->at(0)) ->method('getEMailAddress') ->willReturn('recipient@example.com'); $user ->expects($this->at(1)) ->method('getDisplayName') ->willReturn('John Doe'); /** @var IEMailTemplate|\PHPUnit\Framework\MockObject\MockObject $emailTemplate */ $emailTemplate = $this->createMock(IEMailTemplate::class); $message = $this->createMock(Message::class); $message ->expects($this->at(0)) ->method('setTo') ->with(['recipient@example.com' => 'John Doe']); $message ->expects($this->at(1)) ->method('setFrom') ->with(['no-reply@nextcloud.com' => 'TestCloud']); $message ->expects($this->at(2)) ->method('useTemplate') ->with($emailTemplate); $this->defaults ->expects($this->exactly(1)) ->method('getName') ->willReturn('TestCloud'); $this->mailer ->expects($this->once()) ->method('createMessage') ->willReturn($message); $this->newUserMailHelper->sendMail($user, $emailTemplate); } }