diff options
author | SebastianKrupinski <krupinskis05@gmail.com> | 2025-01-31 21:05:20 -0500 |
---|---|---|
committer | SebastianKrupinski <krupinskis05@gmail.com> | 2025-03-03 19:21:26 -0500 |
commit | 5d5e11b79b6abe92173dac9595d9aa11059673cf (patch) | |
tree | 8c287b4187bf98e207c14e1bdce3c0388896db9c | |
parent | 2582a55300ac83a806d79499c9bbe7db4e310aee (diff) | |
download | nextcloud-server-fix/49431-automatically-disable-sab.tar.gz nextcloud-server-fix/49431-automatically-disable-sab.zip |
fix: automatically disable sabfix/49431-automatically-disable-sab
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
-rw-r--r-- | apps/dav/lib/CardDAV/SyncService.php | 9 | ||||
-rw-r--r-- | apps/dav/tests/unit/CardDAV/SyncServiceTest.php | 266 |
2 files changed, 269 insertions, 6 deletions
diff --git a/apps/dav/lib/CardDAV/SyncService.php b/apps/dav/lib/CardDAV/SyncService.php index cc3d324faf1..3d83e053a5f 100644 --- a/apps/dav/lib/CardDAV/SyncService.php +++ b/apps/dav/lib/CardDAV/SyncService.php @@ -288,6 +288,15 @@ class SyncService { * @return void */ public function syncInstance(?\Closure $progressCallback = null) { + + if ($this->config->getAppValue('dav', 'system_addressbook_exposed', 'none') === 'none') { + $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; + } + } + $systemAddressBook = $this->getLocalSystemAddressBook(); $this->userManager->callForAllUsers(function ($user) use ($systemAddressBook, $progressCallback): void { $this->updateUser($user); diff --git a/apps/dav/tests/unit/CardDAV/SyncServiceTest.php b/apps/dav/tests/unit/CardDAV/SyncServiceTest.php index 5af42e2ea4e..7da39c4f9fd 100644 --- a/apps/dav/tests/unit/CardDAV/SyncServiceTest.php +++ b/apps/dav/tests/unit/CardDAV/SyncServiceTest.php @@ -20,6 +20,7 @@ use OCP\IConfig; 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 @@ use Test\TestCase; 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,256 @@ END:VCARD'; ['https://server.internal:8080/nextcloud/', 'https://server.internal:8080/nextcloud/remote.php/dav/addressbooks/system/system/system'], ]; } + + public function testSyncInstanceWithUsersUnderLimit(): void { + + $this->config->method('getAppValue') + ->willReturnMap([ + ['dav', 'system_addressbook_exposed', 'none', 'none'], + ['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 testSyncInstanceWithWithUsersOverLimit(): void { + $this->config->method('getAppValue') + ->willReturnMap([ + ['dav', 'system_addressbook_exposed', 'none', 'none'], + ['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(); + } + + public function testSyncInstanceWithProgressCallback(): void { + $this->config->method('getAppValue') + ->willReturnMap([ + ['dav', 'system_addressbook_exposed', 'none', 'none'], + ['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 testSyncInstanceWithExposeYes() { + + $this->config->method('getAppValue') + ->willReturnMap([ + ['dav', 'system_addressbook_exposed', 'none', 'yes'] + ]); + + $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 testSyncInstanceWithExposeNo() { + + $this->config->method('getAppValue') + ->willReturnMap([ + ['dav', 'system_addressbook_exposed', 'none', 'no'] + ]); + + $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(); + + } + } |