SyncTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\User_LDAP\Tests\Jobs;
  27. use OCA\User_LDAP\Access;
  28. use OCA\User_LDAP\AccessFactory;
  29. use OCA\User_LDAP\Connection;
  30. use OCA\User_LDAP\ConnectionFactory;
  31. use OCA\User_LDAP\Helper;
  32. use OCA\User_LDAP\Jobs\Sync;
  33. use OCA\User_LDAP\LDAP;
  34. use OCA\User_LDAP\Mapping\UserMapping;
  35. use OCA\User_LDAP\User\Manager;
  36. use OCP\AppFramework\Utility\ITimeFactory;
  37. use OCP\IAvatarManager;
  38. use OCP\IConfig;
  39. use OCP\IDBConnection;
  40. use OCP\IUserManager;
  41. use OCP\Notification\IManager;
  42. use Test\TestCase;
  43. class SyncTest extends TestCase {
  44. /** @var array */
  45. protected $arguments;
  46. /** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $helper;
  48. /** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */
  49. protected $ldapWrapper;
  50. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  51. protected $userManager;
  52. /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
  53. protected $mapper;
  54. /** @var Sync */
  55. protected $sync;
  56. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  57. protected $config;
  58. /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
  59. protected $avatarManager;
  60. /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  61. protected $dbc;
  62. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  63. protected $ncUserManager;
  64. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  65. protected $notificationManager;
  66. /** @var ConnectionFactory|\PHPUnit\Framework\MockObject\MockObject */
  67. protected $connectionFactory;
  68. /** @var AccessFactory|\PHPUnit\Framework\MockObject\MockObject */
  69. protected $accessFactory;
  70. protected function setUp(): void {
  71. parent::setUp();
  72. $this->helper = $this->createMock(Helper::class);
  73. $this->ldapWrapper = $this->createMock(LDAP::class);
  74. $this->userManager = $this->createMock(Manager::class);
  75. $this->mapper = $this->createMock(UserMapping::class);
  76. $this->config = $this->createMock(IConfig::class);
  77. $this->avatarManager = $this->createMock(IAvatarManager::class);
  78. $this->dbc = $this->createMock(IDBConnection::class);
  79. $this->ncUserManager = $this->createMock(IUserManager::class);
  80. $this->notificationManager = $this->createMock(IManager::class);
  81. $this->connectionFactory = $this->createMock(ConnectionFactory::class);
  82. $this->accessFactory = $this->createMock(AccessFactory::class);
  83. $this->arguments = [
  84. 'helper' => $this->helper,
  85. 'ldapWrapper' => $this->ldapWrapper,
  86. 'mapper' => $this->mapper,
  87. 'config' => $this->config,
  88. 'avatarManager' => $this->avatarManager,
  89. 'dbc' => $this->dbc,
  90. 'ncUserManager' => $this->ncUserManager,
  91. 'notificationManager' => $this->notificationManager,
  92. 'connectionFactory' => $this->connectionFactory,
  93. 'accessFactory' => $this->accessFactory,
  94. ];
  95. $this->sync = new Sync($this->createMock(ITimeFactory::class));
  96. }
  97. public function intervalDataProvider() {
  98. return [
  99. [
  100. 0, 1000, 750
  101. ],
  102. [
  103. 22, 0, 50
  104. ],
  105. [
  106. 500, 500, 500
  107. ],
  108. [
  109. 1357, 0, 0
  110. ],
  111. [
  112. 421337, 2000, 3000
  113. ]
  114. ];
  115. }
  116. /**
  117. * @dataProvider intervalDataProvider
  118. */
  119. public function testUpdateInterval($userCount, $pagingSize1, $pagingSize2) {
  120. $this->config->expects($this->once())
  121. ->method('setAppValue')
  122. ->with('user_ldap', 'background_sync_interval', $this->anything())
  123. ->willReturnCallback(function ($a, $k, $interval) {
  124. $this->assertTrue($interval >= SYNC::MIN_INTERVAL);
  125. $this->assertTrue($interval <= SYNC::MAX_INTERVAL);
  126. return true;
  127. });
  128. $this->config->expects($this->atLeastOnce())
  129. ->method('getAppKeys')
  130. ->willReturn([
  131. 'blabla',
  132. 'ldap_paging_size',
  133. 's07blabla',
  134. 'installed',
  135. 's07ldap_paging_size'
  136. ]);
  137. $this->config->expects($this->exactly(2))
  138. ->method('getAppValue')
  139. ->willReturnOnConsecutiveCalls($pagingSize1, $pagingSize2);
  140. $this->mapper->expects($this->atLeastOnce())
  141. ->method('count')
  142. ->willReturn($userCount);
  143. $this->sync->setArgument($this->arguments);
  144. $this->sync->updateInterval();
  145. }
  146. public function moreResultsProvider() {
  147. return [
  148. [ 3, 3, true ],
  149. [ 3, 5, true ],
  150. [ 3, 2, false],
  151. [ 0, 4, false],
  152. [ null, 4, false]
  153. ];
  154. }
  155. /**
  156. * @dataProvider moreResultsProvider
  157. */
  158. public function testMoreResults($pagingSize, $results, $expected) {
  159. $connection = $this->createMock(Connection::class);
  160. $this->connectionFactory->expects($this->any())
  161. ->method('get')
  162. ->willReturn($connection);
  163. $connection->expects($this->any())
  164. ->method('__get')
  165. ->willReturnCallback(function ($key) use ($pagingSize) {
  166. if ($key === 'ldapPagingSize') {
  167. return $pagingSize;
  168. }
  169. return null;
  170. });
  171. /** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */
  172. $access = $this->createMock(Access::class);
  173. $this->accessFactory->expects($this->any())
  174. ->method('get')
  175. ->with($connection)
  176. ->willReturn($access);
  177. $this->userManager->expects($this->any())
  178. ->method('getAttributes')
  179. ->willReturn(['dn', 'uid', 'mail', 'displayname']);
  180. $access->expects($this->once())
  181. ->method('fetchListOfUsers')
  182. ->willReturn(array_pad([], $results, 'someUser'));
  183. $access->expects($this->any())
  184. ->method('combineFilterWithAnd')
  185. ->willReturn('pseudo=filter');
  186. $access->connection = $connection;
  187. $access->userManager = $this->userManager;
  188. $this->sync->setArgument($this->arguments);
  189. $hasMoreResults = $this->sync->runCycle(['prefix' => 's01', 'offset' => 100]);
  190. $this->assertSame($expected, $hasMoreResults);
  191. }
  192. public function cycleDataProvider() {
  193. $lastCycle = ['prefix' => 's01', 'offset' => 1000];
  194. $lastCycle2 = ['prefix' => '', 'offset' => 1000];
  195. return [
  196. [ null, ['s01'], ['prefix' => 's01', 'offset' => 0] ],
  197. [ null, [''], ['prefix' => '', 'offset' => 0] ],
  198. [ $lastCycle, ['s01', 's02'], ['prefix' => 's02', 'offset' => 0] ],
  199. [ $lastCycle, [''], ['prefix' => '', 'offset' => 0] ],
  200. [ $lastCycle2, ['', 's01'], ['prefix' => 's01', 'offset' => 0] ],
  201. [ $lastCycle, [], null ],
  202. ];
  203. }
  204. /**
  205. * @dataProvider cycleDataProvider
  206. */
  207. public function testDetermineNextCycle($cycleData, $prefixes, $expectedCycle) {
  208. $this->helper->expects($this->any())
  209. ->method('getServerConfigurationPrefixes')
  210. ->with(true)
  211. ->willReturn($prefixes);
  212. if (is_array($expectedCycle)) {
  213. $this->config->expects($this->exactly(2))
  214. ->method('setAppValue')
  215. ->withConsecutive(
  216. ['user_ldap', 'background_sync_prefix', $expectedCycle['prefix']],
  217. ['user_ldap', 'background_sync_offset', $expectedCycle['offset']]
  218. );
  219. } else {
  220. $this->config->expects($this->never())
  221. ->method('setAppValue');
  222. }
  223. $this->sync->setArgument($this->arguments);
  224. $nextCycle = $this->sync->determineNextCycle($cycleData);
  225. if ($expectedCycle === null) {
  226. $this->assertNull($nextCycle);
  227. } else {
  228. $this->assertSame($expectedCycle['prefix'], $nextCycle['prefix']);
  229. $this->assertSame($expectedCycle['offset'], $nextCycle['offset']);
  230. }
  231. }
  232. public function testQualifiesToRun() {
  233. $cycleData = ['prefix' => 's01'];
  234. $this->config->expects($this->exactly(2))
  235. ->method('getAppValue')
  236. ->willReturnOnConsecutiveCalls(time() - 60 * 40, time() - 60 * 20);
  237. $this->sync->setArgument($this->arguments);
  238. $this->assertTrue($this->sync->qualifiesToRun($cycleData));
  239. $this->assertFalse($this->sync->qualifiesToRun($cycleData));
  240. }
  241. public function runDataProvider() {
  242. return [
  243. #0 - one LDAP server, reset
  244. [[
  245. 'prefixes' => [''],
  246. 'scheduledCycle' => ['prefix' => '', 'offset' => '4500'],
  247. 'pagingSize' => 500,
  248. 'usersThisCycle' => 0,
  249. 'expectedNextCycle' => ['prefix' => '', 'offset' => '0'],
  250. 'mappedUsers' => 123,
  251. ]],
  252. #1 - 2 LDAP servers, next prefix
  253. [[
  254. 'prefixes' => ['', 's01'],
  255. 'scheduledCycle' => ['prefix' => '', 'offset' => '4500'],
  256. 'pagingSize' => 500,
  257. 'usersThisCycle' => 0,
  258. 'expectedNextCycle' => ['prefix' => 's01', 'offset' => '0'],
  259. 'mappedUsers' => 123,
  260. ]],
  261. #2 - 2 LDAP servers, rotate prefix
  262. [[
  263. 'prefixes' => ['', 's01'],
  264. 'scheduledCycle' => ['prefix' => 's01', 'offset' => '4500'],
  265. 'pagingSize' => 500,
  266. 'usersThisCycle' => 0,
  267. 'expectedNextCycle' => ['prefix' => '', 'offset' => '0'],
  268. 'mappedUsers' => 123,
  269. ]],
  270. ];
  271. }
  272. /**
  273. * @dataProvider runDataProvider
  274. */
  275. public function testRun($runData) {
  276. $this->config->expects($this->any())
  277. ->method('getAppValue')
  278. ->willReturnCallback(function ($app, $key, $default) use ($runData) {
  279. if ($app === 'core' && $key === 'backgroundjobs_mode') {
  280. return 'cron';
  281. }
  282. if ($app = 'user_ldap') {
  283. // for getCycle()
  284. if ($key === 'background_sync_prefix') {
  285. return $runData['scheduledCycle']['prefix'];
  286. }
  287. if ($key === 'background_sync_offset') {
  288. return $runData['scheduledCycle']['offset'];
  289. }
  290. // for qualifiesToRun()
  291. if ($key === $runData['scheduledCycle']['prefix'] . '_lastChange') {
  292. return time() - 60 * 40;
  293. }
  294. // for getMinPagingSize
  295. if ($key === $runData['scheduledCycle']['prefix'] . 'ldap_paging_size') {
  296. return $runData['pagingSize'];
  297. }
  298. }
  299. return $default;
  300. });
  301. $this->config->expects($this->exactly(3))
  302. ->method('setAppValue')
  303. ->withConsecutive(
  304. ['user_ldap', 'background_sync_prefix', $runData['expectedNextCycle']['prefix']],
  305. ['user_ldap', 'background_sync_offset', $runData['expectedNextCycle']['offset']],
  306. ['user_ldap', 'background_sync_interval', $this->anything()]
  307. );
  308. $this->config->expects($this->any())
  309. ->method('getAppKeys')
  310. ->with('user_ldap')
  311. ->willReturn([$runData['scheduledCycle']['prefix'] . 'ldap_paging_size']);
  312. $this->helper->expects($this->any())
  313. ->method('getServerConfigurationPrefixes')
  314. ->with(true)
  315. ->willReturn($runData['prefixes']);
  316. $connection = $this->createMock(Connection::class);
  317. $this->connectionFactory->expects($this->any())
  318. ->method('get')
  319. ->willReturn($connection);
  320. $connection->expects($this->any())
  321. ->method('__get')
  322. ->willReturnCallback(function ($key) use ($runData) {
  323. if ($key === 'ldapPagingSize') {
  324. return $runData['pagingSize'];
  325. }
  326. return null;
  327. });
  328. /** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */
  329. $access = $this->createMock(Access::class);
  330. $this->accessFactory->expects($this->any())
  331. ->method('get')
  332. ->with($connection)
  333. ->willReturn($access);
  334. $this->userManager->expects($this->any())
  335. ->method('getAttributes')
  336. ->willReturn(['dn', 'uid', 'mail', 'displayname']);
  337. $access->expects($this->once())
  338. ->method('fetchListOfUsers')
  339. ->willReturn(array_pad([], $runData['usersThisCycle'], 'someUser'));
  340. $access->expects($this->any())
  341. ->method('combineFilterWithAnd')
  342. ->willReturn('pseudo=filter');
  343. $access->connection = $connection;
  344. $access->userManager = $this->userManager;
  345. $this->mapper->expects($this->any())
  346. ->method('count')
  347. ->willReturn($runData['mappedUsers']);
  348. $this->sync->run($this->arguments);
  349. }
  350. }