CloudIdManagerTest.php 4.7 KB

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