CloudIdManagerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Federation;
  22. use OC\Federation\CloudIdManager;
  23. use OC\Memcache\ArrayCache;
  24. use OCP\Contacts\IManager;
  25. use OCP\EventDispatcher\IEventDispatcher;
  26. use OCP\ICacheFactory;
  27. use OCP\IURLGenerator;
  28. use OCP\IUserManager;
  29. use Test\TestCase;
  30. class CloudIdManagerTest extends TestCase {
  31. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  32. protected $contactsManager;
  33. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  34. private $urlGenerator;
  35. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  36. private $userManager;
  37. /** @var CloudIdManager */
  38. private $cloudIdManager;
  39. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  40. private $cacheFactory;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->contactsManager = $this->createMock(IManager::class);
  44. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  45. $this->userManager = $this->createMock(IUserManager::class);
  46. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  47. $this->cacheFactory->method('createLocal')
  48. ->willReturn(new ArrayCache(''));
  49. $this->cloudIdManager = new CloudIdManager(
  50. $this->contactsManager,
  51. $this->urlGenerator,
  52. $this->userManager,
  53. $this->cacheFactory,
  54. $this->createMock(IEventDispatcher::class)
  55. );
  56. }
  57. public function cloudIdProvider() {
  58. return [
  59. ['test@example.com', 'test', 'example.com', 'test@example.com'],
  60. ['test@example.com/cloud', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  61. ['test@example.com/cloud/', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  62. ['test@example.com/cloud/index.php', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  63. ['test@example.com@example.com', 'test@example.com', 'example.com', 'test@example.com@example.com'],
  64. ];
  65. }
  66. /**
  67. * @dataProvider cloudIdProvider
  68. *
  69. * @param string $cloudId
  70. * @param string $user
  71. * @param string $remote
  72. */
  73. public function testResolveCloudId($cloudId, $user, $remote, $cleanId) {
  74. $displayName = 'Ample Ex';
  75. $this->contactsManager->expects($this->any())
  76. ->method('search')
  77. ->with($cleanId, ['CLOUD'])
  78. ->willReturn([
  79. [
  80. 'CLOUD' => [$cleanId],
  81. 'FN' => 'Ample Ex',
  82. ]
  83. ]);
  84. $cloudId = $this->cloudIdManager->resolveCloudId($cloudId);
  85. $this->assertEquals($user, $cloudId->getUser());
  86. $this->assertEquals($remote, $cloudId->getRemote());
  87. $this->assertEquals($cleanId, $cloudId->getId());
  88. $this->assertEquals($displayName . '@' . $remote, $cloudId->getDisplayId());
  89. }
  90. public function invalidCloudIdProvider() {
  91. return [
  92. ['example.com'],
  93. ['test:foo@example.com'],
  94. ['test/foo@example.com']
  95. ];
  96. }
  97. /**
  98. * @dataProvider invalidCloudIdProvider
  99. *
  100. * @param string $cloudId
  101. *
  102. */
  103. public function testInvalidCloudId($cloudId) {
  104. $this->expectException(\InvalidArgumentException::class);
  105. $this->contactsManager->expects($this->never())
  106. ->method('search');
  107. $this->cloudIdManager->resolveCloudId($cloudId);
  108. }
  109. public function getCloudIdProvider(): array {
  110. return [
  111. ['test', 'example.com', 'test@example.com'],
  112. ['test', 'http://example.com', 'test@http://example.com', 'test@example.com'],
  113. ['test', null, 'test@http://example.com', 'test@example.com', 'http://example.com'],
  114. ['test@example.com', 'example.com', 'test@example.com@example.com'],
  115. ['test@example.com', 'https://example.com', 'test@example.com@example.com'],
  116. ['test@example.com', null, 'test@example.com@example.com'],
  117. ['test@example.com', 'https://example.com/index.php/s/shareToken', 'test@example.com@example.com'],
  118. ];
  119. }
  120. /**
  121. * @dataProvider getCloudIdProvider
  122. *
  123. * @param string $user
  124. * @param null|string $remote
  125. * @param string $id
  126. */
  127. public function testGetCloudId(string $user, ?string $remote, string $id, ?string $searchCloudId = null, ?string $localHost = 'https://example.com'): void {
  128. if ($remote !== null) {
  129. $this->contactsManager->expects($this->any())
  130. ->method('search')
  131. ->with($searchCloudId ?? $id, ['CLOUD'])
  132. ->willReturn([
  133. [
  134. 'CLOUD' => [$searchCloudId ?? $id],
  135. 'FN' => 'Ample Ex',
  136. ]
  137. ]);
  138. } else {
  139. $this->urlGenerator->expects(self::once())
  140. ->method('getAbsoluteUrl')
  141. ->willReturn($localHost);
  142. }
  143. $cloudId = $this->cloudIdManager->getCloudId($user, $remote);
  144. $this->assertEquals($id, $cloudId->getId());
  145. }
  146. }