Skip to content

Commit

Permalink
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 3, 2024
1 parent ed78517 commit 83d3a08
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 18 deletions.
53 changes: 35 additions & 18 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\AppInfo\Application;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\Attachment\AttachmentService;
Expand Down Expand Up @@ -46,7 +47,11 @@ public function __construct(
*/
public function perform(string $userId, string $serviceId, IMessage $message, array $options = []): LocalMessage {
// find user mail account details
$account = $this->accountService->find($userId, (int)$serviceId);
try {
$account = $this->accountService->find($userId, (int)$serviceId);
} catch (\Throwable $e) {
throw new SendException('Error: occurred while retrieving mail account details', 0, $e);
}
// convert mail provider message to mail app message
$localMessage = new LocalMessage();
$localMessage->setType($localMessage::TYPE_OUTGOING);
Expand All @@ -67,12 +72,16 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar
throw new SendException('Invalid Attachment Parameter: MUST contain values for Name, Type and Contents');
}
// convert mail provider attachment to mail app attachment
$attachments[] = $this->attachmentService->addFileFromString(
$userId,
$entry->getName(),
$entry->getType(),
$entry->getContents()
)->jsonSerialize();
try {
$attachments[] = $this->attachmentService->addFileFromString(
$userId,
$entry->getName(),

Check failure on line 78 in lib/Provider/Command/MessageSend.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Provider/Command/MessageSend.php:78:7: PossiblyNullArgument: Argument 2 of OCA\Mail\Service\Attachment\AttachmentService::addFileFromString cannot be null, possibly null value provided (see https://psalm.dev/078)
$entry->getType(),

Check failure on line 79 in lib/Provider/Command/MessageSend.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Provider/Command/MessageSend.php:79:7: PossiblyNullArgument: Argument 3 of OCA\Mail\Service\Attachment\AttachmentService::addFileFromString cannot be null, possibly null value provided (see https://psalm.dev/078)
$entry->getContents()

Check failure on line 80 in lib/Provider/Command/MessageSend.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Provider/Command/MessageSend.php:80:7: PossiblyNullArgument: Argument 4 of OCA\Mail\Service\Attachment\AttachmentService::addFileFromString cannot be null, possibly null value provided (see https://psalm.dev/078)
)->jsonSerialize();
} catch (\Throwable $e) {
throw new SendException('Error: occurred while saving mail message attachment', 0, $e);
}
}
}
// determine if required To address is set
Expand All @@ -84,17 +93,25 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar
$cc = $this->convertAddressArray($message->getCc());
$bcc = $this->convertAddressArray($message->getBcc());
// save message for sending
$localMessage = $this->outboxService->saveMessage(
$account,
$localMessage,
$to,
$cc,
$bcc,
$attachments
);
// evaluate if job scheduler is NOT cron, send message right away otherwise let cron job handle it
if ($this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax') !== 'cron') {
$localMessage = $this->outboxService->sendMessage($localMessage, $account);
try {
$localMessage = $this->outboxService->saveMessage(
$account,
$localMessage,
$to,
$cc,
$bcc,
$attachments
);
} catch (\Throwable $e) {
throw new SendException('Error: occurred while saving mail message', 0, $e);
}
// evaluate if system messages are to be sent instantly
if ($this->config->getAppValue(Application::APP_ID, 'send_system_messages_instantly', 'yes') === 'yes') {
try {
$localMessage = $this->outboxService->sendMessage($localMessage, $account);
} catch (\Throwable $e) {
throw new SendException('Error: occurred while sending mail message', 0, $e);
}
}

return $localMessage;
Expand Down
136 changes: 136 additions & 0 deletions tests/Unit/Provider/Command/MessageSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,140 @@ public function testPerformWithInvalidTo(): void {
// test send message
$this->commandSend->perform('user1', '100', $mailMessage);
}

public function testPerformWithAccountServiceFailure(): void {
// define time factory return
$this->time->method('getTime')->willReturn(1719792000);
// define account service returns
$this->accountService->method('find')->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 retrieving mail account details');
// test send message
$this->commandSend->perform('user1', '100', $mailMessage);
}

public function testPerformWithAttachmentServiceFailure(): 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]
])
);
// define attachment service returns
$this->attachmentService->expects($this->once())->method('addFileFromString')
->with(
'user1',
$this->mailAttachment->getName(),
$this->mailAttachment->getType(),
$this->mailAttachment->getContents()
)->will(
$this->throwException(new \Exception('Something went wrong'))
);
// construct mail provider message with attachment
$mailMessage = $this->mailMessage;
$mailMessage->setAttachments($this->mailAttachment);
// define exception condition
$this->expectException(SendException::class);
$this->expectExceptionMessage('Error: occurred while saving mail message attachment');
// test send message
$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);
// 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 outbox service returns
$this->outboxService->expects($this->once())->method('saveMessage')
->with(
$this->localAccount,
$localMessageFresh,
[['email' => '[email protected]', 'label' => 'User Two']],
[],
[],
[]
)->willReturn($localMessageReturned);
$this->outboxService->expects($this->once())->method('sendMessage')
->with(
$localMessageReturned,
$this->localAccount,
)->will(
$this->throwException(new \Exception('Something went wrong'))
);
// construct mail provider message
$mailMessage = $this->mailMessage;
// define configuration service returns
$this->config->expects($this->once())
->method('getAppValue')
->with('mail', 'send_system_messages_instantly', 'yes')
->willReturn('yes');
// construct mail provider message
$mailMessage = $this->mailMessage;
// define exception condition
$this->expectException(SendException::class);
$this->expectExceptionMessage('Error: occurred while sending mail message');
// test send message
$this->commandSend->perform('user1', '100', $mailMessage);
}
}

0 comments on commit 83d3a08

Please sign in to comment.