LegacyPublicAuthTest.php 7.4 KB

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