From f81af1e733385eabf04476491dd021ac548fa55c Mon Sep 17 00:00:00 2001 From: SebastianKrupinski Date: Wed, 12 Feb 2025 00:16:23 -0500 Subject: [PATCH] fixup! fixup! fix: automatically disable sab Signed-off-by: SebastianKrupinski --- apps/dav/lib/CardDAV/SyncService.php | 1 + .../tests/unit/CardDAV/SyncServiceTest.php | 163 +++++++++++++++++- 2 files changed, 158 insertions(+), 6 deletions(-) diff --git a/apps/dav/lib/CardDAV/SyncService.php b/apps/dav/lib/CardDAV/SyncService.php index 714210d4b4e3e..c14cb56a4fd38 100644 --- a/apps/dav/lib/CardDAV/SyncService.php +++ b/apps/dav/lib/CardDAV/SyncService.php @@ -293,6 +293,7 @@ public function syncInstance(?\Closure $progressCallback = null) { $limit = (int)$this->config->getAppValue('dav', 'system_addressbook_limit', '5000'); if ($this->userManager->countUsersTotal($limit) >= $limit) { $this->config->setAppValue('dav', 'system_addressbook_exposed', 'no'); + return; } } diff --git a/apps/dav/tests/unit/CardDAV/SyncServiceTest.php b/apps/dav/tests/unit/CardDAV/SyncServiceTest.php index 5af42e2ea4ec9..f57a97db2cabe 100644 --- a/apps/dav/tests/unit/CardDAV/SyncServiceTest.php +++ b/apps/dav/tests/unit/CardDAV/SyncServiceTest.php @@ -20,6 +20,7 @@ use OCP\IDBConnection; use OCP\IUser; use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Client\ClientExceptionInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -28,14 +29,15 @@ class SyncServiceTest extends TestCase { - protected CardDavBackend $backend; - protected IUserManager $userManager; - protected IDBConnection $dbConnection; + protected CardDavBackend&MockObject $backend; + protected IUserManager&MockObject $userManager; + protected IDBConnection&MockObject $dbConnection; protected LoggerInterface $logger; - protected Converter $converter; - protected IClient $client; - protected IConfig $config; + protected Converter&MockObject $converter; + protected IClient&MockObject $client; + protected IConfig&MockObject $config; protected SyncService $service; + public function setUp(): void { $addressBook = [ 'id' => 1, @@ -487,4 +489,153 @@ public function providerUseAbsoluteUriReport(): array { ['https://server.internal:8080/nextcloud/', 'https://server.internal:8080/nextcloud/remote.php/dav/addressbooks/system/system/system'], ]; } + + public function testSyncInstance(): void { + + $this->config->method('getAppValue') + ->willReturnMap([ + ['dav', 'system_addressbook_exposed', 'yes', 'yes'], + ['dav', 'system_addressbook_limit', '5000', '5000'] + ]); + + $this->userManager->method('countUsersTotal') + ->willReturn(4999); + + $this->userManager->method('callForAllUsers') + ->willReturnCallback(function ($callback) { + $user = $this->createMock(IUser::class); + $user->method('getBackendClassName')->willReturn('unittest'); + $user->method('getUID')->willReturn('test-user'); + $user->method('isEnabled')->willReturn(true); + $callback($user); + }); + + $this->backend->method('getCards') + ->willReturn([ + ['carddata' => "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:test-user\r\nFN:test-user\r\nEND:VCARD\r\n", 'uri' => 'unittest:test-user.vcf'] + ]); + + $clientService = $this->createMock(IClientService::class); + + $service = $this->getMockBuilder(SyncService::class) + ->setConstructorArgs([ + $this->backend, + $this->userManager, + $this->dbConnection, + $this->logger, + $this->converter, + $clientService, + $this->config + ]) + ->onlyMethods(['ensureSystemAddressBookExists', 'updateUser']) + ->getMock(); + + $service->expects($this->once()) + ->method('ensureSystemAddressBookExists') + ->willReturn([ + 'id' => 1, + 'uri' => 'system', + 'principaluri' => 'principals/system/system', + '{DAV:}displayname' => 'system', + ]); + $service->expects($this->any()) + ->method('updateUser'); + + $service->syncInstance(); + } + + public function testSyncInstanceWithProgressCallback(): void { + $this->config->method('getAppValue') + ->willReturnMap([ + ['dav', 'system_addressbook_exposed', 'yes', 'yes'], + ['dav', 'system_addressbook_limit', '5000', '5000'] + ]); + + $this->userManager->method('countUsersTotal') + ->willReturn(4999); + + $this->userManager->method('callForAllUsers') + ->willReturnCallback(function ($callback) { + $user = $this->createMock(IUser::class); + $user->method('getBackendClassName')->willReturn('unittest'); + $user->method('getUID')->willReturn('test-user'); + $user->method('isEnabled')->willReturn(true); + $callback($user); + }); + + $this->backend->method('getCards') + ->willReturn([ + ['carddata' => "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:test-user\r\nFN:test-user\r\nEND:VCARD\r\n", 'uri' => 'unittest:test-user.vcf'] + ]); + + $clientService = $this->createMock(IClientService::class); + + $service = $this->getMockBuilder(SyncService::class) + ->setConstructorArgs([ + $this->backend, + $this->userManager, + $this->dbConnection, + $this->logger, + $this->converter, + $clientService, + $this->config + ]) + ->onlyMethods(['ensureSystemAddressBookExists', 'updateUser']) + ->getMock(); + + $service->expects($this->once()) + ->method('ensureSystemAddressBookExists') + ->willReturn([ + 'id' => 1, + 'uri' => 'system', + 'principaluri' => 'principals/system/system', + '{DAV:}displayname' => 'system', + ]); + $service->expects($this->any()) + ->method('updateUser'); + + $progressCalled = false; + $progressCallback = function () use (&$progressCalled) { + $progressCalled = true; + }; + + $service->syncInstance($progressCallback); + + $this->assertTrue($progressCalled); + } + + public function testSyncInstanceWithUserLimitExceeded(): void { + $this->config->method('getAppValue') + ->willReturnMap([ + ['dav', 'system_addressbook_exposed', 'yes', 'yes'], + ['dav', 'system_addressbook_limit', '5000', '5000'] + ]); + + $this->userManager->method('countUsersTotal') + ->willReturn(5000); + + $this->config->expects($this->once()) + ->method('setAppValue') + ->with('dav', 'system_addressbook_exposed', 'no'); + + $clientService = $this->createMock(IClientService::class); + + $service = $this->getMockBuilder(SyncService::class) + ->setConstructorArgs([ + $this->backend, + $this->userManager, + $this->dbConnection, + $this->logger, + $this->converter, + $clientService, + $this->config + ]) + ->onlyMethods(['getLocalSystemAddressBook']) + ->getMock(); + + $service->expects($this->never()) + ->method('getLocalSystemAddressBook'); + + $service->syncInstance(); + } }