LegacyPublicAuthTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector;
  8. use OCP\IRequest;
  9. use OCP\ISession;
  10. use OCP\Security\Bruteforce\IThrottler;
  11. use OCP\Share\Exceptions\ShareNotFound;
  12. use OCP\Share\IManager;
  13. use OCP\Share\IShare;
  14. /**
  15. * Class LegacyPublicAuthTest
  16. *
  17. * @group DB
  18. *
  19. * @package OCA\DAV\Tests\unit\Connector
  20. */
  21. class LegacyPublicAuthTest extends \Test\TestCase {
  22. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  23. private $session;
  24. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  25. private $request;
  26. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  27. private $shareManager;
  28. /** @var \OCA\DAV\Connector\LegacyPublicAuth */
  29. private $auth;
  30. /** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
  31. private $throttler;
  32. /** @var string */
  33. private $oldUser;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->session = $this->getMockBuilder(ISession::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->request = $this->getMockBuilder(IRequest::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->shareManager = $this->getMockBuilder(IManager::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->throttler = $this->getMockBuilder(IThrottler::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->auth = new \OCA\DAV\Connector\LegacyPublicAuth(
  49. $this->request,
  50. $this->shareManager,
  51. $this->session,
  52. $this->throttler
  53. );
  54. // Store current user
  55. $this->oldUser = \OC_User::getUser();
  56. }
  57. protected function tearDown(): void {
  58. \OC_User::setIncognitoMode(false);
  59. // Set old user
  60. \OC_User::setUserId($this->oldUser);
  61. \OC_Util::setupFS($this->oldUser);
  62. parent::tearDown();
  63. }
  64. public function testNoShare(): void {
  65. $this->shareManager->expects($this->once())
  66. ->method('getShareByToken')
  67. ->willThrowException(new ShareNotFound());
  68. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  69. $this->assertFalse($result);
  70. }
  71. public function testShareNoPassword(): void {
  72. $share = $this->getMockBuilder(IShare::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $share->method('getPassword')->willReturn(null);
  76. $this->shareManager->expects($this->once())
  77. ->method('getShareByToken')
  78. ->willReturn($share);
  79. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  80. $this->assertTrue($result);
  81. }
  82. public function testSharePasswordFancyShareType(): void {
  83. $share = $this->getMockBuilder(IShare::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $share->method('getPassword')->willReturn('password');
  87. $share->method('getShareType')->willReturn(42);
  88. $this->shareManager->expects($this->once())
  89. ->method('getShareByToken')
  90. ->willReturn($share);
  91. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  92. $this->assertFalse($result);
  93. }
  94. public function testSharePasswordRemote(): void {
  95. $share = $this->getMockBuilder(IShare::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $share->method('getPassword')->willReturn('password');
  99. $share->method('getShareType')->willReturn(IShare::TYPE_REMOTE);
  100. $this->shareManager->expects($this->once())
  101. ->method('getShareByToken')
  102. ->willReturn($share);
  103. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  104. $this->assertTrue($result);
  105. }
  106. public function testSharePasswordLinkValidPassword(): void {
  107. $share = $this->getMockBuilder(IShare::class)
  108. ->disableOriginalConstructor()
  109. ->getMock();
  110. $share->method('getPassword')->willReturn('password');
  111. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  112. $this->shareManager->expects($this->once())
  113. ->method('getShareByToken')
  114. ->willReturn($share);
  115. $this->shareManager->expects($this->once())
  116. ->method('checkPassword')->with(
  117. $this->equalTo($share),
  118. $this->equalTo('password')
  119. )->willReturn(true);
  120. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  121. $this->assertTrue($result);
  122. }
  123. public function testSharePasswordMailValidPassword(): void {
  124. $share = $this->getMockBuilder(IShare::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $share->method('getPassword')->willReturn('password');
  128. $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
  129. $this->shareManager->expects($this->once())
  130. ->method('getShareByToken')
  131. ->willReturn($share);
  132. $this->shareManager->expects($this->once())
  133. ->method('checkPassword')->with(
  134. $this->equalTo($share),
  135. $this->equalTo('password')
  136. )->willReturn(true);
  137. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  138. $this->assertTrue($result);
  139. }
  140. public function testInvalidSharePasswordLinkValidSession(): void {
  141. $share = $this->getMockBuilder(IShare::class)
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. $share->method('getPassword')->willReturn('password');
  145. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  146. $share->method('getId')->willReturn('42');
  147. $this->shareManager->expects($this->once())
  148. ->method('getShareByToken')
  149. ->willReturn($share);
  150. $this->shareManager->method('checkPassword')
  151. ->with(
  152. $this->equalTo($share),
  153. $this->equalTo('password')
  154. )->willReturn(false);
  155. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  156. $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
  157. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  158. $this->assertTrue($result);
  159. }
  160. public function testSharePasswordLinkInvalidSession(): void {
  161. $share = $this->getMockBuilder(IShare::class)
  162. ->disableOriginalConstructor()
  163. ->getMock();
  164. $share->method('getPassword')->willReturn('password');
  165. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  166. $share->method('getId')->willReturn('42');
  167. $this->shareManager->expects($this->once())
  168. ->method('getShareByToken')
  169. ->willReturn($share);
  170. $this->shareManager->method('checkPassword')
  171. ->with(
  172. $this->equalTo($share),
  173. $this->equalTo('password')
  174. )->willReturn(false);
  175. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  176. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  177. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  178. $this->assertFalse($result);
  179. }
  180. public function testSharePasswordMailInvalidSession(): void {
  181. $share = $this->getMockBuilder(IShare::class)
  182. ->disableOriginalConstructor()
  183. ->getMock();
  184. $share->method('getPassword')->willReturn('password');
  185. $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
  186. $share->method('getId')->willReturn('42');
  187. $this->shareManager->expects($this->once())
  188. ->method('getShareByToken')
  189. ->willReturn($share);
  190. $this->shareManager->method('checkPassword')
  191. ->with(
  192. $this->equalTo($share),
  193. $this->equalTo('password')
  194. )->willReturn(false);
  195. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  196. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  197. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  198. $this->assertFalse($result);
  199. }
  200. }