LegacyPublicAuthTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\Connector;
  28. use OCP\IRequest;
  29. use OCP\ISession;
  30. use OCP\Security\Bruteforce\IThrottler;
  31. use OCP\Share\Exceptions\ShareNotFound;
  32. use OCP\Share\IManager;
  33. use OCP\Share\IShare;
  34. /**
  35. * Class LegacyPublicAuthTest
  36. *
  37. * @group DB
  38. *
  39. * @package OCA\DAV\Tests\unit\Connector
  40. */
  41. class LegacyPublicAuthTest extends \Test\TestCase {
  42. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  43. private $session;
  44. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  45. private $request;
  46. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  47. private $shareManager;
  48. /** @var \OCA\DAV\Connector\LegacyPublicAuth */
  49. private $auth;
  50. /** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
  51. private $throttler;
  52. /** @var string */
  53. private $oldUser;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->session = $this->getMockBuilder(ISession::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->request = $this->getMockBuilder(IRequest::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->shareManager = $this->getMockBuilder(IManager::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->throttler = $this->getMockBuilder(IThrottler::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->auth = new \OCA\DAV\Connector\LegacyPublicAuth(
  69. $this->request,
  70. $this->shareManager,
  71. $this->session,
  72. $this->throttler
  73. );
  74. // Store current user
  75. $this->oldUser = \OC_User::getUser();
  76. }
  77. protected function tearDown(): void {
  78. \OC_User::setIncognitoMode(false);
  79. // Set old user
  80. \OC_User::setUserId($this->oldUser);
  81. \OC_Util::setupFS($this->oldUser);
  82. parent::tearDown();
  83. }
  84. public function testNoShare(): void {
  85. $this->shareManager->expects($this->once())
  86. ->method('getShareByToken')
  87. ->willThrowException(new ShareNotFound());
  88. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  89. $this->assertFalse($result);
  90. }
  91. public function testShareNoPassword(): void {
  92. $share = $this->getMockBuilder(IShare::class)
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $share->method('getPassword')->willReturn(null);
  96. $this->shareManager->expects($this->once())
  97. ->method('getShareByToken')
  98. ->willReturn($share);
  99. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  100. $this->assertTrue($result);
  101. }
  102. public function testSharePasswordFancyShareType(): void {
  103. $share = $this->getMockBuilder(IShare::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $share->method('getPassword')->willReturn('password');
  107. $share->method('getShareType')->willReturn(42);
  108. $this->shareManager->expects($this->once())
  109. ->method('getShareByToken')
  110. ->willReturn($share);
  111. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  112. $this->assertFalse($result);
  113. }
  114. public function testSharePasswordRemote(): void {
  115. $share = $this->getMockBuilder(IShare::class)
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $share->method('getPassword')->willReturn('password');
  119. $share->method('getShareType')->willReturn(IShare::TYPE_REMOTE);
  120. $this->shareManager->expects($this->once())
  121. ->method('getShareByToken')
  122. ->willReturn($share);
  123. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  124. $this->assertTrue($result);
  125. }
  126. public function testSharePasswordLinkValidPassword(): void {
  127. $share = $this->getMockBuilder(IShare::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $share->method('getPassword')->willReturn('password');
  131. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  132. $this->shareManager->expects($this->once())
  133. ->method('getShareByToken')
  134. ->willReturn($share);
  135. $this->shareManager->expects($this->once())
  136. ->method('checkPassword')->with(
  137. $this->equalTo($share),
  138. $this->equalTo('password')
  139. )->willReturn(true);
  140. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  141. $this->assertTrue($result);
  142. }
  143. public function testSharePasswordMailValidPassword(): void {
  144. $share = $this->getMockBuilder(IShare::class)
  145. ->disableOriginalConstructor()
  146. ->getMock();
  147. $share->method('getPassword')->willReturn('password');
  148. $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
  149. $this->shareManager->expects($this->once())
  150. ->method('getShareByToken')
  151. ->willReturn($share);
  152. $this->shareManager->expects($this->once())
  153. ->method('checkPassword')->with(
  154. $this->equalTo($share),
  155. $this->equalTo('password')
  156. )->willReturn(true);
  157. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  158. $this->assertTrue($result);
  159. }
  160. public function testInvalidSharePasswordLinkValidSession(): 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('42');
  177. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  178. $this->assertTrue($result);
  179. }
  180. public function testSharePasswordLinkInvalidSession(): void {
  181. $share = $this->getMockBuilder(IShare::class)
  182. ->disableOriginalConstructor()
  183. ->getMock();
  184. $share->method('getPassword')->willReturn('password');
  185. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  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. public function testSharePasswordMailInvalidSession(): void {
  201. $share = $this->getMockBuilder(IShare::class)
  202. ->disableOriginalConstructor()
  203. ->getMock();
  204. $share->method('getPassword')->willReturn('password');
  205. $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
  206. $share->method('getId')->willReturn('42');
  207. $this->shareManager->expects($this->once())
  208. ->method('getShareByToken')
  209. ->willReturn($share);
  210. $this->shareManager->method('checkPassword')
  211. ->with(
  212. $this->equalTo($share),
  213. $this->equalTo('password')
  214. )->willReturn(false);
  215. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  216. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  217. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  218. $this->assertFalse($result);
  219. }
  220. }