Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions tests/php/Unit/Service/MailServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
}
}
Loading