2016-04-01 18:35:37 +03:00
|
|
|
<?php
|
2016-05-26 20:56:05 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-05-26 20:56:05 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector;
|
2016-04-01 18:35:37 +03:00
|
|
|
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
|
|
|
use OCP\Share\IManager;
|
2017-10-25 01:03:28 +03:00
|
|
|
use OCP\Share\IShare;
|
2016-04-01 18:35:37 +03:00
|
|
|
|
2016-04-21 14:36:52 +03:00
|
|
|
/**
|
2016-05-25 17:04:15 +03:00
|
|
|
* Class PublicAuthTest
|
2016-04-21 14:36:52 +03:00
|
|
|
*
|
|
|
|
* @group DB
|
2017-05-04 12:20:20 +03:00
|
|
|
*
|
2016-05-25 17:04:15 +03:00
|
|
|
* @package OCA\DAV\Tests\unit\Connector
|
2016-04-21 14:36:52 +03:00
|
|
|
*/
|
2016-05-25 17:04:15 +03:00
|
|
|
class PublicAuthTest extends \Test\TestCase {
|
2016-04-01 18:35:37 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
|
2016-04-01 18:35:37 +03:00
|
|
|
private $session;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
|
2016-04-01 18:35:37 +03:00
|
|
|
private $request;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-04-01 18:35:37 +03:00
|
|
|
private $shareManager;
|
|
|
|
/** @var \OCA\DAV\Connector\PublicAuth */
|
|
|
|
private $auth;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $oldUser;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-04-01 18:35:37 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->session = $this->getMockBuilder(ISession::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->request = $this->getMockBuilder(IRequest::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->shareManager = $this->getMockBuilder(IManager::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-01 18:35:37 +03:00
|
|
|
|
|
|
|
$this->auth = new \OCA\DAV\Connector\PublicAuth(
|
|
|
|
$this->request,
|
|
|
|
$this->shareManager,
|
|
|
|
$this->session
|
|
|
|
);
|
|
|
|
|
|
|
|
// Store current user
|
|
|
|
$this->oldUser = \OC_User::getUser();
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2016-04-01 18:35:37 +03:00
|
|
|
\OC_User::setIncognitoMode(false);
|
|
|
|
|
|
|
|
// Set old user
|
|
|
|
\OC_User::setUserId($this->oldUser);
|
|
|
|
\OC_Util::setupFS($this->oldUser);
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoShare() {
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willThrowException(new ShareNotFound());
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShareNoPassword() {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-01 18:35:37 +03:00
|
|
|
$share->method('getPassword')->willReturn(null);
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSharePasswordFancyShareType() {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-01 18:35:37 +03:00
|
|
|
$share->method('getPassword')->willReturn('password');
|
|
|
|
$share->method('getShareType')->willReturn(42);
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testSharePasswordRemote() {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-01 18:35:37 +03:00
|
|
|
$share->method('getPassword')->willReturn('password');
|
2020-06-24 17:49:16 +03:00
|
|
|
$share->method('getShareType')->willReturn(IShare::TYPE_REMOTE);
|
2016-04-01 18:35:37 +03:00
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSharePasswordLinkValidPassword() {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-01 18:35:37 +03:00
|
|
|
$share->method('getPassword')->willReturn('password');
|
2020-06-24 17:49:16 +03:00
|
|
|
$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
|
2016-04-01 18:35:37 +03:00
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('checkPassword')->with(
|
|
|
|
$this->equalTo($share),
|
|
|
|
$this->equalTo('password')
|
|
|
|
)->willReturn(true);
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2017-05-04 12:20:20 +03:00
|
|
|
public function testSharePasswordMailValidPassword() {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)
|
2017-05-04 12:20:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$share->method('getPassword')->willReturn('password');
|
2020-06-24 17:49:16 +03:00
|
|
|
$share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
|
2017-05-04 12:20:20 +03:00
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('checkPassword')->with(
|
|
|
|
$this->equalTo($share),
|
|
|
|
$this->equalTo('password')
|
|
|
|
)->willReturn(true);
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2016-04-01 18:35:37 +03:00
|
|
|
public function testSharePasswordLinkValidSession() {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-01 18:35:37 +03:00
|
|
|
$share->method('getPassword')->willReturn('password');
|
2020-06-24 17:49:16 +03:00
|
|
|
$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
|
2016-04-01 18:35:37 +03:00
|
|
|
$share->method('getId')->willReturn('42');
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$this->shareManager->method('checkPassword')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($share),
|
|
|
|
$this->equalTo('password')
|
|
|
|
)->willReturn(false);
|
|
|
|
|
|
|
|
$this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
|
|
|
|
$this->session->method('get')->with('public_link_authenticated')->willReturn('42');
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSharePasswordLinkInvalidSession() {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-01 18:35:37 +03:00
|
|
|
$share->method('getPassword')->willReturn('password');
|
2020-06-24 17:49:16 +03:00
|
|
|
$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
|
2016-04-01 18:35:37 +03:00
|
|
|
$share->method('getId')->willReturn('42');
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$this->shareManager->method('checkPassword')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($share),
|
|
|
|
$this->equalTo('password')
|
|
|
|
)->willReturn(false);
|
|
|
|
|
|
|
|
$this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
|
|
|
|
$this->session->method('get')->with('public_link_authenticated')->willReturn('43');
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2017-05-04 12:20:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
public function testSharePasswordMailInvalidSession() {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)
|
2017-05-04 12:20:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$share->method('getPassword')->willReturn('password');
|
2020-06-24 17:49:16 +03:00
|
|
|
$share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
|
2017-05-04 12:20:20 +03:00
|
|
|
$share->method('getId')->willReturn('42');
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$this->shareManager->method('checkPassword')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($share),
|
|
|
|
$this->equalTo('password')
|
|
|
|
)->willReturn(false);
|
|
|
|
|
|
|
|
$this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
|
|
|
|
$this->session->method('get')->with('public_link_authenticated')->willReturn('43');
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
|
|
|
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2016-04-01 18:35:37 +03:00
|
|
|
}
|