diff options
Diffstat (limited to 'apps/dav/tests')
8 files changed, 261 insertions, 19 deletions
diff --git a/apps/dav/tests/unit/CardDAV/AddressBookImplTest.php b/apps/dav/tests/unit/CardDAV/AddressBookImplTest.php index dff368b923c..a8bfc1b41fd 100644 --- a/apps/dav/tests/unit/CardDAV/AddressBookImplTest.php +++ b/apps/dav/tests/unit/CardDAV/AddressBookImplTest.php @@ -10,6 +10,7 @@ namespace OCA\DAV\Tests\unit\CardDAV; use OCA\DAV\CardDAV\AddressBook; use OCA\DAV\CardDAV\AddressBookImpl; use OCA\DAV\CardDAV\CardDavBackend; +use OCA\DAV\Db\PropertyMapper; use OCP\IURLGenerator; use Sabre\VObject\Component\VCard; use Sabre\VObject\Property\Text; @@ -32,6 +33,9 @@ class AddressBookImplTest extends TestCase { /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject */ private $backend; + /** @var PropertyMapper | \PHPUnit\Framework\MockObject\MockObject */ + private $propertyMapper; + /** @var VCard | \PHPUnit\Framework\MockObject\MockObject */ private $vCard; @@ -50,12 +54,15 @@ class AddressBookImplTest extends TestCase { ->disableOriginalConstructor()->getMock(); $this->vCard = $this->createMock(VCard::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->propertyMapper = $this->createMock(PropertyMapper::class); $this->addressBookImpl = new AddressBookImpl( $this->addressBook, $this->addressBookInfo, $this->backend, - $this->urlGenerator + $this->urlGenerator, + $this->propertyMapper, + null ); } @@ -78,6 +85,8 @@ class AddressBookImplTest extends TestCase { $this->addressBookInfo, $this->backend, $this->urlGenerator, + $this->propertyMapper, + null ] ) ->setMethods(['vCard2Array', 'readCard']) @@ -124,6 +133,8 @@ class AddressBookImplTest extends TestCase { $this->addressBookInfo, $this->backend, $this->urlGenerator, + $this->propertyMapper, + null ] ) ->setMethods(['vCard2Array', 'createUid', 'createEmptyVCard']) @@ -174,6 +185,8 @@ class AddressBookImplTest extends TestCase { $this->addressBookInfo, $this->backend, $this->urlGenerator, + $this->propertyMapper, + null ] ) ->setMethods(['vCard2Array', 'createUid', 'createEmptyVCard', 'readCard']) @@ -211,6 +224,8 @@ class AddressBookImplTest extends TestCase { $this->addressBookInfo, $this->backend, $this->urlGenerator, + $this->propertyMapper, + null ] ) ->setMethods(['vCard2Array', 'createUid', 'createEmptyVCard', 'readCard']) @@ -292,6 +307,8 @@ class AddressBookImplTest extends TestCase { $this->addressBookInfo, $this->backend, $this->urlGenerator, + $this->propertyMapper, + null ] ) ->setMethods(['getUid']) @@ -488,7 +505,9 @@ class AddressBookImplTest extends TestCase { $this->addressBook, $addressBookInfo, $this->backend, - $this->urlGenerator + $this->urlGenerator, + $this->propertyMapper, + null ); $this->assertTrue($addressBookImpl->isSystemAddressBook()); @@ -507,7 +526,9 @@ class AddressBookImplTest extends TestCase { $this->addressBook, $addressBookInfo, $this->backend, - $this->urlGenerator + $this->urlGenerator, + $this->propertyMapper, + 'user2' ); $this->assertFalse($addressBookImpl->isSystemAddressBook()); @@ -527,7 +548,9 @@ class AddressBookImplTest extends TestCase { $this->addressBook, $addressBookInfo, $this->backend, - $this->urlGenerator + $this->urlGenerator, + $this->propertyMapper, + 'user2' ); $this->assertFalse($addressBookImpl->isSystemAddressBook()); diff --git a/apps/dav/tests/unit/CardDAV/ContactsManagerTest.php b/apps/dav/tests/unit/CardDAV/ContactsManagerTest.php index 6b1abb2718d..80f1f2a4445 100644 --- a/apps/dav/tests/unit/CardDAV/ContactsManagerTest.php +++ b/apps/dav/tests/unit/CardDAV/ContactsManagerTest.php @@ -9,6 +9,7 @@ namespace OCA\DAV\Tests\unit\CardDAV; use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\ContactsManager; +use OCA\DAV\Db\PropertyMapper; use OCP\Contacts\IManager; use OCP\IL10N; use OCP\IURLGenerator; @@ -25,9 +26,10 @@ class ContactsManagerTest extends TestCase { $backEnd->method('getAddressBooksForUser')->willReturn([ ['{DAV:}displayname' => 'Test address book', 'uri' => 'default'], ]); + $propertyMapper = $this->createMock(PropertyMapper::class); $l = $this->createMock(IL10N::class); - $app = new ContactsManager($backEnd, $l); + $app = new ContactsManager($backEnd, $l, $propertyMapper); $app->setupContactsProvider($cm, 'user01', $urlGenerator); } } diff --git a/apps/dav/tests/unit/Connector/Sabre/BearerAuthTest.php b/apps/dav/tests/unit/Connector/Sabre/BearerAuthTest.php index 06c070454af..99c2a461557 100644 --- a/apps/dav/tests/unit/Connector/Sabre/BearerAuthTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/BearerAuthTest.php @@ -7,10 +7,12 @@ namespace OCA\DAV\Tests\unit\Connector\Sabre; use OC\User\Session; use OCA\DAV\Connector\Sabre\BearerAuth; +use OCP\IConfig; use OCP\IRequest; use OCP\ISession; use OCP\IUser; use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; use Sabre\HTTP\RequestInterface; use Sabre\HTTP\ResponseInterface; use Test\TestCase; @@ -28,17 +30,21 @@ class BearerAuthTest extends TestCase { /** @var BearerAuth */ private $bearerAuth; + private IConfig&MockObject $config; + protected function setUp(): void { parent::setUp(); $this->userSession = $this->createMock(Session::class); $this->session = $this->createMock(ISession::class); $this->request = $this->createMock(IRequest::class); + $this->config = $this->createMock(IConfig::class); $this->bearerAuth = new BearerAuth( $this->userSession, $this->session, - $this->request + $this->request, + $this->config, ); } diff --git a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php index 18cef58207d..f6c19787e94 100644 --- a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php @@ -286,6 +286,7 @@ class DirectoryTest extends \Test\TestCase { ->willReturnMap([ ['\OCA\Files_Sharing\SharedStorage', false], ['\OC\Files\Storage\Wrapper\Quota', false], + [\OCA\Files_Sharing\External\Storage::class, false], ]); $storage->expects($this->once()) @@ -341,6 +342,7 @@ class DirectoryTest extends \Test\TestCase { ->willReturnMap([ ['\OCA\Files_Sharing\SharedStorage', false], ['\OC\Files\Storage\Wrapper\Quota', true], + [\OCA\Files_Sharing\External\Storage::class, false], ]); $storage->expects($this->once()) diff --git a/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php b/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php index ce91a2a7fa6..a0876d8e483 100644 --- a/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php +++ b/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php @@ -14,6 +14,7 @@ use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\SyncService; use OCA\DAV\Listener\UserEventsListener; +use OCA\DAV\Service\DefaultContactService; use OCP\Defaults; use OCP\IUser; use OCP\IUserManager; @@ -27,6 +28,8 @@ class UserEventsListenerTest extends TestCase { private CardDavBackend&MockObject $cardDavBackend; private Defaults&MockObject $defaults; + private DefaultContactService&MockObject $defaultContactService; + private UserEventsListener $userEventsListener; protected function setUp(): void { @@ -36,12 +39,14 @@ class UserEventsListenerTest extends TestCase { $this->calDavBackend = $this->createMock(CalDavBackend::class); $this->cardDavBackend = $this->createMock(CardDavBackend::class); $this->defaults = $this->createMock(Defaults::class); + $this->defaultContactService = $this->createMock(DefaultContactService::class); $this->userEventsListener = new UserEventsListener( $this->userManager, $this->syncService, $this->calDavBackend, $this->cardDavBackend, $this->defaults, + $this->defaultContactService, ); } diff --git a/apps/dav/tests/unit/Files/MultipartRequestParserTest.php b/apps/dav/tests/unit/Files/MultipartRequestParserTest.php index e6325ab8ad1..40880bdca9c 100644 --- a/apps/dav/tests/unit/Files/MultipartRequestParserTest.php +++ b/apps/dav/tests/unit/Files/MultipartRequestParserTest.php @@ -7,12 +7,14 @@ namespace OCA\DAV\Tests\unit\DAV; use OCA\DAV\BulkUpload\MultipartRequestParser; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; +use Sabre\HTTP\RequestInterface; use Test\TestCase; class MultipartRequestParserTest extends TestCase { - protected LoggerInterface $logger; + protected LoggerInterface&MockObject $logger; protected function setUp(): void { $this->logger = $this->createMock(LoggerInterface::class); @@ -24,6 +26,7 @@ class MultipartRequestParserTest extends TestCase { 'headers' => [ 'Content-Length' => 7, 'X-File-MD5' => '4f2377b4d911f7ec46325fe603c3af03', + 'OC-Checksum' => 'md5:4f2377b4d911f7ec46325fe603c3af03', 'X-File-Path' => '/coucou.txt' ], 'content' => "Coucou\n" @@ -32,7 +35,8 @@ class MultipartRequestParserTest extends TestCase { } private function getMultipartParser(array $parts, array $headers = [], string $boundary = 'boundary_azertyuiop'): MultipartRequestParser { - $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') + /** @var RequestInterface&MockObject $request */ + $request = $this->getMockBuilder(RequestInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -74,7 +78,8 @@ class MultipartRequestParserTest extends TestCase { */ public function testBodyTypeValidation(): void { $bodyStream = 'I am not a stream, but pretend to be'; - $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') + /** @var RequestInterface&MockObject $request */ + $request = $this->getMockBuilder(RequestInterface::class) ->disableOriginalConstructor() ->getMock(); $request->expects($this->any()) @@ -88,15 +93,39 @@ class MultipartRequestParserTest extends TestCase { /** * Test with valid request. * - valid boundary - * - valid md5 hash + * - valid hash * - valid content-length * - valid file content * - valid file path */ public function testValidRequest(): void { - $multipartParser = $this->getMultipartParser( - $this->getValidBodyObject() - ); + $bodyObject = $this->getValidBodyObject(); + unset($bodyObject['0']['headers']['X-File-MD5']); + + $multipartParser = $this->getMultipartParser($bodyObject); + + [$headers, $content] = $multipartParser->parseNextPart(); + + $this->assertSame((int)$headers['content-length'], 7, 'Content-Length header should be the same as provided.'); + $this->assertSame($headers['oc-checksum'], 'md5:4f2377b4d911f7ec46325fe603c3af03', 'OC-Checksum header should be the same as provided.'); + $this->assertSame($headers['x-file-path'], '/coucou.txt', 'X-File-Path header should be the same as provided.'); + + $this->assertSame($content, "Coucou\n", 'Content should be the same'); + } + + /** + * Test with valid request. + * - valid boundary + * - valid md5 hash + * - valid content-length + * - valid file content + * - valid file path + */ + public function testValidRequestWithMd5(): void { + $bodyObject = $this->getValidBodyObject(); + unset($bodyObject['0']['headers']['OC-Checksum']); + + $multipartParser = $this->getMultipartParser($bodyObject); [$headers, $content] = $multipartParser->parseNextPart(); @@ -108,30 +137,47 @@ class MultipartRequestParserTest extends TestCase { } /** + * Test with invalid hash. + */ + public function testInvalidHash(): void { + $bodyObject = $this->getValidBodyObject(); + $bodyObject['0']['headers']['OC-Checksum'] = 'md5:f2377b4d911f7ec46325fe603c3af03'; + unset($bodyObject['0']['headers']['X-File-MD5']); + $multipartParser = $this->getMultipartParser( + $bodyObject + ); + + $this->expectExceptionMessage('Computed md5 hash is incorrect (4f2377b4d911f7ec46325fe603c3af03).'); + $multipartParser->parseNextPart(); + } + + /** * Test with invalid md5 hash. */ public function testInvalidMd5Hash(): void { $bodyObject = $this->getValidBodyObject(); + unset($bodyObject['0']['headers']['OC-Checksum']); $bodyObject['0']['headers']['X-File-MD5'] = 'f2377b4d911f7ec46325fe603c3af03'; $multipartParser = $this->getMultipartParser( $bodyObject ); - $this->expectExceptionMessage('Computed md5 hash is incorrect.'); + $this->expectExceptionMessage('Computed md5 hash is incorrect (4f2377b4d911f7ec46325fe603c3af03).'); $multipartParser->parseNextPart(); } /** - * Test with a null md5 hash. + * Test with a null hash headers. */ - public function testNullMd5Hash(): void { + public function testNullHash(): void { $bodyObject = $this->getValidBodyObject(); + unset($bodyObject['0']['headers']['OC-Checksum']); unset($bodyObject['0']['headers']['X-File-MD5']); $multipartParser = $this->getMultipartParser( $bodyObject ); - $this->expectExceptionMessage('The X-File-MD5 header must not be null.'); + $this->expectExceptionMessage('The hash headers must not be null.'); $multipartParser->parseNextPart(); } @@ -159,7 +205,7 @@ class MultipartRequestParserTest extends TestCase { $bodyObject ); - $this->expectExceptionMessage('Computed md5 hash is incorrect.'); + $this->expectExceptionMessage('Computed md5 hash is incorrect (41060d3ddfdf63e68fc2bf196f652ee9).'); $multipartParser->parseNextPart(); } @@ -173,7 +219,7 @@ class MultipartRequestParserTest extends TestCase { $bodyObject ); - $this->expectExceptionMessage('Computed md5 hash is incorrect.'); + $this->expectExceptionMessage('Computed md5 hash is incorrect (0161002bbee6a744f18741b8a914e413).'); $multipartParser->parseNextPart(); } diff --git a/apps/dav/tests/unit/Service/DefaultContactServiceTest.php b/apps/dav/tests/unit/Service/DefaultContactServiceTest.php new file mode 100644 index 00000000000..9540e77ce6c --- /dev/null +++ b/apps/dav/tests/unit/Service/DefaultContactServiceTest.php @@ -0,0 +1,148 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\DAV\Tests\Unit\Service; + +use OCA\DAV\CardDAV\CardDavBackend; +use OCA\DAV\Service\DefaultContactService; +use OCP\App\IAppManager; +use OCP\Files\AppData\IAppDataFactory; +use OCP\Files\IAppData; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use Symfony\Component\Uid\Uuid; +use Test\TestCase; + +class DefaultContactServiceTest extends TestCase { + private DefaultContactService $service; + private MockObject|CardDavBackend $cardDav; + private MockObject|IAppManager $appManager; + private MockObject|IAppDataFactory $appDataFactory; + private MockObject|LoggerInterface $logger; + + protected function setUp(): void { + parent::setUp(); + + $this->cardDav = $this->createMock(CardDavBackend::class); + $this->appManager = $this->createMock(IAppManager::class); + $this->appDataFactory = $this->createMock(IAppDataFactory::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->service = new DefaultContactService( + $this->cardDav, + $this->appManager, + $this->appDataFactory, + $this->logger + ); + } + + public function testCreateDefaultContactWithInvalidCard(): void { + // Invalid vCard missing required FN property + $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nEND:VCARD"; + + $appData = $this->createMock(IAppData::class); + $folder = $this->createMock(ISimpleFolder::class); + $file = $this->createMock(ISimpleFile::class); + $file->method('getContent')->willReturn($vcardContent); + $folder->method('getFile')->willReturn($file); + $appData->method('getFolder')->willReturn($folder); + $this->appDataFactory->method('get')->willReturn($appData); + + $this->logger->expects($this->once()) + ->method('error') + ->with('Default contact is invalid', $this->anything()); + + $this->cardDav->expects($this->never()) + ->method('createCard'); + + $this->service->createDefaultContact(123); + } + + public function testUidAndRevAreUpdated(): void { + $originalUid = 'original-uid'; + $originalRev = '20200101T000000Z'; + $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nUID:$originalUid\nREV:$originalRev\nEND:VCARD"; + + $appData = $this->createMock(IAppData::class); + $folder = $this->createMock(ISimpleFolder::class); + $file = $this->createMock(ISimpleFile::class); + $file->method('getContent')->willReturn($vcardContent); + $folder->method('getFile')->willReturn($file); + $appData->method('getFolder')->willReturn($folder); + $this->appDataFactory->method('get')->willReturn($appData); + + $capturedCardData = null; + $this->cardDav->expects($this->once()) + ->method('createCard') + ->with( + $this->anything(), + $this->anything(), + $this->callback(function ($cardData) use (&$capturedCardData) { + $capturedCardData = $cardData; + return true; + }), + $this->anything() + )->willReturn(null); + + $this->service->createDefaultContact(123); + + $vcard = \Sabre\VObject\Reader::read($capturedCardData); + $this->assertNotEquals($originalUid, $vcard->UID->getValue()); + $this->assertTrue(Uuid::isValid($vcard->UID->getValue())); + $this->assertNotEquals($originalRev, $vcard->REV->getValue()); + } + + public function testDefaultContactFileDoesNotExist(): void { + $appData = $this->createMock(IAppData::class); + $appData->method('getFolder')->willThrowException(new NotFoundException()); + $this->appDataFactory->method('get')->willReturn($appData); + + $this->cardDav->expects($this->never()) + ->method('createCard'); + + $this->service->createDefaultContact(123); + } + + public function testUidAndRevAreAddedIfMissing(): void { + $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nEND:VCARD"; + + $appData = $this->createMock(IAppData::class); + $folder = $this->createMock(ISimpleFolder::class); + $file = $this->createMock(ISimpleFile::class); + $file->method('getContent')->willReturn($vcardContent); + $folder->method('getFile')->willReturn($file); + $appData->method('getFolder')->willReturn($folder); + $this->appDataFactory->method('get')->willReturn($appData); + + $capturedCardData = 'new-card-data'; + + $this->cardDav + ->expects($this->once()) + ->method('createCard') + ->with( + $this->anything(), + $this->anything(), + $this->callback(function ($cardData) use (&$capturedCardData) { + $capturedCardData = $cardData; + return true; + }), + $this->anything() + ); + + $this->service->createDefaultContact(123); + $vcard = \Sabre\VObject\Reader::read($capturedCardData); + + $this->assertNotNull($vcard->REV); + $this->assertNotNull($vcard->UID); + $this->assertTrue(Uuid::isValid($vcard->UID->getValue())); + } +} diff --git a/apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php b/apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php index e1517eec425..ab5253147a7 100644 --- a/apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php +++ b/apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php @@ -12,6 +12,7 @@ use OCA\DAV\SystemTag\SystemTagNode; use OCA\DAV\SystemTag\SystemTagPlugin; use OCA\DAV\SystemTag\SystemTagsByIdCollection; use OCA\DAV\SystemTag\SystemTagsObjectMappingCollection; +use OCP\Files\IRootFolder; use OCP\IGroupManager; use OCP\IUser; use OCP\IUserSession; @@ -57,6 +58,11 @@ class SystemTagPluginTest extends \Test\TestCase { private $userSession; /** + * @var IRootFolder + */ + private $rootFolder; + + /** * @var IUser */ private $user; @@ -95,13 +101,17 @@ class SystemTagPluginTest extends \Test\TestCase { ->expects($this->any()) ->method('isLoggedIn') ->willReturn(true); + $this->tagMapper = $this->getMockBuilder(ISystemTagObjectMapper::class) ->getMock(); + $this->rootFolder = $this->getMockBuilder(IRootFolder::class) + ->getMock(); $this->plugin = new SystemTagPlugin( $this->tagManager, $this->groupManager, $this->userSession, + $this->rootFolder, $this->tagMapper ); $this->plugin->initialize($this->server); |