123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476 |
- <?php
- namespace OCA\User_LDAP\Tests;
- use OC\User\Backend;
- use OCA\User_LDAP\Access;
- use OCA\User_LDAP\Connection;
- use OCA\User_LDAP\Mapping\AbstractMapping;
- use OCA\User_LDAP\Mapping\UserMapping;
- use OCA\User_LDAP\User\DeletedUsersIndex;
- use OCA\User_LDAP\User\Manager;
- use OCA\User_LDAP\User\OfflineUser;
- use OCA\User_LDAP\User\User;
- use OCA\User_LDAP\User_LDAP;
- use OCA\User_LDAP\User_LDAP as UserLDAP;
- use OCA\User_LDAP\UserPluginManager;
- use OCP\HintException;
- use OCP\IUser;
- use OCP\Notification\IManager as INotificationManager;
- use PHPUnit\Framework\MockObject\MockObject;
- use Psr\Log\LoggerInterface;
- use Test\TestCase;
- class User_LDAPTest extends TestCase {
-
- protected $backend;
-
- protected $access;
-
- protected $offlineUser;
-
- protected $notificationManager;
-
- protected $pluginManager;
-
- protected $connection;
-
- protected $userManager;
-
- protected $logger;
-
- protected $deletedUsersIndex;
- protected function setUp(): void {
- parent::setUp();
- \OC_User::clearBackends();
- \OC::$server->getGroupManager()->clearBackends();
- $this->connection = $this->createMock(Connection::class);
- $this->userManager = $this->createMock(Manager::class);
- $this->access = $this->createMock(Access::class);
- $this->access->connection = $this->connection;
- $this->access->userManager = $this->userManager;
- $this->notificationManager = $this->createMock(INotificationManager::class);
- $this->pluginManager = $this->createMock(UserPluginManager::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->deletedUsersIndex = $this->createMock(DeletedUsersIndex::class);
- $this->backend = new User_LDAP(
- $this->access,
- $this->notificationManager,
- $this->pluginManager,
- $this->logger,
- $this->deletedUsersIndex,
- );
- }
- private function prepareMockForUserExists() {
- $this->access->expects($this->any())
- ->method('username2dn')
- ->willReturnCallback(function ($uid) {
- switch ($uid) {
- case 'gunslinger':
- return 'dnOfRoland,dc=test';
- break;
- case 'formerUser':
- return 'dnOfFormerUser,dc=test';
- break;
- case 'newyorker':
- return 'dnOfNewYorker,dc=test';
- break;
- case 'ladyofshadows':
- return 'dnOfLadyOfShadows,dc=test';
- break;
- default:
- return false;
- }
- });
- $this->access->method('fetchUsersByLoginName')
- ->willReturn([]);
- }
-
- private function prepareAccessForCheckPassword($noDisplayName = false) {
- $this->connection->expects($this->any())
- ->method('__get')
- ->willReturnCallback(function ($name) {
- if ($name === 'ldapLoginFilter') {
- return '%uid';
- }
- return null;
- });
- $this->access->expects($this->any())
- ->method('fetchListOfUsers')
- ->willReturnCallback(function ($filter) {
- if ($filter === 'roland') {
- return [['dn' => ['dnOfRoland,dc=test']]];
- }
- return [];
- });
- $this->access->expects($this->any())
- ->method('fetchUsersByLoginName')
- ->willReturnCallback(function ($uid) {
- if ($uid === 'roland') {
- return [['dn' => ['dnOfRoland,dc=test']]];
- }
- return [];
- });
- $retVal = 'gunslinger';
- if ($noDisplayName === true) {
- $retVal = false;
- }
- $this->access->expects($this->any())
- ->method('dn2username')
- ->with($this->equalTo('dnOfRoland,dc=test'))
- ->willReturn($retVal);
- $this->access->expects($this->any())
- ->method('username2dn')
- ->with($this->equalTo('gunslinger'))
- ->willReturn('dnOfRoland,dc=test');
- $this->access->expects($this->any())
- ->method('stringResemblesDN')
- ->with($this->equalTo('dnOfRoland,dc=test'))
- ->willReturn(true);
- $this->access->expects($this->any())
- ->method('areCredentialsValid')
- ->willReturnCallback(function ($dn, $pwd) {
- if ($pwd === 'dt19') {
- return true;
- }
- return false;
- });
- $this->userManager->expects($this->any())
- ->method('getAttributes')
- ->willReturn(['dn', 'uid', 'mail', 'displayname']);
- }
- public function testCheckPasswordUidReturn(): void {
- $user = $this->createMock(User::class);
- $user->expects($this->any())
- ->method('getUsername')
- ->willReturn('gunslinger');
- $this->prepareAccessForCheckPassword();
- $this->userManager->expects($this->any())
- ->method('get')
- ->willReturn($user);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $backend->checkPassword('roland', 'dt19');
- $this->assertEquals('gunslinger', $result);
- }
- public function testCheckPasswordWrongPassword(): void {
- $this->prepareAccessForCheckPassword();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $backend->checkPassword('roland', 'wrong');
- $this->assertFalse($result);
- }
- public function testCheckPasswordWrongUser(): void {
- $this->prepareAccessForCheckPassword();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $backend->checkPassword('mallory', 'evil');
- $this->assertFalse($result);
- }
- public function testCheckPasswordNoDisplayName(): void {
- $this->prepareAccessForCheckPassword(true);
- $this->prepareAccessForCheckPassword();
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->willReturn(null);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $backend->checkPassword('roland', 'dt19');
- $this->assertFalse($result);
- }
- public function testCheckPasswordPublicAPI(): void {
- $user = $this->createMock(User::class);
- $user->expects($this->any())
- ->method('getUsername')
- ->willReturn('gunslinger');
- $this->prepareAccessForCheckPassword();
- $this->userManager->expects($this->any())
- ->method('get')
- ->willReturn($user);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $user = \OC::$server->getUserManager()->checkPassword('roland', 'dt19');
- $result = false;
- if ($user !== false) {
- $result = $user->getUID();
- }
- $this->assertEquals('gunslinger', $result);
- }
- public function testCheckPasswordPublicAPIWrongPassword(): void {
- $this->prepareAccessForCheckPassword();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $user = \OC::$server->getUserManager()->checkPassword('roland', 'wrong');
- $result = false;
- if ($user !== false) {
- $result = $user->getUID();
- }
- $this->assertFalse($result);
- }
- public function testCheckPasswordPublicAPIWrongUser(): void {
- $this->prepareAccessForCheckPassword();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $user = \OC::$server->getUserManager()->checkPassword('mallory', 'evil');
- $result = false;
- if ($user !== false) {
- $result = $user->getUID();
- }
- $this->assertFalse($result);
- }
- public function testDeleteUserCancel(): void {
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->deleteUser('notme');
- $this->assertFalse($result);
- }
- public function testDeleteUserSuccess(): void {
- $uid = 'jeremy';
- $home = '/var/vhome/jdings/';
- $mapping = $this->createMock(UserMapping::class);
- $mapping->expects($this->once())
- ->method('unmap')
- ->willReturn(true);
- $this->access->expects($this->once())
- ->method('getUserMapper')
- ->willReturn($mapping);
- $this->connection->expects($this->any())
- ->method('getConnectionResource')
- ->willReturn(ldap_connect('ldap://example.com'));
- $this->deletedUsersIndex->expects($this->once())
- ->method('isUserMarked')
- ->with($uid)
- ->willReturn(true);
- $offlineUser = $this->createMock(OfflineUser::class);
- $offlineUser->expects($this->once())
- ->method('getHomePath')
- ->willReturn($home);
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->willReturn($offlineUser);
- $this->userManager->expects($this->once())
- ->method('exists')
- ->with($uid)
- ->willReturn(true);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->deleteUser($uid);
- $this->assertTrue($result);
-
- $this->assertSame($backend->getHome($uid), $home);
- }
- public function testDeleteUserWithPlugin(): void {
- $this->pluginManager->expects($this->once())
- ->method('canDeleteUser')
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('deleteUser')
- ->with('uid')
- ->willReturn(true);
- $this->deletedUsersIndex->expects($this->once())
- ->method('isUserMarked')
- ->with('uid')
- ->willReturn(true);
- $mapper = $this->createMock(UserMapping::class);
- $mapper->expects($this->once())
- ->method('unmap')
- ->with('uid');
- $this->access->expects($this->atLeastOnce())
- ->method('getUserMapper')
- ->willReturn($mapper);
- $this->userManager->expects($this->once())
- ->method('invalidate')
- ->with('uid');
- $this->assertEquals(true, $this->backend->deleteUser('uid'));
- }
-
- private function prepareAccessForGetUsers() {
- $this->access->expects($this->once())
- ->method('escapeFilterPart')
- ->willReturnCallback(function ($search) {
- return $search;
- });
- $this->access->expects($this->any())
- ->method('getFilterPartForUserSearch')
- ->willReturnCallback(function ($search) {
- return $search;
- });
- $this->access->expects($this->any())
- ->method('combineFilterWithAnd')
- ->willReturnCallback(function ($param) {
- return $param[2];
- });
- $this->access->expects($this->any())
- ->method('fetchListOfUsers')
- ->willReturnCallback(function ($search, $a, $l, $o) {
- $users = ['gunslinger', 'newyorker', 'ladyofshadows'];
- if (empty($search)) {
- $result = $users;
- } else {
- $result = [];
- foreach ($users as $user) {
- if (stripos($user, $search) !== false) {
- $result[] = $user;
- }
- }
- }
- if (!is_null($l) || !is_null($o)) {
- $result = array_slice($result, $o, $l);
- }
- return $result;
- });
- $this->access->expects($this->any())
- ->method('nextcloudUserNames')
- ->willReturnArgument(0);
- $this->access->method('fetchUsersByLoginName')
- ->willReturn([]);
- $this->access->userManager->expects($this->any())
- ->method('getAttributes')
- ->willReturn(['dn', 'uid', 'mail', 'displayname']);
- }
- public function testGetUsersNoParam(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->getUsers();
- $this->assertEquals(3, count($result));
- }
- public function testGetUsersLimitOffset(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->getUsers('', 1, 2);
- $this->assertEquals(1, count($result));
- }
- public function testGetUsersLimitOffset2(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->getUsers('', 2, 1);
- $this->assertEquals(2, count($result));
- }
- public function testGetUsersSearchWithResult(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->getUsers('yo');
- $this->assertEquals(2, count($result));
- }
- public function testGetUsersSearchEmptyResult(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->getUsers('nix');
- $this->assertEquals(0, count($result));
- }
- private function getUsers($search = '', $limit = null, $offset = null) {
- $users = \OC::$server->getUserManager()->search($search, $limit, $offset);
- $uids = array_map(function (IUser $user) {
- return $user->getUID();
- }, $users);
- return $uids;
- }
- public function testGetUsersViaAPINoParam(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $this->getUsers();
- $this->assertEquals(3, count($result));
- }
- public function testGetUsersViaAPILimitOffset(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $this->getUsers('', 1, 2);
- $this->assertEquals(1, count($result));
- }
- public function testGetUsersViaAPILimitOffset2(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $this->getUsers('', 2, 1);
- $this->assertEquals(2, count($result));
- }
- public function testGetUsersViaAPISearchWithResult(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $this->getUsers('yo');
- $this->assertEquals(2, count($result));
- }
- public function testGetUsersViaAPISearchEmptyResult(): void {
- $this->prepareAccessForGetUsers();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $result = $this->getUsers('nix');
- $this->assertEquals(0, count($result));
- }
- public function testUserExists(): void {
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $user = $this->createMock(User::class);
- $this->userManager->expects($this->never())
- ->method('get');
- $this->userManager->expects($this->once())
- ->method('exists')
- ->with('gunslinger')
- ->willReturn(true);
- $this->access->expects($this->any())
- ->method('getUserMapper')
- ->willReturn($this->createMock(UserMapping::class));
-
-
- $result = $backend->userExists('gunslinger');
- $this->assertTrue($result);
- }
- public function testUserExistsForDeleted(): void {
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $mapper = $this->createMock(UserMapping::class);
- $mapper->expects($this->any())
- ->method('getUUIDByDN')
- ->with('dnOfFormerUser,dc=test')
- ->willReturn('45673458748');
- $this->access->expects($this->any())
- ->method('getUserMapper')
- ->willReturn($mapper);
- $this->userManager->expects($this->never())
- ->method('get');
- $this->userManager->expects($this->once())
- ->method('exists')
- ->with('formerUser')
- ->willReturn(true);
-
- $this->assertTrue($backend->userExists('formerUser'));
- }
- public function testUserExistsForNeverExisting(): void {
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $this->access->expects($this->any())
- ->method('readAttribute')
- ->willReturnCallback(function ($dn) {
- if ($dn === 'dnOfRoland,dc=test') {
- return [];
- }
- return false;
- });
-
-
- $result = $backend->userExists('mallory');
- $this->assertFalse($result);
- }
- public function testUserExistsPublicAPI(): void {
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- \OC_User::useBackend($backend);
- $user = $this->createMock(User::class);
- $user->expects($this->any())
- ->method('getDN')
- ->willReturn('dnOfRoland,dc=test');
- $this->access->expects($this->any())
- ->method('readAttribute')
- ->willReturnCallback(function ($dn) {
- if ($dn === 'dnOfRoland,dc=test') {
- return [];
- }
- return false;
- });
- $this->userManager->expects($this->never())
- ->method('get');
- $this->userManager->expects($this->once())
- ->method('exists')
- ->with('gunslinger')
- ->willReturn(true);
- $this->access->expects($this->any())
- ->method('getUserMapper')
- ->willReturn($this->createMock(UserMapping::class));
-
- $result = \OC::$server->getUserManager()->userExists('gunslinger');
- $this->assertTrue($result);
- }
- public function testDeleteUserExisting(): void {
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
-
- $result = $backend->deleteUser('gunslinger');
- $this->assertFalse($result);
- }
- public function testGetHomeAbsolutePath(): void {
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $this->connection->expects($this->any())
- ->method('__get')
- ->willReturnCallback(function ($name) {
- if ($name === 'homeFolderNamingRule') {
- return 'attr:testAttribute';
- }
- return null;
- });
- $this->access->expects($this->any())
- ->method('readAttribute')
- ->willReturnCallback(function ($dn, $attr) {
- switch ($dn) {
- case 'dnOfRoland,dc=test':
- if ($attr === 'testAttribute') {
- return ['/tmp/rolandshome/'];
- }
- return [];
- break;
- default:
- return false;
- }
- });
- $user = $this->createMock(User::class);
- $user->expects($this->any())
- ->method('getUsername')
- ->willReturn('gunslinger');
- $user->expects($this->any())
- ->method('getDN')
- ->willReturn('dnOfRoland,dc=test');
- $user->expects($this->any())
- ->method('getHomePath')
- ->willReturn('/tmp/rolandshome/');
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->with('gunslinger')
- ->willReturn($user);
- $this->userManager->expects($this->once())
- ->method('exists')
- ->with('gunslinger')
- ->willReturn(true);
-
-
- $result = $backend->getHome('gunslinger');
- $this->assertEquals('/tmp/rolandshome/', $result);
- }
- public function testGetHomeRelative(): void {
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $dataDir = \OC::$server->getConfig()->getSystemValue(
- 'datadirectory', \OC::$SERVERROOT . '/data');
- $this->connection->expects($this->any())
- ->method('__get')
- ->willReturnCallback(function ($name) {
- if ($name === 'homeFolderNamingRule') {
- return 'attr:testAttribute';
- }
- return null;
- });
- $this->access->expects($this->any())
- ->method('readAttribute')
- ->willReturnCallback(function ($dn, $attr) {
- switch ($dn) {
- case 'dnOfLadyOfShadows,dc=test':
- if ($attr === 'testAttribute') {
- return ['susannah/'];
- }
- return [];
- break;
- default:
- return false;
- }
- });
- $user = $this->createMock(User::class);
- $user->expects($this->any())
- ->method('getUsername')
- ->willReturn('ladyofshadows');
- $user->expects($this->any())
- ->method('getDN')
- ->willReturn('dnOfLadyOfShadows,dc=test');
- $user->expects($this->any())
- ->method('getHomePath')
- ->willReturn($dataDir . '/susannah/');
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->willReturn($user);
- $this->userManager->expects($this->once())
- ->method('exists')
- ->with('ladyofshadows')
- ->willReturn(true);
-
- $result = $backend->getHome('ladyofshadows');
- $this->assertEquals($dataDir . '/susannah/', $result);
- }
- public function testGetHomeNoPath(): void {
- $this->expectException(\Exception::class);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $this->connection->expects($this->any())
- ->method('__get')
- ->willReturnCallback(function ($name) {
- if ($name === 'homeFolderNamingRule') {
- return 'attr:testAttribute';
- }
- return null;
- });
- $this->access->expects($this->any())
- ->method('readAttribute')
- ->willReturnCallback(function ($dn, $attr) {
- switch ($dn) {
- default:
- return false;
- }
- });
- $user = $this->createMock(User::class);
- $user->expects($this->any())
- ->method('getUsername')
- ->willReturn('newyorker');
- $user->expects($this->any())
- ->method('getHomePath')
- ->willThrowException(new \Exception());
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->with('newyorker')
- ->willReturn($user);
- $this->userManager->expects($this->once())
- ->method('exists')
- ->with('newyorker')
- ->willReturn(true);
-
- $result = $backend->getHome('newyorker');
- $this->assertFalse($result);
- }
- public function testGetHomeDeletedUser(): void {
- $uid = 'newyorker';
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $this->connection->expects($this->any())
- ->method('__get')
- ->willReturnCallback(function ($name) {
- if ($name === 'homeFolderNamingRule') {
- return 'attr:testAttribute';
- }
- return null;
- });
- $this->access->expects($this->any())
- ->method('readAttribute')
- ->willReturn([]);
- $userMapper = $this->createMock(UserMapping::class);
- $this->access->expects($this->any())
- ->method('getUserMapper')
- ->willReturn($userMapper);
- $offlineUser = $this->createMock(OfflineUser::class);
- $offlineUser->expects($this->atLeastOnce())
- ->method('getHomePath')
- ->willReturn('');
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->with($uid)
- ->willReturn($offlineUser);
- $this->userManager->expects($this->once())
- ->method('exists')
- ->with($uid)
- ->willReturn(true);
- $result = $backend->getHome($uid);
- $this->assertFalse($result);
- }
- public function testGetHomeWithPlugin(): void {
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::GET_HOME)
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('getHome')
- ->with('uid')
- ->willReturn('result');
- $this->connection->expects($this->any())
- ->method('getFromCache')
- ->willReturnCallback(function ($uid) {
- return true;
- });
-
- $this->assertEquals($this->backend->getHome('uid'), 'result');
- }
- private function prepareAccessForGetDisplayName() {
- $this->connection->expects($this->any())
- ->method('__get')
- ->willReturnCallback(function ($name) {
- if ($name === 'ldapUserDisplayName') {
- return 'displayname';
- } elseif ($name === 'ldapUserDisplayName2') {
- return 'displayname2';
- }
- return null;
- });
- $this->access->expects($this->any())
- ->method('readAttribute')
- ->willReturnCallback(function ($dn, $attr) {
- switch ($dn) {
- case 'dnOfRoland,dc=test':
- if ($attr === 'displayname') {
- return ['Roland Deschain'];
- }
- return [];
- break;
- default:
- return false;
- }
- });
- $this->access->method('fetchUsersByLoginName')
- ->willReturn([]);
- }
- public function testGetDisplayName(): void {
- $this->prepareAccessForGetDisplayName();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $this->connection->expects($this->any())
- ->method('getConnectionResource')
- ->willReturnCallback(function () {
- return true;
- });
- $user1 = $this->createMock(User::class);
- $user1->expects($this->once())
- ->method('composeAndStoreDisplayName')
- ->willReturn('Roland Deschain');
- $user1->expects($this->any())
- ->method('getDN')
- ->willReturn('dnOfRoland,dc=test');
- $user2 = $this->createMock(User::class);
- $user2->expects($this->never())
- ->method('composeAndStoreDisplayName');
- $user2->expects($this->any())
- ->method('getDN')
- ->willReturn('another DN');
- $mapper = $this->createMock(UserMapping::class);
- $mapper->expects($this->any())
- ->method('getUUIDByDN')
- ->willReturnCallback(function ($dn) {
- return $dn;
- });
- $this->userManager->expects($this->any())
- ->method('get')
- ->willReturnCallback(function ($uid) use ($user1, $user2) {
- if ($uid === 'gunslinger') {
- return $user1;
- } elseif ($uid === 'newyorker') {
- return $user2;
- }
- return null;
- });
- $this->userManager->expects($this->any())
- ->method('exists')
- ->willReturnCallback(function ($uid) use ($user1, $user2) {
- if ($uid === 'gunslinger') {
- return true;
- } elseif ($uid === 'newyorker') {
- return true;
- }
- return false;
- });
- $this->access->expects($this->any())
- ->method('getUserMapper')
- ->willReturn($mapper);
- $this->access->expects($this->any())
- ->method('getUserDnByUuid')
- ->willReturnCallback(function ($uuid) {
- return $uuid . '1';
- });
-
- $result = $backend->getDisplayName('gunslinger');
- $this->assertEquals('Roland Deschain', $result);
-
- $result = $backend->getDisplayName('newyorker');
- $this->assertEquals(null, $result);
- }
- public function testGetDisplayNamePublicAPI(): void {
- $this->access->expects($this->any())
- ->method('username2dn')
- ->willReturnCallback(function ($uid) {
- switch ($uid) {
- case 'gunslinger':
- return 'dnOfRoland,dc=test';
- break;
- case 'formerUser':
- return 'dnOfFormerUser,dc=test';
- break;
- case 'newyorker':
- return 'dnOfNewYorker,dc=test';
- break;
- case 'ladyofshadows':
- return 'dnOfLadyOfShadows,dc=test';
- break;
- default:
- return false;
- }
- });
- $this->prepareAccessForGetDisplayName();
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->prepareMockForUserExists();
- $this->connection->expects($this->any())
- ->method('getConnectionResource')
- ->willReturnCallback(function () {
- return true;
- });
- \OC_User::useBackend($backend);
- $user1 = $this->createMock(User::class);
- $user1->expects($this->once())
- ->method('composeAndStoreDisplayName')
- ->willReturn('Roland Deschain');
- $user1->expects($this->any())
- ->method('getDN')
- ->willReturn('dnOfRoland,dc=test');
- $user2 = $this->createMock(User::class);
- $user2->expects($this->never())
- ->method('composeAndStoreDisplayName');
- $user2->expects($this->any())
- ->method('getDN')
- ->willReturn('another DN');
- $mapper = $this->createMock(UserMapping::class);
- $mapper->expects($this->any())
- ->method('getUUIDByDN')
- ->willReturnCallback(function ($dn) {
- return $dn;
- });
- $this->userManager->expects($this->any())
- ->method('get')
- ->willReturnCallback(function ($uid) use ($user1, $user2) {
- if ($uid === 'gunslinger') {
- return $user1;
- } elseif ($uid === 'newyorker') {
- return $user2;
- }
- return null;
- });
- $this->userManager->expects($this->any())
- ->method('exists')
- ->willReturnCallback(function ($uid) use ($user1, $user2) {
- if ($uid === 'gunslinger') {
- return true;
- } elseif ($uid === 'newyorker') {
- return true;
- }
- return false;
- });
- $this->access->expects($this->any())
- ->method('getUserMapper')
- ->willReturn($mapper);
- $this->access->expects($this->any())
- ->method('getUserDnByUuid')
- ->willReturnCallback(function ($uuid) {
- return $uuid . '1';
- });
-
- $result = \OC::$server->getUserManager()->get('gunslinger')?->getDisplayName();
- $this->assertEquals('Roland Deschain', $result);
-
- $result = \OC::$server->getUserManager()->get('newyorker') === null ? 'newyorker' : \OC::$server->getUserManager()->get('newyorker')->getDisplayName();
- $this->assertEquals('newyorker', $result);
- }
- public function testGetDisplayNameWithPlugin(): void {
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::GET_DISPLAYNAME)
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('getDisplayName')
- ->with('uid')
- ->willReturn('result');
- $this->assertEquals($this->backend->getDisplayName('uid'), 'result');
- }
-
-
- public function testCountUsers(): void {
- $this->access->expects($this->once())
- ->method('countUsers')
- ->willReturn(5);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->countUsers();
- $this->assertEquals(5, $result);
- }
- public function testCountUsersFailing(): void {
- $this->access->expects($this->once())
- ->method('countUsers')
- ->willReturn(false);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $result = $backend->countUsers();
- $this->assertFalse($result);
- }
- public function testCountUsersWithPlugin(): void {
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::COUNT_USERS)
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('countUsers')
- ->willReturn(42);
- $this->assertEquals($this->backend->countUsers(), 42);
- }
- public function testLoginName2UserNameSuccess(): void {
- $loginName = 'Alice';
- $username = 'alice';
- $dn = 'uid=alice,dc=what,dc=ever';
- $this->access->expects($this->once())
- ->method('fetchUsersByLoginName')
- ->with($this->equalTo($loginName))
- ->willReturn([['dn' => [$dn]]]);
- $this->access->expects($this->any())
- ->method('stringResemblesDN')
- ->with($this->equalTo($dn))
- ->willReturn(true);
- $this->access->expects($this->any())
- ->method('dn2username')
- ->with($this->equalTo($dn))
- ->willReturn($username);
- $this->connection->expects($this->exactly(2))
- ->method('getFromCache')
- ->with($this->equalTo('loginName2UserName-' . $loginName))
- ->willReturnOnConsecutiveCalls(null, $username);
- $this->connection->expects($this->once())
- ->method('writeToCache')
- ->with($this->equalTo('loginName2UserName-' . $loginName), $this->equalTo($username));
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $user = $this->createMock(User::class);
- $user->expects($this->any())
- ->method('getUsername')
- ->willReturn('alice');
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->with($dn)
- ->willReturn($user);
- $this->userManager->expects($this->never())
- ->method('exists');
- $this->userManager->expects($this->any())
- ->method('getAttributes')
- ->willReturn(['dn', 'uid', 'mail', 'displayname']);
- $name = $backend->loginName2UserName($loginName);
- $this->assertSame($username, $name);
-
- $backend->loginName2UserName($loginName);
- }
- public function testLoginName2UserNameNoUsersOnLDAP(): void {
- $loginName = 'Loki';
- $this->access->expects($this->once())
- ->method('fetchUsersByLoginName')
- ->with($this->equalTo($loginName))
- ->willReturn([]);
- $this->access->expects($this->never())
- ->method('stringResemblesDN');
- $this->access->expects($this->never())
- ->method('dn2username');
- $this->connection->expects($this->exactly(2))
- ->method('getFromCache')
- ->with($this->equalTo('loginName2UserName-' . $loginName))
- ->willReturnOnConsecutiveCalls(null, false);
- $this->connection->expects($this->once())
- ->method('writeToCache')
- ->with($this->equalTo('loginName2UserName-' . $loginName), false);
- $this->userManager->expects($this->any())
- ->method('getAttributes')
- ->willReturn(['dn', 'uid', 'mail', 'displayname']);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $name = $backend->loginName2UserName($loginName);
- $this->assertSame(false, $name);
-
- $backend->loginName2UserName($loginName);
- }
- public function testLoginName2UserNameOfflineUser(): void {
- $loginName = 'Alice';
- $dn = 'uid=alice,dc=what,dc=ever';
- $offlineUser = $this->getMockBuilder(OfflineUser::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->access->expects($this->once())
- ->method('fetchUsersByLoginName')
- ->with($this->equalTo($loginName))
- ->willReturn([['dn' => [$dn]]]);
- $this->connection->expects($this->exactly(2))
- ->method('getFromCache')
- ->with($this->equalTo('loginName2UserName-' . $loginName))
- ->willReturnOnConsecutiveCalls(null, false);
- $this->connection->expects($this->once())
- ->method('writeToCache')
- ->with($this->equalTo('loginName2UserName-' . $loginName), $this->equalTo(false));
- $this->userManager->expects($this->any())
- ->method('get')
- ->with($dn)
- ->willReturn($offlineUser);
- $this->userManager->expects($this->any())
- ->method('getAttributes')
- ->willReturn(['dn', 'uid', 'mail', 'displayname']);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $name = $backend->loginName2UserName($loginName);
- $this->assertSame(false, $name);
-
- $backend->loginName2UserName($loginName);
- }
-
- private function prepareAccessForSetPassword($enablePasswordChange = true) {
- $this->connection->expects($this->any())
- ->method('__get')
- ->willReturnCallback(function ($name) use (&$enablePasswordChange) {
- if ($name === 'ldapLoginFilter') {
- return '%uid';
- }
- if ($name === 'turnOnPasswordChange') {
- return $enablePasswordChange?1:0;
- }
- return null;
- });
- $this->connection->expects($this->any())
- ->method('getFromCache')
- ->willReturnCallback(function ($uid) {
- if ($uid === 'userExists' . 'roland') {
- return true;
- }
- return null;
- });
- $this->access->expects($this->any())
- ->method('fetchListOfUsers')
- ->willReturnCallback(function ($filter) {
- if ($filter === 'roland') {
- return [['dn' => ['dnOfRoland,dc=test']]];
- }
- return [];
- });
- $this->access->expects($this->any())
- ->method('fetchUsersByLoginName')
- ->willReturnCallback(function ($uid) {
- if ($uid === 'roland') {
- return [['dn' => ['dnOfRoland,dc=test']]];
- }
- return [];
- });
- $this->access->expects($this->any())
- ->method('dn2username')
- ->with($this->equalTo('dnOfRoland,dc=test'))
- ->willReturn('roland');
- $this->access->expects($this->any())
- ->method('stringResemblesDN')
- ->with($this->equalTo('dnOfRoland,dc=test'))
- ->willReturn(true);
- $this->access->expects($this->any())
- ->method('setPassword')
- ->willReturnCallback(function ($uid, $password) {
- if (strlen($password) <= 5) {
- throw new HintException('Password fails quality checking policy', '', 19);
- }
- return true;
- });
- }
- public function testSetPasswordInvalid(): void {
- $this->expectException(HintException::class);
- $this->expectExceptionMessage('Password fails quality checking policy');
- $this->prepareAccessForSetPassword($this->access);
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->willReturn($this->createMock(User::class));
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $this->assertTrue(\OC_User::setPassword('roland', 'dt'));
- }
- public function testSetPasswordValid(): void {
- $this->prepareAccessForSetPassword($this->access);
- $this->userManager->expects($this->any())
- ->method('get')
- ->willReturn($this->createMock(User::class));
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- $this->userManager->expects($this->any())
- ->method('get')
- ->willReturn($this->createMock(User::class));
- \OC_User::useBackend($backend);
- $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$'));
- }
- public function testSetPasswordValidDisabled(): void {
- $this->userManager->expects($this->any())
- ->method('get')
- ->willReturn($this->createMock(User::class));
- $this->prepareAccessForSetPassword(false);
- $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);
- \OC_User::useBackend($backend);
- $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$'));
- }
- public function testSetPasswordWithInvalidUser(): void {
- $this->expectException(\Exception::class);
- $this->expectExceptionMessage('LDAP setPassword: Could not get user object for uid NotExistingUser. Maybe the LDAP entry has no set display name attribute?');
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('NotExistingUser')
- ->willReturn(null);
- $this->backend->setPassword('NotExistingUser', 'Password');
- }
- public function testSetPasswordWithUsernameFalse(): void {
- $user = $this->createMock(User::class);
- $user
- ->expects($this->once())
- ->method('getUsername')
- ->willReturn(false);
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('NotExistingUser')
- ->willReturn($user);
-
- $this->assertFalse($this->backend->setPassword('NotExistingUser', 'Password'));
- }
- public function testSetPasswordWithPlugin(): void {
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::SET_PASSWORD)
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('setPassword')
- ->with('uid', 'password')
- ->willReturn('result');
-
- $this->assertEquals($this->backend->setPassword('uid', 'password'), 'result');
- }
- public function avatarDataProvider() {
- return [
- [ 'validImageData', false ],
- [ 'corruptImageData', true ],
- [ false, true]
- ];
- }
-
- public function testCanChangeAvatar($imageData, $expected): void {
- $isValidImage = str_starts_with((string)$imageData, 'valid');
- $user = $this->createMock(User::class);
- $user->expects($this->once())
- ->method('getAvatarImage')
- ->willReturn($imageData);
- $user->expects($this->atMost(1))
- ->method('updateAvatar')
- ->willReturn($isValidImage);
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->willReturn($user);
-
- $this->assertSame($expected, $this->backend->canChangeAvatar('uid'));
- }
- public function testCanChangeAvatarWithPlugin(): void {
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::PROVIDE_AVATAR)
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('canChangeAvatar')
- ->with('uid')
- ->willReturn('result');
- $this->assertEquals($this->backend->canChangeAvatar('uid'), 'result');
- }
- public function testSetDisplayNameWithPlugin(): void {
- $newDisplayName = 'J. Baker';
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::SET_DISPLAYNAME)
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('setDisplayName')
- ->with('uid', $newDisplayName)
- ->willReturn($newDisplayName);
- $this->access->expects($this->once())
- ->method('cacheUserDisplayName');
- $this->assertEquals($newDisplayName, $this->backend->setDisplayName('uid', $newDisplayName));
- }
- public function testSetDisplayNameErrorWithPlugin(): void {
- $this->expectException(HintException::class);
- $newDisplayName = 'J. Baker';
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::SET_DISPLAYNAME)
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('setDisplayName')
- ->with('uid', $newDisplayName)
- ->willThrowException(new HintException('something happned'));
- $this->access->expects($this->never())
- ->method('cacheUserDisplayName');
- $this->backend->setDisplayName('uid', $newDisplayName);
- }
- public function testSetDisplayNameFailing(): void {
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::SET_DISPLAYNAME)
- ->willReturn(false);
- $this->access->expects($this->never())
- ->method('cacheUserDisplayName');
- $this->assertFalse($this->backend->setDisplayName('uid', 'displayName'));
- }
- public function testCreateUserWithPlugin(): void {
- $uid = 'alien6372';
- $uuid = '123-2345-36756-123-2345234-4431';
- $pwd = 'passwørd';
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::CREATE_USER)
- ->willReturn(true);
- $this->pluginManager->expects($this->once())
- ->method('createUser')
- ->with($uid, $pwd)
- ->willReturn('result');
- $this->access->expects($this->atLeastOnce())
- ->method('getUUID')
- ->willReturn($uuid);
- $this->access->expects($this->once())
- ->method('mapAndAnnounceIfApplicable')
- ->with($this->isInstanceOf(AbstractMapping::class), $this->anything(), $uid, $uuid, true);
- $this->access->expects($this->any())
- ->method('getUserMapper')
- ->willReturn($this->createMock(UserMapping::class));
- $this->assertEquals($this->backend->createUser($uid, $pwd), true);
- }
- public function testCreateUserFailing(): void {
- $this->pluginManager->expects($this->once())
- ->method('implementsActions')
- ->with(Backend::CREATE_USER)
- ->willReturn(false);
- $this->assertFalse($this->backend->createUser('uid', 'password'));
- }
- public function actionProvider() {
- return [
- [ 'ldapUserAvatarRule', 'default', Backend::PROVIDE_AVATAR, true] ,
- [ 'ldapUserAvatarRule', 'data:selfiePhoto', Backend::PROVIDE_AVATAR, true],
- [ 'ldapUserAvatarRule', 'none', Backend::PROVIDE_AVATAR, false],
- [ 'turnOnPasswordChange', 0, Backend::SET_PASSWORD, false],
- [ 'turnOnPasswordChange', 1, Backend::SET_PASSWORD, true],
- ];
- }
-
- public function testImplementsAction($configurable, $value, $actionCode, $expected): void {
- $this->pluginManager->expects($this->once())
- ->method('getImplementedActions')
- ->willReturn(0);
- $this->connection->expects($this->any())
- ->method('__get')
- ->willReturnMap([
- [$configurable, $value],
- ]);
- $this->assertSame($expected, $this->backend->implementsActions($actionCode));
- }
- }
|