1414use OCA \DAV \CardDAV \CardDavBackend ;
1515use OCA \DAV \CardDAV \Converter ;
1616use OCA \DAV \CardDAV \SyncService ;
17+ use OCP \AppFramework \Services \IAppConfig ;
1718use OCP \Http \Client \IClient ;
1819use OCP \Http \Client \IClientService ;
1920use OCP \IConfig ;
@@ -36,30 +37,35 @@ class SyncServiceTest extends TestCase {
3637 protected Converter &MockObject $ converter ;
3738 protected IClient &MockObject $ client ;
3839 protected IConfig &MockObject $ config ;
40+ protected IAppConfig &MockObject $ appConfig ;
3941 protected SyncService $ service ;
4042
4143 public function setUp (): void {
4244 parent ::setUp ();
4345
46+ $ this ->backend = $ this ->createMock (CardDavBackend::class);
4447 $ addressBook = [
4548 'id ' => 1 ,
4649 'uri ' => 'system ' ,
4750 'principaluri ' => 'principals/system/system ' ,
4851 '{DAV:}displayname ' => 'system ' ,
4952 // watch out, incomplete address book mock.
5053 ];
51-
52- $ this ->backend = $ this ->createMock (CardDavBackend::class);
5354 $ this ->backend ->method ('getAddressBooksByUri ' )
54- ->with ('principals/system/system ' , 1 )
55- ->willReturn ($ addressBook );
55+ ->willReturnCallback (function ($ principal , $ idOrUri ) use ($ addressBook ) {
56+ if ($ principal === 'principals/system/system ' && ($ idOrUri === 'system ' || $ idOrUri === 1 || $ idOrUri === '1 ' )) {
57+ return $ addressBook ;
58+ }
59+ return null ;
60+ });
5661
5762 $ this ->userManager = $ this ->createMock (IUserManager::class);
5863 $ this ->dbConnection = $ this ->createMock (IDBConnection::class);
5964 $ this ->logger = new NullLogger ();
6065 $ this ->converter = $ this ->createMock (Converter::class);
6166 $ this ->client = $ this ->createMock (IClient::class);
6267 $ this ->config = $ this ->createMock (IConfig::class);
68+ $ this ->appConfig = $ this ->createMock (IAppConfig::class);
6369
6470 $ clientService = $ this ->createMock (IClientService::class);
6571 $ clientService ->method ('newClient ' )
@@ -72,7 +78,8 @@ public function setUp(): void {
7278 $ this ->logger ,
7379 $ this ->converter ,
7480 $ clientService ,
75- $ this ->config
81+ $ this ->config ,
82+ $ this ->appConfig
7683 );
7784 }
7885
@@ -313,8 +320,9 @@ public function testEnsureSystemAddressBookExists(): void {
313320 $ converter = $ this ->createMock (Converter::class);
314321 $ clientService = $ this ->createMock (IClientService::class);
315322 $ config = $ this ->createMock (IConfig::class);
323+ $ appConfig = $ this ->createMock (IAppConfig::class);
316324
317- $ ss = new SyncService ($ backend , $ userManager , $ dbConnection , $ logger , $ converter , $ clientService , $ config );
325+ $ ss = new SyncService ($ backend , $ userManager , $ dbConnection , $ logger , $ converter , $ clientService , $ config, $ appConfig );
318326 $ ss ->ensureSystemAddressBookExists ('principals/users/adam ' , 'contacts ' , []);
319327 }
320328
@@ -360,8 +368,9 @@ public function testUpdateAndDeleteUser(bool $activated, int $createCalls, int $
360368
361369 $ clientService = $ this ->createMock (IClientService::class);
362370 $ config = $ this ->createMock (IConfig::class);
371+ $ appConfig = $ this ->createMock (IAppConfig::class);
363372
364- $ ss = new SyncService ($ backend , $ userManager , $ dbConnection , $ logger , $ converter , $ clientService , $ config );
373+ $ ss = new SyncService ($ backend , $ userManager , $ dbConnection , $ logger , $ converter , $ clientService , $ config, $ appConfig );
365374 $ ss ->updateUser ($ user );
366375
367376 $ ss ->updateUser ($ user );
@@ -402,8 +411,9 @@ public function testDeleteAddressbookWhenAccessRevoked(): void {
402411 $ message = 'Client error: `REPORT https://server2.internal/cloud/remote.php/dav/addressbooks/system/system/system` resulted in a `401 Unauthorized` response:
403412<?xml version="1.0" encoding="utf-8"?>
404413<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
405- <s:exception>Sabre\DA (truncated...)
406- ' ;
414+ <s:exception>Sabre\DAV\Exception\NotAuthenticated</s:exception>
415+ <s:message>No public access to this resource., Username or password was incorrect, No \'Authorization: Bearer \' header found. Either the client didn \'t send one, or the server is mis-configured, Username or password was incorrect</s:message>
416+ </d:error> ' ;
407417
408418 $ reportException = new ClientException (
409419 $ message ,
@@ -481,4 +491,102 @@ public static function providerUseAbsoluteUriReport(): array {
481491 ['https://server.internal:8080/nextcloud/ ' , 'https://server.internal:8080/nextcloud/remote.php/dav/addressbooks/system/system/system ' ],
482492 ];
483493 }
494+
495+ public function testUpdateUserDisablesSystemAddressBookWhenLimitReached (): void {
496+ $ this ->appConfig ->method ('getAppValueBool ' )
497+ ->with ('system_addressbook_exposed ' , true )
498+ ->willReturn (true );
499+ $ this ->appConfig ->method ('getAppValueInt ' )
500+ ->with ('system_addressbook_limit ' , 5000 )
501+ ->willReturn (2 );
502+ $ this ->userManager ->method ('countSeenUsers ' )->willReturn (2 );
503+ $ this ->appConfig ->expects ($ this ->once ())
504+ ->method ('setAppValueBool ' )
505+ ->with ('system_addressbook_exposed ' , false );
506+ $ user = $ this ->createMock (IUser::class);
507+ $ user ->method ('isEnabled ' )->willReturn (false );
508+ $ user ->method ('getBackendClassName ' )->willReturn ('unittest ' );
509+ $ user ->method ('getUID ' )->willReturn ('test-user ' );
510+ $ this ->backend ->method ('getAddressBooksByUri ' )
511+ ->with ('principals/system/system ' , 'system ' )
512+ ->willReturn (['id ' => 1 ]);
513+ $ this ->backend ->expects ($ this ->once ())->method ('deleteCard ' );
514+ $ this ->service ->updateUser ($ user );
515+ }
516+
517+ public function testUpdateUserCreatesCardIfNotExistsAndEnabled (): void {
518+ $ this ->appConfig ->method ('getAppValueBool ' )->willReturn (true );
519+ $ this ->appConfig ->method ('getAppValueInt ' )->willReturn (5000 );
520+ $ user = $ this ->createMock (IUser::class);
521+ $ user ->method ('isEnabled ' )->willReturn (true );
522+ $ user ->method ('getBackendClassName ' )->willReturn ('unittest ' );
523+ $ user ->method ('getUID ' )->willReturn ('test-user ' );
524+ $ this ->backend ->method ('getAddressBooksByUri ' )
525+ ->with ('principals/system/system ' , 'system ' )
526+ ->willReturn (['id ' => 1 ]);
527+ $ this ->backend ->method ('getCard ' )->willReturn (false );
528+ $ vCard = $ this ->createMock (VCard::class);
529+ $ vCard ->method ('serialize ' )->willReturn ('VCARD-DATA ' );
530+ $ this ->converter ->method ('createCardFromUser ' )->willReturn ($ vCard );
531+ $ this ->backend ->expects ($ this ->once ())
532+ ->method ('createCard ' )
533+ ->with (1 , 'unittest:test-user.vcf ' , 'VCARD-DATA ' , false );
534+ $ this ->service ->updateUser ($ user );
535+ }
536+
537+ public function testUpdateUserUpdatesCardIfExistsAndEnabled (): void {
538+ $ this ->appConfig ->method ('getAppValueBool ' )->willReturn (true );
539+ $ this ->appConfig ->method ('getAppValueInt ' )->willReturn (5000 );
540+ $ user = $ this ->createMock (IUser::class);
541+ $ user ->method ('isEnabled ' )->willReturn (true );
542+ $ user ->method ('getBackendClassName ' )->willReturn ('unittest ' );
543+ $ user ->method ('getUID ' )->willReturn ('test-user ' );
544+ $ this ->backend ->method ('getAddressBooksByUri ' )
545+ ->with ('principals/system/system ' , 'system ' )
546+ ->willReturn (['id ' => 1 ]);
547+ $ this ->backend ->method ('getCard ' )->willReturn (['carddata ' => 'foo ' ]);
548+ $ vCard = $ this ->createMock (VCard::class);
549+ $ vCard ->method ('serialize ' )->willReturn ('VCARD-DATA ' );
550+ $ this ->converter ->method ('createCardFromUser ' )->willReturn ($ vCard );
551+ $ this ->backend ->expects ($ this ->once ())
552+ ->method ('updateCard ' )
553+ ->with (1 , 'unittest:test-user.vcf ' , 'VCARD-DATA ' );
554+ $ this ->service ->updateUser ($ user );
555+ }
556+
557+ public function testUpdateUserDeletesCardIfConverterReturnsNull (): void {
558+ $ this ->appConfig ->method ('getAppValueBool ' )->willReturn (true );
559+ $ this ->appConfig ->method ('getAppValueInt ' )->willReturn (5000 );
560+ $ user = $ this ->createMock (IUser::class);
561+ $ user ->method ('isEnabled ' )->willReturn (true );
562+ $ user ->method ('getBackendClassName ' )->willReturn ('unittest ' );
563+ $ user ->method ('getUID ' )->willReturn ('test-user ' );
564+ $ this ->backend ->method ('getAddressBooksByUri ' )
565+ ->with ('principals/system/system ' , 'system ' )
566+ ->willReturn (['id ' => 1 ]);
567+ $ this ->backend ->method ('getCard ' )->willReturn (['carddata ' => 'foo ' ]);
568+ $ this ->converter ->method ('createCardFromUser ' )->willReturn (null );
569+ $ this ->backend ->expects ($ this ->once ())
570+ ->method ('deleteCard ' )
571+ ->with (1 , 'unittest:test-user.vcf ' );
572+ $ this ->service ->updateUser ($ user );
573+ }
574+
575+ public function testUpdateUserDeletesCardIfUserDisabled (): void {
576+ $ this ->appConfig ->method ('getAppValueBool ' )->willReturn (true );
577+ $ this ->appConfig ->method ('getAppValueInt ' )->willReturn (5000 );
578+ $ user = $ this ->createMock (IUser::class);
579+ $ user ->method ('isEnabled ' )->willReturn (false );
580+ $ user ->method ('getBackendClassName ' )->willReturn ('unittest ' );
581+ $ user ->method ('getUID ' )->willReturn ('test-user ' );
582+ $ this ->backend ->method ('getAddressBooksByUri ' )
583+ ->with ('principals/system/system ' , 'system ' )
584+ ->willReturn (['id ' => 1 ]);
585+ $ this ->backend ->expects ($ this ->once ())
586+ ->method ('deleteCard ' )
587+ ->with (1 , 'unittest:test-user.vcf ' );
588+ $ this ->service ->updateUser ($ user );
589+ }
590+
591+
484592}
0 commit comments