Skip to content

Commit

Permalink
fixup! fixup! fix: automatically disable sab
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
  • Loading branch information
SebastianKrupinski committed Feb 12, 2025
1 parent 9424b41 commit f81af1e
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/dav/lib/CardDAV/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
163 changes: 157 additions & 6 deletions apps/dav/tests/unit/CardDAV/SyncServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
}

0 comments on commit f81af1e

Please sign in to comment.