diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-11-19 12:47:50 +0300 |
---|---|---|
committer | GretaD <gretadoci@gmail.com> | 2020-12-03 15:57:54 +0300 |
commit | 87d99c0e5629ae4551d3776bef554dd3c448aebf (patch) | |
tree | ccbd7bb5890fca25b51a10a824b0b4dcd9bf6ffc /tests | |
parent | 44a004276a21e57aeca78b71599b5ec3aa7d5d13 (diff) |
Allow always showing images from trusted senders
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Integration/Db/TrustedSenderMapperTest.php | 127 | ||||
-rw-r--r-- | tests/Unit/Controller/MessagesControllerTest.php | 8 | ||||
-rw-r--r-- | tests/Unit/Service/TrustedSenderServiceTest.php | 133 |
3 files changed, 267 insertions, 1 deletions
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 + ); + } +} |