Skip to content

Commit

Permalink
fixup! feat(ocs): send a message via api
Browse files Browse the repository at this point in the history
  • Loading branch information
miaulalala committed Jun 26, 2024
1 parent 3856957 commit b8b60dc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
24 changes: 8 additions & 16 deletions lib/Controller/MessageApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MessageApiController extends OCSController {

public function __construct(
string $appName,
$UserId,
?string $userId,
IRequest $request,
private AccountService $accountService,
private AliasesService $aliasesService,
Expand All @@ -44,21 +44,9 @@ public function __construct(
private ITimeFactory $time,
) {
parent::__construct($appName, $request);
$this->userId = $UserId;
$this->userId = $userId;
}

/**
* @param int $accountId
* @param string $fromEmail
* @param string $subject
* @param string $body
* @param bool $isHtml
* @param array $to
* @param array $cc
* @param array $bcc
* @param array $references
* @return DataResponse
*/
#[UserRateLimit(limit: 5, period: 100)]
#[NoAdminRequired]
#[NoCSRFRequired]
Expand All @@ -73,6 +61,10 @@ public function send(
array $bcc = [],
array $references = [],
): DataResponse {
if($this->userId === null) {
return new DataResponse('', Http::STATUS_FORBIDDEN);
}

try {
$mailAccount = $this->accountService->find($this->userId, $accountId);
} catch (ClientException $e) {
Expand All @@ -82,7 +74,7 @@ public function send(

if($fromEmail !== $mailAccount->getEmail()) {
try {
$alias = $this->aliasesService->findByAliasEmailAndUserId($fromEmail, $this->userId);
$alias = $this->aliasesService->findByAliasAndUserId($fromEmail, $this->userId);
} catch (DoesNotExistException $e) {
$this->logger->error("Alias $fromEmail for mail account $accountId not found", ['exception' => $e]);
// Cannot send from this email as it is not configured as an alias
Expand Down Expand Up @@ -156,7 +148,7 @@ public function send(

return match ($localMessage->getStatus()) {
LocalMessage::STATUS_PROCESSED => new DataResponse('Success', Http::STATUS_OK),
LocalMessage::STATUS_NO_SENT_MAILBOX => new DataResponse('Configuration errror: Cannot send message without sent mailbox.', Http::STATUS_FORBIDDEN),
LocalMessage::STATUS_NO_SENT_MAILBOX => new DataResponse('Configuration error: Cannot send message without sent mailbox.', Http::STATUS_FORBIDDEN),
LocalMessage::STATUS_SMPT_SEND_FAIL => new DataResponse('An SMTP error occured, please check your mail server logs.', Http::STATUS_INTERNAL_SERVER_ERROR),
LocalMessage::STATUS_IMAP_SENT_MAILBOX_FAIL => new DataResponse('Email was sent but could not be copied to sent mailbox.', Http::STATUS_ACCEPTED),
default => new DataResponse('An error occured, please check the logs.', Http::STATUS_INTERNAL_SERVER_ERROR),
Expand Down
3 changes: 1 addition & 2 deletions lib/Service/AliasesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public function find(int $aliasId, string $currentUserId): Alias {
* @return Alias
* @throws DoesNotExistException
*/
public function findByAliasEmailAndUserId(string $aliasEmail, string $userId): Alias {
public function findByAliasAndUserId(string $aliasEmail, string $userId): Alias {
return $this->aliasMapper->findByAlias($aliasEmail, $userId);

}

/**
Expand Down
20 changes: 10 additions & 10 deletions tests/Unit/Controller/MessageApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function testSend($messageStatus, $expected): void {
->with($this->userId, $this->account->getId())
->willReturn($this->account);
$this->aliasesService->expects(self::never())
->method('findByAliasEmailAndUserId');
->method('findByAliasAndUserId');
$this->request->expects(self::once())
->method('getUploadedFile')
->willReturn(null);
Expand Down Expand Up @@ -127,7 +127,7 @@ public function testSend($messageStatus, $expected): void {
public function mailData() {
return [
[LocalMessage::STATUS_PROCESSED, new DataResponse('Success', Http::STATUS_OK)],
[LocalMessage::STATUS_NO_SENT_MAILBOX, new DataResponse('Configuration errror: Cannot send message without sent mailbox.', Http::STATUS_FORBIDDEN)],
[LocalMessage::STATUS_NO_SENT_MAILBOX, new DataResponse('Configuration error: Cannot send message without sent mailbox.', Http::STATUS_FORBIDDEN)],
[LocalMessage::STATUS_SMPT_SEND_FAIL, new DataResponse('An SMTP error occured, please check your mail server logs.', Http::STATUS_INTERNAL_SERVER_ERROR)],
[LocalMessage::STATUS_IMAP_SENT_MAILBOX_FAIL, new DataResponse('Email was sent but could not be copied to sent mailbox.', Http::STATUS_ACCEPTED)],
[LocalMessage::STATUS_TOO_MANY_RECIPIENTS, new DataResponse('An error occured, please check the logs.', Http::STATUS_INTERNAL_SERVER_ERROR)],
Expand All @@ -143,7 +143,7 @@ public function testSendException($exception, $expected): void {
->with($this->userId, $this->account->getId())
->willReturn($this->account);
$this->aliasesService->expects(self::never())
->method('findByAliasEmailAndUserId');
->method('findByAliasAndUserId');
$this->request->expects(self::once())
->method('getUploadedFile')
->willReturn(null);
Expand Down Expand Up @@ -208,7 +208,7 @@ public function testWithAttachment(): void {
->with($this->userId, $this->account->getId())
->willReturn($this->account);
$this->aliasesService->expects(self::never())
->method('findByAliasEmailAndUserId');
->method('findByAliasAndUserId');
$this->request->expects(self::once())
->method('getUploadedFile')
->willReturn($attachments);
Expand All @@ -223,7 +223,7 @@ public function testWithAttachment(): void {
->method('sendMessage')
->willReturn($this->message);

$expected = new DataResponse(null, Http::STATUS_OK);
$expected = new DataResponse('Success', Http::STATUS_OK);
$actual = $this->controller->send(
$this->accountId,
$this->fromEmail,
Expand Down Expand Up @@ -265,7 +265,7 @@ public function testWithAttachmentError(): void {
->with($this->userId, $this->account->getId())
->willReturn($this->account);
$this->aliasesService->expects(self::never())
->method('findByAliasEmailAndUserId');
->method('findByAliasAndUserId');
$this->request->expects(self::once())
->method('getUploadedFile')
->willReturn($attachments);
Expand Down Expand Up @@ -313,7 +313,7 @@ public function testAlias(): void {
->with($this->userId, $account->getId())
->willReturn($account);
$this->aliasesService->expects(self::once())
->method('findByAliasEmailAndUserId')
->method('findByAliasAndUserId')
->willReturn($alias);
$this->request->expects(self::once())
->method('getUploadedFile')
Expand All @@ -328,7 +328,7 @@ public function testAlias(): void {
->method('sendMessage')
->willReturn($this->message);

$expected = new DataResponse(null, Http::STATUS_OK);
$expected = new DataResponse('Success', Http::STATUS_OK);
$actual = $this->controller->send(
$accountId,
$aliasMail,
Expand All @@ -348,7 +348,7 @@ public function testNoAlias(): void {
->with($this->userId, $this->account->getId())
->willReturn($this->account);
$this->aliasesService->expects(self::once())
->method('findByAliasEmailAndUserId')
->method('findByAliasAndUserId')
->willThrowException(new DoesNotExistException(''));
$this->logger->expects(self::once())
->method('error');
Expand Down Expand Up @@ -384,7 +384,7 @@ public function testNoAccount(): void {
$this->logger->expects(self::once())
->method('error');
$this->aliasesService->expects(self::never())
->method('findByAliasEmailAndUserId');
->method('findByAliasAndUserId');
$this->request->expects(self::never())
->method('getUploadedFile');
$this->attachmentService->expects(self::never())
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Service/AntiSpamServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class AntiSpamServiceTest extends TestCase {
private SmtpClientFactory|MockObject $smtpClientFactory;
private MockObject|ImapMessageMapper $imapMessageMapper;
private LoggerInterface|MockObject $logger;
private MailManager|MockObject $mailManager;

protected function setUp(): void {
parent::setUp();
Expand Down

0 comments on commit b8b60dc

Please sign in to comment.