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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Roeland Jago Douma <rullzer@owncloud.com>
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\Tests\unit\Connector;
  24. use OCP\IRequest;
  25. use OCP\ISession;
  26. use OCP\Share\Exceptions\ShareNotFound;
  27. use OCP\Share\IManager;
  28. /**
  29. * Class PublicAuthTest
  30. *
  31. * @group DB
  32. *
  33. * @package OCA\DAV\Tests\unit\Connector
  34. */
  35. class PublicAuthTest extends \Test\TestCase {
  36. /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
  37. private $session;
  38. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  39. private $request;
  40. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  41. private $shareManager;
  42. /** @var \OCA\DAV\Connector\PublicAuth */
  43. private $auth;
  44. /** @var string */
  45. private $oldUser;
  46. protected function setUp() {
  47. parent::setUp();
  48. $this->session = $this->getMock('\OCP\ISession');
  49. $this->request = $this->getMock('\OCP\IRequest');
  50. $this->shareManager = $this->getMock('\OCP\Share\IManager');
  51. $this->auth = new \OCA\DAV\Connector\PublicAuth(
  52. $this->request,
  53. $this->shareManager,
  54. $this->session
  55. );
  56. // Store current user
  57. $this->oldUser = \OC_User::getUser();
  58. }
  59. protected function tearDown() {
  60. \OC_User::setIncognitoMode(false);
  61. // Set old user
  62. \OC_User::setUserId($this->oldUser);
  63. \OC_Util::setupFS($this->oldUser);
  64. parent::tearDown();
  65. }
  66. public function testNoShare() {
  67. $this->shareManager->expects($this->once())
  68. ->method('getShareByToken')
  69. ->willThrowException(new ShareNotFound());
  70. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  71. $this->assertFalse($result);
  72. }
  73. public function testShareNoPassword() {
  74. $share = $this->getMock('OCP\Share\IShare');
  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() {
  83. $share = $this->getMock('OCP\Share\IShare');
  84. $share->method('getPassword')->willReturn('password');
  85. $share->method('getShareType')->willReturn(42);
  86. $this->shareManager->expects($this->once())
  87. ->method('getShareByToken')
  88. ->willReturn($share);
  89. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  90. $this->assertFalse($result);
  91. }
  92. public function testSharePasswordRemote() {
  93. $share = $this->getMock('OCP\Share\IShare');
  94. $share->method('getPassword')->willReturn('password');
  95. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_REMOTE);
  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 testSharePasswordLinkValidPassword() {
  103. $share = $this->getMock('OCP\Share\IShare');
  104. $share->method('getPassword')->willReturn('password');
  105. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  106. $this->shareManager->expects($this->once())
  107. ->method('getShareByToken')
  108. ->willReturn($share);
  109. $this->shareManager->expects($this->once())
  110. ->method('checkPassword')->with(
  111. $this->equalTo($share),
  112. $this->equalTo('password')
  113. )->willReturn(true);
  114. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  115. $this->assertTrue($result);
  116. }
  117. public function testSharePasswordLinkValidSession() {
  118. $share = $this->getMock('OCP\Share\IShare');
  119. $share->method('getPassword')->willReturn('password');
  120. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  121. $share->method('getId')->willReturn('42');
  122. $this->shareManager->expects($this->once())
  123. ->method('getShareByToken')
  124. ->willReturn($share);
  125. $this->shareManager->method('checkPassword')
  126. ->with(
  127. $this->equalTo($share),
  128. $this->equalTo('password')
  129. )->willReturn(false);
  130. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  131. $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
  132. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  133. $this->assertTrue($result);
  134. }
  135. public function testSharePasswordLinkInvalidSession() {
  136. $share = $this->getMock('OCP\Share\IShare');
  137. $share->method('getPassword')->willReturn('password');
  138. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  139. $share->method('getId')->willReturn('42');
  140. $this->shareManager->expects($this->once())
  141. ->method('getShareByToken')
  142. ->willReturn($share);
  143. $this->shareManager->method('checkPassword')
  144. ->with(
  145. $this->equalTo($share),
  146. $this->equalTo('password')
  147. )->willReturn(false);
  148. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  149. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  150. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  151. $this->assertFalse($result);
  152. }
  153. }