* * @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 Tests\Settings\Mailer; use OCP\Mail\IEMailTemplate; use OC\Mail\Message; use OC\Settings\Mailer\NewUserMailHelper; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Defaults; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; use OCP\IUser; 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 NewUserMailHelper */ private $newUserMailHelper; public function setUp() { parent::setUp(); $this->defaults = $this->createMock(Defaults::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->l10n = $this->createMock(IL10N::class); $this->mailer = $this->createMock(IMailer::class); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->config = $this->createMock(IConfig::class); $this->crypto = $this->createMock(ICrypto::class); $this->l10n->method('t') ->will($this->returnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); })); $this->newUserMailHelper = new NewUserMailHelper( $this->defaults, $this->urlGenerator, $this->l10n, $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->at(0)) ->method('getEmailAddress') ->willReturn('recipient@example.com'); $user ->expects($this->at(1)) ->method('getEmailAddress') ->willReturn('recipient@example.com'); $this->config ->expects($this->at(0)) ->method('getSystemValue') ->with('secret') ->willReturn('MyInstanceWideSecret'); $this->crypto ->expects($this->once()) ->method('encrypt') ->with('12345:MySuperLongSecureRandomToken', 'recipient@example.comMyInstanceWideSecret') ->willReturn('TokenCiphertext'); $user ->expects($this->at(2)) ->method('getUID') ->willReturn('john'); $this->config ->expects($this->at(1)) ->method('setUserValue') ->with('john', 'core', 'lostpassword', 'TokenCiphertext'); $user ->expects($this->at(3)) ->method('getUID') ->willReturn('john'); $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->at(4)) ->method('getDisplayName') ->willReturn('john'); $user ->expects($this->at(5)) ->method('getUID') ->willReturn('john'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $expectedHtmlBody = <<
 

Welcome aboard

 
 

You have now an 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->at(0)) ->method('getDisplayName') ->willReturn('John Doe'); $user ->expects($this->at(1)) ->method('getUID') ->willReturn('john'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $expectedHtmlBody = <<
 

Welcome aboard John Doe

 
 

You have now an 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 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']); $this->defaults ->expects($this->exactly(2)) ->method('getName') ->willReturn('TestCloud'); $message ->expects($this->at(1)) ->method('setSubject') ->with('Your TestCloud account was created'); $message ->expects($this->at(2)) ->method('setHtmlBody') ->with($emailTemplate->renderHTML()); $message ->expects($this->at(3)) ->method('setPlainBody') ->with($emailTemplate->renderText()); $message ->expects($this->at(4)) ->method('setFrom') ->with(['no-reply@nextcloud.com' => 'TestCloud']); $this->mailer ->expects($this->once()) ->method('createMessage') ->willReturn($message); $this->newUserMailHelper->sendMail($user, $emailTemplate); } }