1
0

CloudIdManagerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(): array {
  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. public function testResolveCloudId(string $cloudId, string $user, string $noProtocolRemote, string $cleanId): void {
  70. $displayName = 'Ample Ex';
  71. $this->contactsManager->expects($this->any())
  72. ->method('search')
  73. ->with($cleanId, ['CLOUD'])
  74. ->willReturn([
  75. [
  76. 'CLOUD' => [$cleanId],
  77. 'FN' => 'Ample Ex',
  78. ]
  79. ]);
  80. $cloudId = $this->cloudIdManager->resolveCloudId($cloudId);
  81. $this->assertEquals($user, $cloudId->getUser());
  82. $this->assertEquals('https://' . $noProtocolRemote, $cloudId->getRemote());
  83. $this->assertEquals($cleanId, $cloudId->getId());
  84. $this->assertEquals($displayName . '@' . $noProtocolRemote, $cloudId->getDisplayId());
  85. }
  86. public function invalidCloudIdProvider(): array {
  87. return [
  88. ['example.com'],
  89. ['test:foo@example.com'],
  90. ['test/foo@example.com']
  91. ];
  92. }
  93. /**
  94. * @dataProvider invalidCloudIdProvider
  95. *
  96. * @param string $cloudId
  97. *
  98. */
  99. public function testInvalidCloudId(string $cloudId): void {
  100. $this->expectException(\InvalidArgumentException::class);
  101. $this->contactsManager->expects($this->never())
  102. ->method('search');
  103. $this->cloudIdManager->resolveCloudId($cloudId);
  104. }
  105. public function getCloudIdProvider(): array {
  106. return [
  107. ['test', 'example.com', 'test@example.com', null, 'https://example.com', 'https://example.com'],
  108. ['test', 'http://example.com', 'test@http://example.com', 'test@example.com'],
  109. ['test', null, 'test@http://example.com', 'test@example.com', 'http://example.com', 'http://example.com'],
  110. ['test@example.com', 'example.com', 'test@example.com@example.com', null, 'https://example.com', 'https://example.com'],
  111. ['test@example.com', 'https://example.com', 'test@example.com@example.com'],
  112. ['test@example.com', null, 'test@example.com@example.com', null, 'https://example.com', 'https://example.com'],
  113. ['test@example.com', 'https://example.com/index.php/s/shareToken', 'test@example.com@example.com', null, 'https://example.com', 'https://example.com'],
  114. ];
  115. }
  116. /**
  117. * @dataProvider getCloudIdProvider
  118. */
  119. public function testGetCloudId(string $user, ?string $remote, string $id, ?string $searchCloudId = null, ?string $localHost = 'https://example.com', ?string $expectedRemoteId = null): void {
  120. if ($remote !== null) {
  121. $this->contactsManager->expects($this->any())
  122. ->method('search')
  123. ->with($searchCloudId ?? $id, ['CLOUD'])
  124. ->willReturn([
  125. [
  126. 'CLOUD' => [$searchCloudId ?? $id],
  127. 'FN' => 'Ample Ex',
  128. ]
  129. ]);
  130. } else {
  131. $this->urlGenerator->expects(self::once())
  132. ->method('getAbsoluteUrl')
  133. ->willReturn($localHost);
  134. }
  135. $expectedRemoteId ??= $remote;
  136. $cloudId = $this->cloudIdManager->getCloudId($user, $remote);
  137. $this->assertEquals($id, $cloudId->getId(), 'Cloud ID');
  138. $this->assertEquals($expectedRemoteId, $cloudId->getRemote(), 'Remote URL');
  139. }
  140. }