Skip to content

Commit

Permalink
fixup! feat: mail provider backend
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Sep 9, 2024
1 parent 6652724 commit 55772d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 53 deletions.
24 changes: 10 additions & 14 deletions lib/Provider/Command/MessageSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace OCA\Mail\Provider\Command;

use OCA\Mail\Db\LocalAttachment;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\UploadException;
Expand Down Expand Up @@ -92,26 +93,21 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar
(string)$entry->getName(),
(string)$entry->getType(),
(string)$entry->getContents()
)->jsonSerialize();
);
}
} catch (UploadException $e) {
$this->purgeSavedAttachments($attachments);
throw new SendException('Error: occurred while saving mail message attachment', 0, $e);
}
// save message
try {
$localMessage = $this->outboxService->saveMessage(
$account,
$localMessage,
$to,
$cc,
$bcc,
$attachments
);
} catch (\Throwable $e) {
$this->purgeSavedAttachments($attachments);
throw new SendException('Error: occurred while saving mail message', 0, $e);
}
$localMessage = $this->outboxService->saveMessage(
$account,
$localMessage,
$to,
$cc,
$bcc,
array_map(static fn (LocalAttachment $attachment) => $attachment->jsonSerialize(), $attachments)
);
// send message
try {
$localMessage = $this->outboxService->sendMessage($localMessage, $account);
Expand Down
43 changes: 4 additions & 39 deletions tests/Unit/Provider/Command/MessageSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class MessageSendTest extends TestCase {
private AttachmentService&MockObject $attachmentService;
private MessageSend $commandSend;
private Message $mailMessage;
private Attachment $mailAttachment;
private Account $localAccount;
private array $localMessageData;
private array $localAttachmentData;

protected function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -271,45 +275,6 @@ public function testPerformWithAttachmentServiceFailure(): void {
$this->commandSend->perform('user1', '100', $mailMessage);
}

public function testPerformWithOutboxServiceSaveFailure(): void {
// define time factory return
$this->time->method('getTime')->willReturn(1719792000);
// define account service returns
$this->accountService->method('find')->will(
$this->returnValueMap([
['user1', 100, $this->localAccount]
])
);
// construct mail app message objects
$localMessageFresh = $this->localMessageData;
$localMessageFresh['sendAt'] = $this->time->getTime($localMessageFresh);
$localMessageFresh = LocalMessage::fromParams($localMessageFresh);
$localMessageReturned = $this->localMessageData;
$localMessageReturned['id'] = 1;
$localMessageReturned['recipients'] = [['email' => '[email protected]', 'label' => 'User Two']];
$localMessageReturned['sendAt'] = $this->time->getTime();
$localMessageReturned = LocalMessage::fromParams($localMessageReturned);
// define attachment service returns
$this->outboxService->expects($this->once())->method('saveMessage')
->with(
$this->localAccount,
$localMessageFresh,
[['email' => '[email protected]', 'label' => 'User Two']],
[],
[],
[]
)->will(
$this->throwException(new \Exception('Something went wrong'))
);
// construct mail provider message
$mailMessage = $this->mailMessage;
// define exception condition
$this->expectException(SendException::class);
$this->expectExceptionMessage('Error: occurred while saving mail message');
// test send message
$this->commandSend->perform('user1', '100', $mailMessage);
}

public function testPerformWithOutboxServiceSendFailure(): void {
// define time factory return
$this->time->method('getTime')->willReturn(1719792000);
Expand Down

0 comments on commit 55772d9

Please sign in to comment.