CloudIdManagerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 OCP\Contacts\IManager;
  24. use OCP\IURLGenerator;
  25. use OCP\IUserManager;
  26. use Test\TestCase;
  27. class CloudIdManagerTest extends TestCase {
  28. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  29. protected $contactsManager;
  30. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  31. private $urlGenerator;
  32. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  33. private $userManager;
  34. /** @var CloudIdManager */
  35. private $cloudIdManager;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->contactsManager = $this->createMock(IManager::class);
  39. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  40. $this->userManager = $this->createMock(IUserManager::class);
  41. $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->urlGenerator, $this->userManager);
  42. }
  43. public function cloudIdProvider() {
  44. return [
  45. ['test@example.com', 'test', 'example.com', 'test@example.com'],
  46. ['test@example.com/cloud', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  47. ['test@example.com/cloud/', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  48. ['test@example.com/cloud/index.php', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  49. ['test@example.com@example.com', 'test@example.com', 'example.com', 'test@example.com@example.com'],
  50. ];
  51. }
  52. /**
  53. * @dataProvider cloudIdProvider
  54. *
  55. * @param string $cloudId
  56. * @param string $user
  57. * @param string $remote
  58. */
  59. public function testResolveCloudId($cloudId, $user, $remote, $cleanId) {
  60. $displayName = 'Ample Ex';
  61. $this->contactsManager->expects($this->any())
  62. ->method('search')
  63. ->with($cleanId, ['CLOUD'])
  64. ->willReturn([
  65. [
  66. 'CLOUD' => [$cleanId],
  67. 'FN' => 'Ample Ex',
  68. ]
  69. ]);
  70. $cloudId = $this->cloudIdManager->resolveCloudId($cloudId);
  71. $this->assertEquals($user, $cloudId->getUser());
  72. $this->assertEquals($remote, $cloudId->getRemote());
  73. $this->assertEquals($cleanId, $cloudId->getId());
  74. $this->assertEquals($displayName . '@' . $remote, $cloudId->getDisplayId());
  75. }
  76. public function invalidCloudIdProvider() {
  77. return [
  78. ['example.com'],
  79. ['test:foo@example.com'],
  80. ['test/foo@example.com']
  81. ];
  82. }
  83. /**
  84. * @dataProvider invalidCloudIdProvider
  85. *
  86. * @param string $cloudId
  87. *
  88. */
  89. public function testInvalidCloudId($cloudId) {
  90. $this->expectException(\InvalidArgumentException::class);
  91. $this->contactsManager->expects($this->never())
  92. ->method('search');
  93. $this->cloudIdManager->resolveCloudId($cloudId);
  94. }
  95. public function getCloudIdProvider() {
  96. return [
  97. ['test', 'example.com', 'test@example.com'],
  98. ['test@example.com', 'example.com', 'test@example.com@example.com'],
  99. ['test@example.com', null, 'test@example.com@example.com'],
  100. ];
  101. }
  102. /**
  103. * @dataProvider getCloudIdProvider
  104. *
  105. * @param string $user
  106. * @param string $remote
  107. * @param string $id
  108. */
  109. public function testGetCloudId($user, $remote, $id) {
  110. if ($remote !== null) {
  111. $this->contactsManager->expects($this->any())
  112. ->method('search')
  113. ->with($id, ['CLOUD'])
  114. ->willReturn([
  115. [
  116. 'CLOUD' => [$id],
  117. 'FN' => 'Ample Ex',
  118. ]
  119. ]);
  120. } else {
  121. $this->urlGenerator->expects(self::once())
  122. ->method('getAbsoluteUrl')
  123. ->willReturn('https://example.com');
  124. }
  125. $cloudId = $this->cloudIdManager->getCloudId($user, $remote);
  126. $this->assertEquals($id, $cloudId->getId());
  127. }
  128. }