CloudIdManagerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Federation;
  7. use OC\Federation\CloudIdManager;
  8. use OC\Memcache\ArrayCache;
  9. use OCP\Contacts\IManager;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\ICacheFactory;
  12. use OCP\IURLGenerator;
  13. use OCP\IUserManager;
  14. use Test\TestCase;
  15. class CloudIdManagerTest extends TestCase {
  16. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  17. protected $contactsManager;
  18. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  19. private $urlGenerator;
  20. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  21. private $userManager;
  22. /** @var CloudIdManager */
  23. private $cloudIdManager;
  24. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  25. private $cacheFactory;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->contactsManager = $this->createMock(IManager::class);
  29. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  30. $this->userManager = $this->createMock(IUserManager::class);
  31. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  32. $this->cacheFactory->method('createLocal')
  33. ->willReturn(new ArrayCache(''));
  34. $this->cloudIdManager = new CloudIdManager(
  35. $this->contactsManager,
  36. $this->urlGenerator,
  37. $this->userManager,
  38. $this->cacheFactory,
  39. $this->createMock(IEventDispatcher::class)
  40. );
  41. }
  42. public function cloudIdProvider() {
  43. return [
  44. ['test@example.com', 'test', 'example.com', 'test@example.com'],
  45. ['test@example.com/cloud', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  46. ['test@example.com/cloud/', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  47. ['test@example.com/cloud/index.php', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  48. ['test@example.com@example.com', 'test@example.com', 'example.com', 'test@example.com@example.com'],
  49. ];
  50. }
  51. /**
  52. * @dataProvider cloudIdProvider
  53. *
  54. * @param string $cloudId
  55. * @param string $user
  56. * @param string $remote
  57. */
  58. public function testResolveCloudId($cloudId, $user, $remote, $cleanId) {
  59. $displayName = 'Ample Ex';
  60. $this->contactsManager->expects($this->any())
  61. ->method('search')
  62. ->with($cleanId, ['CLOUD'])
  63. ->willReturn([
  64. [
  65. 'CLOUD' => [$cleanId],
  66. 'FN' => 'Ample Ex',
  67. ]
  68. ]);
  69. $cloudId = $this->cloudIdManager->resolveCloudId($cloudId);
  70. $this->assertEquals($user, $cloudId->getUser());
  71. $this->assertEquals($remote, $cloudId->getRemote());
  72. $this->assertEquals($cleanId, $cloudId->getId());
  73. $this->assertEquals($displayName . '@' . $remote, $cloudId->getDisplayId());
  74. }
  75. public function invalidCloudIdProvider() {
  76. return [
  77. ['example.com'],
  78. ['test:foo@example.com'],
  79. ['test/foo@example.com']
  80. ];
  81. }
  82. /**
  83. * @dataProvider invalidCloudIdProvider
  84. *
  85. * @param string $cloudId
  86. *
  87. */
  88. public function testInvalidCloudId($cloudId) {
  89. $this->expectException(\InvalidArgumentException::class);
  90. $this->contactsManager->expects($this->never())
  91. ->method('search');
  92. $this->cloudIdManager->resolveCloudId($cloudId);
  93. }
  94. public function getCloudIdProvider(): array {
  95. return [
  96. ['test', 'example.com', 'test@example.com'],
  97. ['test', 'http://example.com', 'test@http://example.com', 'test@example.com'],
  98. ['test', null, 'test@http://example.com', 'test@example.com', 'http://example.com', 'http://example.com'],
  99. ['test@example.com', 'example.com', 'test@example.com@example.com'],
  100. ['test@example.com', 'https://example.com', 'test@example.com@example.com'],
  101. ['test@example.com', null, 'test@example.com@example.com', null, 'https://example.com', 'https://example.com'],
  102. ['test@example.com', 'https://example.com/index.php/s/shareToken', 'test@example.com@example.com', null, 'https://example.com', 'https://example.com'],
  103. ];
  104. }
  105. /**
  106. * @dataProvider getCloudIdProvider
  107. *
  108. * @param string $user
  109. * @param null|string $remote
  110. * @param string $id
  111. */
  112. public function testGetCloudId(string $user, ?string $remote, string $id, ?string $searchCloudId = null, ?string $localHost = 'https://example.com', ?string $expectedRemoteId = null): void {
  113. if ($remote !== null) {
  114. $this->contactsManager->expects($this->any())
  115. ->method('search')
  116. ->with($searchCloudId ?? $id, ['CLOUD'])
  117. ->willReturn([
  118. [
  119. 'CLOUD' => [$searchCloudId ?? $id],
  120. 'FN' => 'Ample Ex',
  121. ]
  122. ]);
  123. } else {
  124. $this->urlGenerator->expects(self::once())
  125. ->method('getAbsoluteUrl')
  126. ->willReturn($localHost);
  127. }
  128. $expectedRemoteId ??= $remote;
  129. $cloudId = $this->cloudIdManager->getCloudId($user, $remote);
  130. $this->assertEquals($id, $cloudId->getId(), 'Cloud ID');
  131. $this->assertEquals($expectedRemoteId, $cloudId->getRemote(), 'Remote URL');
  132. }
  133. }