1
0

AbstractUUIDFixTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\User_LDAP\Tests\Migration;
  26. use OCA\User_LDAP\Access;
  27. use OCA\User_LDAP\Helper;
  28. use OCA\User_LDAP\LDAP;
  29. use OCA\User_LDAP\Mapping\AbstractMapping;
  30. use OCA\User_LDAP\Migration\UUIDFix;
  31. use OCA\User_LDAP\Proxy;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\IConfig;
  34. use Test\TestCase;
  35. abstract class AbstractUUIDFixTest extends TestCase {
  36. protected Helper $helper;
  37. protected IConfig $config;
  38. protected LDAP $ldap;
  39. protected AbstractMapping $mapper;
  40. protected UUIDFix $job;
  41. protected Proxy $proxy;
  42. protected Access $access;
  43. protected ITimeFactory $time;
  44. protected bool $isUser = true;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->ldap = $this->createMock(LDAP::class);
  48. $this->config = $this->createMock(IConfig::class);
  49. $this->access = $this->createMock(Access::class);
  50. $this->time = $this->createMock(ITimeFactory::class);
  51. $this->helper = $this->createMock(Helper::class);
  52. $this->helper->expects($this->any())
  53. ->method('getServerConfigurationPrefixes')
  54. ->with(true)
  55. ->willReturn(['s01', 's03']);
  56. }
  57. protected function instantiateJob($className) {
  58. $this->job = new $className($this->time, $this->mapper, $this->proxy);
  59. $this->proxy->expects($this->any())
  60. ->method('getLDAPAccess')
  61. ->willReturn($this->access);
  62. }
  63. public function testRunSingleRecord() {
  64. $args = [
  65. 'records' => [
  66. 0 => [
  67. 'name' => 'Someone',
  68. 'dn' => 'uid=Someone,dc=Somewhere',
  69. 'uuid' => 'kaput'
  70. ]
  71. ]
  72. ];
  73. $correctUUID = '4355-AED3-9D73-03AD';
  74. $this->access->expects($this->once())
  75. ->method('getUUID')
  76. ->with($args['records'][0]['dn'], $this->isUser)
  77. ->willReturn($correctUUID);
  78. $this->mapper->expects($this->once())
  79. ->method('setUUIDbyDN')
  80. ->with($correctUUID, $args['records'][0]['dn']);
  81. $this->job->run($args);
  82. }
  83. public function testRunValidRecord() {
  84. $correctUUID = '4355-AED3-9D73-03AD';
  85. $args = [
  86. 'records' => [
  87. 0 => [
  88. 'name' => 'Someone',
  89. 'dn' => 'uid=Someone,dc=Somewhere',
  90. 'uuid' => $correctUUID
  91. ]
  92. ]
  93. ];
  94. $this->access->expects($this->once())
  95. ->method('getUUID')
  96. ->with($args['records'][0]['dn'], $this->isUser)
  97. ->willReturn($correctUUID);
  98. $this->mapper->expects($this->never())
  99. ->method('setUUIDbyDN');
  100. $this->job->run($args);
  101. }
  102. public function testRunRemovedRecord() {
  103. $args = [
  104. 'records' => [
  105. 0 => [
  106. 'name' => 'Someone',
  107. 'dn' => 'uid=Someone,dc=Somewhere',
  108. 'uuid' => 'kaput'
  109. ]
  110. ]
  111. ];
  112. $this->access->expects($this->once())
  113. ->method('getUUID')
  114. ->with($args['records'][0]['dn'], $this->isUser)
  115. ->willReturn(false);
  116. $this->mapper->expects($this->never())
  117. ->method('setUUIDbyDN');
  118. $this->job->run($args);
  119. }
  120. public function testRunManyRecords() {
  121. $args = [
  122. 'records' => [
  123. 0 => [
  124. 'name' => 'Someone',
  125. 'dn' => 'uid=Someone,dc=Somewhere',
  126. 'uuid' => 'kaput'
  127. ],
  128. 1 => [
  129. 'name' => 'kdslkdsaIdsal',
  130. 'dn' => 'uid=kdslkdsaIdsal,dc=Somewhere',
  131. 'uuid' => 'AED3-4355-03AD-9D73'
  132. ],
  133. 2 => [
  134. 'name' => 'Paperboy',
  135. 'dn' => 'uid=Paperboy,dc=Somewhere',
  136. 'uuid' => 'kaput'
  137. ]
  138. ]
  139. ];
  140. $correctUUIDs = ['4355-AED3-9D73-03AD', 'AED3-4355-03AD-9D73', 'AED3-9D73-4355-03AD'];
  141. $this->access->expects($this->exactly(3))
  142. ->method('getUUID')
  143. ->withConsecutive(
  144. [$args['records'][0]['dn'], $this->isUser],
  145. [$args['records'][1]['dn'], $this->isUser],
  146. [$args['records'][2]['dn'], $this->isUser]
  147. )
  148. ->willReturnOnConsecutiveCalls($correctUUIDs[0], $correctUUIDs[1], $correctUUIDs[2]);
  149. $this->mapper->expects($this->exactly(2))
  150. ->method('setUUIDbyDN')
  151. ->withConsecutive(
  152. [$correctUUIDs[0], $args['records'][0]['dn']],
  153. [$correctUUIDs[2], $args['records'][2]['dn']]
  154. );
  155. $this->job->run($args);
  156. }
  157. }