Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--appinfo/info.xml2
-rw-r--r--appinfo/routes.php10
-rw-r--r--lib/AppInfo/Application.php3
-rw-r--r--lib/Contracts/ITrustedSenderService.php32
-rwxr-xr-xlib/Controller/MessagesController.php28
-rw-r--r--lib/Controller/TrustedSendersController.php86
-rw-r--r--lib/Db/TrustedSender.php43
-rw-r--r--lib/Db/TrustedSenderMapper.php75
-rw-r--r--lib/Migration/Version1080Date20201119084820.php43
-rw-r--r--lib/Service/TrustedSenderService.php65
-rw-r--r--src/components/Message.vue5
-rw-r--r--src/components/MessageHTMLBody.vue13
-rw-r--r--src/service/TrustedSenderService.js14
-rw-r--r--tests/Integration/Db/TrustedSenderMapperTest.php127
-rw-r--r--tests/Unit/Controller/MessagesControllerTest.php8
-rw-r--r--tests/Unit/Service/TrustedSenderServiceTest.php133
16 files changed, 682 insertions, 5 deletions
diff --git a/appinfo/info.xml b/appinfo/info.xml
index d3bc6cd56..3f4442dbe 100644
--- a/appinfo/info.xml
+++ b/appinfo/info.xml
@@ -12,7 +12,7 @@
- **🙈 We’re not reinventing the wheel!** Based on the great [Horde](http://horde.org) libraries.
- **📬 Want to host your own mail server?** We don’t have to reimplement this as you could set up [Mail-in-a-Box](https://mailinabox.email)!
]]></description>
- <version>1.8.0-alpha.1</version>
+ <version>1.8.0-alpha.2</version>
<licence>agpl</licence>
<author>Christoph Wurst</author>
<author>Greta Doçi</author>
diff --git a/appinfo/routes.php b/appinfo/routes.php
index 4009ded8c..3093b5c73 100644
--- a/appinfo/routes.php
+++ b/appinfo/routes.php
@@ -189,6 +189,16 @@ return [
'url' => '/api/settings/provisioning',
'verb' => 'DELETE'
],
+ [
+ 'name' => 'trusted_senders#setTrusted',
+ 'url' => '/api/trustedsenders/{email}',
+ 'verb' => 'PUT'
+ ],
+ [
+ 'name' => 'trusted_senders#removeTrust',
+ 'url' => '/api/trustedsenders/{email}',
+ 'verb' => 'DELETE'
+ ],
],
'resources' => [
'accounts' => ['url' => '/api/accounts'],
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
index d81684cb6..f8ef744b1 100644
--- a/lib/AppInfo/Application.php
+++ b/lib/AppInfo/Application.php
@@ -31,6 +31,7 @@ use OCA\Mail\Contracts\IAvatarService;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailSearch;
use OCA\Mail\Contracts\IMailTransmission;
+use OCA\Mail\Contracts\ITrustedSenderService;
use OCA\Mail\Contracts\IUserPreferences;
use OCA\Mail\Dashboard\ImportantMailWidget;
use OCA\Mail\Dashboard\UnreadMailWidget;
@@ -60,6 +61,7 @@ use OCA\Mail\Service\AvatarService;
use OCA\Mail\Service\MailManager;
use OCA\Mail\Service\MailTransmission;
use OCA\Mail\Service\Search\MailSearch;
+use OCA\Mail\Service\TrustedSenderService;
use OCA\Mail\Service\UserPreferenceSevice;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
@@ -96,6 +98,7 @@ class Application extends App implements IBootstrap {
$context->registerServiceAlias(IMailManager::class, MailManager::class);
$context->registerServiceAlias(IMailSearch::class, MailSearch::class);
$context->registerServiceAlias(IMailTransmission::class, MailTransmission::class);
+ $context->registerServiceAlias(ITrustedSenderService::class, TrustedSenderService::class);
$context->registerServiceAlias(IUserPreferences::class, UserPreferenceSevice::class);
$context->registerEventListener(DraftSavedEvent::class, DeleteDraftListener::class);
diff --git a/lib/Contracts/ITrustedSenderService.php b/lib/Contracts/ITrustedSenderService.php
new file mode 100644
index 000000000..4e2ecb22d
--- /dev/null
+++ b/lib/Contracts/ITrustedSenderService.php
@@ -0,0 +1,32 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Mail\Contracts;
+
+interface ITrustedSenderService {
+ public function isTrusted(string $uid, string $email): bool;
+
+ public function trust(string $uid, string $email, ?bool $trust = true);
+}
diff --git a/lib/Controller/MessagesController.php b/lib/Controller/MessagesController.php
index b46b4e10d..dacad4b08 100755
--- a/lib/Controller/MessagesController.php
+++ b/lib/Controller/MessagesController.php
@@ -34,6 +34,8 @@ use Exception;
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailSearch;
+use OCA\Mail\Contracts\ITrustedSenderService;
+use OCA\Mail\Db\Message;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Http\AttachmentDownloadResponse;
@@ -91,6 +93,9 @@ class MessagesController extends Controller {
/** @var ContentSecurityPolicyNonceManager */
private $nonceManager;
+ /** @var ITrustedSenderService */
+ private $trustedSenderService;
+
/**
* @param string $appName
* @param IRequest $request
@@ -105,6 +110,7 @@ class MessagesController extends Controller {
* @param IMimeTypeDetector $mimeTypeDetector
* @param IURLGenerator $urlGenerator
* @param ContentSecurityPolicyNonceManager $nonceManager
+ * @param ITrustedSenderService $trustedSenderService
*/
public function __construct(string $appName,
IRequest $request,
@@ -118,7 +124,8 @@ class MessagesController extends Controller {
IL10N $l10n,
IMimeTypeDetector $mimeTypeDetector,
IURLGenerator $urlGenerator,
- ContentSecurityPolicyNonceManager $nonceManager) {
+ ContentSecurityPolicyNonceManager $nonceManager,
+ ITrustedSenderService $trustedSenderService) {
parent::__construct($appName, $request);
$this->accountService = $accountService;
@@ -133,6 +140,7 @@ class MessagesController extends Controller {
$this->urlGenerator = $urlGenerator;
$this->mailManager = $mailManager;
$this->nonceManager = $nonceManager;
+ $this->trustedSenderService = $trustedSenderService;
}
/**
@@ -149,6 +157,7 @@ class MessagesController extends Controller {
* @throws ClientException
* @throws ServiceException
*/
+
public function index(int $mailboxId,
int $cursor = null,
string $filter = null,
@@ -246,6 +255,7 @@ class MessagesController extends Controller {
$json['accountId'] = $account->getId();
$json['mailboxId'] = $mailbox->getId();
$json['databaseId'] = $message->getId();
+ $json['isSenderTrusted'] = $this->isSenderTrusted($message);
$response = new JSONResponse($json);
@@ -255,6 +265,22 @@ class MessagesController extends Controller {
return $response;
}
+ private function isSenderTrusted(Message $message): bool {
+ $from = $message->getFrom();
+ $first = $from->first();
+ if ($first === null) {
+ return false;
+ }
+ $email = $first->getEmail();
+ if ($email === null) {
+ return false;
+ }
+ return $this->trustedSenderService->isTrusted(
+ $this->currentUserId,
+ $email
+ );
+ }
+
/**
* @NoAdminRequired
* @NoCSRFRequired
diff --git a/lib/Controller/TrustedSendersController.php b/lib/Controller/TrustedSendersController.php
new file mode 100644
index 000000000..014b72586
--- /dev/null
+++ b/lib/Controller/TrustedSendersController.php
@@ -0,0 +1,86 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Mail\Controller;
+
+use OCA\Mail\AppInfo\Application;
+use OCA\Mail\Contracts\ITrustedSenderService;
+use OCA\Mail\Http\JsonResponse;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\IRequest;
+
+class TrustedSendersController extends Controller {
+
+ /** @var string|null */
+ private $uid;
+
+ /** @var ITrustedSenderService */
+ private $trustedSenderService;
+
+ public function __construct(IRequest $request,
+ ?string $UserId,
+ ITrustedSenderService $trustedSenderService) {
+ parent::__construct(Application::APP_ID, $request);
+
+ $this->uid = $UserId;
+ $this->trustedSenderService = $trustedSenderService;
+ }
+
+ /**
+ * @NoAdminRequired
+ * @TrapError
+ *
+ * @param string $email
+ *
+ * @return JsonResponse
+ */
+ public function setTrusted(string $email): JsonResponse {
+ $this->trustedSenderService->trust(
+ $this->uid,
+ $email
+ );
+
+ return JsonResponse::success(null, Http::STATUS_CREATED);
+ }
+
+ /**
+ * @NoAdminRequired
+ * @TrapError
+ *
+ * @param string $email
+ *
+ * @return JsonResponse
+ */
+ public function removeTrust(string $email): JsonResponse {
+ $this->trustedSenderService->trust(
+ $this->uid,
+ $email,
+ false
+ );
+
+ return JsonResponse::success(null);
+ }
+}
diff --git a/lib/Db/TrustedSender.php b/lib/Db/TrustedSender.php
new file mode 100644
index 000000000..86902de8f
--- /dev/null
+++ b/lib/Db/TrustedSender.php
@@ -0,0 +1,43 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Mail\Db;
+
+use OCP\AppFramework\Db\Entity;
+
+/**
+ * @method setEmail(string $email): void
+ * @method getEmail(): string
+ * @method setUserId(string $userId): void
+ * @method getUserId(): string
+ */
+class TrustedSender extends Entity {
+
+ /** @var string */
+ protected $email;
+
+ /** @var string */
+ protected $userId;
+}
diff --git a/lib/Db/TrustedSenderMapper.php b/lib/Db/TrustedSenderMapper.php
new file mode 100644
index 000000000..0be0afe0c
--- /dev/null
+++ b/lib/Db/TrustedSenderMapper.php
@@ -0,0 +1,75 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Mail\Db;
+
+use OCP\AppFramework\Db\QBMapper;
+use OCP\IDBConnection;
+
+class TrustedSenderMapper extends QBMapper {
+ public function __construct(IDBConnection $db) {
+ parent::__construct($db, 'mail_trusted_senders');
+ }
+
+ public function exists(string $uid, string $email): bool {
+ $qb = $this->db->getQueryBuilder();
+
+ $select = $qb->select('*')
+ ->from($this->getTableName())
+ ->where(
+ $qb->expr()->eq('user_id', $qb->createNamedParameter($uid)),
+ $qb->expr()->eq('email', $qb->createNamedParameter($email))
+ );
+
+ /** @var TrustedSender[] $rows */
+ $rows = $this->findEntities($select);
+
+ return !empty($rows);
+ }
+
+ public function create(string $uid, string $email): void {
+ $qb = $this->db->getQueryBuilder();
+
+ $insert = $qb->insert($this->getTableName())
+ ->values([
+ 'user_id' => $qb->createNamedParameter($uid),
+ 'email' => $qb->createNamedParameter($email),
+ ]);
+
+ $insert->execute();
+ }
+
+ public function remove(string $uid, string $email): void {
+ $qb = $this->db->getQueryBuilder();
+
+ $delete = $qb->delete($this->getTableName())
+ ->where(
+ $qb->expr()->eq('user_id', $qb->createNamedParameter($uid)),
+ $qb->expr()->eq('email', $qb->createNamedParameter($email))
+ );
+
+ $delete->execute();
+ }
+}
diff --git a/lib/Migration/Version1080Date20201119084820.php b/lib/Migration/Version1080Date20201119084820.php
new file mode 100644
index 000000000..92d908d03
--- /dev/null
+++ b/lib/Migration/Version1080Date20201119084820.php
@@ -0,0 +1,43 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OCA\Mail\Migration;
+
+use Closure;
+use OCP\DB\ISchemaWrapper;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+
+class Version1080Date20201119084820 extends SimpleMigrationStep {
+
+ /**
+ * @param IOutput $output
+ * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
+ * @param array $options
+ * @return ISchemaWrapper
+ */
+ public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
+ /** @var ISchemaWrapper $schema */
+ $schema = $schemaClosure();
+
+ $table = $schema->createTable('mail_trusted_senders');
+ $table->addColumn('id', 'integer', [
+ 'autoincrement' => true,
+ 'notnull' => true,
+ 'length' => 4,
+ ]);
+ $table->addColumn('email', 'string', [
+ 'notnull' => true,
+ 'length' => 255,
+ ]);
+ $table->addColumn('user_id', 'string', [
+ 'notnull' => true,
+ 'length' => 64,
+ ]);
+ $table->setPrimaryKey(['id']);
+ $table->addUniqueIndex(['email', 'user_id'], 'mail_trusted_sender_uniq');
+
+ return $schema;
+ }
+}
diff --git a/lib/Service/TrustedSenderService.php b/lib/Service/TrustedSenderService.php
new file mode 100644
index 000000000..f70b87f39
--- /dev/null
+++ b/lib/Service/TrustedSenderService.php
@@ -0,0 +1,65 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Mail\Service;
+
+use OCA\Mail\Contracts\ITrustedSenderService;
+use OCA\Mail\Db\TrustedSenderMapper;
+
+class TrustedSenderService implements ITrustedSenderService {
+
+ /** @var TrustedSenderMapper */
+ private $mapper;
+
+ public function __construct(TrustedSenderMapper $mapper) {
+ $this->mapper = $mapper;
+ }
+
+ public function isTrusted(string $uid, string $email): bool {
+ return $this->mapper->exists(
+ $uid,
+ $email
+ );
+ }
+
+ public function trust(string $uid, string $email, ?bool $trust = true): void {
+ if ($trust && $this->isTrusted($uid, $email)) {
+ // Nothing to do
+ return;
+ }
+
+ if ($trust) {
+ $this->mapper->create(
+ $uid,
+ $email
+ );
+ } else {
+ $this->mapper->remove(
+ $uid,
+ $email
+ );
+ }
+ }
+}
diff --git a/src/components/Message.vue b/src/components/Message.vue
index c3fc1fa00..1173bc16a 100644
--- a/src/components/Message.vue
+++ b/src/components/Message.vue
@@ -24,7 +24,10 @@
<div v-if="message.itineraries.length > 0" class="message-itinerary">
<Itinerary :entries="message.itineraries" :message-id="message.messageId" />
</div>
- <MessageHTMLBody v-if="message.hasHtmlBody" :url="htmlUrl" :full-height="fullHeight" />
+ <MessageHTMLBody v-if="message.hasHtmlBody"
+ :url="htmlUrl"
+ :message="message"
+ :full-height="fullHeight" />
<MessageEncryptedBody v-else-if="isEncrypted" :body="message.body" :from="from" />
<MessagePlainTextBody v-else :body="message.body" :signature="message.signature" />
<Popover v-if="message.attachments[0]" class="attachment-popover">
diff --git a/src/components/MessageHTMLBody.vue b/src/components/MessageHTMLBody.vue
index b0af31512..e853ff083 100644
--- a/src/components/MessageHTMLBody.vue
+++ b/src/components/MessageHTMLBody.vue
@@ -21,6 +21,7 @@
<script>
import { iframeResizer } from 'iframe-resizer'
import PrintScout from 'printscout'
+import { trustSender } from '../service/TrustedSenderService'
import logger from '../logger'
const scout = new PrintScout()
@@ -37,11 +38,16 @@ export default {
required: false,
default: false,
},
+ message: {
+ required: true,
+ type: Object,
+ },
},
data() {
return {
loading: true,
hasBlockedContent: false,
+ isSenderTrusted: this.message.isSenderTrusted,
}
},
beforeMount() {
@@ -81,6 +87,9 @@ export default {
|| iframeDoc.querySelectorAll('[data-original-style]').length > 0
this.loading = false
+ if (this.isSenderTrusted) {
+ this.onShowBlockedContent()
+ }
},
onAfterPrint() {
this.$refs.iframe.style.setProperty('height', '')
@@ -88,7 +97,7 @@ export default {
onBeforePrint() {
this.$refs.iframe.style.setProperty('height', `${this.getIframeDoc().body.scrollHeight}px`, 'important')
},
- onShowBlockedContent() {
+ async onShowBlockedContent() {
const iframeDoc = this.getIframeDoc()
logger.debug('showing external images')
iframeDoc.querySelectorAll('[data-original-src]').forEach((node) => {
@@ -100,6 +109,8 @@ export default {
.forEach((node) => node.setAttribute('style', node.getAttribute('data-original-style')))
this.hasBlockedContent = false
+ trustSender(this.message.from[0].email, true)
+
},
},
}
diff --git a/src/service/TrustedSenderService.js b/src/service/TrustedSenderService.js
new file mode 100644
index 000000000..8bde3062c
--- /dev/null
+++ b/src/service/TrustedSenderService.js
@@ -0,0 +1,14 @@
+import { generateUrl } from '@nextcloud/router'
+import axios from '@nextcloud/axios'
+
+export async function trustSender(email, trustFlag) {
+ const url = generateUrl('/apps/mail/api/trustedsenders/{email}', {
+ email,
+ })
+
+ if (trustFlag) {
+ await axios.put(url)
+ } else {
+ await axios.delete(url)
+ }
+}
diff --git a/tests/Integration/Db/TrustedSenderMapperTest.php b/tests/Integration/Db/TrustedSenderMapperTest.php
new file mode 100644
index 000000000..c5bbbe845
--- /dev/null
+++ b/tests/Integration/Db/TrustedSenderMapperTest.php
@@ -0,0 +1,127 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Mail\Tests\Integration\Db;
+
+use ChristophWurst\Nextcloud\Testing\DatabaseTransaction;
+use ChristophWurst\Nextcloud\Testing\TestCase;
+use ChristophWurst\Nextcloud\Testing\TestUser;
+use OCA\Mail\Db\TrustedSenderMapper;
+use OCP\IDBConnection;
+use OCP\IUser;
+
+class TrustedSenderMapperTest extends TestCase {
+ use DatabaseTransaction, TestUser;
+
+ /** @var IDBConnection */
+ private $db;
+
+ /** @var IUser */
+ private $user;
+
+ /** @var TrustedSenderMapper */
+ private $mapper;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ /** @var IDBConnection $db */
+ $this->db = \OC::$server->get(IDBConnection::class);
+ $this->user = $this->createTestUser();
+
+ $this->mapper = new TrustedSenderMapper(
+ $this->db
+ );
+ }
+
+ public function testExistsButDoesNot(): void {
+ $exists = $this->mapper->exists($this->user->getUID(), "christoph@next.cloud");
+
+ $this->assertFalse($exists);
+ }
+
+ public function testExists(): void {
+ $uid = $this->user->getUID();
+ $qb = $this->db->getQueryBuilder();
+ $qb->insert('mail_trusted_senders')
+ ->values([
+ 'user_id' => $qb->createNamedParameter($uid),
+ 'email' => $qb->createNamedParameter('christoph@next.cloud'),
+ ])
+ ->execute();
+
+ $exists = $this->mapper->exists($uid, "christoph@next.cloud");
+
+ $this->assertTrue($exists);
+ }
+
+ public function testCreate(): void {
+ $uid = $this->user->getUID();
+ $this->mapper->create(
+ $uid,
+ "christoph@next.cloud"
+ );
+
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('*')
+ ->from('mail_trusted_senders')
+ ->where(
+ $qb->expr()->eq('user_id', $qb->createNamedParameter($uid)),
+ $qb->expr()->eq('email', $qb->createNamedParameter("christoph@next.cloud"))
+ );
+ $result = $qb->execute();
+ $rows = $result->fetchAll();
+ $result->closeCursor();
+ $this->assertCount(1, $rows);
+ }
+
+ public function testRemove(): void {
+ $uid = $this->user->getUID();
+ $qb = $this->db->getQueryBuilder();
+ $qb->insert('mail_trusted_senders')
+ ->values([
+ 'user_id' => $qb->createNamedParameter($uid),
+ 'email' => $qb->createNamedParameter('christoph@next.cloud'),
+ ])
+ ->execute();
+
+ $this->mapper->remove(
+ $uid,
+ "christoph@next.cloud"
+ );
+
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('*')
+ ->from('mail_trusted_senders')
+ ->where(
+ $qb->expr()->eq('user_id', $qb->createNamedParameter($uid)),
+ $qb->expr()->eq('email', $qb->createNamedParameter("christoph@next.cloud"))
+ );
+ $result = $qb->execute();
+ $rows = $result->fetchAll();
+ $result->closeCursor();
+ $this->assertEmpty($rows);
+ }
+}
diff --git a/tests/Unit/Controller/MessagesControllerTest.php b/tests/Unit/Controller/MessagesControllerTest.php
index 23806aadc..46cd7774d 100644
--- a/tests/Unit/Controller/MessagesControllerTest.php
+++ b/tests/Unit/Controller/MessagesControllerTest.php
@@ -31,6 +31,7 @@ use OCA\Mail\Account;
use OCA\Mail\Attachment;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailSearch;
+use OCA\Mail\Contracts\ITrustedSenderService;
use OCA\Mail\Controller\MessagesController;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Http\AttachmentDownloadResponse;
@@ -110,6 +111,9 @@ class MessagesControllerTest extends TestCase {
/** @var MockObject|ContentSecurityPolicyNonceManager */
private $nonceManager;
+ /** @var MockObject|ITrustedSenderService */
+ private $trustedSenderService;
+
/** @var ITimeFactory */
private $oldFactory;
@@ -130,6 +134,7 @@ class MessagesControllerTest extends TestCase {
$this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->nonceManager = $this->createMock(ContentSecurityPolicyNonceManager::class);
+ $this->trustedSenderService = $this->createMock(ITrustedSenderService::class);
$timeFactory = $this->createMocK(ITimeFactory::class);
$timeFactory->expects($this->any())
@@ -153,7 +158,8 @@ class MessagesControllerTest extends TestCase {
$this->l10n,
$this->mimeTypeDetector,
$this->urlGenerator,
- $this->nonceManager
+ $this->nonceManager,
+ $this->trustedSenderService
);
$this->account = $this->createMock(Account::class);
diff --git a/tests/Unit/Service/TrustedSenderServiceTest.php b/tests/Unit/Service/TrustedSenderServiceTest.php
new file mode 100644
index 000000000..e37b80387
--- /dev/null
+++ b/tests/Unit/Service/TrustedSenderServiceTest.php
@@ -0,0 +1,133 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Mail\Tests\Unit\Service;
+
+use ChristophWurst\Nextcloud\Testing\TestCase;
+use OCA\Mail\Db\TrustedSenderMapper;
+use OCA\Mail\Service\TrustedSenderService;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class TrustedSenderServiceTest extends TestCase {
+
+ /** @var TrustedSenderMapper|MockObject */
+ private $mapper;
+
+ /** @var TrustedSenderService */
+ private $service;
+
+ protected function setUp(): void {
+ $this->mapper = $this->createMock(TrustedSenderMapper::class);
+
+ $this->service = new TrustedSenderService(
+ $this->mapper
+ );
+ }
+
+ public function testIsTrusted(): void {
+ $uid = 'greta';
+ $email = 'christoph@next.cloud';
+ $this->mapper->expects($this->once())
+ ->method('exists')
+ ->with($uid, $email)
+ ->willReturn(true);
+
+ $trusted = $this->service->isTrusted(
+ $uid,
+ $email
+ );
+
+ $this->assertTrue($trusted);
+ }
+
+ public function testIsNotTrusted(): void {
+ $uid = 'greta';
+ $email = 'christoph@next.cloud';
+ $this->mapper->expects($this->once())
+ ->method('exists')
+ ->with($uid, $email)
+ ->willReturn(false);
+
+ $trusted = $this->service->isTrusted(
+ $uid,
+ $email
+ );
+
+ $this->assertFalse($trusted);
+ }
+
+ public function testTrustAlreadyTrusted(): void {
+ $uid = 'greta';
+ $email = 'christoph@next.cloud';
+ $this->mapper->expects($this->once())
+ ->method('exists')
+ ->with($uid, $email)
+ ->willReturn(true);
+ $this->mapper->expects($this->never())
+ ->method('create');
+ $this->mapper->expects($this->never())
+ ->method('remove');
+
+ $this->service->trust(
+ $uid,
+ $email
+ );
+ }
+
+ public function testTrustNew(): void {
+ $uid = 'greta';
+ $email = 'christoph@next.cloud';
+ $this->mapper->expects($this->once())
+ ->method('exists')
+ ->with($uid, $email)
+ ->willReturn(false);
+ $this->mapper->expects($this->once())
+ ->method('create')
+ ->with($uid, $email);
+
+ $this->service->trust(
+ $uid,
+ $email
+ );
+ }
+
+ public function testRemoveTrust(): void {
+ $uid = 'greta';
+ $email = 'christoph@next.cloud';
+ $this->mapper->expects($this->never())
+ ->method('exists');
+ $this->mapper->expects($this->never())
+ ->method('create');
+ $this->mapper->expects($this->once())
+ ->method('remove')
+ ->with($uid, $email);
+
+ $this->service->trust(
+ $uid,
+ $email,
+ false
+ );
+ }
+}