diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index 5f24ff1d04..92b89c911e 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -181,7 +181,9 @@ public function sendCodeToSign(string $email, string $name, string $code): void } $message->useTemplate($emailTemplate); try { - $this->mailer->send($message); + if ($this->mailer->send($message) !== []) { + throw new \RuntimeException('Mail transport reported rejected recipients'); + } } catch (\Exception $e) { $this->logger->error('Mail with code to sign document could not be sent: ' . $e->getMessage()); throw new LibresignException('Mail with code to sign document could not be sent', 1); diff --git a/tests/php/Unit/Service/MailServiceTest.php b/tests/php/Unit/Service/MailServiceTest.php index 45a4d69105..06591c9d1a 100644 --- a/tests/php/Unit/Service/MailServiceTest.php +++ b/tests/php/Unit/Service/MailServiceTest.php @@ -11,11 +11,14 @@ use OCA\Libresign\Db\File; use OCA\Libresign\Db\FileMapper; use OCA\Libresign\Db\SignRequest; +use OCA\Libresign\Exception\LibresignException; use OCA\Libresign\Service\MailService; use OCP\IAppConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; +use OCP\Mail\IMessage; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; @@ -113,4 +116,36 @@ public function testFailToSendMailToUnsignedUser():void { $actual = $this->service->notifyUnsignedUser($signRequest, 'a@b.coop'); $this->assertNull($actual); } + + public function testSendCodeToSignAcceptsSuccessfulSubmission(): void { + $template = $this->createMock(IEMailTemplate::class); + $this->mailer->method('createEMailTemplate')->willReturn($template); + $message = $this->createMock(IMessage::class); + $this->mailer->method('createMessage')->willReturn($message); + $message->expects($this->once()) + ->method('setTo') + ->with(['recipient@example.com']); + $this->mailer->expects($this->once()) + ->method('send') + ->with($message) + ->willReturn([]); + + $this->service->sendCodeToSign('recipient@example.com', '', '123456'); + } + + public function testSendCodeToSignReportsRejectedRecipients(): void { + $template = $this->createMock(IEMailTemplate::class); + $this->mailer->method('createEMailTemplate')->willReturn($template); + $message = $this->createMock(IMessage::class); + $this->mailer->method('createMessage')->willReturn($message); + $this->mailer->expects($this->once()) + ->method('send') + ->with($message) + ->willReturn(['recipient@example.com']); + $this->logger->expects($this->once())->method('error'); + + $this->expectException(LibresignException::class); + $this->expectExceptionMessage('Mail with code to sign document could not be sent'); + $this->service->sendCodeToSign('recipient@example.com', 'Recipient', '123456'); + } }