You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PublicAuthTest.php 7.9KB

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