2017-11-04 00:58:28 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-04 00:58:28 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-11-04 00:58:28 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\User_LDAP\Tests\Jobs;
|
|
|
|
|
2018-01-05 16:27:36 +03:00
|
|
|
use OCA\User_LDAP\Access;
|
|
|
|
use OCA\User_LDAP\AccessFactory;
|
|
|
|
use OCA\User_LDAP\Connection;
|
|
|
|
use OCA\User_LDAP\ConnectionFactory;
|
2017-11-04 00:58:28 +03:00
|
|
|
use OCA\User_LDAP\Helper;
|
|
|
|
use OCA\User_LDAP\Jobs\Sync;
|
|
|
|
use OCA\User_LDAP\LDAP;
|
|
|
|
use OCA\User_LDAP\Mapping\UserMapping;
|
|
|
|
use OCA\User_LDAP\User\Manager;
|
2017-11-08 03:51:14 +03:00
|
|
|
use OCP\IAvatarManager;
|
2017-11-04 00:58:28 +03:00
|
|
|
use OCP\IConfig;
|
2017-11-08 03:51:14 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\Notification\IManager;
|
2017-11-04 00:58:28 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class SyncTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $arguments;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-04 00:58:28 +03:00
|
|
|
protected $helper;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-04 00:58:28 +03:00
|
|
|
protected $ldapWrapper;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-04 00:58:28 +03:00
|
|
|
protected $userManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-04 00:58:28 +03:00
|
|
|
protected $mapper;
|
|
|
|
/** @var Sync */
|
|
|
|
protected $sync;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 03:51:14 +03:00
|
|
|
protected $config;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 03:51:14 +03:00
|
|
|
protected $avatarManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 03:51:14 +03:00
|
|
|
protected $dbc;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 03:51:14 +03:00
|
|
|
protected $ncUserManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 03:51:14 +03:00
|
|
|
protected $notificationManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ConnectionFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-01-05 16:27:36 +03:00
|
|
|
protected $connectionFactory;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var AccessFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-01-05 16:27:36 +03:00
|
|
|
protected $accessFactory;
|
2017-11-04 00:58:28 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-11-04 00:58:28 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->helper = $this->createMock(Helper::class);
|
|
|
|
$this->ldapWrapper = $this->createMock(LDAP::class);
|
|
|
|
$this->userManager = $this->createMock(Manager::class);
|
|
|
|
$this->mapper = $this->createMock(UserMapping::class);
|
2017-11-08 03:51:14 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
$this->avatarManager = $this->createMock(IAvatarManager::class);
|
|
|
|
$this->dbc = $this->createMock(IDBConnection::class);
|
|
|
|
$this->ncUserManager = $this->createMock(IUserManager::class);
|
|
|
|
$this->notificationManager = $this->createMock(IManager::class);
|
2018-01-05 16:27:36 +03:00
|
|
|
$this->connectionFactory = $this->createMock(ConnectionFactory::class);
|
|
|
|
$this->accessFactory = $this->createMock(AccessFactory::class);
|
2017-11-04 00:58:28 +03:00
|
|
|
|
|
|
|
$this->arguments = [
|
|
|
|
'helper' => $this->helper,
|
|
|
|
'ldapWrapper' => $this->ldapWrapper,
|
|
|
|
'userManager' => $this->userManager,
|
|
|
|
'mapper' => $this->mapper,
|
2017-11-08 03:51:14 +03:00
|
|
|
'config' => $this->config,
|
|
|
|
'avatarManager' => $this->avatarManager,
|
|
|
|
'dbc' => $this->dbc,
|
|
|
|
'ncUserManager' => $this->ncUserManager,
|
|
|
|
'notificationManager' => $this->notificationManager,
|
2018-01-05 16:27:36 +03:00
|
|
|
'connectionFactory' => $this->connectionFactory,
|
|
|
|
'accessFactory' => $this->accessFactory,
|
2017-11-04 00:58:28 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->sync = new Sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function intervalDataProvider() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
0, 1000, 750
|
|
|
|
],
|
|
|
|
[
|
|
|
|
22, 0, 50
|
|
|
|
],
|
|
|
|
[
|
|
|
|
500, 500, 500
|
|
|
|
],
|
|
|
|
[
|
|
|
|
1357, 0, 0
|
|
|
|
],
|
|
|
|
[
|
|
|
|
421337, 2000, 3000
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider intervalDataProvider
|
|
|
|
*/
|
|
|
|
public function testUpdateInterval($userCount, $pagingSize1, $pagingSize2) {
|
2017-11-08 03:51:14 +03:00
|
|
|
$this->config->expects($this->once())
|
2017-11-04 00:58:28 +03:00
|
|
|
->method('setAppValue')
|
|
|
|
->with('user_ldap', 'background_sync_interval', $this->anything())
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($a, $k, $interval) {
|
2017-11-04 00:58:28 +03:00
|
|
|
$this->assertTrue($interval >= SYNC::MIN_INTERVAL);
|
|
|
|
$this->assertTrue($interval <= SYNC::MAX_INTERVAL);
|
|
|
|
return true;
|
|
|
|
});
|
2017-11-08 03:51:14 +03:00
|
|
|
$this->config->expects($this->atLeastOnce())
|
2017-11-04 00:58:28 +03:00
|
|
|
->method('getAppKeys')
|
|
|
|
->willReturn([
|
|
|
|
'blabla',
|
|
|
|
'ldap_paging_size',
|
|
|
|
's07blabla',
|
|
|
|
'installed',
|
|
|
|
's07ldap_paging_size'
|
|
|
|
]);
|
2017-11-08 03:51:14 +03:00
|
|
|
$this->config->expects($this->exactly(2))
|
2017-11-04 00:58:28 +03:00
|
|
|
->method('getAppValue')
|
|
|
|
->willReturnOnConsecutiveCalls($pagingSize1, $pagingSize2);
|
|
|
|
|
|
|
|
$this->mapper->expects($this->atLeastOnce())
|
|
|
|
->method('count')
|
|
|
|
->willReturn($userCount);
|
|
|
|
|
|
|
|
$this->sync->setArgument($this->arguments);
|
|
|
|
$this->sync->updateInterval();
|
|
|
|
}
|
|
|
|
|
2018-01-05 16:27:36 +03:00
|
|
|
public function moreResultsProvider() {
|
|
|
|
return [
|
|
|
|
[ 3, 3, true ],
|
|
|
|
[ 3, 5, true ],
|
2018-01-11 15:20:17 +03:00
|
|
|
[ 3, 2, false],
|
2018-01-11 16:47:51 +03:00
|
|
|
[ 0, 4, false],
|
|
|
|
[ null, 4, false]
|
2018-01-05 16:27:36 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider moreResultsProvider
|
|
|
|
*/
|
|
|
|
public function testMoreResults($pagingSize, $results, $expected) {
|
|
|
|
$connection = $this->createMock(Connection::class);
|
|
|
|
$this->connectionFactory->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->willReturn($connection);
|
|
|
|
$connection->expects($this->any())
|
|
|
|
->method('__get')
|
|
|
|
->willReturnCallback(function ($key) use ($pagingSize) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($key === 'ldapPagingSize') {
|
2018-01-05 16:27:36 +03:00
|
|
|
return $pagingSize;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */
|
2018-01-05 16:27:36 +03:00
|
|
|
$access = $this->createMock(Access::class);
|
|
|
|
$this->accessFactory->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with($connection)
|
|
|
|
->willReturn($access);
|
|
|
|
|
2020-06-25 00:34:37 +03:00
|
|
|
$this->userManager->expects($this->any())
|
|
|
|
->method('getAttributes')
|
|
|
|
->willReturn(['dn', 'uid', 'mail', 'displayname']);
|
|
|
|
|
2018-01-05 16:27:36 +03:00
|
|
|
$access->expects($this->once())
|
|
|
|
->method('fetchListOfUsers')
|
|
|
|
->willReturn(array_pad([], $results, 'someUser'));
|
2020-06-25 00:34:37 +03:00
|
|
|
$access->expects($this->any())
|
|
|
|
->method('combineFilterWithAnd')
|
|
|
|
->willReturn('pseudo=filter');
|
2018-01-05 16:27:36 +03:00
|
|
|
$access->connection = $connection;
|
|
|
|
$access->userManager = $this->userManager;
|
|
|
|
|
|
|
|
$this->sync->setArgument($this->arguments);
|
|
|
|
$hasMoreResults = $this->sync->runCycle(['prefix' => 's01', 'offset' => 100]);
|
|
|
|
$this->assertSame($expected, $hasMoreResults);
|
|
|
|
}
|
|
|
|
|
2018-01-08 14:57:14 +03:00
|
|
|
public function cycleDataProvider() {
|
2018-01-08 15:08:18 +03:00
|
|
|
$lastCycle = ['prefix' => 's01', 'offset' => 1000];
|
|
|
|
$lastCycle2 = ['prefix' => '', 'offset' => 1000];
|
2018-01-08 14:57:14 +03:00
|
|
|
return [
|
|
|
|
[ null, ['s01'], ['prefix' => 's01', 'offset' => 0] ],
|
2018-01-08 15:08:18 +03:00
|
|
|
[ null, [''], ['prefix' => '', 'offset' => 0] ],
|
|
|
|
[ $lastCycle, ['s01', 's02'], ['prefix' => 's02', 'offset' => 0] ],
|
|
|
|
[ $lastCycle, [''], ['prefix' => '', 'offset' => 0] ],
|
|
|
|
[ $lastCycle2, ['', 's01'], ['prefix' => 's01', 'offset' => 0] ],
|
|
|
|
[ $lastCycle, [], null ],
|
2018-01-08 14:57:14 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider cycleDataProvider
|
|
|
|
*/
|
|
|
|
public function testDetermineNextCycle($cycleData, $prefixes, $expectedCycle) {
|
|
|
|
$this->helper->expects($this->any())
|
|
|
|
->method('getServerConfigurationPrefixes')
|
|
|
|
->with(true)
|
|
|
|
->willReturn($prefixes);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (is_array($expectedCycle)) {
|
2018-01-08 14:57:14 +03:00
|
|
|
$this->config->expects($this->exactly(2))
|
|
|
|
->method('setAppValue')
|
|
|
|
->withConsecutive(
|
|
|
|
['user_ldap', 'background_sync_prefix', $expectedCycle['prefix']],
|
|
|
|
['user_ldap', 'background_sync_offset', $expectedCycle['offset']]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('setAppValue');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sync->setArgument($this->arguments);
|
|
|
|
$nextCycle = $this->sync->determineNextCycle($cycleData);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($expectedCycle === null) {
|
2018-01-08 14:57:14 +03:00
|
|
|
$this->assertNull($nextCycle);
|
|
|
|
} else {
|
|
|
|
$this->assertSame($expectedCycle['prefix'], $nextCycle['prefix']);
|
|
|
|
$this->assertSame($expectedCycle['offset'], $nextCycle['offset']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 15:08:18 +03:00
|
|
|
public function testQualifiesToRun() {
|
|
|
|
$cycleData = ['prefix' => 's01'];
|
|
|
|
|
|
|
|
$this->config->expects($this->exactly(2))
|
|
|
|
->method('getAppValue')
|
|
|
|
->willReturnOnConsecutiveCalls(time() - 60*40, time() - 60*20);
|
|
|
|
|
|
|
|
$this->sync->setArgument($this->arguments);
|
|
|
|
$this->assertTrue($this->sync->qualifiesToRun($cycleData));
|
|
|
|
$this->assertFalse($this->sync->qualifiesToRun($cycleData));
|
|
|
|
}
|
|
|
|
|
2018-01-08 15:44:45 +03:00
|
|
|
public function runDataProvider() {
|
|
|
|
return [
|
|
|
|
#0 - one LDAP server, reset
|
|
|
|
[[
|
|
|
|
'prefixes' => [''],
|
|
|
|
'scheduledCycle' => ['prefix' => '', 'offset' => '4500'],
|
|
|
|
'pagingSize' => 500,
|
|
|
|
'usersThisCycle' => 0,
|
|
|
|
'expectedNextCycle' => ['prefix' => '', 'offset' => '0'],
|
|
|
|
'mappedUsers' => 123,
|
|
|
|
]],
|
2018-01-08 15:46:07 +03:00
|
|
|
#1 - 2 LDAP servers, next prefix
|
2018-01-08 15:44:45 +03:00
|
|
|
[[
|
|
|
|
'prefixes' => ['', 's01'],
|
|
|
|
'scheduledCycle' => ['prefix' => '', 'offset' => '4500'],
|
|
|
|
'pagingSize' => 500,
|
|
|
|
'usersThisCycle' => 0,
|
|
|
|
'expectedNextCycle' => ['prefix' => 's01', 'offset' => '0'],
|
|
|
|
'mappedUsers' => 123,
|
|
|
|
]],
|
2018-01-08 15:46:07 +03:00
|
|
|
#2 - 2 LDAP servers, rotate prefix
|
|
|
|
[[
|
|
|
|
'prefixes' => ['', 's01'],
|
|
|
|
'scheduledCycle' => ['prefix' => 's01', 'offset' => '4500'],
|
|
|
|
'pagingSize' => 500,
|
|
|
|
'usersThisCycle' => 0,
|
|
|
|
'expectedNextCycle' => ['prefix' => '', 'offset' => '0'],
|
|
|
|
'mappedUsers' => 123,
|
|
|
|
]],
|
2018-01-08 15:44:45 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider runDataProvider
|
|
|
|
*/
|
|
|
|
public function testRun($runData) {
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getAppValue')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($app, $key, $default) use ($runData) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($app === 'core' && $key === 'backgroundjobs_mode') {
|
2018-01-08 15:44:45 +03:00
|
|
|
return 'cron';
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($app = 'user_ldap') {
|
2018-01-08 15:44:45 +03:00
|
|
|
// for getCycle()
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($key === 'background_sync_prefix') {
|
2018-01-08 15:44:45 +03:00
|
|
|
return $runData['scheduledCycle']['prefix'];
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($key === 'background_sync_offset') {
|
2018-01-08 15:44:45 +03:00
|
|
|
return $runData['scheduledCycle']['offset'];
|
|
|
|
}
|
|
|
|
// for qualifiesToRun()
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($key === $runData['scheduledCycle']['prefix'] . '_lastChange') {
|
2018-01-08 15:44:45 +03:00
|
|
|
return time() - 60*40;
|
|
|
|
}
|
|
|
|
// for getMinPagingSize
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($key === $runData['scheduledCycle']['prefix'] . 'ldap_paging_size') {
|
2018-01-08 15:44:45 +03:00
|
|
|
return $runData['pagingSize'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
});
|
|
|
|
$this->config->expects($this->exactly(3))
|
|
|
|
->method('setAppValue')
|
|
|
|
->withConsecutive(
|
|
|
|
['user_ldap', 'background_sync_prefix', $runData['expectedNextCycle']['prefix']],
|
|
|
|
['user_ldap', 'background_sync_offset', $runData['expectedNextCycle']['offset']],
|
|
|
|
['user_ldap', 'background_sync_interval', $this->anything()]
|
|
|
|
);
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getAppKeys')
|
|
|
|
->with('user_ldap')
|
|
|
|
->willReturn([$runData['scheduledCycle']['prefix'] . 'ldap_paging_size']);
|
|
|
|
|
|
|
|
$this->helper->expects($this->any())
|
|
|
|
->method('getServerConfigurationPrefixes')
|
|
|
|
->with(true)
|
|
|
|
->willReturn($runData['prefixes']);
|
|
|
|
|
|
|
|
$connection = $this->createMock(Connection::class);
|
|
|
|
$this->connectionFactory->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->willReturn($connection);
|
|
|
|
$connection->expects($this->any())
|
|
|
|
->method('__get')
|
|
|
|
->willReturnCallback(function ($key) use ($runData) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($key === 'ldapPagingSize') {
|
2018-01-08 15:44:45 +03:00
|
|
|
return $runData['pagingSize'];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */
|
2018-01-08 15:44:45 +03:00
|
|
|
$access = $this->createMock(Access::class);
|
|
|
|
$this->accessFactory->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with($connection)
|
|
|
|
->willReturn($access);
|
|
|
|
|
2020-06-25 00:34:37 +03:00
|
|
|
$this->userManager->expects($this->any())
|
|
|
|
->method('getAttributes')
|
|
|
|
->willReturn(['dn', 'uid', 'mail', 'displayname']);
|
|
|
|
|
2018-01-08 15:44:45 +03:00
|
|
|
$access->expects($this->once())
|
|
|
|
->method('fetchListOfUsers')
|
|
|
|
->willReturn(array_pad([], $runData['usersThisCycle'], 'someUser'));
|
2020-06-25 00:34:37 +03:00
|
|
|
$access->expects($this->any())
|
|
|
|
->method('combineFilterWithAnd')
|
|
|
|
->willReturn('pseudo=filter');
|
2018-01-08 15:44:45 +03:00
|
|
|
$access->connection = $connection;
|
|
|
|
$access->userManager = $this->userManager;
|
|
|
|
|
|
|
|
$this->mapper->expects($this->any())
|
|
|
|
->method('count')
|
|
|
|
->willReturn($runData['mappedUsers']);
|
|
|
|
|
|
|
|
$this->sync->run($this->arguments);
|
|
|
|
}
|
2017-11-04 00:58:28 +03:00
|
|
|
}
|